2018-03-24 21:24:20 +00:00
|
|
|
import { LegacyWallet } from './legacy-wallet';
|
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2018-03-20 22:41:07 +02:00
|
|
|
|
|
|
|
export class SegwitBech32Wallet extends LegacyWallet {
|
2018-12-28 16:52:06 +01:00
|
|
|
static type = 'segwitBech32';
|
|
|
|
static typeReadable = 'P2 WPKH';
|
2018-03-20 22:41:07 +02:00
|
|
|
|
|
|
|
getAddress() {
|
|
|
|
if (this._address) return this._address;
|
|
|
|
let address;
|
|
|
|
try {
|
|
|
|
let keyPair = bitcoin.ECPair.fromWIF(this.secret);
|
|
|
|
let pubKey = keyPair.getPublicKeyBuffer();
|
2018-07-07 14:04:32 +01:00
|
|
|
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey));
|
2018-03-20 22:41:07 +02:00
|
|
|
address = bitcoin.address.fromOutputScript(scriptPubKey);
|
|
|
|
} catch (err) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._address = address;
|
|
|
|
|
|
|
|
return this._address;
|
|
|
|
}
|
2018-06-24 22:27:34 +01:00
|
|
|
|
|
|
|
static witnessToAddress(witness) {
|
|
|
|
const pubKey = Buffer.from(witness, 'hex');
|
|
|
|
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
|
2018-07-07 14:04:32 +01:00
|
|
|
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash);
|
|
|
|
return bitcoin.address.fromOutputScript(scriptPubKey, bitcoin.networks.bitcoin);
|
2018-06-24 22:27:34 +01:00
|
|
|
}
|
2019-01-05 20:23:51 +00:00
|
|
|
|
|
|
|
static scriptPubKeyToAddress(scriptPubKey) {
|
|
|
|
const scriptPubKey2 = Buffer.from(scriptPubKey, 'hex');
|
|
|
|
return bitcoin.address.fromOutputScript(scriptPubKey2, bitcoin.networks.bitcoin);
|
|
|
|
}
|
2018-03-20 22:41:07 +02:00
|
|
|
}
|