Add sweepAll to Electrum wallet RPC client.
Consolidate payto, deserialize, and broadcast into a single sweep helper for max-payment withdrawals.
This commit is contained in:
@@ -88,6 +88,85 @@ export class ElectrumWalletRpcClient {
|
||||
};
|
||||
}
|
||||
|
||||
async sweepAll(
|
||||
destinationAddress: string,
|
||||
feeRateSatVbyte: number
|
||||
): Promise<{ txHash: string; amountAtomic: string }> {
|
||||
const signedTransaction = await this.payToMax(destinationAddress, feeRateSatVbyte);
|
||||
const transaction = await this.deserializeTransaction(signedTransaction);
|
||||
|
||||
const amountAtomic = this.sumOutputValueAtomic(transaction, destinationAddress);
|
||||
|
||||
if (amountAtomic === '0') {
|
||||
throw new Error('Withdrawal transaction has no spendable output to the destination address');
|
||||
}
|
||||
|
||||
const txHash = await this.broadcast(signedTransaction);
|
||||
|
||||
return { txHash, amountAtomic };
|
||||
}
|
||||
|
||||
private async payToMax(destinationAddress: string, feeRateSatVbyte: number): Promise<string> {
|
||||
const { walletPassword } = this.configService.get('electrumWallet') as Config['electrumWallet'];
|
||||
|
||||
const signedTransaction = await this.call<string>(
|
||||
'payto',
|
||||
{
|
||||
destination: destinationAddress,
|
||||
amount: '!',
|
||||
feerate: String(feeRateSatVbyte),
|
||||
password: walletPassword
|
||||
},
|
||||
{ timeoutMs: 120_000 }
|
||||
);
|
||||
|
||||
if (!signedTransaction) {
|
||||
throw new Error('Electrum wallet RPC payto returned no transaction');
|
||||
}
|
||||
|
||||
return signedTransaction;
|
||||
}
|
||||
|
||||
private async broadcast(signedTransaction: string): Promise<string> {
|
||||
const txHash = await this.call<string>('broadcast', { tx: signedTransaction });
|
||||
|
||||
if (!txHash) {
|
||||
throw new Error('Electrum wallet RPC broadcast returned no transaction hash');
|
||||
}
|
||||
|
||||
return txHash;
|
||||
}
|
||||
|
||||
private async deserializeTransaction(signedTransaction: string): Promise<ElectrumWalletDeserializedTransaction> {
|
||||
return this.call<ElectrumWalletDeserializedTransaction>('deserialize', { tx: signedTransaction });
|
||||
}
|
||||
|
||||
private sumOutputValueAtomic(transaction: ElectrumWalletDeserializedTransaction, address: string): string {
|
||||
if (!Array.isArray(transaction.outputs)) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
return transaction.outputs.reduce((sum, output) => {
|
||||
if (output.address !== address || !Number.isFinite(output.value_sats) || output.value_sats <= 0) {
|
||||
return sum;
|
||||
}
|
||||
|
||||
return addAtomic(sum, String(output.value_sats));
|
||||
}, '0');
|
||||
}
|
||||
|
||||
async getSeed(): Promise<string> {
|
||||
const { walletPassword } = this.configService.get('electrumWallet') as Config['electrumWallet'];
|
||||
|
||||
const mnemonic = await this.call<string>('getseed', { password: walletPassword });
|
||||
|
||||
if (!mnemonic) {
|
||||
throw new Error('Electrum wallet RPC getseed returned no mnemonic');
|
||||
}
|
||||
|
||||
return mnemonic;
|
||||
}
|
||||
|
||||
async createAddress(label?: string): Promise<string> {
|
||||
const address = await this.call<string>('createnewaddress');
|
||||
|
||||
@@ -120,12 +199,8 @@ export class ElectrumWalletRpcClient {
|
||||
}
|
||||
|
||||
const serializedTransaction = await this.call<string>('gettransaction', { txid: txHash });
|
||||
|
||||
const transaction = await this.call<ElectrumWalletDeserializedTransaction>('deserialize', {
|
||||
tx: serializedTransaction
|
||||
});
|
||||
|
||||
const amountAtomic = this.sumIncomingOutputValueAtomic(transaction, address);
|
||||
const transaction = await this.deserializeTransaction(serializedTransaction);
|
||||
const amountAtomic = this.sumOutputValueAtomic(transaction, address);
|
||||
|
||||
return this.mapIncomingTransfer(entry, amountAtomic, blockHeight);
|
||||
})
|
||||
@@ -134,20 +209,6 @@ export class ElectrumWalletRpcClient {
|
||||
return transfers.filter((transfer): transfer is ElectrumWalletIncomingTransfer => transfer !== null);
|
||||
}
|
||||
|
||||
private sumIncomingOutputValueAtomic(transaction: ElectrumWalletDeserializedTransaction, address: string): string {
|
||||
if (!Array.isArray(transaction.outputs)) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
return transaction.outputs.reduce((sum, output) => {
|
||||
if (output.address !== address || !Number.isFinite(output.value_sats) || output.value_sats <= 0) {
|
||||
return sum;
|
||||
}
|
||||
|
||||
return addAtomic(sum, String(output.value_sats));
|
||||
}, '0');
|
||||
}
|
||||
|
||||
private mapIncomingTransfer(
|
||||
entry: ElectrumWalletAddressHistoryEntry,
|
||||
amountAtomic: string,
|
||||
|
||||
Reference in New Issue
Block a user