From 79ba571d8ced41b341f4dfa1f551bcc24a22e87b Mon Sep 17 00:00:00 2001 From: Rasil Baidar Date: Thu, 9 Dec 2021 21:58:44 +0545 Subject: [PATCH] wrote test for wallet util --- src/services/index.js | 2 +- src/utils/blockchain/wallet.js | 112 ++++++++++++++++----------------- tests/wallet/wallet.spec.js | 105 +++++++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 57 deletions(-) create mode 100644 tests/wallet/wallet.spec.js diff --git a/src/services/index.js b/src/services/index.js index c78fedc..a137a23 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -17,6 +17,7 @@ export async function registerToAgency(payload) { export async function getPackageDetails(id) { try { + if (!id) throw new Error('Must send id'); const res = await axios.get(`${API.NFT}/token/${id}`); return res.data; } catch (e) { @@ -32,4 +33,3 @@ export async function checkApproval(walletAddress) { throw Error(e); } } - diff --git a/src/utils/blockchain/wallet.js b/src/utils/blockchain/wallet.js index 4ddcbf9..b6b7936 100644 --- a/src/utils/blockchain/wallet.js +++ b/src/utils/blockchain/wallet.js @@ -1,56 +1,56 @@ -import { ethers } from 'ethers'; - -import DataService from '../../services/db'; - -const Wallet = { - isValidMnemonic: ethers.utils.isValidMnemonic, - - async connectProvider(wallet, network) { - if (!network) network = await DataService.getNetwork(); - const { url } = network; - const provider = new ethers.providers.JsonRpcProvider(url); - return wallet.connect(provider); - }, - - async getProvider() { - if (!network) network = await DataService.getNetwork(); - const { url } = network; - const provider = new ethers.providers.JsonRpcProvider(url); - return provider; - }, - - async loadFromJson(passcode, encryptedJsonWallet) { - if (!passcode) { - throw Error('Passcode must be set first'); - } - const wallet = await ethers.Wallet.fromEncryptedJson(encryptedJsonWallet, passcode.toString()); - return this.connectProvider(wallet); - }, - - async loadWallet(passcode) { - const wlt = await DataService.getWallet(); - return this.loadFromJson(passcode, wlt); - }, - - async create(passcode, mnemonic) { - if (!passcode) { - throw Error('Passcode must be set first'); - } - let wallet = null; - if (mnemonic) wallet = ethers.Wallet.fromMnemonic(mnemonic); - else wallet = ethers.Wallet.createRandom(); - - wallet = await this.connectProvider(wallet); - const encryptedWallet = await wallet.encrypt(passcode.toString()); - return { wallet, encryptedWallet }; - }, - - async loadFromPrivateKey(privateKey) { - if (!privateKey) return null; - let wallet = await new ethers.Wallet(privateKey); - if (!wallet) throw Error('Wallet not found'); - return this.connectProvider(wallet); - } -}; - -export default Wallet; +import { ethers } from 'ethers'; + +import DataService from '../../services/db'; + +const Wallet = { + isValidMnemonic: ethers.utils.isValidMnemonic, + + async connectProvider(wallet, network) { + if (!network) network = await DataService.getNetwork(); + const { url } = network; + const provider = new ethers.providers.JsonRpcProvider(url); + return wallet.connect(provider); + }, + + async getProvider(network = '') { + if (!network) network = await DataService.getNetwork(); + const { url } = network; + const provider = new ethers.providers.JsonRpcProvider(url); + return provider; + }, + + async loadFromJson(passcode, encryptedJsonWallet) { + if (!passcode) { + throw Error('Passcode must be set first'); + } + const wallet = await ethers.Wallet.fromEncryptedJson(encryptedJsonWallet, passcode.toString()); + return this.connectProvider(wallet); + }, + + async loadWallet(passcode) { + const wlt = await DataService.getWallet(); + return this.loadFromJson(passcode, wlt); + }, + + async create(passcode, mnemonic) { + if (!passcode) { + throw Error('Passcode must be set first'); + } + let wallet = null; + if (mnemonic) wallet = ethers.Wallet.fromMnemonic(mnemonic); + else wallet = ethers.Wallet.createRandom(); + + wallet = await this.connectProvider(wallet); + const encryptedWallet = await wallet.encrypt(passcode.toString()); + return { wallet, encryptedWallet }; + }, + + async loadFromPrivateKey(privateKey) { + if (!privateKey) return null; + let wallet = new ethers.Wallet(privateKey); + if (!wallet) throw Error('Wallet not found'); + return this.connectProvider(wallet); + } +}; + +export default Wallet; diff --git a/tests/wallet/wallet.spec.js b/tests/wallet/wallet.spec.js new file mode 100644 index 0000000..a1e64fe --- /dev/null +++ b/tests/wallet/wallet.spec.js @@ -0,0 +1,105 @@ +import { ethers } from 'ethers'; + +import 'regenerator-runtime/runtime'; +import Wallet from '../../src/utils/blockchain/wallet'; +import { NETWORKS } from '../../src/constants/networks'; +import 'fake-indexeddb/auto'; +import DataService from '../../src/services/db'; + +jest.setTimeout(100000); + +const mnemonic = 'announce room limb pattern dry unit scale effort smooth jazz weasel alcohol'; +class MockWallet { + constructor() { + this.mnemonic = mnemonic; + this.wallet = {}; + this.encryptedWallet = {}; + this.setWalletFromMnemonic(); + } + + setWalletFromMnemonic() { + const wallet = ethers.Wallet.fromMnemonic(this.mnemonic); + this.wallet = wallet; + } + getWallet() { + return this.wallet; + } + setEncryptedWallet(encrypted) { + this.encryptedWallet(encrypted); + } + getEncryptedWallet() { + return this.encryptedWallet; + } + getPrivateKey() { + return this.wallet.privateKey; + } +} + +describe('Test all methods in utils/wallet.js file', () => { + const passcode = '123123'; + + const globalWallet = {}; + + const mockWallet = new MockWallet(); + + it('catches error while trying to create wallet without properly', async () => { + // const wallet = await Wallet.create(); + try { + await Wallet.create(); + } catch (e) { + expect(e.message).toMatch('Passcode must be set first'); + } + }); + it('creates wallet properly', async () => { + // const wallet = await Wallet.create(); + + const { wallet, encryptedWallet } = await Wallet.create(passcode); + + globalWallet.wallet = wallet; + globalWallet.encryptedWallet = encryptedWallet; + await DataService.saveWallet(encryptedWallet); + }); + + it('creates wallet from mnemonic properly', async () => { + const { wallet } = await Wallet.create(passcode, mnemonic); + const savedMockWallet = await Wallet.connectProvider(mockWallet.getWallet()); + expect(wallet.getAddress()).toEqual(savedMockWallet.getAddress()); + }); + + it('loads wallet from encryptedJson', async () => { + const unlockedWallet = await Wallet.loadFromJson(passcode, globalWallet.encryptedWallet); + + const savedMockWallet = await Wallet.connectProvider(mockWallet.getWallet()); + + expect(unlockedWallet.getAddress()).toEqual(savedMockWallet.getAddress()); + }); + + it('loads wallet from private key', async () => { + const walletFromPrivateKey = await Wallet.loadFromPrivateKey(mockWallet.getPrivateKey()); + + const savedMockWallet = await Wallet.connectProvider(mockWallet.getWallet()); + + expect(walletFromPrivateKey.getAddress()).toEqual(savedMockWallet.getAddress()); + }); + it('gets provider', async () => { + const defNetwork = NETWORKS.find(ntwrk => ntwrk.default === true); + const mockProvider = new ethers.providers.JsonRpcProvider(defNetwork.url); + + const provider = await Wallet.getProvider(); + expect(provider.getNetwork()).toMatchObject(mockProvider.getNetwork()); + }); + it('loads wallet from indexDb', async () => { + const wallet = await Wallet.loadWallet(passcode); + const savedMockWallet = await Wallet.connectProvider(mockWallet.getWallet()); + + expect(wallet.getAddress()).toEqual(savedMockWallet.getAddress()); + }); + + it('catches error when loading wallet from Json if passcode not sent', async () => { + try { + await Wallet.create(); + } catch (e) { + expect(e.message).toMatch('Passcode must be set first'); + } + }); +});