diff --git a/package-lock.json b/package-lock.json index d08de14..8d91468 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +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-mantle-controller": "^1.0.1", "@getsafle/vault-optimism-controller": "^1.0.8", "@getsafle/vault-polygon-controller": "^1.2.8", @@ -1933,6 +1934,204 @@ "proxy-from-env": "^1.1.0" } }, + "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==", + "license": "MIT", + "dependencies": { + "@ethereumjs/common": "2.6.0", + "@ethereumjs/tx": "^3.4.0", + "axios": "^1.6.2", + "bip39": "^3.0.4", + "browser-passworder": "^2.0.3", + "crypto": "^1.0.1", + "crypto-js": "^4.2.0", + "eth-hd-keyring": "^3.6.0", + "eth-sig-util": "^3.0.1", + "eth-simple-keyring": "^4.2.0", + "ethereumjs-abi": "^0.6.8", + "ethereumjs-tx": "^2.1.2", + "ethereumjs-util": "^7.1.5", + "events": "^3.3.0", + "loglevel": "^1.7.1", + "nyc": "^17.0.0", + "obs-store": "^4.0.3", + "web3": "^1.6.0" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/@ethereumjs/tx": { + "version": "3.5.2", + "resolved": "https://registry.npmjs.org/@ethereumjs/tx/-/tx-3.5.2.tgz", + "integrity": "sha512-gQDNJWKrSDGu2w7w0PzVXVBNMzb7wwdDOmOqczmhNjqFxFuIbhVJDwiGEnxFNC2/b8ifcZzY7MLcluizohRzNw==", + "license": "MPL-2.0", + "dependencies": { + "@ethereumjs/common": "^2.6.4", + "ethereumjs-util": "^7.1.5" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/@ethereumjs/tx/node_modules/@ethereumjs/common": { + "version": "2.6.5", + "resolved": "https://registry.npmjs.org/@ethereumjs/common/-/common-2.6.5.tgz", + "integrity": "sha512-lRyVQOeCDaIVtgfbowla32pzeDv2Obr8oR8Put5RdUBNRGr1VGPGQNGP6elWIpgK3YdpzqTOh4GyUGOureVeeA==", + "license": "MIT", + "dependencies": { + "crc-32": "^1.2.0", + "ethereumjs-util": "^7.1.5" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/axios": { + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/cliui": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-6.0.0.tgz", + "integrity": "sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==", + "license": "ISC", + "dependencies": { + "string-width": "^4.2.0", + "strip-ansi": "^6.0.0", + "wrap-ansi": "^6.2.0" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/convert-source-map": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.9.0.tgz", + "integrity": "sha512-ASFBup0Mz1uyiIjANan1jzLQami9z1PoYSZCiiYW2FczPbenXc45FZdBZLzOT+r6+iciuEModtmCti+hjaAk0A==", + "license": "MIT" + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/istanbul-lib-instrument": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/istanbul-lib-instrument/-/istanbul-lib-instrument-6.0.3.tgz", + "integrity": "sha512-Vtgk7L/R2JHyyGW07spoFlB8/lpjiOLTjMdms6AFMraYt3BaJauod/NGrfnVG/y4Ix1JEuMRPDPEj2ua+zz1/Q==", + "license": "BSD-3-Clause", + "dependencies": { + "@babel/core": "^7.23.9", + "@babel/parser": "^7.23.9", + "@istanbuljs/schema": "^0.1.3", + "istanbul-lib-coverage": "^3.2.0", + "semver": "^7.5.4" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/nyc": { + "version": "17.0.0", + "resolved": "https://registry.npmjs.org/nyc/-/nyc-17.0.0.tgz", + "integrity": "sha512-ISp44nqNCaPugLLGGfknzQwSwt10SSS5IMoPR7GLoMAyS18Iw5js8U7ga2VF9lYuMZ42gOHr3UddZw4WZltxKg==", + "license": "ISC", + "dependencies": { + "@istanbuljs/load-nyc-config": "^1.0.0", + "@istanbuljs/schema": "^0.1.2", + "caching-transform": "^4.0.0", + "convert-source-map": "^1.7.0", + "decamelize": "^1.2.0", + "find-cache-dir": "^3.2.0", + "find-up": "^4.1.0", + "foreground-child": "^2.0.0", + "get-package-type": "^0.1.0", + "glob": "^7.1.6", + "istanbul-lib-coverage": "^3.0.0", + "istanbul-lib-hook": "^3.0.0", + "istanbul-lib-instrument": "^6.0.2", + "istanbul-lib-processinfo": "^2.0.2", + "istanbul-lib-report": "^3.0.0", + "istanbul-lib-source-maps": "^4.0.0", + "istanbul-reports": "^3.0.2", + "make-dir": "^3.0.0", + "node-preload": "^0.2.1", + "p-map": "^3.0.0", + "process-on-spawn": "^1.0.0", + "resolve-from": "^5.0.0", + "rimraf": "^3.0.0", + "signal-exit": "^3.0.2", + "spawn-wrap": "^2.0.0", + "test-exclude": "^6.0.0", + "yargs": "^15.0.2" + }, + "bin": { + "nyc": "bin/nyc.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/semver": { + "version": "7.6.3", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.3.tgz", + "integrity": "sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==", + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/y18n": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.3.tgz", + "integrity": "sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==", + "license": "ISC" + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/yargs": { + "version": "15.4.1", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-15.4.1.tgz", + "integrity": "sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==", + "license": "MIT", + "dependencies": { + "cliui": "^6.0.0", + "decamelize": "^1.2.0", + "find-up": "^4.1.0", + "get-caller-file": "^2.0.1", + "require-directory": "^2.1.1", + "require-main-filename": "^2.0.0", + "set-blocking": "^2.0.0", + "string-width": "^4.2.0", + "which-module": "^2.0.0", + "y18n": "^4.0.0", + "yargs-parser": "^18.1.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@getsafle/vault-evm-controller/node_modules/yargs-parser": { + "version": "18.1.3", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.3.tgz", + "integrity": "sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==", + "license": "ISC", + "dependencies": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + }, + "engines": { + "node": ">=6" + } + }, "node_modules/@getsafle/vault-mantle-controller": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@getsafle/vault-mantle-controller/-/vault-mantle-controller-1.0.1.tgz", @@ -5603,6 +5802,13 @@ "node": ">= 8" } }, + "node_modules/crypto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/crypto/-/crypto-1.0.1.tgz", + "integrity": "sha512-VxBKmeNcqQdiUQUW2Tzq0t377b54N2bMtXO/qiLa+6eRRmmC4qT3D4OnTGoT/U6O9aklQ/jTwbOtRMTTY8G0Ig==", + "deprecated": "This package is no longer supported. It's now a built-in Node module. If you've depended on crypto, you should switch to the one that's built-in.", + "license": "ISC" + }, "node_modules/crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", diff --git a/package.json b/package.json index 34847ca..6be5a31 100644 --- a/package.json +++ b/package.json @@ -57,6 +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-mantle-controller": "^1.0.1", "@getsafle/vault-optimism-controller": "^1.0.8", "@getsafle/vault-polygon-controller": "^1.2.8", diff --git a/src/chains/index.js b/src/chains/index.js index b5db95f..8ac2dc2 100644 --- a/src/chains/index.js +++ b/src/chains/index.js @@ -1,51 +1,62 @@ -const ethereum = require("@getsafle/vault-eth-controller"); -const bsc = require("@getsafle/vault-bsc-controller"); -const polygon = require("@getsafle/vault-polygon-controller"); +const EvmController = require("@getsafle/vault-evm-controller"); const bitcoin = require("@getsafle/vault-bitcoin-controller"); -const optimism = require("@getsafle/vault-optimism-controller"); -const arbitrum = require("@getsafle/vault-arbitrum-controller"); -const mantle = require("@getsafle/vault-mantle-controller"); -const velas = require("@getsafle/vault-velas-controller"); -const avalanche = require("@getsafle/vault-avalanche-controller"); -const base = require("@getsafle/vault-base-controller"); -const zkEVM = require("@getsafle/vault-polygon-zkevm-controller"); const stacks = require("@getsafle/vault-stacks-controller"); const solana = require("@getsafle/vault-sol-controller"); -const bevm = require("@getsafle/vault-bevm-controller"); -const rootstock = require("@getsafle/vault-rootstock-controller"); - +//we don't have to put rpc and chain id since our vault get web3 as a argument in all methods const evmChains = { - ethereum: "ETH", - bsc: "BSC", - polygon: "MATIC", - optimism: "OP", - arbitrum: "ARB", - mantle: "MNT", - velas: "VLX", - avalanche: "AVAX", - base: "BASE", - zkEVM: "ZKEVM", - bevm: "BTC", - rootstock: "RBTC", + ethereum: { symbol: "ETH", txType: 2 }, + bsc: { symbol: "BSC", txType: 0 }, + polygon: { symbol: "MATIC", txType: 2 }, + optimism: { symbol: "OP", txType: 2 }, + arbitrum: { symbol: "ARB", txType: 2 }, + mantle: { symbol: "MNT", txType: 2 }, + 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 }, +}; + +const nonEvmChains = { + bitcoin: "BTC", + stacks: "STX", + solana: "SOL", }; -const nonEvmChains = { bitcoin: "BTC", stacks: "STX", solana: "SOL" }; + +// Create an object with all EVM chains using the same controller but initialized with the appropriate txType +const evmControllers = Object.entries(evmChains).reduce( + (acc, [chain, info]) => { + acc[chain] = EvmController; + return acc; + }, + {} +); + +// Create an object with just the symbols for EVM chains +const evmChainSymbols = Object.entries(evmChains).reduce( + (acc, [chain, info]) => { + acc[chain] = info.symbol; + return acc; + }, + {} +); module.exports = { - ethereum, - bsc, - polygon, + ...evmControllers, bitcoin, - optimism, - arbitrum, - mantle, - velas, - avalanche, - base, - zkEVM, stacks, solana, - bevm, - rootstock, - evmChains, + evmChains: evmChainSymbols, nonEvmChains, + getEvmChainInfo: (chain) => evmChains[chain], + addEvmChain: (chainName, chainInfo) => { + if (evmChains[chainName]) { + throw new Error("Chain already exists"); + } + evmChains[chainName] = chainInfo; + evmControllers[chainName] = EvmController; + + evmChainSymbols[chainName] = chainInfo.symbol; + }, }; diff --git a/src/lib/vault.js b/src/lib/vault.js index 8ab213e..bfe54be 100644 --- a/src/lib/vault.js +++ b/src/lib/vault.js @@ -1,205 +1,318 @@ -const CryptoJS = require('crypto-js'); -const { KeyringController } = require('@getsafle/vault-eth-controller'); -const BitcoinKeyringController= require('@getsafle/vault-bitcoin-controller').KeyringController ; -const StacksKeyringController = require('@getsafle/vault-stacks-controller').KeyringController; -const SolanaKeyringController = require('@getsafle/vault-sol-controller').KeyringController; -const bip39 = require('bip39'); - -const helper = require('../utils/helper'); -const Keyring = require('./keyring'); -const Chains = require('../chains'); - -const ERROR_MESSAGE = require('../constants/responses'); +const CryptoJS = require("crypto-js"); +const { KeyringController } = require("@getsafle/vault-evm-controller"); +const BitcoinKeyringController = + require("@getsafle/vault-bitcoin-controller").KeyringController; +const StacksKeyringController = + require("@getsafle/vault-stacks-controller").KeyringController; +const SolanaKeyringController = + require("@getsafle/vault-sol-controller").KeyringController; +const bip39 = require("bip39"); + +const helper = require("../utils/helper"); +const Keyring = require("./keyring"); +const Chains = require("../chains"); + +const ERROR_MESSAGE = require("../constants/responses"); class Vault extends Keyring { - - constructor({vault, encryptionKey}) { - super(); - this.chain = 'ethereum'; - this.vault = vault; - this.initializeKeyringController(); - if (vault && encryptionKey) { - this.initializeDecryptedVault(vault, encryptionKey); - } + constructor({ vault, encryptionKey }) { + super(); + this.chain = "ethereum"; + this.vault = vault; + this.initializeKeyringController(); + if (vault && encryptionKey) { + this.initializeDecryptedVault(vault, encryptionKey); } - - initializeDecryptedVault(vault, encryptionKey) { - const { decryptedVault, error } = helper.validateEncryptionKey(vault, JSON.stringify(encryptionKey)); - if (error) { - return { error } - } - this.decryptedVault = decryptedVault; - + } + + initializeDecryptedVault(vault, encryptionKey) { + const { decryptedVault, error } = helper.validateEncryptionKey( + vault, + JSON.stringify(encryptionKey) + ); + if (error) { + return { error }; } - - initializeKeyringController() { - const keyringController = new KeyringController({ - encryptor: { - encrypt(pass, object) { - const ciphertext = CryptoJS.AES.encrypt(JSON.stringify(object), pass).toString(); - - return ciphertext; - }, - decrypt(pass, encryptedString) { - const bytes = CryptoJS.AES.decrypt(encryptedString, pass); - const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); - - return decryptedData; - }, + this.decryptedVault = decryptedVault; + } + + initializeKeyringController() { + const evmChainInfo = Chains.getEvmChainInfo(this.chain); + const keyringController = new KeyringController({ + txType: evmChainInfo.txType, + + encryptor: { + encrypt(pass, object) { + const ciphertext = CryptoJS.AES.encrypt( + JSON.stringify(object), + pass + ).toString(); + return ciphertext; }, - }); - - this.keyringInstance = keyringController; + decrypt(pass, encryptedString) { + const bytes = CryptoJS.AES.decrypt(encryptedString, pass); + const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8)); + return decryptedData; + }, + }, + }); + + this.keyringInstance = keyringController; + } + + initializeSupportedChainKeyringController(mnemonic) { + const keyringController = new BitcoinKeyringController({ + mnemonic: mnemonic, + }); + this["bitcoin"] = keyringController; + + const stacksKeyringController = new StacksKeyringController({ + mnemonic: mnemonic, + }); + this["stacks"] = stacksKeyringController; + + const solanaKeyringController = new SolanaKeyringController({ + mnemonic: mnemonic, + }); + this["solana"] = solanaKeyringController; + } + + async generateMnemonic(entropy) { + var mnemonic; + + if (entropy) { + mnemonic = bip39.entropyToMnemonic(entropy); + } else { + mnemonic = bip39.generateMnemonic(); } - initializeSupportedChainKeyringController(mnemonic) { - const keyringController = new BitcoinKeyringController({mnemonic:mnemonic}); - this["bitcoin"] = keyringController; + return mnemonic; + } - const stacksKeyringController = new StacksKeyringController({mnemonic:mnemonic}); - this["stacks"] = stacksKeyringController; - - const solanaKeyringController = new SolanaKeyringController({mnemonic:mnemonic}); - this["solana"] = solanaKeyringController; + async changeNetwork(chain) { + if ( + !Chains.evmChains.hasOwnProperty(chain) && + !Chains.nonEvmChains.hasOwnProperty(chain) + ) { + throw ERROR_MESSAGE.CHAIN_NOT_SUPPORTED; } - - async generateMnemonic(entropy) { - var mnemonic; - - if(entropy) { - mnemonic = bip39.entropyToMnemonic(entropy); - } else { - mnemonic = bip39.generateMnemonic(); - } - - return mnemonic; + this.chain = chain; + if (Chains.evmChains.hasOwnProperty(chain)) { + this.initializeKeyringController(); } - - async changeNetwork(chain) { - if (chain !== 'ethereum' && !Chains.evmChains.hasOwnProperty(chain) && !Chains.nonEvmChains.hasOwnProperty(chain)) { - throw ERROR_MESSAGE.CHAIN_NOT_SUPPORTED; - } - this.chain = chain; + } + + // async addNetwork(chainName, chainInfo) { + // try { + // Chains.addEvmChain(chainName, chainInfo); + // return { response: `Network ${chainName} added successfully` }; + // } catch (error) { + // return { error: error.message }; + // } + // } + + async generateVault(encryptionKey, pin, mnemonic) { + if ( + typeof pin != "string" || + pin.match(/^[0-9]+$/) === null || + pin < 0 || + pin.length != 6 + ) { + return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; } - async generateVault(encryptionKey, pin, mnemonic) { - - if (typeof(pin) != 'string'|| pin.match(/^[0-9]+$/) === null || pin < 0 || pin.length !=6 ) { - return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; - } - - if(!encryptionKey || pin === undefined || pin === null) { - return { error: ERROR_MESSAGE.ENTER_CREDS }; - } - - await this.keyringInstance.createNewVaultAndRestore(JSON.stringify(encryptionKey), mnemonic); - - const accounts = await this.keyringInstance.getAccounts(); - - const privData = await helper.generatePrivData(mnemonic, pin); - - const rawVault = { eth: { public: [ { address: accounts[0], isDeleted: false, isImported: false, label: 'EVM Wallet 1' } ], private: privData, numberOfAccounts: 1 }} - - this.initializeSupportedChainKeyringController(mnemonic); - - for (const chain of Object.keys(Chains.nonEvmChains)) { - let addedAcc - if (chain === 'stacks') { - addedAcc = (await this[chain].generateWallet()).address; - } else { - addedAcc = (await this[chain].addAccount()).address; - } - let label = `${Chains.nonEvmChains[chain]} Wallet 1` - rawVault[chain] = { public: [ { address: addedAcc, isDeleted: false, isImported: false, label: label } ], numberOfAccounts: 1 } - - } + if (!encryptionKey || pin === undefined || pin === null) { + return { error: ERROR_MESSAGE.ENTER_CREDS }; + } - const vault = await helper.cryptography(JSON.stringify(rawVault), JSON.stringify(encryptionKey), 'encryption'); + await this.keyringInstance.createNewVaultAndRestore( + JSON.stringify(encryptionKey), + mnemonic + ); + + const accounts = await this.keyringInstance.getAccounts(); + + const privData = await helper.generatePrivData(mnemonic, pin); + + const rawVault = { + eth: { + public: [ + { + address: accounts[0], + isDeleted: false, + isImported: false, + label: "EVM Wallet 1", + }, + ], + private: privData, + numberOfAccounts: 1, + }, + }; + + this.initializeSupportedChainKeyringController(mnemonic); + + for (const chain of Object.keys(Chains.nonEvmChains)) { + let addedAcc; + if (chain === "stacks") { + addedAcc = (await this[chain].generateWallet()).address; + } else { + addedAcc = (await this[chain].addAccount()).address; + } + let label = `${Chains.nonEvmChains[chain]} Wallet 1`; + rawVault[chain] = { + public: [ + { + address: addedAcc, + isDeleted: false, + isImported: false, + label: label, + }, + ], + numberOfAccounts: 1, + }; + } - this.initializeDecryptedVault(vault, encryptionKey); + const vault = await helper.cryptography( + JSON.stringify(rawVault), + JSON.stringify(encryptionKey), + "encryption" + ); - this.vault = vault; + this.initializeDecryptedVault(vault, encryptionKey); - this.logs.updateState({ - logs: [{ timestamp: Date.now(), action: 'vault-generation', vault: this.vault }], - }); + this.vault = vault; - return { response: vault }; + this.logs.updateState({ + logs: [ + { + timestamp: Date.now(), + action: "vault-generation", + vault: this.vault, + }, + ], + }); + + return { response: vault }; + } + + async recoverVault( + mnemonic, + encryptionKey, + pin, + unmarshalApiKey, + recoverMechanism = "transactions", + logs = {} + ) { + if ( + typeof pin != "string" || + pin.match(/^[0-9]+$/) === null || + pin < 0 || + pin.length != 6 + ) { + return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; } - async recoverVault(mnemonic, encryptionKey, pin, unmarshalApiKey, recoverMechanism = 'transactions', logs = {}) { - - if (typeof(pin) != 'string'|| pin.match(/^[0-9]+$/) === null || pin < 0 || pin.length !=6 ) { - return { error: ERROR_MESSAGE.INCORRECT_PIN_TYPE }; - } - - if (!encryptionKey) { - return { error : ERROR_MESSAGE.ENTER_CREDS } - } + if (!encryptionKey) { + return { error: ERROR_MESSAGE.ENTER_CREDS }; + } - if(recoverMechanism === 'transactions' && !unmarshalApiKey) { - return { error: ERROR_MESSAGE.INVALID_API_KEY }; - } + if (recoverMechanism === "transactions" && !unmarshalApiKey) { + return { error: ERROR_MESSAGE.INVALID_API_KEY }; + } - const vaultState = await this.keyringInstance.createNewVaultAndRestore(JSON.stringify(encryptionKey), mnemonic); + const vaultState = await this.keyringInstance.createNewVaultAndRestore( + JSON.stringify(encryptionKey), + mnemonic + ); + + let accountsArray = []; + if (recoverMechanism === "transactions") { + accountsArray = await helper.getAccountsFromTransactions( + vaultState.keyrings[0].accounts[0], + this.keyringInstance, + vaultState, + unmarshalApiKey + ); + } else if (recoverMechanism === "logs") { + accountsArray = await helper.getAccountsFromLogs( + "ethereum", + this.keyringInstance, + vaultState, + logs, + vaultState.keyrings[0].accounts[0] + ); + } - let accountsArray = []; - if(recoverMechanism === 'transactions') { - accountsArray = await helper.getAccountsFromTransactions(vaultState.keyrings[0].accounts[0], this.keyringInstance, vaultState, unmarshalApiKey) - } - else if (recoverMechanism === 'logs') { - accountsArray = await helper.getAccountsFromLogs('ethereum', this.keyringInstance, vaultState, logs, vaultState.keyrings[0].accounts[0]) + const privData = await helper.generatePrivData(mnemonic, pin); + + const numberOfAccounts = accountsArray.length; + + let rawVault = { + eth: { public: accountsArray, private: privData, numberOfAccounts }, + }; + + const nonEvmChainList = Object.keys(Chains.nonEvmChains); + + //generate other chain's keyring instance and get accounts from logs + let obj = {}; + for (let chain of nonEvmChainList) { + const keyringInstance = await helper.getCoinInstance( + chain.toLowerCase(), + mnemonic + ); + let address; + if (chain === "stacks") { + address = (await keyringInstance.generateWallet()).address; + } else { + address = (await keyringInstance.addAccount()).address; + } + + const accArray = await helper.getAccountsFromLogs( + chain, + keyringInstance, + vaultState, + logs, + address + ); + + if (chain === "stacks") { + for (let ele of accArray) { + ele.address = ele.address.toUpperCase(); } - - const privData = await helper.generatePrivData(mnemonic, pin); - - const numberOfAccounts = accountsArray.length; - - let rawVault = { eth: { public: accountsArray, private: privData, numberOfAccounts } } - - const nonEvmChainList = Object.keys(Chains.nonEvmChains); - - //generate other chain's keyring instance and get accounts from logs - let obj = {} - for ( let chain of nonEvmChainList) { - const keyringInstance = await helper.getCoinInstance(chain.toLowerCase(), mnemonic); - let address - if(chain === 'stacks') { - address = (await keyringInstance.generateWallet()).address; - } else { - address = (await keyringInstance.addAccount()).address; - } - - const accArray = await helper.getAccountsFromLogs(chain, keyringInstance, vaultState, logs, address); - - if(chain === 'stacks') { - for( let ele of accArray) { - ele.address = ele.address.toUpperCase(); - } - } - const numberOfAcc = accArray.length; - - rawVault[chain.toLowerCase()] = { public: accArray, numberOfAccounts: numberOfAcc } + } + const numberOfAcc = accArray.length; - } + rawVault[chain.toLowerCase()] = { + public: accArray, + numberOfAccounts: numberOfAcc, + }; + } - this.decryptedVault = rawVault + this.decryptedVault = rawVault; - const vault = await helper.cryptography(JSON.stringify(rawVault), JSON.stringify(encryptionKey), 'encryption'); + const vault = await helper.cryptography( + JSON.stringify(rawVault), + JSON.stringify(encryptionKey), + "encryption" + ); - this.vault = vault; + this.vault = vault; - this.logs.getState().logs.push({ timestamp: Date.now(), action: 'vault-recovery', vault: this.vault }); + this.logs.getState().logs.push({ + timestamp: Date.now(), + action: "vault-recovery", + vault: this.vault, + }); - return { response: vault }; - } + return { response: vault }; + } - getSupportedChains() { - const evmChains = Chains.evmChains; - const nonEvmChains = Chains.nonEvmChains; + getSupportedChains() { + const evmChains = Chains.evmChains; + const nonEvmChains = Chains.nonEvmChains; - return { response: { evmChains, nonEvmChains } }; - } + return { response: { evmChains, nonEvmChains } }; + } } -module.exports = Vault \ No newline at end of file +module.exports = Vault;