REF: silent-payments minor refactor

This commit is contained in:
overtorment 2024-05-31 20:59:03 +01:00
parent f52ba46db5
commit 1cffbf2696

View File

@ -17,7 +17,7 @@ import ecc from '../../blue_modules/noble_ecc';
import { randomBytes } from '../rng'; import { randomBytes } from '../rng';
import { AbstractHDWallet } from './abstract-hd-wallet'; import { AbstractHDWallet } from './abstract-hd-wallet';
import { CreateTransactionResult, CreateTransactionTarget, CreateTransactionUtxo, Transaction, Utxo } from './types'; import { CreateTransactionResult, CreateTransactionTarget, CreateTransactionUtxo, Transaction, Utxo } from './types';
import { SilentPayment } from 'silent-payments'; import { SilentPayment, UTXOType as SPUTXOType, UTXO as SPUTXO } from 'silent-payments';
const ECPair = ECPairFactory(ecc); const ECPair = ECPairFactory(ecc);
const bip32 = BIP32Factory(ecc); const bip32 = BIP32Factory(ecc);
@ -1185,22 +1185,14 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
let { inputs, outputs, fee } = this.coinselect(utxos, targets, feeRate); let { inputs, outputs, fee } = this.coinselect(utxos, targets, feeRate);
let hasSilentPaymentOutput = false; const hasSilentPaymentOutput: boolean = !!outputs.find(o => o.address?.startsWith('sp1'));
outputs.map(o => {
if (o.address?.startsWith('sp1')) hasSilentPaymentOutput = true;
return null; // because map func demands to return at least something
});
if (hasSilentPaymentOutput) { if (hasSilentPaymentOutput) {
if (!this.allowSilentPaymentSend()) { if (!this.allowSilentPaymentSend()) {
throw new Error('This wallet can not send to SilentPayment address'); throw new Error('This wallet can not send to SilentPayment address');
} }
// doing a clone of coinselected UTXOs:
const spUtxos: any[] = [];
// for a single wallet all utxos gona be the same type, so we define it only once: // for a single wallet all utxos gona be the same type, so we define it only once:
let utxoType: string = 'non-eligible'; let utxoType: SPUTXOType = 'non-eligible';
switch (this.segwitType) { switch (this.segwitType) {
case 'p2sh(p2wpkh)': case 'p2sh(p2wpkh)':
utxoType = 'p2sh-p2wpkh'; utxoType = 'p2sh-p2wpkh';
@ -1208,20 +1200,12 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
case 'p2wpkh': case 'p2wpkh':
utxoType = 'p2wpkh'; utxoType = 'p2wpkh';
break; break;
} default:
// @ts-ignore override // @ts-ignore override
if (this.type === 'HDlegacyP2PKH') utxoType = 'p2pkh'; if (this.type === 'HDlegacyP2PKH') utxoType = 'p2pkh';
}
inputs.map(u => const spUtxos: SPUTXO[] = inputs.map(u => ({ ...u, utxoType, wif: u.wif! }));
spUtxos.push({
txid: u.txid,
vout: u.vout,
wif: u.wif,
utxoType,
}),
);
const sp = new SilentPayment(); const sp = new SilentPayment();
outputs = sp.createTransaction(spUtxos, outputs) as CoinSelectOutput[]; outputs = sp.createTransaction(spUtxos, outputs) as CoinSelectOutput[];
} }
@ -1289,6 +1273,7 @@ export class AbstractHDElectrumWallet extends AbstractHDWallet {
if (output.address?.startsWith('PM')) { if (output.address?.startsWith('PM')) {
// ok its BIP47 payment code, so we need to unwrap a joint address for the receiver and use it instead: // ok its BIP47 payment code, so we need to unwrap a joint address for the receiver and use it instead:
output.address = this._getNextFreePaymentCodeAddressSend(output.address); output.address = this._getNextFreePaymentCodeAddressSend(output.address);
// ^^^ trusting that notification transaction is in place
} }
psbt.addOutput({ psbt.addOutput({