mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-19 18:00:17 +01:00
52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
import { LegacyWallet } from './legacy-wallet';
|
|
const bitcoin = require('bitcoinjs-lib');
|
|
|
|
export class SegwitBech32Wallet extends LegacyWallet {
|
|
static type = 'segwitBech32';
|
|
static typeReadable = 'P2 WPKH';
|
|
|
|
getAddress() {
|
|
if (this._address) return this._address;
|
|
let address;
|
|
try {
|
|
let keyPair = bitcoin.ECPair.fromWIF(this.secret);
|
|
address = bitcoin.payments.p2wpkh({
|
|
pubkey: keyPair.publicKey,
|
|
}).address;
|
|
} catch (err) {
|
|
return false;
|
|
}
|
|
this._address = address;
|
|
|
|
return this._address;
|
|
}
|
|
|
|
static witnessToAddress(witness) {
|
|
const pubKey = Buffer.from(witness, 'hex');
|
|
return bitcoin.payments.p2wpkh({
|
|
pubkey: pubKey,
|
|
network: bitcoin.networks.bitcoin,
|
|
}).address;
|
|
}
|
|
|
|
/**
|
|
* Converts script pub key to bech32 address if it can. Returns FALSE if it cant.
|
|
*
|
|
* @param scriptPubKey
|
|
* @returns {boolean|string} Either bech32 address or false
|
|
*/
|
|
static scriptPubKeyToAddress(scriptPubKey) {
|
|
const scriptPubKey2 = Buffer.from(scriptPubKey, 'hex');
|
|
let ret;
|
|
try {
|
|
ret = bitcoin.payments.p2wpkh({
|
|
output: scriptPubKey2,
|
|
network: bitcoin.networks.bitcoin,
|
|
}).address;
|
|
} catch (_) {
|
|
return false;
|
|
}
|
|
return ret;
|
|
}
|
|
}
|