@@ -66,7 +66,7 @@ function decrypt(key: Buffer, r: Buffer, ciphertext: Buffer) {
6666 return plaintext
6767}
6868
69- export function generateRSAKeyPair ( ) {
69+ export function generateRSAKeyPair ( ) : crypto . KeyPairSyncResult < Buffer , Buffer > {
7070 // Generate a new RSA key pair
7171 return crypto . generateKeyPairSync ( "rsa" , {
7272 modulusLength : 2048 ,
@@ -97,9 +97,9 @@ export function decryptRSA(privateKey: Buffer, ciphertext: Buffer) {
9797 )
9898}
9999
100- export function decryptValue ( ctAmount : bigint , userKey : string ) {
100+ export function decryptUint ( ciphertext : bigint , userKey : string ) {
101101 // Convert CT to bytes
102- let ctString = ctAmount . toString ( hexBase )
102+ let ctString = ciphertext . toString ( hexBase )
103103 let ctArray = Buffer . from ( ctString , "hex" )
104104 while ( ctArray . length < 32 ) {
105105 // When the first bits are 0, bigint bit size is less than 32 and need to re-add the bits
@@ -116,13 +116,25 @@ export function decryptValue(ctAmount: bigint, userKey: string) {
116116 return parseInt ( decryptedMessage . toString ( "hex" ) , block_size )
117117}
118118
119+ export function decryptString ( ciphertext : Array < bigint > , userKey : string ) {
120+ let decryptedStr = new Array < number > ( ciphertext . length )
121+
122+ for ( let i = 0 ; i < ciphertext . length ; i ++ ) {
123+ decryptedStr [ i ] = decryptUint ( ciphertext [ i ] , userKey )
124+ }
125+
126+ let decoder = new TextDecoder ( )
127+
128+ return decoder . decode ( new Uint8Array ( decryptedStr ) )
129+ }
130+
119131export function sign ( message : string , privateKey : string ) {
120132 const key = new SigningKey ( privateKey )
121133 const sig = key . sign ( message )
122134 return Buffer . concat ( [ getBytes ( sig . r ) , getBytes ( sig . s ) , getBytes ( `0x0${ sig . v - 27 } ` ) ] )
123135}
124136
125- export async function prepareIT (
137+ export function prepareUintIT (
126138 plaintext : bigint ,
127139 sender : { wallet : BaseWallet ; userKey : string } ,
128140 contractAddress : string ,
@@ -149,6 +161,26 @@ export async function prepareIT(
149161 return { ctInt, signature }
150162}
151163
164+ export async function prepareStringIT (
165+ plaintext : string ,
166+ sender : { wallet : BaseWallet ; userKey : string } ,
167+ contractAddress : string ,
168+ functionSelector : string
169+ ) {
170+ let encoder = new TextEncoder ( )
171+
172+ let encodedStr = encoder . encode ( plaintext )
173+
174+ let encryptedStr = new Array < { ciphertext : bigint , signature : Buffer } > ( plaintext . length )
175+
176+ for ( let i = 0 ; i < plaintext . length ; i ++ ) {
177+ const { ctInt, signature } = prepareUintIT ( BigInt ( encodedStr [ i ] ) , sender , contractAddress , functionSelector )
178+ encryptedStr [ i ] = { ciphertext : ctInt , signature }
179+ }
180+
181+ return encryptedStr
182+ }
183+
152184export function createRandomUserKey ( ) {
153185 return crypto . randomBytes ( block_size ) . toString ( "hex" )
154- }
186+ }
0 commit comments