@@ -58,7 +58,7 @@ export function decrypt(key: Uint8Array, r: Uint8Array, ciphertext: Uint8Array):
5858
5959export function generateRSAKeyPair ( ) : { publicKey : Uint8Array ; privateKey : Uint8Array } {
6060 // Generate a new RSA key pair
61- const rsaKeyPair = forge . pki . rsa . generateKeyPair ( { bits : 2048 } )
61+ const rsaKeyPair = forge . pki . rsa . generateKeyPair ( { bits : 2048 } )
6262
6363 // Convert keys to DER format
6464 const privateKey = forge . asn1 . toDer ( forge . pki . privateKeyToAsn1 ( rsaKeyPair . privateKey ) ) . data
@@ -76,7 +76,7 @@ export function decryptRSA(privateKey: Uint8Array, ciphertext: string): string {
7676
7777 // Decrypt using RSA-OAEP
7878 const rsaPrivateKey = forge . pki . privateKeyFromPem ( privateKeyPEM ) ;
79-
79+
8080 const decrypted = rsaPrivateKey . decrypt ( forge . util . hexToBytes ( ciphertext ) , 'RSA-OAEP' , {
8181 md : forge . md . sha256 . create ( )
8282 } ) ;
@@ -133,15 +133,15 @@ export function buildInputText(
133133 const keyBytes = encodeKey ( sender . userKey )
134134
135135 // Encrypt the plaintext using AES key
136- const { ciphertext, r } = encrypt ( keyBytes , plaintextBytes )
136+ const { ciphertext, r} = encrypt ( keyBytes , plaintextBytes )
137137 const ct = new Uint8Array ( [ ...ciphertext , ...r ] )
138138
139139 // Convert the ciphertext to BigInt
140140 const ctInt = decodeUint ( ct )
141141
142142 const signature = signInputText ( sender , contractAddress , functionSelector , ctInt ) ;
143143
144- return { ctInt, signature }
144+ return { ctInt, signature}
145145}
146146
147147export async function buildStringInputText (
@@ -157,8 +157,8 @@ export async function buildStringInputText(
157157 let encryptedStr = new Array < { ciphertext : bigint , signature : Uint8Array } > ( plaintext . length )
158158
159159 for ( let i = 0 ; i < plaintext . length ; i ++ ) {
160- const { ctInt, signature } = buildInputText ( BigInt ( encodedStr [ i ] ) , sender , contractAddress , functionSelector )
161- encryptedStr [ i ] = { ciphertext : ctInt , signature }
160+ const { ctInt, signature} = buildInputText ( BigInt ( encodedStr [ i ] ) , sender , contractAddress , functionSelector )
161+ encryptedStr [ i ] = { ciphertext : ctInt , signature}
162162 }
163163
164164 return encryptedStr
@@ -227,38 +227,38 @@ export function encodeString(str: string): Uint8Array {
227227
228228export function encodeKey ( userKey : string ) : Uint8Array {
229229 const keyBytes = new Uint8Array ( 16 )
230-
230+
231231 for ( let i = 0 ; i < 32 ; i += 2 ) {
232- keyBytes [ i / 2 ] = parseInt ( userKey . slice ( i , i + 2 ) , HEX_BASE )
232+ keyBytes [ i / 2 ] = parseInt ( userKey . slice ( i , i + 2 ) , HEX_BASE )
233233 }
234-
234+
235235 return keyBytes
236236}
237-
237+
238238export function encodeUint ( plaintext : bigint ) : Uint8Array {
239239 // Convert the plaintext to bytes in little-endian format
240240
241241 const plaintextBytes = new Uint8Array ( BLOCK_SIZE ) // Allocate a buffer of size 16 bytes
242-
242+
243243 for ( let i = 15 ; i >= 0 ; i -- ) {
244- plaintextBytes [ i ] = Number ( plaintext & BigInt ( 255 ) )
245- plaintext >>= BigInt ( 8 )
244+ plaintextBytes [ i ] = Number ( plaintext & BigInt ( 255 ) )
245+ plaintext >>= BigInt ( 8 )
246246 }
247-
247+
248248 return plaintextBytes
249249}
250250
251251export function decodeUint ( plaintextBytes : Uint8Array ) : bigint {
252252 const plaintext : Array < string > = [ ]
253-
253+
254254 let byte = ''
255-
255+
256256 for ( let i = 0 ; i < plaintextBytes . length ; i ++ ) {
257- byte = plaintextBytes [ i ] . toString ( HEX_BASE ) . padStart ( 2 , '0' ) // ensure that the zero byte is represented using two digits
258-
259- plaintext . push ( byte )
257+ byte = plaintextBytes [ i ] . toString ( HEX_BASE ) . padStart ( 2 , '0' ) // ensure that the zero byte is represented using two digits
258+
259+ plaintext . push ( byte )
260260 }
261-
261+
262262 return BigInt ( "0x" + plaintext . join ( "" ) )
263263}
264264
0 commit comments