From ffe9aaf061f95f71829a67a95f4b7f3322155e9b Mon Sep 17 00:00:00 2001 From: Spencer Miller Date: Wed, 9 Oct 2024 13:48:24 +0300 Subject: [PATCH] testnet support --- package-lock.json | 12 ++++++------ package.json | 6 +++--- src/providers/JsonRpcSigner.ts | 2 +- src/types.ts | 2 +- src/utils/constants.ts | 10 ++++++++-- src/utils/onboard.ts | 10 ++++++---- src/wallet/Wallet.ts | 2 +- test/wallet.test.ts | 4 ++-- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6f57e12..cf19005 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "@coti-io/coti-ethers", - "version": "0.1.0", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@coti-io/coti-ethers", - "version": "0.1.0", + "version": "1.0.0", "license": "Apache 2.0", "dependencies": { - "@coti-io/coti-sdk-typescript": "0.6.6", + "@coti-io/coti-sdk-typescript": "1.0.3", "ethers": "^6.13.1" }, "devDependencies": { @@ -28,9 +28,9 @@ "integrity": "sha512-96Z2IP3mYmF1Xg2cDm8f1gWGf/HUVedQ3FMifV4kG/PQ4yEP51xDtRAEfhVNt5f/uzpNkZHwWQuUcu6D6K+Ekw==" }, "node_modules/@coti-io/coti-sdk-typescript": { - "version": "0.6.6", - "resolved": "https://registry.npmjs.org/@coti-io/coti-sdk-typescript/-/coti-sdk-typescript-0.6.6.tgz", - "integrity": "sha512-bRnEWNWTLLk04fVNUUTyvjX6Tczdez2QLdIt2GM7pOyfSM8+d8bKgBgCyAP5tXOJqHD09xvXeDHf8B4yGGRJXQ==", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@coti-io/coti-sdk-typescript/-/coti-sdk-typescript-1.0.3.tgz", + "integrity": "sha512-Y4LWBmRyQSffD0cIkd6pGCdtJ/IRYszO/MHa4g/kgbCIc3iVnP6zVkjROaH1mIgWGprtj/JoQ54Gms4ABHsC0w==", "dependencies": { "node-forge": "^1.3.1" } diff --git a/package.json b/package.json index 59b8588..6e83d80 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@coti-io/coti-ethers", - "version": "0.1.0", + "version": "1.0.0", "license": "Apache 2.0", "description": "A Web3 library for interacting with the COTI V2 blockchain.", "main": "dist/index.js", @@ -20,7 +20,7 @@ "typescript": "^5.4.5" }, "dependencies": { - "@coti-io/coti-sdk-typescript": "0.6.6", + "@coti-io/coti-sdk-typescript": "1.0.3", "ethers": "^6.13.1" }, "scripts": { @@ -28,4 +28,4 @@ "prepare": "tsc", "test": "mocha --require ts-node/register ./test/wallet.test.ts" } -} +} \ No newline at end of file diff --git a/src/providers/JsonRpcSigner.ts b/src/providers/JsonRpcSigner.ts index 60445b8..7865c21 100644 --- a/src/providers/JsonRpcSigner.ts +++ b/src/providers/JsonRpcSigner.ts @@ -212,7 +212,7 @@ export class JsonRpcSigner extends BaseJsonRpcSigner { this.setAesKey(await recoverAesFromTx(this._userOnboardInfo.txHash, this._userOnboardInfo.rsaKey, onboardContractAddress, this.provider)) else { - const accountBalance = await getAccountBalance(this.address, this.provider || getDefaultProvider(CotiNetwork.Devnet)) + const accountBalance = await getAccountBalance(this.address, this.provider || getDefaultProvider(CotiNetwork.Testnet)) if (accountBalance > BigInt(0)) this.setUserOnboardInfo(await onboard(onboardContractAddress, this)) else diff --git a/src/types.ts b/src/types.ts index 6cbeda0..0304db0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ export enum CotiNetwork { - Devnet = 'https://devnet.coti.io/rpc' + Testnet = 'https://testnet.coti.io/rpc' } export type OnboardInfo = { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 077264c..5ddc965 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,4 +1,4 @@ -export const ONBOARD_CONTRACT_ADDRESS = "0x413370ed41FB9EE3aea0B1B91FD336cC0be1Bad6" +export const ONBOARD_CONTRACT_ADDRESS = "0x60eA13A5f263f77f7a2832cfEeF1729B1688477c" export const ONBOARD_CONTRACT_ABI = [ { @@ -13,7 +13,13 @@ export const ONBOARD_CONTRACT_ABI = [ { "indexed": false, "internalType": "bytes", - "name": "userKey", + "name": "userKey1", + "type": "bytes" + }, + { + "indexed": false, + "internalType": "bytes", + "name": "userKey2", "type": "bytes" } ], diff --git a/src/utils/onboard.ts b/src/utils/onboard.ts index 683d27c..9a90ae2 100644 --- a/src/utils/onboard.ts +++ b/src/utils/onboard.ts @@ -1,6 +1,6 @@ import { Contract, ContractRunner, keccak256, Provider } from "ethers" import { getDefaultProvider } from "./network" -import { decryptRSA, generateRSAKeyPair, sign } from "@coti-io/coti-sdk-typescript" +import { decryptRSA, generateRSAKeyPair, recoverUserKey, sign } from "@coti-io/coti-sdk-typescript" import { CotiNetwork, RsaKeyPair } from "../types" import { ONBOARD_CONTRACT_ABI } from "./constants" import { Wallet } from "../wallet/Wallet" @@ -32,10 +32,12 @@ export async function onboard(defaultOnboardContractAddress: string, signer: Wal if (!decodedLog) { throw new Error("failed to onboard account") } - const encryptedKey = decodedLog.args.userKey + + const userKey1 = decodedLog.args.userKey1.substring(2); + const userKey2 = decodedLog.args.userKey2.substring(2); return { - aesKey: decryptRSA(privateKey, encryptedKey.substring(2)), + aesKey: recoverUserKey(privateKey, userKey1, userKey2), rsaKey: {publicKey: publicKey, privateKey: privateKey}, txHash: receipt.hash } @@ -53,7 +55,7 @@ export async function recoverAesFromTx(txHash: string, try { const receipt = provider ? await provider.getTransactionReceipt(txHash) - : await getDefaultProvider(CotiNetwork.Devnet).getTransactionReceipt(txHash) + : await getDefaultProvider(CotiNetwork.Testnet).getTransactionReceipt(txHash) if (!receipt || !receipt.logs || !receipt.logs[0]) { console.error("failed to get onboard tx info") diff --git a/src/wallet/Wallet.ts b/src/wallet/Wallet.ts index 2e57dd4..03c1370 100644 --- a/src/wallet/Wallet.ts +++ b/src/wallet/Wallet.ts @@ -135,7 +135,7 @@ export class Wallet extends BaseWallet { this.setAesKey(await recoverAesFromTx(this._userOnboardInfo.txHash, this._userOnboardInfo.rsaKey, onboardContractAddress, this.provider)) else { - const accountBalance = await getAccountBalance(this.address, this.provider || getDefaultProvider(CotiNetwork.Devnet)) + const accountBalance = await getAccountBalance(this.address, this.provider || getDefaultProvider(CotiNetwork.Testnet)) if (accountBalance > BigInt(0)) this.setUserOnboardInfo(await onboard(onboardContractAddress, this)) else diff --git a/test/wallet.test.ts b/test/wallet.test.ts index 2973d4f..14e17b2 100644 --- a/test/wallet.test.ts +++ b/test/wallet.test.ts @@ -11,7 +11,7 @@ describe("Wallet tests", function () { let wallet: Wallet it('Should successfully create wallet without aes key', function () { - const provider = getDefaultProvider(CotiNetwork.Devnet) + const provider = getDefaultProvider(CotiNetwork.Testnet) wallet = new Wallet(pk, provider); expect(wallet.address).to.equal(new etherWallet(pk).address); expect(wallet.getUserOnboardInfo()).to.be.undefined @@ -53,7 +53,7 @@ describe("Wallet tests", function () { }); it('Should recover aes key from tx hash and rsa key', async function () { - const provider = getDefaultProvider(CotiNetwork.Devnet) + const provider = getDefaultProvider(CotiNetwork.Testnet) const wallet = new Wallet(pk, provider); await wallet.generateOrRecoverAes() const onBoardInfo = {