Skip to content

Commit 9acb79a

Browse files
committed
added support for string encryption
1 parent e6eb633 commit 9acb79a

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/account/confidential-account.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { BaseWallet, Wallet, Contract, Provider, JsonRpcProvider } from "ethers"
2-
import { decryptValue, prepareIT } from "../libs/crypto"
1+
import { BaseWallet, Wallet, Contract, Provider } from "ethers"
2+
import { decryptString, decryptUint, prepareStringIT, prepareUintIT } from "../libs/crypto"
33
import { getDefaultProvider } from "../provider"
44
import { onboard } from "./onboard"
55

66
export class ConfidentialAccount {
77
constructor(readonly wallet: BaseWallet, readonly userKey: string) {}
88

99
public decryptValue(ciphertextValue: bigint) {
10-
return decryptValue(ciphertextValue, this.userKey)
10+
return decryptUint(ciphertextValue, this.userKey)
1111
}
1212

1313
public encryptValue(plaintextValue: bigint | number, contractAddress: string, functionSelector: string) {
14-
return prepareIT(BigInt(plaintextValue), this, contractAddress, functionSelector)
14+
return prepareUintIT(BigInt(plaintextValue), this, contractAddress, functionSelector)
1515
}
1616

1717
public static async onboard(wallet: BaseWallet, contract?: Contract): Promise<ConfidentialAccount> {

src/libs/crypto.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
119131
export 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+
152184
export function createRandomUserKey() {
153185
return crypto.randomBytes(block_size).toString("hex")
154-
}
186+
}

0 commit comments

Comments
 (0)