From 064ebdddd057657a067afdeb192b3e5f3a0c741b Mon Sep 17 00:00:00 2001 From: Husienvora Date: Wed, 25 Sep 2024 18:46:50 +0530 Subject: [PATCH 1/9] sign typed message and updating public rpcs for tests --- package-lock.json | 5 +- package.json | 2 + src/lib/keyring.js | 95 +++++++++++++++++++++++++++ src/lib/test/keyring.test.js | 120 +++++++++++++++++++++++++++++++++-- 4 files changed, 216 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 11af544..46ab175 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "@getsafle/safle-vault", - "version": "2.9.0", + "version": "2.9.1", "license": "MIT", "dependencies": { "@getsafle/safle-identity-wallet": "^1.3.0", @@ -28,7 +28,9 @@ "@getsafle/vault-stacks-controller": "^1.0.5", "@getsafle/vault-velas-controller": "^1.3.1", "bip39": "^3.0.4", + "crypto": "^1.0.1", "crypto-js": "^4.1.1", + "eth-sig-util": "^3.0.1", "ethers": "^5.5.3", "jest": "^29.4.3", "limiter": "^2.1.0", @@ -6362,6 +6364,7 @@ "resolved": "https://registry.npmjs.org/eth-sig-util/-/eth-sig-util-3.0.1.tgz", "integrity": "sha512-0Us50HiGGvZgjtWTyAI/+qTzYPMLy5Q451D0Xy68bxq1QMWdoOddDwGvsqcFT27uohKgalM9z/yxplyt+mY2iQ==", "deprecated": "Deprecated in favor of '@metamask/eth-sig-util'", + "license": "ISC", "dependencies": { "ethereumjs-abi": "^0.6.8", "ethereumjs-util": "^5.1.1", diff --git a/package.json b/package.json index a70fe5f..083b958 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,9 @@ "@getsafle/vault-stacks-controller": "^1.0.5", "@getsafle/vault-velas-controller": "^1.3.1", "bip39": "^3.0.4", + "crypto": "^1.0.1", "crypto-js": "^4.1.1", + "eth-sig-util": "^3.0.1", "ethers": "^5.5.3", "jest": "^29.4.3", "limiter": "^2.1.0", diff --git a/src/lib/keyring.js b/src/lib/keyring.js index c69ee68..8ab3213 100644 --- a/src/lib/keyring.js +++ b/src/lib/keyring.js @@ -591,7 +591,102 @@ class Keyring { return { response: signedMessage }; } } + async signTypedMessage(address, data, pin, encryptionKey, rpcUrl = "") { + if ( + typeof pin != "string" || + pin.match(/^[0-9]+$/) === null || + pin < 0 || + pin.length != 6 + ) { + return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; + } + + const res = await this.validatePin(pin); + + if (res.response == false || res.error) { + return { error: ERROR_MESSAGE.INCORRECT_PIN }; + } + + const err = helper.validateEncryptionKey( + this.vault, + JSON.stringify(encryptionKey) + ); + + if (err.error) { + return { error: err.error }; + } + + const { error, response } = await this.exportPrivateKey(address, pin); + + if (error) { + return { error }; + } + + const { privateKey, isImported } = response; + + if (isImported) { + const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)); + + if (this.chain === "ethereum") { + const signedMessage = await this.keyringInstance.sign( + data, + privateKey, + web3 + ); + + return { response: signedMessage.message }; + } + + if (Chains.evmChains.hasOwnProperty(this.chain)) { + const keyringInstance = await helper.getCoinInstance(this.chain); + + const signedMessage = await keyringInstance.sign( + data, + privateKey, + web3 + ); + + return { response: signedMessage.message }; + } + + if (Chains?.[this.chain]) { + const { signedMessage } = await this[this.chain].signTypedMessage( + data, + address, + privateKey + ); + return { response: signedMessage }; + } + + return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY }; + } else { + const accounts = await this.getAccounts(); + if (accounts.response.filter((e) => e.address === address).length < 1) { + return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT }; + } + + if ( + Chains.evmChains.hasOwnProperty(this.chain) || + this.chain === "ethereum" + ) { + const msgParams = { from: address, data: data }; + + const signedMsg = await this.keyringInstance.signTypedMessage( + msgParams + ); + + return { response: signedMsg }; + } + + const { signedMessage } = await this[this.chain].signTypedMessage( + data, + address + ); + + return { response: signedMessage }; + } + } async signTransaction(rawTx, pin, rpcUrl) { if ( typeof pin != "string" || diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index d783518..c283fef 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -1,13 +1,15 @@ jest.setTimeout(30000); - +const crypto = require("crypto"); +const assert = require("assert"); const { before } = require("lodash"); let KeyRing = require("../keyring"); let Vault = require("../vault"); +const sigUtil = require("eth-sig-util"); const Web3 = require("web3"); const NETWORKS = { ethereum: { - URL: "https://eth-goerli.public.blastapi.io", - CHAIN_ID: 5, + URL: "https://eth.llamarpc.com", + CHAIN_ID: 1, }, bsc: { URL: "https://data-seed-prebsc-1-s1.binance.org:8545/", @@ -18,8 +20,8 @@ const NETWORKS = { CHAIN_ID: 80001, }, optimism: { - URL: "https://optimism-goerli.public.blastapi.io", - CHAIN_ID: 420, + URL: "https://optimism.llamarpc.com", + CHAIN_ID: 10, }, arbitrum: { URL: "https://sepolia-rollup.arbitrum.io/rpc", @@ -666,6 +668,114 @@ describe("sign", () => { }); }); +describe("sign message", () => { + Object.keys(NETWORKS).forEach((chainName) => { + const networkConfig = getNetworkConfig(chainName); + vault.changeNetwork(chainName); + test(`signMessage for ${chainName}`, async () => { + const message = `Hello, eth!`; + let web3 = new Web3(networkConfig.url); + const hash = crypto.createHash("sha256").update(message).digest(); + const hexData = web3.utils.toHex(hash); + + const msgParams = { + from: "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", + data: hexData, + }; + + const raw_sign = await vault.signMessage( + "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", + msgParams.data, + pin, + bufView, + ethUrl + ); + console.log(raw_sign); + }); + test(`signTypeMessage for ${chainName}`, async () => { + const accounts = ["0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd"]; + + // Ensure accounts is not empty + if (accounts.length === 0) { + throw new Error("No accounts found"); + } + + // console.log(accounts[0]); + + let msgParams = { + from: "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", + data: { + types: { + EIP712Domain: [ + { name: "name", type: "string" }, + { name: "version", type: "string" }, + { name: "chainId", type: "uint256" }, + { name: "verifyingContract", type: "address" }, + ], + Person: [ + { name: "name", type: "string" }, + { name: "wallet", type: "address" }, + ], + Mail: [ + { name: "from", type: "Person" }, + { name: "to", type: "Person" }, + { name: "contents", type: "string" }, + ], + }, + primaryType: "Mail", + domain: { + name: "Ether Mail", + version: "1", + chainId: NETWORKS[chainName].CHAIN_ID, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + }, + message: { + from: { + name: "Cow", + wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", + }, + to: { + name: "Bob", + wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", + }, + contents: "Hello, Bob!", + }, + }, + }; + + const rawSignature = await vault.signTypedMessage( + "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", + msgParams.data, + pin, + bufView, + networkConfig.url + ); + console.log("Raw signature:", rawSignature); + + // Verify the signature + msgParams = { ...msgParams.data, from: msgParams.from }; + console.log(); + const recoveredAddress = sigUtil.recoverTypedSignature({ + data: msgParams, + sig: rawSignature.response, + }); + + // console.log("Recovered address:", recoveredAddress); + // console.log("Original address:", msgParams.from); + + // Compare the recovered address with the original signer's address + const isSignatureValid = + recoveredAddress.toLowerCase() === msgParams.from.toLowerCase(); + + assert( + isSignatureValid, + `Signature verification failed for ${chainName}` + ); + assert(rawSignature, `Failed to Sign Message for ${chainName}`); + }); + }); +}); + describe("validateMnemonic", () => { let signUpPhrase = "join danger verb slide lava blossom garment school panel shaft damp ghost"; From 9cbf124fc5e68683b9d27404fc70de33b393893e Mon Sep 17 00:00:00 2001 From: Husienvora Date: Mon, 21 Oct 2024 11:24:19 +0530 Subject: [PATCH 2/9] adding more tests --- .env | 8 + package-lock.json | 21 +- package.json | 3 +- src/lib/keyring.js | 161 ++-- src/lib/test/keyring.test.js | 1375 +++++++++++++++++++--------------- src/lib/test/vault.test.js | 7 +- src/lib/vault.js | 5 +- 7 files changed, 929 insertions(+), 651 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..2523d9e --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +MNEMONIC="vintage reflect pass input polar enlist giggle judge render position also document" +PIN="696969" +PRIVATE_KEY_EVM="0x7a9633b8103fec11c9e855a6b6c8c072e9af311a69b92ab0ad8186b1fb57371f" +PRIVATE_KEY_BTC= "KzQfcdjDRUwpVmKKev6k2aAeJFJ359Ht9Umxdg77MTzf2E3bzGsC" +EVM_ADDRESS_1='0x6bbc122fa843f3ed30d23f8cdd9a430d1f898d07' +EVM_ADDRESS_2="0xbae949ddb4d8ac763c12f206db842b9b2d49a464" +BITCOIN_ADDRESS_1="bc1qugw5q5yrzw86wnw3lgldm8en0c736k4hvceuzl" +BITCOIN_ADDRESS_2="bc1qta5f6q32cphxt5rck3kuspukac7keqvxyuk4cl" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 46ab175..095f333 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@getsafle/vault-bitcoin-controller": "^2.0.7", "@getsafle/vault-bsc-controller": "^1.2.4", "@getsafle/vault-eth-controller": "^1.4.6", - "@getsafle/vault-evm-controller": "^1.0.0", + "@getsafle/vault-evm-controller": "^1.0.1", "@getsafle/vault-mantle-controller": "^1.0.1", "@getsafle/vault-optimism-controller": "^1.0.8", "@getsafle/vault-polygon-controller": "^1.2.8", @@ -30,6 +30,7 @@ "bip39": "^3.0.4", "crypto": "^1.0.1", "crypto-js": "^4.1.1", + "dotenv": "^16.4.5", "eth-sig-util": "^3.0.1", "ethers": "^5.5.3", "jest": "^29.4.3", @@ -1937,9 +1938,9 @@ } }, "node_modules/@getsafle/vault-evm-controller": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@getsafle/vault-evm-controller/-/vault-evm-controller-1.0.0.tgz", - "integrity": "sha512-AHL1gJNEdMdvX7iBNNpKxXA3bm8YYggpGywJ9untiyBRwZiSVXuUMmCPbqe23me8Lk1nxyl0TLyDw33iq0SIoQ==", + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@getsafle/vault-evm-controller/-/vault-evm-controller-1.0.1.tgz", + "integrity": "sha512-dlleo03yLkLRY2jMjrNml1i2h5KvRodhqD2cN2/3xnhUbHJGCf1mTf6YhZOKlpt3dn4HcriNuxwyUyjrRGgDTg==", "license": "MIT", "dependencies": { "@ethereumjs/common": "2.6.0", @@ -6070,6 +6071,18 @@ "resolved": "https://registry.npmjs.org/dom-walk/-/dom-walk-0.1.2.tgz", "integrity": "sha512-6QvTW9mrGeIegrFXdtQi9pk7O/nSK6lSdXW2eqUspN5LWD7UTji2Fqw5V2YLjBpHEoU9Xl/eUWNpDeZvoyOv2w==" }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/drbg.js": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/drbg.js/-/drbg.js-1.0.1.tgz", diff --git a/package.json b/package.json index 083b958..8942819 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "@getsafle/vault-bitcoin-controller": "^2.0.7", "@getsafle/vault-bsc-controller": "^1.2.4", "@getsafle/vault-eth-controller": "^1.4.6", - "@getsafle/vault-evm-controller": "^1.0.0", + "@getsafle/vault-evm-controller": "^1.0.1", "@getsafle/vault-mantle-controller": "^1.0.1", "@getsafle/vault-optimism-controller": "^1.0.8", "@getsafle/vault-polygon-controller": "^1.2.8", @@ -69,6 +69,7 @@ "bip39": "^3.0.4", "crypto": "^1.0.1", "crypto-js": "^4.1.1", + "dotenv": "^16.4.5", "eth-sig-util": "^3.0.1", "ethers": "^5.5.3", "jest": "^29.4.3", diff --git a/src/lib/keyring.js b/src/lib/keyring.js index 8ab3213..4094d84 100644 --- a/src/lib/keyring.js +++ b/src/lib/keyring.js @@ -623,70 +623,99 @@ class Keyring { } const { privateKey, isImported } = response; - if (isImported) { - const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl)); - - if (this.chain === "ethereum") { - const signedMessage = await this.keyringInstance.sign( - data, - privateKey, - web3 + if ( + Chains.evmChains.hasOwnProperty(this.chain) || + this.chain === "ethereum" + ) { + const signedMsg = await this.keyringInstance.customSignTypedMessage( + "0x" + privateKey, + data ); - return { response: signedMessage.message }; + return { response: signedMsg }; } + return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY }; + } - if (Chains.evmChains.hasOwnProperty(this.chain)) { - const keyringInstance = await helper.getCoinInstance(this.chain); + const accounts = await this.getAccounts(); - const signedMessage = await keyringInstance.sign( - data, - privateKey, - web3 - ); + if (accounts.response.filter((e) => e.address === address).length < 1) { + return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT }; + } - return { response: signedMessage.message }; - } + if ( + Chains.evmChains.hasOwnProperty(this.chain) || + this.chain === "ethereum" + ) { + const msgParams = { from: address, data: data }; - if (Chains?.[this.chain]) { - const { signedMessage } = await this[this.chain].signTypedMessage( - data, - address, - privateKey - ); - return { response: signedMessage }; - } + const signedMsg = await this.keyringInstance.signTypedMessage(msgParams); - return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY }; - } else { - const accounts = await this.getAccounts(); + return { response: signedMsg }; + } - if (accounts.response.filter((e) => e.address === address).length < 1) { - return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT }; - } + const { signedMessage } = await this[this.chain].signTypedMessage( + data, + address + ); - if ( - Chains.evmChains.hasOwnProperty(this.chain) || - this.chain === "ethereum" - ) { - const msgParams = { from: address, data: data }; + return { response: signedMessage }; + } + async personalSign(address, data, pin, encryptionKey, rpcUrl = "") { + if ( + typeof pin != "string" || + pin.match(/^[0-9]+$/) === null || + pin < 0 || + pin.length != 6 + ) { + return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; + } - const signedMsg = await this.keyringInstance.signTypedMessage( - msgParams - ); + const res = await this.validatePin(pin); - return { response: signedMsg }; - } + if (res.response == false || res.error) { + return { error: ERROR_MESSAGE.INCORRECT_PIN }; + } - const { signedMessage } = await this[this.chain].signTypedMessage( - data, - address + const err = helper.validateEncryptionKey( + this.vault, + JSON.stringify(encryptionKey) + ); + + if (err.error) { + return { error: err.error }; + } + + const { error, response } = await this.exportPrivateKey(address, pin); + + if (error) { + return { error }; + } + + const { privateKey, isImported } = response; + + const accounts = await this.getAccounts(); + + if (accounts.response.filter((e) => e.address === address).length < 1) { + return { error: ERROR_MESSAGE.NONEXISTENT_KEYRING_ACCOUNT }; + } + + if ( + Chains.evmChains.hasOwnProperty(this.chain) || + this.chain === "ethereum" + ) { + const signedMsg = await this.keyringInstance.customPersonalSign( + "0x" + privateKey, + data ); - return { response: signedMessage }; + return { response: signedMsg }; + } else { + return { error: ERROR_MESSAGE.UNSUPPORTED_NON_EVM_FUNCTIONALITY }; } } + async signTransaction(rawTx, pin, rpcUrl) { if ( typeof pin != "string" || @@ -716,14 +745,13 @@ class Keyring { if (isImported) { const signedTransaction = await this.keyringInstance.signTransaction( rawTx, - web3, privateKey ); return { response: signedTransaction }; } else { const signedTx = await this.keyringInstance.signTransaction( rawTx, - web3 + privateKey ); return { response: signedTx }; } @@ -1178,7 +1206,42 @@ class Keyring { }); if (isDuplicateAddress) { - return { error: ERROR_MESSAGE.ADDRESS_ALREADY_PRESENT }; + try { + Object.keys(this.decryptedVault) + .slice(0, -1) + .forEach((chain) => { + this.decryptedVault[chain]?.public?.forEach( + (account, index) => { + if (account.address === address && account.isDeleted) { + this.decryptedVault[chain].public[ + index + ].isDeleted = false; + } + } + ); + }); + + const vault = await helper.cryptography( + JSON.stringify(this.decryptedVault), + JSON.stringify(encryptionKey), + "encryption" + ); + + this.vault = vault; + + this.logs.getState().logs.push({ + timestamp: Date.now(), + action: "import-wallet", + vault: this.vault, + chain: this.chain, + address, + }); + + return { response: { vault, address } }; + } catch (error) { + console.error("Error processing duplicate address:", error); + throw error; // or handle it as appropriate for your application + } } } diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index c283fef..a74763d 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -4,43 +4,48 @@ const assert = require("assert"); const { before } = require("lodash"); let KeyRing = require("../keyring"); let Vault = require("../vault"); +require("dotenv").config(); const sigUtil = require("eth-sig-util"); const Web3 = require("web3"); const NETWORKS = { + bitcoin: { + URL: "https://bitcoin-mainnet.gateway.tatum.io", + CHAIN_ID: 999999, + }, ethereum: { - URL: "https://eth.llamarpc.com", - CHAIN_ID: 1, + URL: "https://sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", + CHAIN_ID: 11155111, }, bsc: { - URL: "https://data-seed-prebsc-1-s1.binance.org:8545/", + URL: "https://bsc-testnet.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 97, }, polygon: { - URL: "https://polygon-amoy-bor-rpc.publicnode.com", - CHAIN_ID: 80001, + URL: "https://polygon-amoy.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", + CHAIN_ID: 80002, }, optimism: { - URL: "https://optimism.llamarpc.com", - CHAIN_ID: 10, + URL: "https://optimism-sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", + CHAIN_ID: 11155420, }, arbitrum: { - URL: "https://sepolia-rollup.arbitrum.io/rpc", + URL: "https://arbitrum-sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 421614, }, mantle: { - URL: "https://rpc.mantle.xyz", - CHAIN_ID: 5001, - }, - velas: { - URL: "https://explorer.testnet.velas.com/rpc", - CHAIN_ID: 111, + URL: "https://mantle-sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", + CHAIN_ID: 5003, }, + // velas: { + // URL: "https://explorer.testnet.velas.com/rpc", + // CHAIN_ID: 111, + // }, avalanche: { - URL: "https://api.avax-test.network/ext/bc/C/rpc", + URL: "https://avalanche-fuji.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 43113, }, base: { - URL: "https://base-sepolia.blockpi.network/v1/rpc/public", + URL: "https://base-sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 84532, }, zkEVM: { @@ -64,12 +69,13 @@ const chainConfigs = { optimism: { symbol: "OP", txType: 2 }, arbitrum: { symbol: "ARB", txType: 2 }, mantle: { symbol: "MNT", txType: 2 }, - velas: { symbol: "VLX", txType: 0 }, + // velas: { symbol: "VLX", txType: 0 }, avalanche: { symbol: "AVAX", txType: 2 }, base: { symbol: "BASE", txType: 2 }, zkEVM: { symbol: "ZKEVM", txType: 2 }, bevm: { symbol: "BTC", txType: 0 }, rootstock: { symbol: "RBTC", txType: 0 }, + bitcoin: { symbol: "BTC", txType: 0 }, }; // Add the helper function @@ -90,16 +96,15 @@ const bufView = [ 105, 178, 182, 108, 174, 199, 124, 141, 155, 73, 21, 85, 81, 109, 78, 233, 152, 108, 242, 238, 192, 31, 147, 86, 174, 195, 55, 229, 4, 36, ]; -let phrase = - "fun rough treat scan glimpse region century purpose expire video remind second"; -let pin = "696969"; +let phrase = process.env.MNEMONIC; +let pin = process.env.PIN; let result; let vault = new Vault({}); let vaultAddress; let privateKey; let accAddress; -let privateKeyImp = - "0x7a9633b8103fec11c9e855a6b6c8c072e9af311a69b92ab0ad8186b1fb57371f"; +let privateKeyImp = process.env.PRIVATE_KEY_EVM; +let privateKeybtcImp = process.env.PRIVATE_KEY_BTC; let impAccAddress; let chains; @@ -108,6 +113,7 @@ const polygonRpcUrl = "https://polygon.llamarpc.com"; const bscRpcUrl = "https://rpc.ankr.com/bsc"; beforeAll(async () => { result = await vault.generateVault(bufView, pin, phrase); + vaultAddress = result.response; await vault.getAccounts(bufView); }); @@ -224,43 +230,66 @@ describe("exportPrivateKey", () => { }); describe("importWallet", () => { - test("importWallet/valid import", async () => { - let result = await vault.importWallet("0x" + privateKeyImp, pin, bufView); - impAccAddress = result.response.address; - expect(result).toHaveProperty("response.address"); - }); - - test("importWallet/valid address exists already", async () => { - let result = await vault.importWallet("0x" + privateKey, pin, bufView); - expect(result.response).toHaveProperty("vault"); - }); + const evmaddresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + const bitcoinadresses = [ + process.env.BITCOIN_ADDRESS_1, + process.env.BITCOIN_ADDRESS_2, + ]; + Object.keys(NETWORKS).map((chainName) => { + const networkConfig = getNetworkConfig(chainName); - test("importWallet/empty private key", async () => { - try { - let result = await vault.importWallet(null, pin, bufView); - } catch (e) { - expect(e.message).toBe( - "Cannot read properties of null (reading 'startsWith')" - ); + let addresses; + let impprivatekey; + if (chainName == "bitcoin") { + addresses = bitcoinadresses; + impprivatekey = privateKeybtcImp; + } else { + addresses = evmaddresses; + impprivatekey = "0x" + privateKeyImp; } - }); - test("importWallet/empty pin", async () => { - let result = await vault.importWallet("0x" + privateKey, null, bufView); - expect(result.error).toBe("Wrong pin type, format or length"); - }); - test("importWallet/incorrect pin", async () => { - let result = await vault.importWallet("0x" + privateKey, "111111", bufView); - expect(result.error).toBe("Incorrect pin"); - }); - test("importWallet/empty encryption key", async () => { - let result = await vault.importWallet("0x" + privateKey, pin, null); - expect(result.error).toBe("Incorrect Encryption Key or vault string"); - }); + test(`importWallet/valid import ${chainName}`, async () => { + let var1 = chainName; + let var2 = impprivatekey; + vault.changeNetwork(chainName); + let result = await vault.importWallet(impprivatekey, pin, bufView); + impAccAddress = result.response.address; - test("importWallet/empty all params", async () => { - let result = await vault.importWallet(null, null, null); - expect(result.error).toBe("Wrong pin type, format or length"); + expect(result).toHaveProperty("response.address"); + }); + + test("importWallet/valid address exists already", async () => { + let result = await vault.importWallet(impprivatekey, pin, bufView); + expect(result.response).toHaveProperty("vault"); + }); + + test("importWallet/empty private key", async () => { + try { + let result = await vault.importWallet(null, pin, bufView); + } catch (e) { + expect(e.message).toBe( + "Cannot read properties of null (reading 'startsWith')" + ); + } + }); + + test("importWallet/empty pin", async () => { + let result = await vault.importWallet(impprivatekey, null, bufView); + expect(result.error).toBe("Wrong pin type, format or length"); + }); + test("importWallet/incorrect pin", async () => { + let result = await vault.importWallet(impprivatekey, "111111", bufView); + expect(result.error).toBe("Incorrect pin"); + }); + test("importWallet/empty encryption key", async () => { + let result = await vault.importWallet(impprivatekey, pin, null); + expect(result.error).toBe("Incorrect Encryption Key or vault string"); + }); + + test("importWallet/empty all params", async () => { + let result = await vault.importWallet(null, null, null); + expect(result.error).toBe("Wrong pin type, format or length"); + }); }); }); @@ -522,256 +551,651 @@ describe("getBalance", () => { }); describe("updateLabel", () => { - test("updateLabel/valid", async () => { - let result = await vault.updateLabel( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - bufView, - "Wallet 1" - ); - expect(result).toHaveProperty("response"); - }); - - test("updateLabel/invalid address", async () => { - let result = await vault.updateLabel("adeded", bufView, "Wallet 1"); - expect(result.error).toBe("This address is not present in the vault"); - }); - - test("updateLabel/empty address", async () => { - let result = await vault.updateLabel(null, bufView, "Wallet 1"); - expect(result.error).toBe("This address is not present in the vault"); - }); - test("updateLabel/invalid encryption key", async () => { - let result = await vault.updateLabel( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - "afers", - "Wallet 1" - ); - expect(result.error).toBe("Incorrect Encryption Key or vault string"); - }); - test("updateLabel/empty encryption key", async () => { - let result = await vault.updateLabel( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - null, - "Wallet 1" - ); - expect(result.error).toBe("Incorrect Encryption Key or vault string"); - }); - test("updateLabel/empty label", async () => { - try { + const addresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + addresses.forEach((address, index) => { + test(`updateLabel/valid using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + await vault.importWallet("0x" + privateKeyImp, pin, bufView); let result = await vault.updateLabel( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", + address.toLowerCase(), bufView, - null + "Wallet 1" ); - } catch (e) { - expect(e.message).toBe("chainName is not defined"); - } - }); - test("updateLabel/all empty params", async () => { - let result = await vault.updateLabel(null, null, null); - expect(result.error).toBe("Incorrect Encryption Key or vault string"); - }); -}); - -describe("sign", () => { - test("sign/valid", async () => { - let data = "hello world"; - console.log("sign/valid--->", pin, ethUrl); - await vault.restoreKeyringState(vault, pin, bufView); - - let result = await vault.sign( - data, - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - pin, - ethUrl - ); - console.log("sign/valid--->", result); - expect(result.response).toHaveProperty("signature"); - }); + expect(result).toHaveProperty("response"); + }); - test("sign/empty data", async () => { - let data = "hello world"; - let result = await vault.sign( - "", - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - pin, - ethUrl - ); - expect(result.response).toHaveProperty("signature"); - }); + test(`updateLabel/invalid address using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let result = await vault.updateLabel("adeded", bufView, "Wallet 1"); + expect(result.error).toBe("This address is not present in the vault"); + }); - test("sign/empty address", async () => { - let data = "hello world"; - try { - let result = await vault.sign(data, null, pin, ethUrl); - } catch (e) { - expect(e.message).toBe( - "Cannot read properties of null (reading 'toLowerCase')" + test(`updateLabel/empty address using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let result = await vault.updateLabel(null, bufView, "Wallet 1"); + expect(result.error).toBe("This address is not present in the vault"); + }); + test(`updateLabel/invalid encryption key using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let result = await vault.updateLabel( + address.toLowerCase(), + "afers", + "Wallet 1" ); - } - }); - test("sign/invalid address", async () => { - let data = "hello world"; - try { - let result = await vault.sign(data, "abc", pin, ethUrl); - } catch (e) { - expect(e.message).toBe( - 'Given address "abc" is not a valid Ethereum address.' + expect(result.error).toBe("Incorrect Encryption Key or vault string"); + }); + test(`updateLabel/empty encryption key using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let result = await vault.updateLabel( + address.toLowerCase(), + null, + "Wallet 1" ); - } - }); - test("sign/empty pin", async () => { - let data = "hello world"; - - let result = await vault.sign(data, "abc", null, ethUrl); - expect(result.error).toBe("Wrong pin type, format or length"); + expect(result.error).toBe("Incorrect Encryption Key or vault string"); + }); + test(`updateLabel/empty label using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + try { + let result = await vault.updateLabel( + address.toLowerCase(), + bufView, + null + ); + } catch (e) { + expect(e.message).toBe("chainName is not defined"); + } + }); + test(`updateLabel/all empty params using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let result = await vault.updateLabel(null, null, null); + expect(result.error).toBe("Incorrect Encryption Key or vault string"); + }); }); - test("sign/incorrect pin", async () => { - let data = "hello world"; +}); - let result = await vault.sign(data, "abc", "111111", ethUrl); - expect(result.error).toBe("Incorrect pin"); - }); - test("sign/invalid pin", async () => { - let data = "hello world"; - let result = await vault.sign(data, accAddress, "abc", ethUrl); - expect(result.error).toBe("Wrong pin type, format or length"); - }); +describe("sign", () => { + const evmaddresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + const bitcoinadresses = [ + process.env.BITCOIN_ADDRESS_1, + process.env.BITCOIN_ADDRESS_2, + ]; + Object.keys(NETWORKS) + .filter((key) => key !== "bitcoin") + .forEach((chainName) => { + const networkConfig = getNetworkConfig(chainName); - test("sign/empty url", async () => { - let data = "hello world"; - let result = await vault.sign( - data, - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - pin, - null - ); - expect(result.response).toHaveProperty("signature"); - }); + let addresses; + let impprivatekey; + if (chainName == "bitcoin") { + addresses = bitcoinadresses; + impprivatekey = privateKeybtcImp; + } else { + addresses = evmaddresses; + impprivatekey = "0x" + privateKeyImp; + } + addresses.forEach((address, index) => { + test(`sign/valid for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + vault.changeNetwork(chainName); + await vault.importWallet(impprivatekey, pin, bufView); + + console.log("sign/valid--->", pin, networkConfig.url); + await vault.restoreKeyringState(vault, pin, bufView); + + let result = await vault.sign( + data, + address.toLowerCase(), + pin, + networkConfig.url + ); + console.log("sign/valid--->", result); + expect(result.response).toHaveProperty("signature"); + }); + + test(`sign/empty data for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + let result = await vault.sign( + "", + address.toLowerCase(), + pin, + networkConfig.url + ); + expect(result.response).toHaveProperty("signature"); + }); + + test(`sign/empty address for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + try { + let result = await vault.sign(data, null, pin, networkConfig.url); + } catch (e) { + expect(e.message).toBe( + "Cannot read properties of null (reading 'toLowerCase')" + ); + } + }); + test(`sign/invalid address using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + try { + let result = await vault.sign(data, "abc", pin, networkConfig.url); + } catch (e) { + expect(e.message).toBe( + 'Given address "abc" is not a valid Ethereum address.' + ); + } + }); + test(`sign/empty pin for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + + let result = await vault.sign(data, "abc", null, networkConfig.url); + expect(result.error).toBe("Wrong pin type, format or length"); + }); + test(`sign/incorrect for ${chainName} pin using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + + let result = await vault.sign( + data, + "abc", + "111111", + networkConfig.url + ); + expect(result.error).toBe("Incorrect pin"); + }); + test(`sign/invalid for ${chainName} pin using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + let result = await vault.sign( + data, + accAddress, + "abc", + networkConfig.url + ); + expect(result.error).toBe("Wrong pin type, format or length"); + }); + + test(`sign/empty for ${chainName} url using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + let result = await vault.sign(data, address.toLowerCase(), pin, null); + expect(result.response).toHaveProperty("signature"); + }); + + test(`sign/invalid url for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + let result = await vault.sign( + data, + address.toLowerCase(), + pin, + "abc" + ); + expect(result.response).toHaveProperty("signature"); + }); + + test(`sign/all params empty for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let data = "hello world"; + + let result = await vault.sign(null, null, null, null); + expect(result.error).toBe("Wrong pin type, format or length"); + }); + }); + }); +}); - test("sign/invalid url", async () => { - let data = "hello world"; - let result = await vault.sign( - data, - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - pin, - "abc" - ); - expect(result.response).toHaveProperty("signature"); - }); +describe("sign message", () => { + const evmaddresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + const bitcoinadresses = [ + process.env.BITCOIN_ADDRESS_1, + process.env.BITCOIN_ADDRESS_2, + ]; + Object.keys(NETWORKS).forEach(async (chainName) => { + const networkConfig = getNetworkConfig(chainName); - test("sign/all params empty", async () => { - let data = "hello world"; + let addresses; + let impprivatekey; + if (chainName == "bitcoin") { + addresses = bitcoinadresses; + impprivatekey = privateKeybtcImp; + } else { + addresses = evmaddresses; + impprivatekey = "0x" + privateKeyImp; + } + addresses.forEach((address, index) => { + test(`signMessage for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + vault.changeNetwork(chainName); + await vault.importWallet(impprivatekey, pin, bufView); + + const message = `Hello, eth!`; + let web3 = new Web3(networkConfig.url); + const hash = crypto.createHash("sha256").update(message).digest(); + const hexData = web3.utils.toHex(hash); + + const msgParams = { + from: address, + data: hexData, + }; + + const raw_sign = await vault.signMessage( + address, + msgParams.data, + pin, + bufView, + ethUrl + ); + console.log(raw_sign); + }); - let result = await vault.sign(null, null, null, null); - expect(result.error).toBe("Wrong pin type, format or length"); + // Skip signTypedMessage and personalSign tests for Bitcoin + if (chainName !== "bitcoin") { + test(`signTypeMessage for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let msgParams = { + from: address, + data: { + types: { + EIP712Domain: [ + { name: "name", type: "string" }, + { name: "version", type: "string" }, + { name: "chainId", type: "uint256" }, + { name: "verifyingContract", type: "address" }, + ], + Person: [ + { name: "name", type: "string" }, + { name: "wallet", type: "address" }, + ], + Mail: [ + { name: "from", type: "Person" }, + { name: "to", type: "Person" }, + { name: "contents", type: "string" }, + ], + }, + primaryType: "Mail", + domain: { + name: "Ether Mail", + version: "1", + chainId: NETWORKS[chainName].CHAIN_ID, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + }, + message: { + from: { + name: "Cow", + wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", + }, + to: { + name: "Bob", + wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", + }, + contents: "Hello, Bob!", + }, + }, + }; + + const rawSignature = await vault.signTypedMessage( + address, + msgParams.data, + pin, + bufView, + networkConfig.url + ); + console.log("Raw signature:", rawSignature); + + // Verify the signature + msgParams = { ...msgParams.data, from: msgParams.from }; + const recoveredAddress = sigUtil.recoverTypedSignature({ + data: msgParams, + sig: rawSignature.response, + }); + + const isSignatureValid = + recoveredAddress.toLowerCase() === msgParams.from.toLowerCase(); + + assert( + isSignatureValid, + `Signature verification failed for ${chainName} using address ${address}` + ); + assert( + rawSignature, + `Failed to Sign Message for ${chainName} using address ${address}` + ); + }); + + test(`personalSign for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + const message = "Hello, personal sign!"; + const msgParams = { + from: address, + data: message, + }; + + const rawSignature = await vault.personalSign( + address, + msgParams.data, + pin, + bufView, + networkConfig.url + ); + + console.log("Raw signature:", rawSignature); + + // Verify the signature + let web3 = new Web3(networkConfig.url); + const recoveredAddress = web3.eth.accounts.recover( + message, + rawSignature.response + ); + + const isSignatureValid = + recoveredAddress.toLowerCase() === address.toLowerCase(); + + assert( + isSignatureValid, + `Signature verification failed for ${chainName} using address ${address}` + ); + + assert( + rawSignature, + `Failed to Personal Sign Message for ${chainName} using address ${address}` + ); + }); + } + }); }); }); -describe("sign message", () => { +describe("signTransaction", () => { + const evmaddresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + const bitcoinadresses = [ + process.env.BITCOIN_ADDRESS_1, + process.env.BITCOIN_ADDRESS_2, + ]; Object.keys(NETWORKS).forEach((chainName) => { const networkConfig = getNetworkConfig(chainName); - vault.changeNetwork(chainName); - test(`signMessage for ${chainName}`, async () => { - const message = `Hello, eth!`; - let web3 = new Web3(networkConfig.url); - const hash = crypto.createHash("sha256").update(message).digest(); - const hexData = web3.utils.toHex(hash); - - const msgParams = { - from: "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - data: hexData, - }; - - const raw_sign = await vault.signMessage( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - msgParams.data, - pin, - bufView, - ethUrl - ); - console.log(raw_sign); - }); - test(`signTypeMessage for ${chainName}`, async () => { - const accounts = ["0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd"]; - // Ensure accounts is not empty - if (accounts.length === 0) { - throw new Error("No accounts found"); + let addresses; + let impprivatekey; + if (chainName == "bitcoin") { + addresses = bitcoinadresses; + impprivatekey = privateKeybtcImp; + } else { + addresses = evmaddresses; + impprivatekey = "0x" + privateKeyImp; + } + addresses.forEach((address, index) => { + test(`signTransaction/valid for ${chainName} using ${ + index === 1 ? "imported" : "" + } address ${address}`, async () => { + vault.changeNetwork(chainName); + const web3 = new Web3(networkConfig.url); + await vault.importWallet(impprivatekey, pin, bufView); + + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; + await vault.getActiveChains(); + try { + let result = await vault.signTransaction( + rawTx, + pin, + networkConfig.url + ); + + // Add more specific assertions based on the expected result structure + } catch (e) { + expect(e.message).toBe("Cannot read property 'salt' of undefined"); + } + }); + + test(`signTransaction/empty raw tx for ${chainName} using ${ + index === 1 ? "imported" : "" } + address ${address}`, async () => { + let from = process.env.EVM_ADDRESS_1; + const web3 = new Web3(networkConfig.url); + try { + let result = await vault.signTransaction({}, pin, networkConfig.url); + } catch (e) { + expect(e.message).toBe( + "Cannot read properties of undefined (reading 'toLowerCase')" + ); + } + }); - // console.log(accounts[0]); - - let msgParams = { - from: "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - data: { - types: { - EIP712Domain: [ - { name: "name", type: "string" }, - { name: "version", type: "string" }, - { name: "chainId", type: "uint256" }, - { name: "verifyingContract", type: "address" }, - ], - Person: [ - { name: "name", type: "string" }, - { name: "wallet", type: "address" }, - ], - Mail: [ - { name: "from", type: "Person" }, - { name: "to", type: "Person" }, - { name: "contents", type: "string" }, - ], - }, - primaryType: "Mail", - domain: { - name: "Ether Mail", - version: "1", - chainId: NETWORKS[chainName].CHAIN_ID, - verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", - }, - message: { - from: { - name: "Cow", - wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", - }, - to: { - name: "Bob", - wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", - }, - contents: "Hello, Bob!", - }, - }, - }; - - const rawSignature = await vault.signTypedMessage( - "0x80f850d6bfa120bcc462df27cf94d7d23bd8b7fd", - msgParams.data, - pin, - bufView, - networkConfig.url - ); - console.log("Raw signature:", rawSignature); - - // Verify the signature - msgParams = { ...msgParams.data, from: msgParams.from }; - console.log(); - const recoveredAddress = sigUtil.recoverTypedSignature({ - data: msgParams, - sig: rawSignature.response, + test(`signTransaction/invalid raw for ${chainName} using ${ + index === 1 ? "imported" : "" + } address ${address}`, async () => { + try { + let result = await vault.signTransaction( + "invalid", + pin, + networkConfig.url + ); + } catch (e) { + expect(e.message).toBe( + "Cannot read properties of undefined (reading 'toLowerCase')" + ); + } }); - // console.log("Recovered address:", recoveredAddress); - // console.log("Original address:", msgParams.from); + test(`signTransaction/empty pin for ${chainName} using ${ + index === 1 ? "imported" : "" + } address ${address}`, async () => { + const web3 = new Web3(networkConfig.url); + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; - // Compare the recovered address with the original signer's address - const isSignatureValid = - recoveredAddress.toLowerCase() === msgParams.from.toLowerCase(); + let result = await vault.signTransaction( + rawTx, + null, + networkConfig.url + ); + expect(result.error).toBe("Wrong pin type, format or length"); + }); - assert( - isSignatureValid, - `Signature verification failed for ${chainName}` - ); - assert(rawSignature, `Failed to Sign Message for ${chainName}`); + test(`signTransaction/invalid pin for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let from = address.toLowerCase(); + const web3 = new Web3(networkConfig.url); + + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; + + let result = await vault.signTransaction( + "evwf", + "afewf", + networkConfig.url + ); + expect(result.error).toBe("Wrong pin type, format or length"); + }); + test(`signTransaction/incorrect pin for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let from = address.toLowerCase(); + const web3 = new Web3(networkConfig.url); + + const rawTx = { + to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address + from: from.toLowerCase(), //sender address + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + data: "0x0", // method to generate data is provided below + + type: "0x2", + }; + + let result = await vault.signTransaction( + "evwf", + "112344", + networkConfig.url + ); + expect(result.error).toBe("Incorrect pin"); + }); + + test(`signTransaction/empty polygon rpc for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let from = address.toLowerCase(); + const web3 = new Web3(networkConfig.url); + + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; + + try { + let result = await vault.signTransaction(rawTx, pin, null); + } catch (e) { + expect(e.message).toBe( + "CONNECTION ERROR: Couldn't connect to node http://localhost:8545." + ); + } + }); + + test(`signTransaction/invalid polygon rpc for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let from = address.toLowerCase(); + const web3 = new Web3(networkConfig.url); + + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; + let invalidRpc = "efrwgrwdvfr"; + try { + let result = await vault.signTransaction(rawTx, pin, invalidRpc); + } catch (e) { + expect(e.message).toBe( + `CONNECTION ERROR: Couldn't connect to node ${invalidRpc}.` + ); + } + }); + + test(`signTransaction/all empty params for ${chainName} using ${ + index == 1 ? "imported" : "" + } address ${address}`, async () => { + let from = address.toLowerCase(); + const web3 = new Web3(networkConfig.url); + + const rawTx = { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), + gasLimit: web3.utils.numberToHex(21000), + maxPriorityFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("42.25770", "gwei")) + ), + maxFeePerGas: web3.utils.numberToHex( + parseFloat(web3.utils.toWei("150.99", "gwei")) + ), + amount: 0, + satPerByte: 1, + data: "0x0", + type: "0x2", + }; + let invalidRpc = "efrwgrwdvfr"; + + let result = await vault.signTransaction(null, null, null); + expect(result.error).toBe("Wrong pin type, format or length"); + }); }); }); }); @@ -980,323 +1404,90 @@ describe("Add new network", () => { }); }); -describe("signTransaction", () => { - Object.keys(NETWORKS).forEach((chainName) => { - const networkConfig = getNetworkConfig(chainName); - vault.changeNetwork(chainName); - test(`signTransaction/valid for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - await vault.getActiveChains(); - try { - let result = await vault.signTransaction(rawTx, pin, networkConfig.url); - } catch (e) { - expect(e.message).toBe("Cannot read property 'salt' of undefined"); - } - }); - - test(`signTransaction/empty raw tx for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - try { - let result = await vault.signTransaction({}, pin, networkConfig.url); - } catch (e) { - expect(e.message).toBe( - "Cannot read properties of undefined (reading 'toLowerCase')" - ); - } - }); +describe("getFees", () => { + const evmAddresses = [process.env.EVM_ADDRESS_1, process.env.EVM_ADDRESS_2]; + const bitcoinAddresses = [ + process.env.BITCOIN_ADDRESS_1, + process.env.BITCOIN_ADDRESS_2, + ]; - test(`signTransaction/invalid raw for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - try { - let result = await vault.signTransaction( - "evwf", - pin, - networkConfig.url - ); - } catch (e) { - expect(e.message).toBe( - "Cannot read properties of undefined (reading 'toLowerCase')" - ); - } - }); - - test(`signTransaction/empty pin for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - - let result = await vault.signTransaction("evwf", null, networkConfig.url); - expect(result.error).toBe("Wrong pin type, format or length"); - }); - - test(`signTransaction/invalid pin for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - - let result = await vault.signTransaction( - "evwf", - "afewf", - networkConfig.url - ); - expect(result.error).toBe("Wrong pin type, format or length"); - }); - test(`signTransaction/incorrect pin for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - - let result = await vault.signTransaction( - "evwf", - "112344", - networkConfig.url - ); - expect(result.error).toBe("Incorrect pin"); - }); - - test(`signTransaction/empty polygon rpc for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - - try { - let result = await vault.signTransaction("evwf", pin, null); - } catch (e) { - expect(e.message).toBe( - "CONNECTION ERROR: Couldn't connect to node http://localhost:8545." - ); - } - }); - - test(`signTransaction/invalid polygon rpc for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - let invalidRpc = "efrwgrwdvfr"; - try { - let result = await vault.signTransaction("evwf", pin, invalidRpc); - } catch (e) { - expect(e.message).toBe( - `CONNECTION ERROR: Couldn't connect to node ${invalidRpc}.` - ); - } - }); - - test(`signTransaction/all empty params for ${chainName}`, async () => { - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - const nonce = await web3.eth.getTransactionCount(from.toLowerCase()); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", //recepient address - from: from.toLowerCase(), //sender address - value: web3.utils.numberToHex(web3.utils.toWei("0.001", "ether")), - gasLimit: web3.utils.numberToHex(21000), //method to compute gas provided below - maxPriorityFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("42.25770", "gwei")) - ), - maxFeePerGas: web3.utils.numberToHex( - parseFloat(web3.utils.toWei("150.99", "gwei")) - ), - data: "0x0", // method to generate data is provided below - nonce: nonce, - type: "0x2", - }; - let invalidRpc = "efrwgrwdvfr"; - - let result = await vault.signTransaction(null, null, null); - expect(result.error).toBe("Wrong pin type, format or length"); - }); - }); -}); - -describe("get Fees", () => { Object.keys(NETWORKS).forEach((chainName) => { - test(`get Fees, validate for ${chainName}`, async () => { - const networkConfig = getNetworkConfig(chainName); - vault.changeNetwork(chainName); - - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); - - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", - from: from.toLowerCase(), - value: web3.utils.numberToHex(web3.utils.toWei("0", "ether")), - chainID: networkConfig.chainId, - }; - - let result = await vault.getFees(rawTx, networkConfig.url); - expect(result.response).toHaveProperty("gasLimit"); - expect(result.response).toHaveProperty("fees"); - - // Additional checks specific to the chain - if (networkConfig.txType === 2) { - expect(result.response.fees.fast).toHaveProperty("maxFeePerGas"); - expect(result.response.fees.fast).toHaveProperty( - "maxPriorityFeePerGas" - ); - } else { - expect(result.response.fees.fast).toHaveProperty("gasPrice"); - } - }); - - test(`get fees, invalid for ${chainName}`, async () => { - const networkConfig = getNetworkConfig(chainName); - vault.changeNetwork(chainName); + const networkConfig = getNetworkConfig(chainName); - let from = "0x80F850d6BFA120Bcc462df27cF94d7D23bd8B7FD"; - const web3 = new Web3(networkConfig.url); + let addresses; + if (chainName === "bitcoin") { + addresses = bitcoinAddresses; + } else { + addresses = evmAddresses; + } - const rawTx = { - to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", - from: from.toLowerCase(), - value: web3.utils.numberToHex(web3.utils.toWei("0", "ether")), - chainID: networkConfig.chainId, - }; + addresses.forEach((address, index) => { + test(`getFees/valid for ${chainName} using address ${address}`, async () => { + vault.changeNetwork(chainName); + const web3 = + chainName !== "bitcoin" ? new Web3(networkConfig.url) : null; + + const rawTx = + chainName === "bitcoin" + ? { + to: addresses[index === 0 ? 1 : 0], + from: address, + value: "0.0001", // BTC value + } + : { + to: addresses[index === 0 ? 1 : 0], + from: address.toLowerCase(), + value: web3.utils.numberToHex(web3.utils.toWei("0", "ether")), + chainID: networkConfig.chainId, + }; + + let result = await vault.getFees(rawTx, networkConfig.url); + + if (chainName === "bitcoin") { + expect(result.response).toHaveProperty("transactionSize"); + expect(result.response).toHaveProperty("fees"); + expect(result.response.fees).toHaveProperty("slow"); + expect(result.response.fees).toHaveProperty("standard"); + expect(result.response.fees).toHaveProperty("fast"); + expect(result.response.fees.slow).toHaveProperty("satPerByte"); + expect(result.response.fees.standard).toHaveProperty("satPerByte"); + expect(result.response.fees.fast).toHaveProperty("satPerByte"); + expect(typeof result.response.transactionSize).toBe("number"); + expect(typeof result.response.fees.slow.satPerByte).toBe("number"); + expect(typeof result.response.fees.standard.satPerByte).toBe( + "number" + ); + expect(typeof result.response.fees.fast.satPerByte).toBe("number"); + } else { + expect(result.response).toHaveProperty("gasLimit"); + expect(result.response).toHaveProperty("fees"); + if (networkConfig.txType === 2) { + expect(result.response.fees.fast).toHaveProperty("maxFeePerGas"); + expect(result.response.fees.fast).toHaveProperty( + "maxPriorityFeePerGas" + ); + } else { + expect(result.response.fees.fast).toHaveProperty("gasPrice"); + } + } + }); - try { - let result = await vault.getFees(rawTx, "invalid_url"); - fail("Should have thrown an error"); - } catch (e) { - expect(e.message).toBe( - "CONNECTION ERROR: Couldn't connect to node invalid_url." - ); - } + test(`getFees/network error for ${chainName} using address ${address}`, async () => { + // Mock axios to simulate a network error + + const rawTx = + chainName === "bitcoin" + ? { from: address } + : { from: address.toLowerCase() }; + + try { + let result = await vault.getFees(rawTx, "invalid_url"); + } catch (e) { + expect(e.message).toBe( + "CONNECTION ERROR: Couldn't connect to node invalid_url." + ); + } + }); }); }); }); diff --git a/src/lib/test/vault.test.js b/src/lib/test/vault.test.js index a0db9c9..7cf5e61 100644 --- a/src/lib/test/vault.test.js +++ b/src/lib/test/vault.test.js @@ -1,6 +1,6 @@ jest.setTimeout(30000); const crypto = require("crypto"); - +require("dotenv").config(); let Vault = require("../vault"); const bufView = [ 48, 0, 236, 187, 187, 172, 177, 90, 255, 184, 9, 116, 142, 96, 197, 158, 87, @@ -8,9 +8,8 @@ const bufView = [ 105, 178, 182, 108, 174, 199, 124, 141, 155, 73, 21, 85, 81, 109, 78, 233, 152, 108, 242, 238, 192, 31, 147, 86, 174, 195, 55, 229, 4, 36, ]; -let phrase = - "fun rough treat scan glimpse region century purpose expire video remind second"; -let pin = "696969"; +let phrase = process.env.MNEMONIC; +let pin = process.env.PIN; let vault = new Vault({}); const logs = [ diff --git a/src/lib/vault.js b/src/lib/vault.js index 1cf5238..554ded5 100644 --- a/src/lib/vault.js +++ b/src/lib/vault.js @@ -40,7 +40,10 @@ class Vault extends Keyring { const evmChainInfo = Chains.getEvmChainInfo(this.chain); const keyringController = new KeyringController({ txType: evmChainInfo.txType, - + persistKeyrings: this.keyringInstance?.keyrings, + persistStore: this.keyringInstance?.store, + persistmemStore: this.keyringInstance?.memStore, + persistImported: this.keyringInstance?.importedWallets, encryptor: { encrypt(pass, object) { const ciphertext = CryptoJS.AES.encrypt( From 3f9773bcd8800251a0160009c29667bf1ad2f722 Mon Sep 17 00:00:00 2001 From: Husienvora Date: Mon, 21 Oct 2024 11:25:35 +0530 Subject: [PATCH 3/9] adding more tests --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 29e01d2..09cc525 100644 --- a/.gitignore +++ b/.gitignore @@ -41,7 +41,7 @@ Thumbs.db *.app *.exe *.war - +.env # Large media files *.mp4 *.tiff From 1d3ea23f5fc573e000639dc17b539421649ed83b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Oct 2024 12:20:02 +0530 Subject: [PATCH 4/9] adding env --- .github/workflows/main.yml | 8 ++++++++ CHANGELOG.md | 1 + src/lib/test/keyring.test.js | 20 +++++++++++++------- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1e97967..030e70a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,6 +17,14 @@ jobs: env: CI: true GITHUB_ACTIONS: true + MNEMONIC:"vintage reflect pass input polar enlist giggle judge render position also document" + PIN:"696969" + PRIVATE_KEY_EVM:"0x7a9633b8103fec11c9e855a6b6c8c072e9af311a69b92ab0ad8186b1fb57371f" + PRIVATE_KEY_BTC: "KzQfcdjDRUwpVmKKev6k2aAeJFJ359Ht9Umxdg77MTzf2E3bzGsC" + EVM_ADDRESS_1:'0x6bbc122fa843f3ed30d23f8cdd9a430d1f898d07' + EVM_ADDRESS_2:"0xbae949ddb4d8ac763c12f206db842b9b2d49a464" + BITCOIN_ADDRESS_1:"bc1qugw5q5yrzw86wnw3lgldm8en0c736k4hvceuzl" + BITCOIN_ADDRESS_2:"bc1qta5f6q32cphxt5rck3kuspukac7keqvxyuk4cl" steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ff4ac9..ae6b9dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -674,3 +674,4 @@ -Integrated vault-evm-controller -Resolved issue for unarchival of a wallet +-Adding test for bitcoin diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index a74763d..e29bf9b 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -5,13 +5,10 @@ const { before } = require("lodash"); let KeyRing = require("../keyring"); let Vault = require("../vault"); require("dotenv").config(); +const helper = require("../../utils/helper"); const sigUtil = require("eth-sig-util"); const Web3 = require("web3"); -const NETWORKS = { - bitcoin: { - URL: "https://bitcoin-mainnet.gateway.tatum.io", - CHAIN_ID: 999999, - }, +let NETWORKS = { ethereum: { URL: "https://sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 11155111, @@ -49,7 +46,7 @@ const NETWORKS = { CHAIN_ID: 84532, }, zkEVM: { - URL: "https://polygon-zkevm.drpc.org", + URL: "https://1rpc.io/polygon/zkevm", CHAIN_ID: 1442, }, bevm: { @@ -61,7 +58,16 @@ const NETWORKS = { CHAIN_ID: 31, }, }; - +if (!helper.isTestEnvironment()) { + NETWORKS = { + bitcoin: { + URL: "https://bitcoin-mainnet.gateway.tatum.io", + CHAIN_ID: 999999, + }, + ...NETWORKS, + }; +} +console.log(NETWORKS); const chainConfigs = { ethereum: { symbol: "ETH", txType: 2 }, bsc: { symbol: "BSC", txType: 0 }, From fed3ead28f78e4e6203423bdefbd082f56907560 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 21 Oct 2024 12:36:14 +0530 Subject: [PATCH 5/9] test comments --- src/lib/test/keyring.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index e29bf9b..c295e6a 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -58,6 +58,8 @@ let NETWORKS = { CHAIN_ID: 31, }, }; + +//uncomment the below if statement to run test for bitcoin and make sure you override the balance check in bitcoin controller if (!helper.isTestEnvironment()) { NETWORKS = { bitcoin: { @@ -67,7 +69,7 @@ if (!helper.isTestEnvironment()) { ...NETWORKS, }; } -console.log(NETWORKS); + const chainConfigs = { ethereum: { symbol: "ETH", txType: 2 }, bsc: { symbol: "BSC", txType: 0 }, From d8de462f1576956e2bea6c4f8a9c010542e7f3cc Mon Sep 17 00:00:00 2001 From: Husienvora Date: Mon, 21 Oct 2024 17:56:10 +0530 Subject: [PATCH 6/9] removing bitcoin test --- src/lib/test/keyring.test.js | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index c295e6a..c81b0e1 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -33,10 +33,10 @@ let NETWORKS = { URL: "https://mantle-sepolia.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 5003, }, - // velas: { - // URL: "https://explorer.testnet.velas.com/rpc", - // CHAIN_ID: 111, - // }, + velas: { + URL: "https://explorer.testnet.velas.com/rpc", + CHAIN_ID: 111, + }, avalanche: { URL: "https://avalanche-fuji.infura.io/v3/0611b8c478b14db0b7d29e51466ff925", CHAIN_ID: 43113, @@ -59,17 +59,6 @@ let NETWORKS = { }, }; -//uncomment the below if statement to run test for bitcoin and make sure you override the balance check in bitcoin controller -if (!helper.isTestEnvironment()) { - NETWORKS = { - bitcoin: { - URL: "https://bitcoin-mainnet.gateway.tatum.io", - CHAIN_ID: 999999, - }, - ...NETWORKS, - }; -} - const chainConfigs = { ethereum: { symbol: "ETH", txType: 2 }, bsc: { symbol: "BSC", txType: 0 }, @@ -77,7 +66,7 @@ const chainConfigs = { optimism: { symbol: "OP", txType: 2 }, arbitrum: { symbol: "ARB", txType: 2 }, mantle: { symbol: "MNT", txType: 2 }, - // velas: { symbol: "VLX", txType: 0 }, + velas: { symbol: "VLX", txType: 0 }, avalanche: { symbol: "AVAX", txType: 2 }, base: { symbol: "BASE", txType: 2 }, zkEVM: { symbol: "ZKEVM", txType: 2 }, From 8e6b6d9af90cc39e6cdc8343fd7b2efa46055f24 Mon Sep 17 00:00:00 2001 From: Husienvora Date: Mon, 21 Oct 2024 18:48:26 +0530 Subject: [PATCH 7/9] fixing test --- .env | 8 -------- .github/workflows/main.yml | 1 + src/lib/test/vault.test.js | 17 ++++++++++++++--- 3 files changed, 15 insertions(+), 11 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index 2523d9e..0000000 --- a/.env +++ /dev/null @@ -1,8 +0,0 @@ -MNEMONIC="vintage reflect pass input polar enlist giggle judge render position also document" -PIN="696969" -PRIVATE_KEY_EVM="0x7a9633b8103fec11c9e855a6b6c8c072e9af311a69b92ab0ad8186b1fb57371f" -PRIVATE_KEY_BTC= "KzQfcdjDRUwpVmKKev6k2aAeJFJ359Ht9Umxdg77MTzf2E3bzGsC" -EVM_ADDRESS_1='0x6bbc122fa843f3ed30d23f8cdd9a430d1f898d07' -EVM_ADDRESS_2="0xbae949ddb4d8ac763c12f206db842b9b2d49a464" -BITCOIN_ADDRESS_1="bc1qugw5q5yrzw86wnw3lgldm8en0c736k4hvceuzl" -BITCOIN_ADDRESS_2="bc1qta5f6q32cphxt5rck3kuspukac7keqvxyuk4cl" \ No newline at end of file diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 030e70a..aadaa83 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,6 +17,7 @@ jobs: env: CI: true GITHUB_ACTIONS: true + OLD_MNEMONIC:"fun rough treat scan glimpse region century purpose expire video remind second" MNEMONIC:"vintage reflect pass input polar enlist giggle judge render position also document" PIN:"696969" PRIVATE_KEY_EVM:"0x7a9633b8103fec11c9e855a6b6c8c072e9af311a69b92ab0ad8186b1fb57371f" diff --git a/src/lib/test/vault.test.js b/src/lib/test/vault.test.js index 7cf5e61..cbe7383 100644 --- a/src/lib/test/vault.test.js +++ b/src/lib/test/vault.test.js @@ -1,14 +1,19 @@ jest.setTimeout(30000); const crypto = require("crypto"); -require("dotenv").config(); + let Vault = require("../vault"); + +require("dotenv").config(); + const bufView = [ 48, 0, 236, 187, 187, 172, 177, 90, 255, 184, 9, 116, 142, 96, 197, 158, 87, 35, 26, 101, 187, 30, 116, 138, 50, 131, 166, 50, 51, 197, 198, 83, 238, 167, 105, 178, 182, 108, 174, 199, 124, 141, 155, 73, 21, 85, 81, 109, 78, 233, 152, 108, 242, 238, 192, 31, 147, 86, 174, 195, 55, 229, 4, 36, ]; +let oldphrase = process.env.OLD_MNEMONIC; let phrase = process.env.MNEMONIC; + let pin = process.env.PIN; let vault = new Vault({}); @@ -162,7 +167,7 @@ describe("recoverVault", () => { test("recoverVault/logs valid", async () => { let result = await vault.recoverVault( - phrase, + oldphrase, bufView, pin, null, @@ -173,7 +178,13 @@ describe("recoverVault", () => { }); test("recoverVault/logs empty logs valid", async () => { - let result = await vault.recoverVault(phrase, bufView, pin, null, "logs"); + let result = await vault.recoverVault( + oldphrase, + bufView, + pin, + null, + "logs" + ); expect(result).toHaveProperty("response"); }); From d35d0da9b006624ccb5f3fe5a232151f55d82d10 Mon Sep 17 00:00:00 2001 From: Husienvora Date: Mon, 21 Oct 2024 19:17:09 +0530 Subject: [PATCH 8/9] merging with dev --- src/lib/test/keyring.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib/test/keyring.test.js b/src/lib/test/keyring.test.js index c81b0e1..5122ad2 100644 --- a/src/lib/test/keyring.test.js +++ b/src/lib/test/keyring.test.js @@ -94,6 +94,7 @@ const bufView = [ 152, 108, 242, 238, 192, 31, 147, 86, 174, 195, 55, 229, 4, 36, ]; let phrase = process.env.MNEMONIC; +console.log(phrase, "phrase"); let pin = process.env.PIN; let result; let vault = new Vault({}); From 1667574381bbf4b287fc8ea42211093e22e6504b Mon Sep 17 00:00:00 2001 From: Husienvora Date: Tue, 22 Oct 2024 11:15:01 +0530 Subject: [PATCH 9/9] updating version --- CHANGELOG.md | 3 ++- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae6b9dc..c278239 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -670,8 +670,9 @@ - Update BEVM controller version - Fixed validatePin function for tests -### 2.9.1 (2024-09-23) +### 2.9.2 (2024-09-23) -Integrated vault-evm-controller -Resolved issue for unarchival of a wallet -Adding test for bitcoin +-Adding sensitive info in env github pipeline diff --git a/package-lock.json b/package-lock.json index 095f333..f2a9a52 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@getsafle/safle-vault", - "version": "2.9.1", + "version": "2.9.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@getsafle/safle-vault", - "version": "2.9.1", + "version": "2.9.2", "license": "MIT", "dependencies": { "@getsafle/safle-identity-wallet": "^1.3.0", diff --git a/package.json b/package.json index 8942819..f969175 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getsafle/safle-vault", - "version": "2.9.1", + "version": "2.9.2", "description": "Safle Vault is a non-custodial, flexible and highly available crypto wallet which can be used to access dapps, send/receive crypto and store identity. Vault SDK is used to manage the vault and provide methods to generate vault, add new accounts, update the state and also enable the user to perform several vault related operations.", "main": "src/index.js", "scripts": {