BlueWallet/class/wallets/hd-legacy-p2pkh-wallet.ts

112 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-02-23 07:32:47 +00:00
import BIP32Factory, { BIP32Interface } from 'bip32';
import { Psbt } from 'bitcoinjs-lib';
import { CoinSelectReturnInput } from 'coinselect';
import ecc from '../../blue_modules/noble_ecc';
2024-02-23 07:32:47 +00:00
import { AbstractHDElectrumWallet } from './abstract-hd-electrum-wallet';
2022-01-17 15:22:15 +00:00
const bip32 = BIP32Factory(ecc);
const BlueElectrum = require('../../blue_modules/BlueElectrum');
/**
* HD Wallet (BIP39).
* In particular, BIP44 (P2PKH legacy addressess)
* @see https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki
*/
2020-03-08 21:46:43 +00:00
export class HDLegacyP2PKHWallet extends AbstractHDElectrumWallet {
2024-03-15 23:05:15 +03:00
static readonly type = 'HDlegacyP2PKH';
static readonly typeReadable = 'HD Legacy (BIP44 P2PKH)';
// @ts-ignore: override
public readonly type = HDLegacyP2PKHWallet.type;
// @ts-ignore: override
public readonly typeReadable = HDLegacyP2PKHWallet.typeReadable;
static readonly derivationPath = "m/44'/0'/0'";
2018-12-19 12:22:57 +01:00
allowSend() {
return true;
}
2021-02-18 16:37:43 +03:00
allowCosignPsbt() {
return true;
}
2021-03-09 14:26:56 +03:00
allowSignVerifyMessage() {
return true;
}
allowMasterFingerprint() {
return true;
}
allowXpub() {
return true;
}
2023-03-15 20:42:25 +00:00
allowBIP47() {
return true;
}
getXpub() {
if (this._xpub) {
return this._xpub; // cache hit
}
const seed = this._getSeed();
2022-01-17 15:22:15 +00:00
const root = bip32.fromSeed(seed);
const path = this.getDerivationPath();
2024-02-23 07:32:47 +00:00
if (!path) {
throw new Error('Internal error: no path');
}
const child = root.derivePath(path).neutered();
this._xpub = child.toBase58();
return this._xpub;
}
2024-02-23 07:32:47 +00:00
_hdNodeToAddress(hdNode: BIP32Interface): string {
2023-03-15 21:30:00 +00:00
return this._nodeToLegacyAddress(hdNode);
}
2018-12-19 12:22:57 +01:00
2024-02-23 07:32:47 +00:00
async fetchUtxo(): Promise<void> {
await super.fetchUtxo();
// now we need to fetch txhash for each input as required by PSBT
const txhexes = await BlueElectrum.multiGetTransactionByTxid(
this.getUtxo().map(x => x.txid),
50,
false,
);
for (const u of this.getUtxo()) {
if (txhexes[u.txid]) u.txhex = txhexes[u.txid];
2018-12-19 12:22:57 +01:00
}
}
2024-02-23 07:32:47 +00:00
_addPsbtInput(psbt: Psbt, input: CoinSelectReturnInput, sequence: number, masterFingerprintBuffer: Buffer) {
if (!input.address) {
throw new Error('Internal error: no address on Utxo during _addPsbtInput()');
}
const pubkey = this._getPubkeyByAddress(input.address);
2024-02-23 07:32:47 +00:00
const path = this._getDerivationPathByAddress(input.address);
if (!pubkey || !path) {
throw new Error('Internal error: pubkey or path are invalid');
}
if (!input.txhex) throw new Error('UTXO is missing txhex of the input, which is required by PSBT for non-segwit input');
psbt.addInput({
hash: input.txid,
index: input.vout,
sequence,
bip32Derivation: [
{
masterFingerprint: masterFingerprintBuffer,
path,
pubkey,
},
],
// non-segwit inputs now require passing the whole previous tx as Buffer
nonWitnessUtxo: Buffer.from(input.txhex, 'hex'),
});
return psbt;
}
}