BlueWallet/class/segwit-bech-wallet.js
2018-06-24 22:27:34 +01:00

44 lines
1.1 KiB
JavaScript

import { LegacyWallet } from './legacy-wallet';
const bitcoin = require('bitcoinjs-lib');
export class SegwitBech32Wallet extends LegacyWallet {
constructor() {
super();
this.type = 'segwitBech32';
}
getTypeReadable() {
return 'P2 WPKH';
}
getAddress() {
if (this._address) return this._address;
let address;
try {
let keyPair = bitcoin.ECPair.fromWIF(this.secret);
let pubKey = keyPair.getPublicKeyBuffer();
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(
bitcoin.crypto.hash160(pubKey),
);
address = bitcoin.address.fromOutputScript(scriptPubKey);
} catch (err) {
return false;
}
this._address = address;
return this._address;
}
static witnessToAddress(witness) {
const pubKey = Buffer.from(witness, 'hex');
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(
pubKeyHash,
);
return bitcoin.address.fromOutputScript(
scriptPubKey,
bitcoin.networks.bitcoin,
);
}
}