Skip to content

Instantly share code, notes, and snippets.

@pau1m
Created June 15, 2018 07:41
Show Gist options
  • Save pau1m/32ed59df25b8d216509eecd4b8cd26aa to your computer and use it in GitHub Desktop.
Save pau1m/32ed59df25b8d216509eecd4b8cd26aa to your computer and use it in GitHub Desktop.
Ethereum account generation from seed
const Web3: any = require('web3'); // tslint:disable-line:variable-name
const bip39: any = require('bip39');
const hdkey: any = require('ethereumjs-wallet/hdkey');
const web3: any = new Web3();
export class Accounts {
private seed: string;
constructor(seed: string) {
this.seed = seed;
}
private getSeed(): string {
return this.seed;
}
getPrivateKey(index: number): string {
const hdwallet: any = hdkey.fromMasterSeed(bip39.mnemonicToSeed(this.getSeed));
const walletHdPath: any = "m/44'/60'/0'/0/"; // tslint:disable-line:quotemark
const wallet: any = hdwallet.derivePath(walletHdPath + index).getWallet();
const privateKey = wallet.getPrivateKey().toString('hex');
return '0x' + privateKey;
}
getAddress(index: number): string {
const privateKey: string = this.getPrivateKey(index);
const account: any = web3.eth.accounts.privateKeyToAccount(privateKey);
return account.address;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment