2018-07-21 14:52:54 +02:00
|
|
|
import { LegacyWallet } from './';
|
|
|
|
import { AbstractHDWallet } from './abstract-hd-wallet';
|
2018-07-07 23:15:14 +02:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
|
|
|
const bip39 = require('bip39');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* HD Wallet (BIP39).
|
|
|
|
* In particular, BIP44 (P2PKH legacy addressess)
|
|
|
|
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
|
|
|
|
*/
|
2018-07-21 14:52:54 +02:00
|
|
|
export class HDLegacyP2PKHWallet extends AbstractHDWallet {
|
2018-07-07 23:15:14 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.type = 'HDlegacyP2PKH';
|
|
|
|
}
|
|
|
|
|
|
|
|
getTypeReadable() {
|
|
|
|
return 'HD Legacy (BIP44 P2PKH)';
|
|
|
|
}
|
|
|
|
|
|
|
|
getXpub() {
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this._xpub) {
|
|
|
|
return this._xpub; // cache hit
|
|
|
|
}
|
2018-07-07 23:15:14 +02:00
|
|
|
let mnemonic = this.secret;
|
|
|
|
let seed = bip39.mnemonicToSeed(mnemonic);
|
|
|
|
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
|
|
|
|
let path = "m/44'/0'/0'";
|
|
|
|
let child = root.derivePath(path).neutered();
|
2018-07-22 16:49:59 +02:00
|
|
|
this._xpub = child.toBase58();
|
|
|
|
return this._xpub;
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getExternalWIFByIndex(index) {
|
|
|
|
index = index * 1; // cast to int
|
2018-07-22 16:49:59 +02:00
|
|
|
if (index < 0) return '';
|
2018-07-07 23:15:14 +02:00
|
|
|
let mnemonic = this.secret;
|
|
|
|
let seed = bip39.mnemonicToSeed(mnemonic);
|
|
|
|
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
let path = "m/44'/0'/0'/0/" + index;
|
|
|
|
let child = root.derivePath(path);
|
|
|
|
return child.keyPair.toWIF();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getInternalWIFByIndex(index) {
|
|
|
|
index = index * 1; // cast to int
|
|
|
|
let mnemonic = this.secret;
|
|
|
|
let seed = bip39.mnemonicToSeed(mnemonic);
|
|
|
|
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
let path = "m/44'/0'/0'/1/" + index;
|
|
|
|
let child = root.derivePath(path);
|
|
|
|
return child.keyPair.toWIF();
|
|
|
|
}
|
|
|
|
|
|
|
|
_getExternalAddressByIndex(index) {
|
|
|
|
index = index * 1; // cast to int
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this.external_addresses_cache[index]) return this.external_addresses_cache[index]; // cache hit
|
2018-07-07 23:15:14 +02:00
|
|
|
let mnemonic = this.secret;
|
|
|
|
let seed = bip39.mnemonicToSeed(mnemonic);
|
|
|
|
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
let path = "m/44'/0'/0'/0/" + index;
|
|
|
|
let child = root.derivePath(path);
|
|
|
|
|
|
|
|
let w = new LegacyWallet();
|
|
|
|
w.setSecret(child.keyPair.toWIF());
|
2018-07-22 16:49:59 +02:00
|
|
|
return (this.external_addresses_cache[index] = w.getAddress());
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_getInternalAddressByIndex(index) {
|
|
|
|
index = index * 1; // cast to int
|
2018-07-22 16:49:59 +02:00
|
|
|
if (this.internal_addresses_cache[index]) return this.internal_addresses_cache[index]; // cache hit
|
2018-07-07 23:15:14 +02:00
|
|
|
let mnemonic = this.secret;
|
|
|
|
let seed = bip39.mnemonicToSeed(mnemonic);
|
|
|
|
let root = bitcoin.HDNode.fromSeedBuffer(seed);
|
|
|
|
|
|
|
|
let path = "m/44'/0'/0'/1/" + index;
|
|
|
|
let child = root.derivePath(path);
|
|
|
|
|
|
|
|
let w = new LegacyWallet();
|
|
|
|
w.setSecret(child.keyPair.toWIF());
|
2018-07-22 16:49:59 +02:00
|
|
|
return (this.internal_addresses_cache[index] = w.getAddress());
|
2018-07-07 23:15:14 +02:00
|
|
|
}
|
|
|
|
}
|