Skip to content

Commit

Permalink
testnet support
Browse files Browse the repository at this point in the history
  • Loading branch information
spencer-xyz committed Oct 9, 2024
1 parent 5ead291 commit ffe9aaf
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -20,12 +20,12 @@
"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": {
"build": "tsc",
"prepare": "tsc",
"test": "mocha --require ts-node/register ./test/wallet.test.ts"
}
}
}
2 changes: 1 addition & 1 deletion src/providers/JsonRpcSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export enum CotiNetwork {
Devnet = 'https://devnet.coti.io/rpc'
Testnet = 'https://testnet.coti.io/rpc'
}

export type OnboardInfo = {
Expand Down
10 changes: 8 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const ONBOARD_CONTRACT_ADDRESS = "0x413370ed41FB9EE3aea0B1B91FD336cC0be1Bad6"
export const ONBOARD_CONTRACT_ADDRESS = "0x60eA13A5f263f77f7a2832cfEeF1729B1688477c"

export const ONBOARD_CONTRACT_ABI = [
{
Expand All @@ -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"
}
],
Expand Down
10 changes: 6 additions & 4 deletions src/utils/onboard.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit ffe9aaf

Please sign in to comment.