Skip to content

Commit

Permalink
v0.11.0 (#28)
Browse files Browse the repository at this point in the history
* Switch HDkeys library with bip32 
* Optimizations all around
* HD Wallet addresses generations are now async by default to unblock the thread
  • Loading branch information
kanatliemre authored Nov 19, 2021
1 parent 0dcc100 commit c7045be
Show file tree
Hide file tree
Showing 21 changed files with 489 additions and 147 deletions.
20 changes: 8 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ on:
push:
branches:
- master
- dev
pull_request:

env:
node_version: 16

jobs:
Test:
runs-on: ${{ matrix.os }}
Expand All @@ -16,12 +20,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-node-
cache: yarn
node-version: ${{ env.node_version }}
- run: yarn --frozen-lockfile
- run: yarn test --ci --coverage --maxWorkers=2
Lint:
Expand All @@ -30,11 +30,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
restore-keys: ${{ runner.os }}-node-
cache: yarn
node-version: ${{ env.node_version }}
- run: yarn --frozen-lockfile
- run: yarn lint
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@
"@ethereumjs/tx": "3.3.0",
"@ledgerhq/hw-app-eth": "6.12.2",
"@obsidiansystems/hw-app-avalanche": "0.2.2",
"@openzeppelin/contracts": "4.3.2",
"@openzeppelin/contracts": "4.3.3",
"avalanche": "3.8.1",
"big.js": "^6.1.1",
"bip32": "^2.0.6",
"bip32-path": "^0.4.2",
"bip39": "^3.0.4",
"bn.js": "5.1.1",
"create-hash": "1.2.0",
"ethereumjs-util": "^7.0.7",
"ethers": "^5.1.4",
"hdkey": "2.0.1",
"moment": "^2.29.1",
"rollup-plugin-commonjs": "^10.1.0",
"sockette": "^2.0.6",
Expand Down
11 changes: 8 additions & 3 deletions src/Asset/Erc20Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ export default class Erc20Token {
//@ts-ignore
let contract = new web3.eth.Contract(ERC20Abi.abi, address);

let contractCalls = await Promise.all([
contract.methods.name().call(),
contract.methods.symbol().call(),
contract.methods.decimals().call(),
]);
// Purify the values for XSS protection
let name = xss(await contract.methods.name().call());
let symbol = xss(await contract.methods.symbol().call());
let decimals = parseInt(await contract.methods.decimals().call());
let name = xss(contractCalls[0]);
let symbol = xss(contractCalls[1]);
let decimals = parseInt(contractCalls[2]);

if (!activeNetwork) {
throw NO_NETWORK;
Expand Down
1 change: 1 addition & 0 deletions src/Explorer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './utils';
8 changes: 3 additions & 5 deletions src/Explorer/Explorer.ts → src/Explorer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { explorer_api } from '@/Network/network';
import { NO_EXPLORER_API } from '@/errors';

async function isAddressUsedX(addr: string) {
export async function isAddressUsedX(addr: string) {
if (!explorer_api) {
throw NO_EXPLORER_API;
}
Expand All @@ -14,7 +14,7 @@ async function isAddressUsedX(addr: string) {
else return false;
}

async function getAddressDetailX(addr: string) {
export async function getAddressDetailX(addr: string) {
if (!explorer_api) {
throw NO_EXPLORER_API;
}
Expand All @@ -27,7 +27,7 @@ async function getAddressDetailX(addr: string) {
}

// Given an array of addresses, checks which chain each address was already used on
async function getAddressChains(addrs: string[]) {
export async function getAddressChains(addrs: string[]) {
if (!explorer_api) {
throw NO_EXPLORER_API;
}
Expand All @@ -46,5 +46,3 @@ async function getAddressChains(addrs: string[]) {

return res.data.addressChains;
}

export { getAddressDetailX, isAddressUsedX, getAddressChains };
4 changes: 2 additions & 2 deletions src/Network/providers/AVMWebSocketProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default class AVMWebSocketProvider {
* Creates a bloom filter from the addresses of the tracked wallets and subscribes to
* transactions on the node.
*/
updateFilterAddresses(): void {
updateFilterAddresses() {
if (!this.isConnected) {
return;
}
Expand All @@ -97,7 +97,7 @@ export default class AVMWebSocketProvider {
let addrs = [];
for (let i = 0; i < wallets.length; i++) {
let w = wallets[i];
let externalAddrs = w.getExternalAddressesX();
let externalAddrs = w.getExternalAddressesXSync();
let addrsLen = externalAddrs.length;
let startIndex = Math.max(0, addrsLen - FILTER_ADDRESS_SIZE);
let addAddrs = externalAddrs.slice(startIndex);
Expand Down
57 changes: 44 additions & 13 deletions src/Wallet/HDWalletAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { WalletProvider } from '@/Wallet/Wallet';
import HdScanner from '@/Wallet/HdScanner';
import HDKey from 'hdkey';
import { UTXOSet as AVMUTXOSet } from 'avalanche/dist/apis/avm/utxos';
import { avalanche } from '@/Network/network';
import { UTXOSet as PlatformUTXOSet } from 'avalanche/dist/apis/platformvm';
import { iHDWalletIndex } from '@/Wallet/types';
import { bintools } from '@/common';
import { networkEvents } from '@/Network/eventEmitter';
import * as bip32 from 'bip32';
import { NetworkConfig } from '@/Network';

export abstract class HDWalletAbstract extends WalletProvider {
protected internalScan: HdScanner;
protected externalScan: HdScanner;
protected accountKey: HDKey;
protected accountKey: bip32.BIP32Interface;
public isHdReady = false;

protected constructor(accountKey: HDKey) {
protected constructor(accountKey: bip32.BIP32Interface) {
super();

this.internalScan = new HdScanner(accountKey, true);
Expand Down Expand Up @@ -69,35 +68,67 @@ export abstract class HDWalletAbstract extends WalletProvider {
/**
* Returns every external X chain address used by the wallet up to now.
*/
public getExternalAddressesX(): string[] {
return this.externalScan.getAllAddresses('X');
public async getExternalAddressesX(): Promise<string[]> {
return await this.externalScan.getAllAddresses('X');
}

/**
* Returns every external X chain address used by the wallet up to now.
*/
public getExternalAddressesXSync(): string[] {
return this.externalScan.getAllAddressesSync('X');
}

/**
* Returns every internal X chain address used by the wallet up to now.
*/
public getInternalAddressesX(): string[] {
return this.internalScan.getAllAddresses('X');
public async getInternalAddressesX(): Promise<string[]> {
return await this.internalScan.getAllAddresses('X');
}

/**
* Returns every internal X chain address used by the wallet up to now.
*/
public getInternalAddressesXSync(): string[] {
return this.internalScan.getAllAddressesSync('X');
}

/**
* Returns every X chain address used by the wallet up to now (internal + external).
*/
public async getAllAddressesX(): Promise<string[]> {
return [...(await this.getExternalAddressesX()), ...(await this.getInternalAddressesX())];
}

/**
* Returns every X chain address used by the wallet up to now (internal + external).
*/
public getAllAddressesX(): string[] {
return [...this.getExternalAddressesX(), ...this.getInternalAddressesX()];
public getAllAddressesXSync(): string[] {
return [...this.getExternalAddressesXSync(), ...this.getInternalAddressesXSync()];
}

public getExternalAddressesP(): string[] {
public async getExternalAddressesP(): Promise<string[]> {
return this.externalScan.getAllAddresses('P');
}

public getExternalAddressesPSync(): string[] {
return this.externalScan.getAllAddressesSync('P');
}

/**
* Returns every P chain address used by the wallet up to now.
*/
public getAllAddressesP(): string[] {
public getAllAddressesP(): Promise<string[]> {
return this.getExternalAddressesP();
}

/**
* Returns every P chain address used by the wallet up to now.
*/
public getAllAddressesPSync(): string[] {
return this.getExternalAddressesPSync();
}

/**
* Scans the network and initializes internal and external addresses on P and X chains.
* - Heavy operation
Expand All @@ -120,7 +151,7 @@ export abstract class HDWalletAbstract extends WalletProvider {
};
}

public async setHdIndices(external: number, internal: number) {
public setHdIndices(external: number, internal: number) {
this.externalScan.setIndex(external);
this.internalScan.setIndex(internal);

Expand Down
Loading

0 comments on commit c7045be

Please sign in to comment.