mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-19 18:00:17 +01:00
44 lines
1.1 KiB
JavaScript
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,
|
|
);
|
|
}
|
|
}
|