Skip to content

Commit

Permalink
testnet onboard account.
Browse files Browse the repository at this point in the history
change provider default rpc url
  • Loading branch information
eitanz-coti committed Sep 16, 2024
1 parent 1430b52 commit c55aebd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 57 deletions.
84 changes: 45 additions & 39 deletions src/account/onboard-contract.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
export const ONBOARD_CONTRACT_ADDRESS = "0x413370ed41FB9EE3aea0B1B91FD336cC0be1Bad6"
export const ONBOARD_CONTRACT_ADDRESS = "0x60eA13A5f263f77f7a2832cfEeF1729B1688477c"
export const ONBOARD_CONTRACT_ABI = [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_from",
"type": "address"
},
{
"indexed": false,
"internalType": "bytes",
"name": "userKey",
"type": "bytes"
}
],
"name": "AccountOnboarded",
"type": "event"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "publicKey",
"type": "bytes"
},
{
"internalType": "bytes",
"name": "signedEK",
"type": "bytes"
}
],
"name": "onboardAccount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "_from",
"type": "address"
},
{
"indexed": false,
"internalType": "bytes",
"name": "userKey1",
"type": "bytes"
},
{
"indexed": false,
"internalType": "bytes",
"name": "userKey2",
"type": "bytes"
}
],
"name": "AccountOnboarded",
"type": "event"
},
{
"inputs": [
{
"internalType": "bytes",
"name": "publicKey",
"type": "bytes"
},
{
"internalType": "bytes",
"name": "signedEK",
"type": "bytes"
}
],
"name": "onboardAccount",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
]
28 changes: 15 additions & 13 deletions src/account/onboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BaseWallet, Contract, keccak256, Signer} from "ethers"
import {decryptRSA, generateRSAKeyPair, sign} from "../crypto_utils"
import {generateRSAKeyPair, recoverUserKey, sign} from "../crypto_utils"
import {ONBOARD_CONTRACT_ABI, ONBOARD_CONTRACT_ADDRESS} from "./onboard-contract"

function getDefaultContract(wallet: Signer) {
Expand All @@ -9,16 +9,18 @@ function getDefaultContract(wallet: Signer) {
export async function onboard(user: BaseWallet, contract = getDefaultContract(user)) {
const {publicKey, privateKey} = generateRSAKeyPair()

const signedEK = sign(keccak256(publicKey), user.privateKey)
const receipt = await (await contract.onboardAccount(publicKey, signedEK, { gasLimit: 12000000 })).wait()
if (!receipt || !receipt.logs || !receipt.logs[0]) {
throw new Error("failed to onboard account")
}
const decodedLog = contract.interface.parseLog(receipt.logs[0])
if (!decodedLog) {
throw new Error("failed to onboard account")
}
const encryptedKey = decodedLog.args.userKey

return decryptRSA(privateKey, encryptedKey.substring(2))
const signedEK = sign(keccak256(publicKey), user.privateKey)
const receipt = await (await contract.onboardAccount(publicKey, signedEK, {gasLimit: 12000000})).wait()
if (!receipt || !receipt.logs || !receipt.logs[0]) {
throw new Error("failed to onboard account")
}
const decodedLog = contract.interface.parseLog(receipt.logs[0])
if (!decodedLog) {
throw new Error("failed to onboard account")
}

const userKey1 = decodedLog.args.userKey1.substring(2);
const userKey2 = decodedLog.args.userKey2.substring(2);

return recoverUserKey(privateKey, userKey1, userKey2)
}
22 changes: 19 additions & 3 deletions src/crypto_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,30 @@ export function decryptRSA(privateKey: Uint8Array, ciphertext: string): string {
for (let i = 0; i < decryptedBytes.length; i++) {
userKey.push(
decryptedBytes[i]
.toString(16)
.padStart(2, '0') // make sure each cell is one byte
.toString(16)
.padStart(2, '0') // make sure each cell is one byte
)
}

return userKey.join("")
}

export function recoverUserKey(privateKey: Uint8Array, encryptedKeyShare0: string, encryptedKeyShare1: string): string {
const decryptedKeyShare0: string = decryptRSA(privateKey, encryptedKeyShare0);
const decryptedKeyShare1: string = decryptRSA(privateKey, encryptedKeyShare1);

const bufferKeyShare0 = Buffer.from(decryptedKeyShare0, 'hex'); // 'hex' because decryptRSA is returning hex-encoded string
const bufferKeyShare1 = Buffer.from(decryptedKeyShare1, 'hex');

const aesKey = Buffer.alloc(bufferKeyShare0.length);
for (let i = 0; i < bufferKeyShare0.length; i++) {
aesKey[i] = bufferKeyShare0[i] ^ bufferKeyShare1[i];
}

return aesKey.toString('hex');
}


export function sign(message: string, privateKey: string) {
const key = new SigningKey(privateKey)
const sig = key.sign(message)
Expand Down Expand Up @@ -266,4 +282,4 @@ function encryptRandomNumber(r: string | Uint8Array, key: Uint8Array) {
const encryptedR = encodeString(cipher.output.data).slice(0, BLOCK_SIZE)

return encryptedR
}
}
4 changes: 2 additions & 2 deletions src/ethers_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function getAccountBalance(address: string, provider: Provider) {
return provider.getBalance(address);
}

export function initEtherProvider(rpcUrl: string = "https://devnet.coti.io/rpc") {
export function initEtherProvider(rpcUrl: string = "https://testnet.coti.io/rpc") {
return new JsonRpcProvider(rpcUrl)
}

Expand Down Expand Up @@ -117,4 +117,4 @@ export async function isProviderConnected(provider: Provider): Promise<boolean>
if (!network)
return false
return true
}
}

0 comments on commit c55aebd

Please sign in to comment.