Skip to content

Commit 40e7391

Browse files
spencer-xyzgmesika-coti
authored andcommitted
move decrypt functions to crypto_utils
1 parent a9eb9da commit 40e7391

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

src/crypto_utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@ export async function buildStringInputText(
160160
return encryptedStr
161161
}
162162

163+
export function decryptUint(ciphertext: bigint, userKey: string) {
164+
// Convert CT to bytes
165+
let ctString = ciphertext.toString(hexBase)
166+
let ctArray = Buffer.from(ctString, "hex")
167+
while (ctArray.length < 32) {
168+
// When the first bits are 0, bigint bit size is less than 32 and need to re-add the bits
169+
ctString = "0" + ctString
170+
ctArray = Buffer.from(ctString, "hex")
171+
}
172+
// Split CT into two 128-bit arrays r and cipher
173+
const cipher = ctArray.subarray(0, block_size)
174+
const r = ctArray.subarray(block_size)
175+
176+
// Decrypt the cipher
177+
const decryptedMessage = decrypt(Buffer.from(userKey, "hex"), r, cipher)
178+
179+
return BigInt("0x" + decryptedMessage.toString("hex"))
180+
}
181+
182+
export function decryptString(ciphertext: Array<bigint>, userKey: string) {
183+
let decryptedStr = new Array<number>(ciphertext.length)
184+
185+
for (let i = 0; i < ciphertext.length; i++) {
186+
decryptedStr[i] = Number(decryptUint(ciphertext[i], userKey))
187+
}
188+
189+
let decoder = new TextDecoder()
190+
191+
return decoder.decode(new Uint8Array(decryptedStr))
192+
}
193+
163194
export function generateAesKey() {
164195
return crypto.randomBytes(block_size).toString("hex")
165196
}

src/ethers_utils.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import {ethers, JsonRpcProvider, Provider, toNumber, TransactionRequest, Wallet,} from "ethers";
2-
import {decrypt} from "./crypto_utils";
3-
4-
const block_size = 16 // AES block size in bytes
5-
const hexBase = 16
62

73
export async function printNetworkDetails(provider: Provider) {
84
if (!await isProviderConnected(provider)) {
@@ -113,37 +109,6 @@ export async function isGasEstimationValid(provider: Provider, tx: TransactionRe
113109
return {valid: true, gasEstimation: estimatedGas}
114110
}
115111

116-
export function decryptUint(ciphertext: bigint, userKey: string) {
117-
// Convert CT to bytes
118-
let ctString = ciphertext.toString(hexBase)
119-
let ctArray = Buffer.from(ctString, "hex")
120-
while (ctArray.length < 32) {
121-
// When the first bits are 0, bigint bit size is less than 32 and need to re-add the bits
122-
ctString = "0" + ctString
123-
ctArray = Buffer.from(ctString, "hex")
124-
}
125-
// Split CT into two 128-bit arrays r and cipher
126-
const cipher = ctArray.subarray(0, block_size)
127-
const r = ctArray.subarray(block_size)
128-
129-
// Decrypt the cipher
130-
const decryptedMessage = decrypt(Buffer.from(userKey, "hex"), r, cipher)
131-
132-
return BigInt("0x" + decryptedMessage.toString("hex"))
133-
}
134-
135-
export function decryptString(ciphertext: Array<bigint>, userKey: string) {
136-
let decryptedStr = new Array<number>(ciphertext.length)
137-
138-
for (let i = 0; i < ciphertext.length; i++) {
139-
decryptedStr[i] = Number(decryptUint(ciphertext[i], userKey))
140-
}
141-
142-
let decoder = new TextDecoder()
143-
144-
return decoder.decode(new Uint8Array(decryptedStr))
145-
}
146-
147112
export async function isProviderConnected(provider: Provider): Promise<boolean> {
148113
if (provider == undefined) {
149114
throw Error('Provider does not exist.')

0 commit comments

Comments
 (0)