Skip to content

Commit e22f554

Browse files
spencer-xyzgmesika-coti
authored andcommitted
enable browser support
1 parent 40e7391 commit e22f554

File tree

6 files changed

+254
-135
lines changed

6 files changed

+254
-135
lines changed

README.md

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,7 @@ Decrypts the given ciphertext using RSA-OAEP with the provided private key.
155155

156156
### Input text decryption/encryption and Signing
157157

158-
### 1. `decryptValue(ctAmount: bigint, userKey: string)`
159-
160-
Decrypts the given ciphertext amount using the user's key.
161-
162-
- **Parameters**:
163-
- `ctAmount`: Ciphertext amount in `bigint`.
164-
- `userKey`: User's key in hexadecimal format.
165-
- **Returns**:
166-
- Decrypted value as an integer.
167-
168-
### 2. `sign(message: string, privateKey: string)`
158+
### 1. `sign(message: string, privateKey: string)`
169159

170160
Signs the given message using the provided private key.
171161

@@ -175,7 +165,7 @@ Signs the given message using the provided private key.
175165
- **Returns**:
176166
- Signature as a concatenation of `r`, `s`, and `v` values.
177167

178-
### 3. `signInputText(wallet: BaseWallet, userKey: string, contractAddress: string , functionSelector: string, ct: Buffer)`
168+
### 2. `signInputText(wallet: BaseWallet, userKey: string, contractAddress: string , functionSelector: string, ct: Buffer)`
179169

180170
Signs the given message using the provided private key.
181171

@@ -188,7 +178,7 @@ Signs the given message using the provided private key.
188178
- **Returns**:
189179
- `signature`: The generated signature.
190180

191-
### 4.`buildInputText(plaintext: bigint, sender: { wallet: BaseWallet; userKey: string }, contractAddress: string, functionSelector: string)`
181+
### 3.`buildInputText(plaintext: bigint, sender: { wallet: BaseWallet; userKey: string }, contractAddress: string, functionSelector: string)`
192182

193183
Builds input text by encrypting the plaintext and signing it.
194184
**Parameters:**
@@ -203,7 +193,7 @@ Builds input text by encrypting the plaintext and signing it.
203193
- `intCipherText`: The integer representation of the ciphertext.
204194
- `signature`: The generated signature.
205195

206-
### 5.`buildStringInputText((plaintext: string, sender: { wallet: BaseWallet; userKey: string }, contractAddress: string, functionSelector: string)`
196+
### 4.`buildStringInputText((plaintext: string, sender: { wallet: BaseWallet; userKey: string }, contractAddress: string, functionSelector: string)`
207197

208198
Builds input text by encrypting the plaintext and signing it.
209199
**Parameters:**
@@ -218,6 +208,32 @@ Builds input text by encrypting the plaintext and signing it.
218208
- `intCipherText`: The integer representation of the ciphertext.
219209
- `signature`: The generated signature.
220210

211+
### 5. `decryptUint(ciphertext: bigint, userKey: string)`
212+
213+
Decrypts a value stored in a contract using a user key.
214+
215+
**Parameters:**
216+
217+
- `ciphertext`: The value to be decrypted.
218+
- `userKey`: The user's AES key.
219+
220+
**Returns:**
221+
222+
- `result`: The decrypted value.
223+
224+
### 6. `decryptString(ciphertext: Array<bigint>, userKey: string)`
225+
226+
Decrypts a value stored in a contract using a user key.
227+
228+
**Parameters:**
229+
230+
- `ciphertext`: The value to be decrypted.
231+
- `userKey`: The user's AES key.
232+
233+
**Returns:**
234+
235+
- `result`: The decrypted value.
236+
221237
# ether_utils.ts
222238

223239
This TypeScript library, `ethers_utils.ts`, provides ethers functionality to interact with the COTI-v2 network. Below is
@@ -365,33 +381,7 @@ Checks if the provided gas units are sufficient for the transaction.
365381
- `valid`: Boolean indicating if the gas units are sufficient.
366382
- `gas_estimate`: The estimated gas units.
367383

