[feat]: add hdnode as option

dev
neeboo 5 years ago
parent aae20f81a7
commit d7cd6fe65a
  1. 48
      packages/harmony-account/src/hdnode.ts
  2. 1
      packages/harmony-account/src/index.ts
  3. 2
      packages/harmony-account/src/wallet.ts
  4. 10
      packages/harmony-contract/src/abi/abiCoder.ts
  5. 8
      packages/harmony-crypto/src/keystore.ts
  6. 2
      packages/harmony-utils/src/chain.ts
  7. 1
      packages/harmony-utils/src/index.ts
  8. 7
      packages/harmony-utils/src/tools.ts

@ -0,0 +1,48 @@
import { bip39, hdkey } from '@harmony-js/crypto';
import { HDPath } from '@harmony-js/utils';
export class HDNode extends hdkey {
static new() {
return new HDNode(bip39.generateMnemonic(), 0);
}
static add(phrase: string, index: number) {
return new HDNode(phrase, index);
}
static isValidMnemonic(phrase: string): boolean {
if (phrase.trim().split(/\s+/g).length < 12) {
return false;
}
return bip39.validateMnemonic(phrase);
}
static generateMnemonic(): string {
return bip39.generateMnemonic();
}
private path: string;
private mnemonic?: string;
private entropy?: string;
private childKey?: hdkey;
constructor(menmonic?: string, index: number = 0) {
super();
this.path = HDPath;
this.mnemonic = menmonic;
this.entropy = this.mnemonic ? this.getEntropy(this.mnemonic) : undefined;
this.childKey = this.entropy
? this.getChildKey(this.entropy, index)
: undefined;
}
getEntropy(mnemonic: string) {
return bip39.mnemonicToEntropy(mnemonic);
}
getChildKey(entropy: string, index: number) {
const master = HDNode.fromMasterSeed(Buffer.from(entropy, 'hex'));
return master.derive(`${this.path}${index}`);
}
get _privateKey() {
return this.childKey ? this.childKey.privateKey.toString('hex') : '';
}
get _publicKey() {
return this.childKey ? this.childKey.publicKey.toString('hex') : '';
}
}

@ -2,3 +2,4 @@ export * from './account';
export * from './wallet';
export * from './types';
export * from './utils';
export * from './hdnode';

@ -1,8 +1,8 @@
import { bip39, hdkey, EncryptOptions, getAddress } from '@harmony-js/crypto';
import { Messenger } from '@harmony-js/network';
import { isPrivateKey, isAddress } from '@harmony-js/utils';
import { Account } from './account';
import { Transaction } from '@harmony-js/transaction';
import { Account } from './account';
import { defaultMessenger } from './utils';
class Wallet {

@ -19,7 +19,7 @@ import {
checkNew,
bytesPadRight,
} from '@harmony-js/crypto';
import { hexToBN } from '@harmony-js/utils';
import { hexToBN, defineReadOnly } from '@harmony-js/utils';
const NegativeOne: BN = new BN(-1);
const One: BN = new BN(1);
@ -1130,14 +1130,6 @@ function splitNesting(value: string): any[] {
return result;
}
export function defineReadOnly(object: any, name: string, value: any): void {
Object.defineProperty(object, name, {
enumerable: true,
value,
writable: false,
});
}
// @TODO: Is there a way to return "class"?
const paramTypeSimple: { [key: string]: any } = {
address: CoderAddress,

@ -5,13 +5,7 @@ import uuid from 'uuid';
import { isPrivateKey } from '@harmony-js/utils';
import { randomBytes } from './random';
import { getAddressFromPrivateKey } from './keyTool';
import {
concat,
// arrayify,
// hexDataLength,
// hexToByteArray,
hexToIntArray,
} from './bytes';
import { concat, hexToIntArray } from './bytes';
import { keccak256 } from './keccak256';
import {
KDF,

@ -41,3 +41,5 @@ export abstract class HarmonyCore {
return this.chainId;
}
}
export const HDPath = `m/44'/60'/0'/0/`;

@ -2,3 +2,4 @@ export * from './validators';
export * from './transformers';
export * from './utils';
export * from './chain';
export * from './tools';

@ -0,0 +1,7 @@
export function defineReadOnly(object: any, name: string, value: any): void {
Object.defineProperty(object, name, {
enumerable: true,
value,
writable: false,
});
}
Loading…
Cancel
Save