Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 8050025

Browse files
author
Spencer Miller
committed
migrate to coti-ethers
1 parent 6b2a945 commit 8050025

File tree

10 files changed

+68
-76
lines changed

10 files changed

+68
-76
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
"nativeTransfer": "ts-node src/main.ts nativeTransfer"
1111
},
1212
"devDependencies": {
13-
"@coti-io/coti-sdk-typescript": "0.6.4",
13+
"@coti-io/coti-ethers": "0.1.1-b",
1414
"dotenv": "^16.4.5",
15-
"ethers": "^6.12.1",
1615
"ts-node": "^10.9.2",
1716
"typescript": "^5.4.5"
1817
}

src/examples/dataOnChain.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
1-
import {Provider} from "ethers"
2-
import {
3-
buildStringInputText,
4-
type ConfidentialAccount,
5-
decryptString,
6-
decryptUint,
7-
generateAesKey
8-
} from "@coti-io/coti-sdk-typescript"
1+
import { itString, itUint, Provider, Wallet } from "@coti-io/coti-ethers"
92
import {getContract} from "../util/contracts"
103
import {assert} from "../util/assert"
114
import {validateTxStatus} from "../util/general-utils";
125

136
const gasLimit = 12000000
147

15-
function getDataOnChainContract(user: ConfidentialAccount) {
16-
return getContract("DataOnChain", user.wallet)
8+
function getDataOnChainContract(user: Wallet) {
9+
return getContract("DataOnChain", user)
1710
}
1811

19-
export async function dataOnChainExample(provider: Provider, user: ConfidentialAccount) {
12+
export async function dataOnChainExample(provider: Provider, user: Wallet) {
2013
const contract = getDataOnChainContract(user)
2114
const value = BigInt(100)
2215
console.log(`setting network encrypted value: ${value}`)
2316
await (await contract.setSomeEncryptedValue(value, {gasLimit})).wait()
2417

2518
const networkEncryptedValue = await contract.getNetworkSomeEncryptedValue()
2619
console.log(`Network encrypted value: ${networkEncryptedValue}`)
27-
console.log(`Network decrypted value: ${user.decryptUint(networkEncryptedValue)}`)
20+
console.log(`Network decrypted value: ${await user.decryptValue(networkEncryptedValue)}`)
2821

2922
await (await contract.setUserSomeEncryptedValue({gasLimit})).wait()
3023
console.log(`setting user encrypted value: ${value}`)
3124

3225
const userEncryptedValue = await contract.getUserSomeEncryptedValue()
3326
console.log(`User encrypted value: ${userEncryptedValue}`)
34-
console.log(`User decrypted value: ${user.decryptUint(userEncryptedValue)}`)
35-
36-
const otherUserKey = generateAesKey()
37-
console.log(`Other User decrypted value: ${decryptUint(userEncryptedValue, otherUserKey)}`)
27+
console.log(`User decrypted value: ${await user.decryptValue(userEncryptedValue)}`)
3828

3929
const value2 = BigInt(555)
4030
await setValueWithEncryptedInput(contract, user, value2)
4131

4232
await (await contract.add({gasLimit})).wait()
4333

4434
const encryptedResult = await contract.getUserArithmeticResult()
45-
const decryptedResult = user.decryptUint(encryptedResult)
35+
const decryptedResult = await user.decryptValue(encryptedResult)
4636
const expectedResult = value + value2
4737
assert(
4838
decryptedResult === expectedResult,
@@ -55,10 +45,10 @@ export async function dataOnChainExample(provider: Provider, user: ConfidentialA
5545
}
5646

5747
async function testUserEncryptedString(contract: ReturnType<typeof getDataOnChainContract>,
58-
user: ConfidentialAccount) {
48+
user: Wallet) {
5949
const testString = 'test string'
6050
const func = contract.setSomeEncryptedStringEncryptedInput
61-
const encryptedString = await buildStringInputText(testString, user, await contract.getAddress(), func.fragment.selector)
51+
const encryptedString = await user.encryptValue(testString, await contract.getAddress(), func.fragment.selector) as itString
6252
let response = await (await contract.setSomeEncryptedStringEncryptedInput(encryptedString, {gasLimit})).wait()
6353
if (!validateTxStatus(response)) {
6454
throw Error("tx setSomeEncryptedStringEncryptedInput failed")
@@ -68,7 +58,7 @@ async function testUserEncryptedString(contract: ReturnType<typeof getDataOnChai
6858
throw Error("tx to setUserSomeEncryptedStringEncryptedInput failed")
6959
}
7060
const userEncyData = await contract.getUserSomeEncryptedStringEncryptedInput()
71-
const decryptedUserString: string = decryptString(userEncyData, user.userKey)
61+
const decryptedUserString = await user.decryptValue(userEncyData)
7262
assert(testString === decryptedUserString,
7363
`Expected test result to be ${testString}, but got ${decryptedUserString}`
7464
)
@@ -77,22 +67,22 @@ async function testUserEncryptedString(contract: ReturnType<typeof getDataOnChai
7767

7868
async function setValueWithEncryptedInput(
7969
contract: ReturnType<typeof getDataOnChainContract>,
80-
user: ConfidentialAccount,
70+
user: Wallet,
8171
value: bigint
8272
) {
8373
console.log(`setting network encrypted value using user encrypted value: ${value}`)
8474
const func = contract.setSomeEncryptedValueEncryptedInput
8575
const {
8676
ciphertext,
8777
signature
88-
} = user.encryptUint(value.valueOf(), await contract.getAddress(), func.fragment.selector)
78+
} = await user.encryptValue(value.valueOf(), await contract.getAddress(), func.fragment.selector) as itUint
8979

9080
await (await func(ciphertext, signature, {gasLimit})).wait()
9181

9282
await (await contract.setUserSomeEncryptedValueEncryptedInput({gasLimit})).wait()
9383

9484
const userEncryptedValue = await contract.getUserSomeEncryptedValueEncryptedInput()
95-
const decryptedValue = user.decryptUint(userEncryptedValue)
85+
const decryptedValue = await user.decryptValue(userEncryptedValue)
9686
assert(decryptedValue === value, `Expected value to be ${value}, but got ${decryptedValue}`)
9787
console.log(`User decrypted using user encrypted value: ${decryptedValue}`)
9888
}

src/examples/erc20.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
import {Provider, Wallet} from "ethers"
2-
import {buildInputText, type ConfidentialAccount, decryptUint} from "@coti-io/coti-sdk-typescript"
1+
import {itUint, Provider, Wallet} from "@coti-io/coti-ethers"
32
import {getContract} from "../util/contracts"
43
import {assert} from "../util/assert"
54

65
const gasLimit = 12000000
76

8-
async function assertBalance(token: ReturnType<typeof getTokenContract>, amount: number, user: ConfidentialAccount) {
7+
async function assertBalance(token: ReturnType<typeof getTokenContract>, amount: number, user: Wallet) {
98
const ctBalance = await token.balanceOf()
10-
let balance = Number(decryptUint(ctBalance, user.userKey))
9+
let balance = Number(await user.decryptValue(ctBalance))
1110
assert(balance === amount, `Expected balance to be ${amount}, but got ${balance}`)
1211
return balance
1312
}
1413

1514
async function assertAllowance(
1615
token: ReturnType<typeof getTokenContract>,
1716
amount: number,
18-
owner: ConfidentialAccount,
17+
owner: Wallet,
1918
spenderAddress: string
2019
) {
21-
const ctAllowance = await token.allowance(owner.wallet.address, spenderAddress)
22-
let allowance = Number(decryptUint(ctAllowance, owner.userKey))
20+
const ctAllowance = await token.allowance(owner.address, spenderAddress)
21+
let allowance = Number(await owner.decryptValue(ctAllowance))
2322
assert(allowance === amount, `Expected allowance to be ${amount}, but got ${allowance}`)
2423
}
2524

26-
function getTokenContract(user: ConfidentialAccount) {
27-
return getContract("ERC20Example", user.wallet)
25+
function getTokenContract(user: Wallet) {
26+
return getContract("ERC20Example", user)
2827
}
2928

30-
export async function erc20Example(provider: Provider, user: ConfidentialAccount) {
29+
export async function erc20Example(provider: Provider, user: Wallet) {
3130
const token = getTokenContract(user)
3231
const otherWallet = new Wallet(Wallet.createRandom(provider).privateKey)
3332

3433
const transferAmount = 5
3534

36-
let balance = Number(decryptUint(await token.balanceOf(), user.userKey))
35+
let balance = Number(await user.decryptValue(await token.balanceOf()))
3736
if (balance === 0) {
3837
await (await token.setBalance(100000000, {gasLimit})).wait()
3938
balance = await assertBalance(token, 100000000, user)
@@ -53,7 +52,7 @@ export async function erc20Example(provider: Provider, user: ConfidentialAccount
5352
async function clearTransfer(
5453
token: ReturnType<typeof getTokenContract>,
5554
initlalBalance: number,
56-
owner: ConfidentialAccount,
55+
owner: Wallet,
5756
alice: Wallet,
5857
transferAmount: number
5958
) {
@@ -71,15 +70,15 @@ async function clearTransfer(
7170
async function confidentialTransfer(
7271
token: ReturnType<typeof getTokenContract>,
7372
initlalBalance: number,
74-
owner: ConfidentialAccount,
73+
owner: Wallet,
7574
alice: Wallet,
7675
transferAmount: number
7776
) {
7877
console.log("************* Transfer confidential ", transferAmount, " from my account to Alice *************")
7978

8079
const func = token["transfer(address,uint256,bytes,bool)"]
8180
const selector = func.fragment.selector
82-
const {ciphertext, signature} = await buildInputText(BigInt(transferAmount), owner, await token.getAddress(), selector)
81+
const {ciphertext, signature} = await owner.encryptValue(BigInt(transferAmount), await token.getAddress(), selector) as itUint
8382

8483
await (await func(alice.address, ciphertext, signature, false, {gasLimit})).wait()
8584
return await assertBalance(token, initlalBalance - transferAmount, owner)
@@ -88,7 +87,7 @@ async function confidentialTransfer(
8887
async function clearTransferFromWithoutAllowance(
8988
token: ReturnType<typeof getTokenContract>,
9089
initlalBalance: number,
91-
owner: ConfidentialAccount,
90+
owner: Wallet,
9291
alice: Wallet,
9392
transferAmount: number
9493
) {
@@ -101,14 +100,14 @@ async function clearTransferFromWithoutAllowance(
101100
await (await token.approveClear(alice.address, 0, {gasLimit})).wait()
102101

103102
const func = token["transferFrom(address,address,uint64,bool)"]
104-
await (await func(owner.wallet.address, alice.address, transferAmount, true, {gasLimit})).wait()
103+
await (await func(owner.address, alice.address, transferAmount, true, {gasLimit})).wait()
105104

106105
return await assertBalance(token, initlalBalance, owner)
107106
}
108107

109108
async function clearApprove(
110109
token: ReturnType<typeof getTokenContract>,
111-
owner: ConfidentialAccount,
110+
owner: Wallet,
112111
alice: Wallet,
113112
approveAmount: number
114113
) {
@@ -119,7 +118,7 @@ async function clearApprove(
119118

120119
async function confidentialApprove(
121120
token: ReturnType<typeof getTokenContract>,
122-
owner: ConfidentialAccount,
121+
owner: Wallet,
123122
alice: Wallet,
124123
approveAmount: number
125124
) {
@@ -129,7 +128,7 @@ async function confidentialApprove(
129128

130129
const func = token["approve(address,uint256,bytes)"]
131130
const selector = func.fragment.selector
132-
const {ciphertext, signature} = await buildInputText(BigInt(approveAmount), owner, await token.getAddress(), selector)
131+
const {ciphertext, signature} = await await owner.encryptValue(BigInt(approveAmount), await token.getAddress(), selector) as itUint
133132
await (await func(alice.address, ciphertext, signature, {gasLimit})).wait()
134133

135134
await assertAllowance(token, approveAmount, owner, alice.address)
@@ -138,31 +137,31 @@ async function confidentialApprove(
138137
async function clearTransferFrom(
139138
token: ReturnType<typeof getTokenContract>,
140139
initlalBalance: number,
141-
owner: ConfidentialAccount,
140+
owner: Wallet,
142141
alice: Wallet,
143142
transferAmount: number
144143
) {
145144
console.log("************* TransferFrom clear ", transferAmount, " from my account to Alice *************")
146145

147146
const func = token["transferFrom(address,address,uint64,bool)"]
148-
await (await func(owner.wallet.address, alice.address, transferAmount, true, {gasLimit})).wait()
147+
await (await func(owner.address, alice.address, transferAmount, true, {gasLimit})).wait()
149148

150149
return await assertBalance(token, initlalBalance - transferAmount, owner)
151150
}
152151

153152
async function confidentialTransferFrom(
154153
token: ReturnType<typeof getTokenContract>,
155154
initlalBalance: number,
156-
owner: ConfidentialAccount,
155+
owner: Wallet,
157156
alice: Wallet,
158157
transferAmount: number
159158
) {
160159
console.log("************* TransferFrom confidential ", transferAmount, " from my account to Alice *************")
161160

162161
const func = token["transferFrom(address,address,uint256,bytes,bool)"]
163162
const selector = func.fragment.selector
164-
let {ciphertext, signature} = await buildInputText(BigInt(transferAmount), owner, await token.getAddress(), selector)
165-
await (await func(owner.wallet.address, alice.address, ciphertext, signature, false, {gasLimit})).wait()
163+
let {ciphertext, signature} = await owner.encryptValue(BigInt(transferAmount), await token.getAddress(), selector) as itUint
164+
await (await func(owner.address, alice.address, ciphertext, signature, false, {gasLimit})).wait()
166165

167166
return await assertBalance(token, initlalBalance - transferAmount, owner)
168167
}

src/examples/nativeTransfer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {Provider, Wallet} from "ethers";
2-
import {getNativeBalance, transferNative} from "@coti-io/coti-sdk-typescript";
1+
import {getNativeBalance, Provider, transferNative, Wallet} from "@coti-io/coti-ethers";
32
import {getWallet} from "../util/general-utils";
43

54
export async function nativeTransfer(provider: Provider) {

src/main.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@ import {erc20Example} from "./examples/erc20"
44
import {loadDeployments} from "./util/contracts"
55
import {dataOnChainExample} from "./examples/dataOnChain"
66
import {
7-
initEtherProvider,
7+
CotiNetwork,
8+
getDefaultProvider,
89
isProviderConnected,
910
printAccountDetails,
1011
printNetworkDetails,
1112
validateAddress
12-
} from "@coti-io/coti-sdk-typescript/dist/ethers_utils";
13+
} from "@coti-io/coti-ethers";
1314
import {nativeTransfer} from "./examples/nativeTransfer";
1415

1516
dotenv.config()
1617

1718
async function main() {
1819
loadDeployments()
19-
const provider = initEtherProvider();
20+
const provider = getDefaultProvider(CotiNetwork.Devnet);
2021
if (!await isProviderConnected(provider))
2122
throw Error('provider not connected')
2223
await printNetworkDetails(provider)
2324

2425
const owner = await setupAccount(provider)
25-
await printAccountDetails(provider, owner.wallet.address)
26+
await printAccountDetails(provider, owner.address)
2627

27-
const validAddress = await validateAddress(owner.wallet.address)
28+
const validAddress = await validateAddress(owner.address)
2829
if (!validAddress.valid) {
2930
throw Error('Invalid address')
3031
}

src/util/contracts.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from "fs"
22
import path from "path"
3-
import { Contract, Interface, ContractRunner } from "ethers"
3+
import { Contract, Interface, ContractRunner } from "@coti-io/coti-ethers"
44
import type { ERC20Example, AccountOnboard, DataOnChain } from "../../confidentiality-contracts/typechain-types"
55

66
type DeployedContract = { ERC20Example: ERC20Example; AccountOnboard: AccountOnboard; DataOnChain: DataOnChain }
@@ -10,14 +10,11 @@ const deployedContracts: Record<string, Contract> = {}
1010

1111
export function loadDeployments() {
1212
const files = fs.readdirSync(deploymentsDir)
13-
// console.log(`Found ${files.length} deployments -> ${files.join(", ")}`)
1413

1514
files.map((f) => {
1615
const { address, abi } = JSON.parse(fs.readFileSync(path.join(deploymentsDir, f), "utf-8"))
1716
deployedContracts[f.replace(".json", "")] = new Contract(address, Interface.from(JSON.stringify(abi)))
1817
})
19-
20-
// console.log("Loaded all deployments", Object.keys(deployedContracts))
2118
}
2219

2320
export function getContract<C extends keyof DeployedContract>(name: C, contractRunner: ContractRunner) {

src/util/general-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ContractTransactionReceipt, Provider, Wallet} from "ethers";
1+
import {Provider, Wallet} from "@coti-io/coti-ethers";
22
import fs from "fs";
33

44
export function getWallet(provider: Provider) {

src/util/onboard.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import {ConfidentialAccount, getAccountBalance} from "@coti-io/coti-sdk-typescript/"
2-
import { Wallet, Provider } from "ethers"
1+
import { getAccountBalance, Provider, Wallet } from "@coti-io/coti-ethers"
32
import {getWallet, setEnvValue} from "./general-utils";
43

54
export async function setupAccount(provider: Provider) {
@@ -10,15 +9,15 @@ export async function setupAccount(provider: Provider) {
109

1110
const toAccount = async (wallet: Wallet, userKey: string | undefined) => {
1211
if (userKey) {
13-
return new ConfidentialAccount(wallet, userKey)
12+
return wallet
1413
}
1514

1615
console.log("************* Onboarding user ", wallet.address, " *************")
17-
const account = await ConfidentialAccount.onboard(wallet)
16+
await wallet.generateOrRecoverAes()
1817
console.log("************* Onboarded! created user key and saved into .env file *************")
1918

20-
setEnvValue("USER_KEY", account.userKey)
21-
return account
19+
setEnvValue("USER_KEY", wallet.getUserOnboardInfo()?.aesKey!)
20+
return wallet
2221
}
2322

2423
return toAccount(wallet, process.env.USER_KEY)

0 commit comments

Comments
 (0)