368-
### 13. `decryptUint(ciphertext: bigint, userKey: string)`
369-
370-
Decrypts a value stored in a contract using a user key.
371-
372-
**Parameters:**
373-
374-
- `ciphertext`: The value to be decrypted.
375-
- `userKey`: The user's AES key.
376-
377-
**Returns:**
378-
379-
- `result`: The decrypted value.
380-
381-
### 14. `decryptString(ciphertext: Array<bigint>, userKey: string)`
382-
383-
Decrypts a value stored in a contract using a user key.
384-
385-
**Parameters:**
386-
387-
- `ciphertext`: The value to be decrypted.
388-
- `userKey`: The user's AES key.
389-
390-
**Returns:**
391-
392-
- `result`: The decrypted value.
393-
394-
### 15.`isProviderConnected(provider: Provider)`
384+
### 13.`isProviderConnected(provider: Provider)`
395385

396386
Checks if the Web3 instance is connected.
397387

@@ -403,7 +393,7 @@ Checks if the Web3 instance is connected.
403393

404394
- `connected`: Boolean indicating if Web3 is connected.
405395

406-
### 16. `getNativeBalance(address: string, provider: Provider)`
396+
### 14. `getNativeBalance(address: string, provider: Provider)`
407397

408398
Retrieves the native balance of an address in Ether.
409399

package-lock.json

Lines changed: 37 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
"build": "tsc"
1212
},
1313
"devDependencies": {
14+
"@types/node-forge": "^1.3.11",
1415
"ethers": "^6.12.1",
1516
"ts-node": "^10.9.2",
1617
"typescript": "^5.4.5"
18+
},
19+
"dependencies": {
20+
"node-forge": "^1.3.1"
1721
}
1822
}

src/account/confidential-account.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {BaseWallet, Contract, Provider, Wallet} from "ethers"
2-
import {buildInputText} from "../crypto_utils"
2+
import {buildInputText, buildStringInputText, decryptString, decryptUint} from "../crypto_utils"
33
import {onboard} from "./onboard"
4-
import {decryptUint, initEtherProvider} from "../ethers_utils";
4+
import {initEtherProvider} from "../ethers_utils";
55

66
export class ConfidentialAccount {
77
constructor(readonly wallet: BaseWallet, readonly userKey: string) {
@@ -20,7 +20,15 @@ export class ConfidentialAccount {
2020
return decryptUint(ciphertextValue, this.userKey)
2121
}
2222

23-
public encryptValue(plaintextValue: bigint | number, contractAddress: string, functionSelector: string) {
23+
public decryptString(ciphertextValue: bigint[]) {
24+
return decryptString(ciphertextValue, this.userKey)
25+
}
26+
27+
public encryptUint(plaintextValue: bigint | number, contractAddress: string, functionSelector: string) {
2428
return buildInputText(BigInt(plaintextValue), this, contractAddress, functionSelector)
2529
}
30+
31+
public encryptString(plaintextValue: string, contractAddress: string, functionSelector: string) {
32+
return buildStringInputText(plaintextValue, this, contractAddress, functionSelector)
33+
}
2634
}

src/account/onboard.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,20 @@ export async function onboard(user: BaseWallet, contract = getDefaultContract(us
1010
const {publicKey, privateKey} = generateRSAKeyPair()
1111

1212
const signedEK = sign(keccak256(publicKey), user.privateKey)
13+
1314
const receipt = await (await contract.OnboardAccount(publicKey, signedEK, {gasLimit: 12000000})).wait()
15+
1416
if (!receipt || !receipt.logs || !receipt.logs[0]) {
1517
throw new Error("failed to onboard account")
1618
}
19+
1720
const decodedLog = contract.interface.parseLog(receipt.logs[0])
21+
1822
if (!decodedLog) {
1923
throw new Error("failed to onboard account")
2024
}
21-
const encryptedKey = decodedLog.args.userKey
22-
const buf = Buffer.from(encryptedKey.substring(2), "hex")
23-
return decryptRSA(privateKey, buf).toString("hex")
25+
26+
const encryptedKey = decodedLog.args.userKey.substring(2)
27+
28+
return decryptRSA(privateKey, encryptedKey)
2429
}

0 commit comments

Comments
 (0)