Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest tests for wallet utils #50

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,4 +33,3 @@ export async function checkApproval(walletAddress) {
throw Error(e);
}
}

112 changes: 56 additions & 56 deletions src/utils/blockchain/wallet.js
Original file line number Diff line number Diff line change
@@ -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;
105 changes: 105 additions & 0 deletions tests/wallet/wallet.spec.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
});