@btc-vision/transaction 1.8.7-beta.0 → 1.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/browser/btc-vision-bitcoin.js +364 -364
- package/browser/index.js +1022 -903
- package/browser/noble-curves.js +1183 -2156
- package/browser/src/transaction/builders/TransactionBuilder.d.ts +1 -0
- package/browser/src/transaction/offline/OfflineTransactionManager.d.ts +50 -0
- package/browser/src/transaction/offline/interfaces/ISerializableState.d.ts +10 -2
- package/browser/vendors.js +1395 -1395
- package/build/transaction/TransactionFactory.js +1 -1
- package/build/transaction/builders/FundingTransaction.js +17 -13
- package/build/transaction/builders/TransactionBuilder.d.ts +1 -0
- package/build/transaction/builders/TransactionBuilder.js +32 -7
- package/build/transaction/offline/OfflineTransactionManager.d.ts +50 -0
- package/build/transaction/offline/OfflineTransactionManager.js +142 -1
- package/build/transaction/offline/TransactionSerializer.js +9 -0
- package/build/transaction/offline/interfaces/ISerializableState.d.ts +10 -2
- package/build/transaction/offline/interfaces/ISerializableState.js +3 -2
- package/build/transaction/shared/TweakedTransaction.js +27 -5
- package/package.json +1 -1
- package/src/transaction/TransactionFactory.ts +1 -1
- package/src/transaction/builders/FundingTransaction.ts +21 -16
- package/src/transaction/builders/TransactionBuilder.ts +46 -10
- package/src/transaction/offline/OfflineTransactionManager.ts +191 -1
- package/src/transaction/offline/TransactionSerializer.ts +11 -0
- package/src/transaction/offline/interfaces/ISerializableState.ts +10 -2
- package/src/transaction/shared/TweakedTransaction.ts +33 -5
- package/test/add-refund-output.test.ts +96 -11
- package/test/csv-multisig-offline-edges.test.ts +293 -0
- package/test/csv-multisig-offline.test.ts +202 -0
- package/test/transaction-builders.test.ts +69 -3
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -361,7 +361,7 @@ export class TransactionFactory {
|
|
|
361
361
|
estimatedFees: resp.estimatedFees,
|
|
362
362
|
tx: resp.tx.toHex(),
|
|
363
363
|
nextUTXOs: this.getAllNewUTXOs(resp.original, resp.tx, parameters.from),
|
|
364
|
-
inputUtxos: parameters.utxos,
|
|
364
|
+
inputUtxos: [...parameters.utxos, ...(parameters.feeUtxos ?? [])],
|
|
365
365
|
};
|
|
366
366
|
}
|
|
367
367
|
/**
|
|
@@ -22,9 +22,12 @@ export class FundingTransaction extends TransactionBuilder {
|
|
|
22
22
|
throw new Error('Recipient address is required');
|
|
23
23
|
}
|
|
24
24
|
this.addInputsFromUTXO();
|
|
25
|
-
// When autoAdjustAmount is enabled
|
|
26
|
-
//
|
|
27
|
-
|
|
25
|
+
// When autoAdjustAmount is enabled, estimate the fee against the final
|
|
26
|
+
// transaction shape and reduce the output amount whenever amount + fee
|
|
27
|
+
// would exceed the total input. This covers both the send-max case
|
|
28
|
+
// (amount == totalInput) and the near-total case where amount alone fits
|
|
29
|
+
// but leaves no room for the requested feeRate.
|
|
30
|
+
if (this.autoAdjustAmount) {
|
|
28
31
|
// Add temporary outputs matching the ACTUAL final transaction shape
|
|
29
32
|
// so the fee estimate accounts for the real vsize.
|
|
30
33
|
const numOutputs = this.splitInputsInto > 1 ? this.splitInputsInto : 1;
|
|
@@ -48,21 +51,25 @@ export class FundingTransaction extends TransactionBuilder {
|
|
|
48
51
|
});
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
|
-
// If a note is present, add a temporary OP_RETURN since it affects vsize.
|
|
52
54
|
if (this.note) {
|
|
53
55
|
this.addOPReturn(this.note);
|
|
54
56
|
}
|
|
57
|
+
if (this.anchor) {
|
|
58
|
+
this.addAnchor();
|
|
59
|
+
}
|
|
55
60
|
const estimatedFee = await this.estimateTransactionFees();
|
|
56
61
|
// Remove all temporary outputs.
|
|
57
|
-
const tempCount = numOutputs + (this.note ? 1 : 0);
|
|
62
|
+
const tempCount = numOutputs + (this.note ? 1 : 0) + (this.anchor ? 1 : 0);
|
|
58
63
|
for (let i = 0; i < tempCount; i++) {
|
|
59
64
|
this.outputs.pop();
|
|
60
65
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
66
|
+
if (this.amount + estimatedFee > this.totalInputAmount) {
|
|
67
|
+
const adjustedAmount = this.totalInputAmount - estimatedFee;
|
|
68
|
+
if (adjustedAmount < TransactionBuilder.MINIMUM_DUST) {
|
|
69
|
+
throw new Error(`Insufficient funds: after deducting fee of ${estimatedFee} sats, remaining amount ${adjustedAmount} sats is below minimum dust`);
|
|
70
|
+
}
|
|
71
|
+
this.amount = adjustedAmount;
|
|
64
72
|
}
|
|
65
|
-
this.amount = adjustedAmount;
|
|
66
73
|
}
|
|
67
74
|
// Add the primary output(s) first
|
|
68
75
|
if (this.splitInputsInto > 1) {
|
|
@@ -70,10 +77,7 @@ export class FundingTransaction extends TransactionBuilder {
|
|
|
70
77
|
}
|
|
71
78
|
else if (this.isPubKeyDestination) {
|
|
72
79
|
const toHexClean = this.to.startsWith('0x') ? this.to.slice(2) : this.to;
|
|
73
|
-
const pubKeyScript = script.compile([
|
|
74
|
-
fromHex(toHexClean),
|
|
75
|
-
opcodes.OP_CHECKSIG,
|
|
76
|
-
]);
|
|
80
|
+
const pubKeyScript = script.compile([fromHex(toHexClean), opcodes.OP_CHECKSIG]);
|
|
77
81
|
this.addOutput({
|
|
78
82
|
value: toSatoshi(this.amount),
|
|
79
83
|
script: pubKeyScript,
|
|
@@ -12,6 +12,7 @@ import { type Feature, Features } from '../../generators/Features.js';
|
|
|
12
12
|
export declare const MINIMUM_AMOUNT_REWARD: bigint;
|
|
13
13
|
export declare const MINIMUM_AMOUNT_CA: bigint;
|
|
14
14
|
export declare const ANCHOR_SCRIPT: Uint8Array<ArrayBufferLike>;
|
|
15
|
+
export declare const TOLERANCE = 1;
|
|
15
16
|
/**
|
|
16
17
|
* Allows to build a transaction like you would on Ethereum.
|
|
17
18
|
* @description The transaction builder class
|
|
@@ -17,6 +17,10 @@ import { getLevelFromPublicKeyLength } from '../../generators/MLDSAData.js';
|
|
|
17
17
|
export const MINIMUM_AMOUNT_REWARD = 330n; //540n;
|
|
18
18
|
export const MINIMUM_AMOUNT_CA = 297n;
|
|
19
19
|
export const ANCHOR_SCRIPT = fromHex('51024e73');
|
|
20
|
+
// Tolerance for fees
|
|
21
|
+
// Less than 1 is percentage: 0.10 equals +- 10%
|
|
22
|
+
// 1 or more is satoshis, 5 equals +- 5 sat
|
|
23
|
+
export const TOLERANCE = 1;
|
|
20
24
|
/**
|
|
21
25
|
* Allows to build a transaction like you would on Ethereum.
|
|
22
26
|
* @description The transaction builder class
|
|
@@ -284,10 +288,23 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
284
288
|
* @throws {Error} - If something went wrong
|
|
285
289
|
*/
|
|
286
290
|
async signPSBT() {
|
|
287
|
-
if (
|
|
288
|
-
|
|
291
|
+
if (!this.utxos.length)
|
|
292
|
+
throw new Error('No UTXOs specified');
|
|
293
|
+
if (this.to &&
|
|
294
|
+
!this.isPubKeyDestination &&
|
|
295
|
+
!EcKeyPair.verifyContractAddress(this.to, this.network)) {
|
|
296
|
+
throw new Error('Invalid contract address. The contract address must be a taproot address.');
|
|
289
297
|
}
|
|
290
|
-
|
|
298
|
+
if (this.signed)
|
|
299
|
+
throw new Error('Transaction is already signed');
|
|
300
|
+
this.signed = true;
|
|
301
|
+
await this.buildTransaction();
|
|
302
|
+
const built = await this.internalBuildTransaction(this.transaction);
|
|
303
|
+
if (!built)
|
|
304
|
+
throw new Error('Could not sign transaction');
|
|
305
|
+
if (this.regenerated)
|
|
306
|
+
throw new Error('Transaction was regenerated');
|
|
307
|
+
return this.transaction;
|
|
291
308
|
}
|
|
292
309
|
/**
|
|
293
310
|
* Add an input to the transaction.
|
|
@@ -619,13 +636,21 @@ export class TransactionBuilder extends TweakedTransaction {
|
|
|
619
636
|
if (this.debugFees) {
|
|
620
637
|
this.warn(`Amount to send back (${sendBackAmount} sat) is less than minimum dust. Fee without change: ${feeWithoutChange} sats`);
|
|
621
638
|
}
|
|
622
|
-
if (this.totalInputAmount <= amountSpent) {
|
|
623
|
-
throw new Error(`Insufficient funds: need ${amountSpent + feeWithoutChange} sats but only have ${this.totalInputAmount} sats`);
|
|
624
|
-
}
|
|
625
639
|
if (expectRefund && sendBackAmount < 0n) {
|
|
626
|
-
throw new Error(`Insufficient funds: need at least ${-sendBackAmount} more sats to cover fees.`);
|
|
640
|
+
throw new Error(`Insufficient funds for fee: need at least ${-sendBackAmount} more sats to cover fees.`);
|
|
627
641
|
}
|
|
628
642
|
}
|
|
643
|
+
const amountNeeded = amountSpent + this.transactionFee;
|
|
644
|
+
const tolerance = TOLERANCE < 1
|
|
645
|
+
? BigInt(Math.ceil(Number(this.transactionFee) * TOLERANCE))
|
|
646
|
+
: BigInt(Math.ceil(TOLERANCE));
|
|
647
|
+
if (this.totalInputAmount < amountSpent) {
|
|
648
|
+
throw new Error(`Insufficient funds: need ${amountNeeded} sats but only have ${this.totalInputAmount} sats`);
|
|
649
|
+
}
|
|
650
|
+
if (this.totalInputAmount < amountNeeded - tolerance) {
|
|
651
|
+
const missingAmount = amountNeeded - this.totalInputAmount - tolerance;
|
|
652
|
+
throw new Error(`Insufficient funds for fee: need at least ${missingAmount} more sats to cover fees.`);
|
|
653
|
+
}
|
|
629
654
|
if (this.debugFees) {
|
|
630
655
|
this.log(`Final fee: ${this.transactionFee} sats, Change output: ${this.feeOutput ? `${this.feeOutput.value} sats` : 'none'}`);
|
|
631
656
|
}
|
|
@@ -5,6 +5,19 @@ import { TransactionBuilder } from '../builders/TransactionBuilder.js';
|
|
|
5
5
|
import type { ISerializableTransactionState, PrecomputedData } from './interfaces/ISerializableState.js';
|
|
6
6
|
import { type ReconstructionOptions } from './TransactionReconstructor.js';
|
|
7
7
|
import type { IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters, ITransactionParameters } from '../interfaces/ITransactionParameters.js';
|
|
8
|
+
/**
|
|
9
|
+
* Per-input CSV multisig signing status.
|
|
10
|
+
*/
|
|
11
|
+
export interface CSVMultisigInputStatus {
|
|
12
|
+
/** PSBT input index */
|
|
13
|
+
readonly inputIndex: number;
|
|
14
|
+
/** Threshold required to spend this input */
|
|
15
|
+
readonly required: number;
|
|
16
|
+
/** Number of distinct cosigner signatures collected so far */
|
|
17
|
+
readonly collected: number;
|
|
18
|
+
/** Hex-encoded x-only pubkeys of the cosigners that have signed */
|
|
19
|
+
readonly signers: string[];
|
|
20
|
+
}
|
|
8
21
|
/**
|
|
9
22
|
* Export options for offline transaction signing
|
|
10
23
|
*/
|
|
@@ -246,4 +259,41 @@ export declare class OfflineTransactionManager {
|
|
|
246
259
|
* @returns Updated state
|
|
247
260
|
*/
|
|
248
261
|
static multiSigUpdatePsbt(serializedState: string, psbtBase64: string): string;
|
|
262
|
+
/**
|
|
263
|
+
* Inspect collaborative signing progress on all CSV multisig inputs.
|
|
264
|
+
* Returns an empty array when no partial PSBT has been produced yet.
|
|
265
|
+
*/
|
|
266
|
+
static csvMultisigGetStatus(serializedState: string): CSVMultisigInputStatus[];
|
|
267
|
+
/**
|
|
268
|
+
* Add the given signer's contribution to every CSV multisig input it can sign.
|
|
269
|
+
* No-op for inputs whose tapscript does not contain the signer's x-only pubkey.
|
|
270
|
+
*
|
|
271
|
+
* First call (no partial PSBT yet) builds the PSBT from the funding params
|
|
272
|
+
* using the provided signer as the builder's signer. Subsequent calls load
|
|
273
|
+
* the carried PSBT and add the signer's tapScriptSig where applicable.
|
|
274
|
+
*/
|
|
275
|
+
static addCSVMultisigSignature(serializedState: string, signer: Signer | UniversalSigner): Promise<{
|
|
276
|
+
state: string;
|
|
277
|
+
final: boolean;
|
|
278
|
+
perInput: CSVMultisigInputStatus[];
|
|
279
|
+
}>;
|
|
280
|
+
/**
|
|
281
|
+
* Finalize every CSV multisig input on the partial PSBT and extract the
|
|
282
|
+
* raw transaction hex. Throws if any input is still below its threshold.
|
|
283
|
+
*/
|
|
284
|
+
static csvMultisigFinalize(serializedState: string): string;
|
|
285
|
+
/**
|
|
286
|
+
* Sign every CSV multisig input whose tapscript contains the signer's
|
|
287
|
+
* x-only pubkey and does not yet carry a signature from that pubkey.
|
|
288
|
+
*/
|
|
289
|
+
private static addSignerToCSVInputs;
|
|
290
|
+
/**
|
|
291
|
+
* Build per-input status for every CSV multisig input.
|
|
292
|
+
*
|
|
293
|
+
* Walks state.utxos so we can detect CSV multisig inputs even after the
|
|
294
|
+
* builder has already finalized them (which clears tapLeafScript). For
|
|
295
|
+
* partially-signed inputs the signers list reflects collected tapScriptSig
|
|
296
|
+
* entries; for finalized inputs collected is set to the threshold.
|
|
297
|
+
*/
|
|
298
|
+
private static computeCSVMultisigStatus;
|
|
249
299
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fromHex, Psbt, toHex } from '@btc-vision/bitcoin';
|
|
1
|
+
import { fromHex, Psbt, toHex, toXOnly } from '@btc-vision/bitcoin';
|
|
2
2
|
import {} from '@btc-vision/ecpair';
|
|
3
3
|
import { TransactionType } from '../enums/TransactionType.js';
|
|
4
4
|
import { TransactionBuilder } from '../builders/TransactionBuilder.js';
|
|
@@ -7,6 +7,7 @@ import { TransactionSerializer } from './TransactionSerializer.js';
|
|
|
7
7
|
import { TransactionReconstructor, } from './TransactionReconstructor.js';
|
|
8
8
|
import { TransactionStateCapture } from './TransactionStateCapture.js';
|
|
9
9
|
import { isMultiSigSpecificData } from './interfaces/ITypeSpecificData.js';
|
|
10
|
+
import { CSVMultisigProvider } from '../mineable/CSVMultisigProvider.js';
|
|
10
11
|
/**
|
|
11
12
|
* Main entry point for offline transaction signing workflow.
|
|
12
13
|
*
|
|
@@ -435,4 +436,144 @@ export class OfflineTransactionManager {
|
|
|
435
436
|
};
|
|
436
437
|
return TransactionSerializer.toBase64(newState);
|
|
437
438
|
}
|
|
439
|
+
/**
|
|
440
|
+
* Inspect collaborative signing progress on all CSV multisig inputs.
|
|
441
|
+
* Returns an empty array when no partial PSBT has been produced yet.
|
|
442
|
+
*/
|
|
443
|
+
static csvMultisigGetStatus(serializedState) {
|
|
444
|
+
const state = TransactionSerializer.fromBase64(serializedState);
|
|
445
|
+
if (!state.partialPsbtBase64)
|
|
446
|
+
return [];
|
|
447
|
+
const network = TransactionReconstructor['nameToNetwork'](state.baseParams.networkName);
|
|
448
|
+
const psbt = Psbt.fromBase64(state.partialPsbtBase64, { network });
|
|
449
|
+
return this.computeCSVMultisigStatus(state, psbt, network);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Add the given signer's contribution to every CSV multisig input it can sign.
|
|
453
|
+
* No-op for inputs whose tapscript does not contain the signer's x-only pubkey.
|
|
454
|
+
*
|
|
455
|
+
* First call (no partial PSBT yet) builds the PSBT from the funding params
|
|
456
|
+
* using the provided signer as the builder's signer. Subsequent calls load
|
|
457
|
+
* the carried PSBT and add the signer's tapScriptSig where applicable.
|
|
458
|
+
*/
|
|
459
|
+
static async addCSVMultisigSignature(serializedState, signer) {
|
|
460
|
+
const state = TransactionSerializer.fromBase64(serializedState);
|
|
461
|
+
const network = TransactionReconstructor['nameToNetwork'](state.baseParams.networkName);
|
|
462
|
+
let psbt;
|
|
463
|
+
if (state.partialPsbtBase64) {
|
|
464
|
+
psbt = Psbt.fromBase64(state.partialPsbtBase64, { network });
|
|
465
|
+
this.addSignerToCSVInputs(psbt, signer, network);
|
|
466
|
+
}
|
|
467
|
+
else {
|
|
468
|
+
const builder = this.importForSigning(serializedState, { signer });
|
|
469
|
+
psbt = await builder.signPSBT();
|
|
470
|
+
// Best-effort: ensure the signer's contribution is recorded on every
|
|
471
|
+
// CSV input where they're a cosigner (the builder already attempts
|
|
472
|
+
// this, but this guards against builds that bailed early).
|
|
473
|
+
this.addSignerToCSVInputs(psbt, signer, network);
|
|
474
|
+
}
|
|
475
|
+
const perInput = this.computeCSVMultisigStatus(state, psbt, network);
|
|
476
|
+
const final = perInput.length > 0 && perInput.every((s) => s.collected >= s.required);
|
|
477
|
+
const newState = {
|
|
478
|
+
...state,
|
|
479
|
+
partialPsbtBase64: psbt.toBase64(),
|
|
480
|
+
};
|
|
481
|
+
return {
|
|
482
|
+
state: TransactionSerializer.toBase64(newState),
|
|
483
|
+
final,
|
|
484
|
+
perInput,
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Finalize every CSV multisig input on the partial PSBT and extract the
|
|
489
|
+
* raw transaction hex. Throws if any input is still below its threshold.
|
|
490
|
+
*/
|
|
491
|
+
static csvMultisigFinalize(serializedState) {
|
|
492
|
+
const state = TransactionSerializer.fromBase64(serializedState);
|
|
493
|
+
if (!state.partialPsbtBase64) {
|
|
494
|
+
throw new Error('No partial PSBT in state — call addCSVMultisigSignature first');
|
|
495
|
+
}
|
|
496
|
+
const network = TransactionReconstructor['nameToNetwork'](state.baseParams.networkName);
|
|
497
|
+
const psbt = Psbt.fromBase64(state.partialPsbtBase64, { network });
|
|
498
|
+
for (let i = 0; i < psbt.data.inputs.length; i++) {
|
|
499
|
+
const input = psbt.data.inputs[i];
|
|
500
|
+
if (!input.tapLeafScript || input.tapLeafScript.length === 0)
|
|
501
|
+
continue;
|
|
502
|
+
const leaf = input.tapLeafScript[0];
|
|
503
|
+
if (!leaf)
|
|
504
|
+
continue;
|
|
505
|
+
const addr = CSVMultisigProvider.deriveAddressFromTapscript(leaf.script, network);
|
|
506
|
+
if (!addr)
|
|
507
|
+
continue;
|
|
508
|
+
CSVMultisigProvider.finalizePsbtInput(psbt, i, network, addr);
|
|
509
|
+
}
|
|
510
|
+
return psbt.extractTransaction(true, true).toHex();
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Sign every CSV multisig input whose tapscript contains the signer's
|
|
514
|
+
* x-only pubkey and does not yet carry a signature from that pubkey.
|
|
515
|
+
*/
|
|
516
|
+
static addSignerToCSVInputs(psbt, signer, network) {
|
|
517
|
+
const signerXOnly = toXOnly(signer.publicKey);
|
|
518
|
+
const signerXOnlyHex = toHex(signerXOnly);
|
|
519
|
+
for (let i = 0; i < psbt.data.inputs.length; i++) {
|
|
520
|
+
const input = psbt.data.inputs[i];
|
|
521
|
+
if (!input.tapLeafScript || input.tapLeafScript.length === 0)
|
|
522
|
+
continue;
|
|
523
|
+
const leaf = input.tapLeafScript[0];
|
|
524
|
+
if (!leaf)
|
|
525
|
+
continue;
|
|
526
|
+
const addr = CSVMultisigProvider.deriveAddressFromTapscript(leaf.script, network);
|
|
527
|
+
if (!addr)
|
|
528
|
+
continue;
|
|
529
|
+
const isCosigner = addr.config.pubkeys.some((pk) => toHex(pk) === signerXOnlyHex);
|
|
530
|
+
if (!isCosigner)
|
|
531
|
+
continue;
|
|
532
|
+
const alreadySigned = (input.tapScriptSig ?? []).some((s) => toHex(s.pubkey) === signerXOnlyHex);
|
|
533
|
+
if (alreadySigned)
|
|
534
|
+
continue;
|
|
535
|
+
psbt.signTaprootInput(i, signer);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Build per-input status for every CSV multisig input.
|
|
540
|
+
*
|
|
541
|
+
* Walks state.utxos so we can detect CSV multisig inputs even after the
|
|
542
|
+
* builder has already finalized them (which clears tapLeafScript). For
|
|
543
|
+
* partially-signed inputs the signers list reflects collected tapScriptSig
|
|
544
|
+
* entries; for finalized inputs collected is set to the threshold.
|
|
545
|
+
*/
|
|
546
|
+
static computeCSVMultisigStatus(state, psbt, network) {
|
|
547
|
+
const result = [];
|
|
548
|
+
for (let i = 0; i < state.utxos.length; i++) {
|
|
549
|
+
const utxo = state.utxos[i];
|
|
550
|
+
if (!utxo?.witnessScript)
|
|
551
|
+
continue;
|
|
552
|
+
const tapscript = fromHex(utxo.witnessScript);
|
|
553
|
+
const addr = CSVMultisigProvider.deriveAddressFromTapscript(tapscript, network);
|
|
554
|
+
if (!addr)
|
|
555
|
+
continue;
|
|
556
|
+
const input = psbt.data.inputs[i];
|
|
557
|
+
if (!input)
|
|
558
|
+
continue;
|
|
559
|
+
if (input.finalScriptWitness) {
|
|
560
|
+
// Already finalized — by construction it carried >= threshold sigs.
|
|
561
|
+
result.push({
|
|
562
|
+
inputIndex: i,
|
|
563
|
+
required: addr.config.threshold,
|
|
564
|
+
collected: addr.config.threshold,
|
|
565
|
+
signers: [],
|
|
566
|
+
});
|
|
567
|
+
continue;
|
|
568
|
+
}
|
|
569
|
+
const tapSigs = input.tapScriptSig ?? [];
|
|
570
|
+
result.push({
|
|
571
|
+
inputIndex: i,
|
|
572
|
+
required: addr.config.threshold,
|
|
573
|
+
collected: tapSigs.length,
|
|
574
|
+
signers: tapSigs.map((s) => toHex(s.pubkey)),
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
return result;
|
|
578
|
+
}
|
|
438
579
|
}
|
|
@@ -35,6 +35,8 @@ export class TransactionSerializer {
|
|
|
35
35
|
this.writeTypeSpecificData(writer, state.typeSpecificData);
|
|
36
36
|
// Write precomputed data
|
|
37
37
|
this.writePrecomputedData(writer, state.precomputedData);
|
|
38
|
+
// v2: partial PSBT (empty string when absent)
|
|
39
|
+
writer.writeStringWithLength(state.partialPsbtBase64 ?? '');
|
|
38
40
|
// Get buffer and calculate checksum
|
|
39
41
|
const dataBuffer = writer.getBuffer();
|
|
40
42
|
const checksum = this.calculateChecksum(dataBuffer);
|
|
@@ -82,6 +84,12 @@ export class TransactionSerializer {
|
|
|
82
84
|
const typeSpecificData = this.readTypeSpecificData(reader, header.transactionType);
|
|
83
85
|
// Read precomputed data
|
|
84
86
|
const precomputedData = this.readPrecomputedData(reader);
|
|
87
|
+
// v2+: partial PSBT (collaborative signing carryover)
|
|
88
|
+
let partialPsbtBase64;
|
|
89
|
+
if (header.formatVersion >= 2) {
|
|
90
|
+
const stored = reader.readStringWithLength();
|
|
91
|
+
partialPsbtBase64 = stored.length > 0 ? stored : undefined;
|
|
92
|
+
}
|
|
85
93
|
return {
|
|
86
94
|
header,
|
|
87
95
|
baseParams,
|
|
@@ -92,6 +100,7 @@ export class TransactionSerializer {
|
|
|
92
100
|
signerMappings,
|
|
93
101
|
typeSpecificData,
|
|
94
102
|
precomputedData,
|
|
103
|
+
...(partialPsbtBase64 !== undefined ? { partialPsbtBase64 } : {}),
|
|
95
104
|
};
|
|
96
105
|
}
|
|
97
106
|
/**
|
|
@@ -2,9 +2,10 @@ import { TransactionType } from '../../enums/TransactionType.js';
|
|
|
2
2
|
import { ChainId } from '../../../network/ChainId.js';
|
|
3
3
|
import type { TypeSpecificData } from './ITypeSpecificData.js';
|
|
4
4
|
/**
|
|
5
|
-
* Format version for serialization compatibility
|
|
5
|
+
* Format version for serialization compatibility.
|
|
6
|
+
* v2 adds the optional `partialPsbtBase64` field on the top-level state.
|
|
6
7
|
*/
|
|
7
|
-
export declare const SERIALIZATION_FORMAT_VERSION =
|
|
8
|
+
export declare const SERIALIZATION_FORMAT_VERSION = 2;
|
|
8
9
|
/**
|
|
9
10
|
* Magic byte for identifying serialized transaction state
|
|
10
11
|
*/
|
|
@@ -129,4 +130,11 @@ export interface ISerializableTransactionState {
|
|
|
129
130
|
readonly typeSpecificData: TypeSpecificData;
|
|
130
131
|
/** Pre-computed data for deterministic rebuild */
|
|
131
132
|
readonly precomputedData: PrecomputedData;
|
|
133
|
+
/**
|
|
134
|
+
* Partial PSBT (base64) carrying signatures collected so far.
|
|
135
|
+
* Used by collaborative flows like CSV multisig where multiple cosigners
|
|
136
|
+
* accumulate signatures across hops before finalization.
|
|
137
|
+
* Only present once at least one signing hop has run.
|
|
138
|
+
*/
|
|
139
|
+
readonly partialPsbtBase64?: string;
|
|
132
140
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { TransactionType } from '../../enums/TransactionType.js';
|
|
2
2
|
import { ChainId } from '../../../network/ChainId.js';
|
|
3
3
|
/**
|
|
4
|
-
* Format version for serialization compatibility
|
|
4
|
+
* Format version for serialization compatibility.
|
|
5
|
+
* v2 adds the optional `partialPsbtBase64` field on the top-level state.
|
|
5
6
|
*/
|
|
6
|
-
export const SERIALIZATION_FORMAT_VERSION =
|
|
7
|
+
export const SERIALIZATION_FORMAT_VERSION = 2;
|
|
7
8
|
/**
|
|
8
9
|
* Magic byte for identifying serialized transaction state
|
|
9
10
|
*/
|
|
@@ -580,10 +580,23 @@ export class TweakedTransaction extends Logger {
|
|
|
580
580
|
await this.signInputsSequential(transaction);
|
|
581
581
|
}
|
|
582
582
|
}
|
|
583
|
+
let allFinalized = true;
|
|
583
584
|
for (let i = 0; i < transaction.data.inputs.length; i++) {
|
|
584
|
-
|
|
585
|
+
try {
|
|
586
|
+
transaction.finalizeInput(i, this.customFinalizerP2SH.bind(this));
|
|
587
|
+
}
|
|
588
|
+
catch (e) {
|
|
589
|
+
// CSV multisig inputs may legitimately be below threshold during
|
|
590
|
+
// a collaborative signing hop. Leave them unfinalized so the
|
|
591
|
+
// partial PSBT can travel to the next cosigner.
|
|
592
|
+
if (this.csvMultisigInputs.has(i)) {
|
|
593
|
+
allFinalized = false;
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
throw e;
|
|
597
|
+
}
|
|
585
598
|
}
|
|
586
|
-
this.finalized =
|
|
599
|
+
this.finalized = allFinalized;
|
|
587
600
|
}
|
|
588
601
|
/**
|
|
589
602
|
* Signs all inputs sequentially in batches of 20.
|
|
@@ -1155,11 +1168,20 @@ export class TweakedTransaction extends Logger {
|
|
|
1155
1168
|
const signer = this.signer;
|
|
1156
1169
|
// then, we sign all the remaining inputs with the wallet signer.
|
|
1157
1170
|
await signer.multiSignPsbt([transaction]);
|
|
1158
|
-
|
|
1171
|
+
let allFinalized = true;
|
|
1159
1172
|
for (let i = 0; i < transaction.data.inputs.length; i++) {
|
|
1160
|
-
|
|
1173
|
+
try {
|
|
1174
|
+
transaction.finalizeInput(i, this.customFinalizerP2SH.bind(this));
|
|
1175
|
+
}
|
|
1176
|
+
catch (e) {
|
|
1177
|
+
if (this.csvMultisigInputs.has(i)) {
|
|
1178
|
+
allFinalized = false;
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
throw e;
|
|
1182
|
+
}
|
|
1161
1183
|
}
|
|
1162
|
-
this.finalized =
|
|
1184
|
+
this.finalized = allFinalized;
|
|
1163
1185
|
}
|
|
1164
1186
|
isCSVScript(decompiled) {
|
|
1165
1187
|
return decompiled.some((op) => op === opcodes.OP_CHECKSEQUENCEVERIFY);
|
package/package.json
CHANGED
|
@@ -556,7 +556,7 @@ export class TransactionFactory {
|
|
|
556
556
|
estimatedFees: resp.estimatedFees,
|
|
557
557
|
tx: resp.tx.toHex(),
|
|
558
558
|
nextUTXOs: this.getAllNewUTXOs(resp.original, resp.tx, parameters.from),
|
|
559
|
-
inputUtxos: parameters.utxos,
|
|
559
|
+
inputUtxos: [...parameters.utxos, ...(parameters.feeUtxos ?? [])],
|
|
560
560
|
};
|
|
561
561
|
}
|
|
562
562
|
|
|
@@ -31,9 +31,12 @@ export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDI
|
|
|
31
31
|
|
|
32
32
|
this.addInputsFromUTXO();
|
|
33
33
|
|
|
34
|
-
// When autoAdjustAmount is enabled
|
|
35
|
-
//
|
|
36
|
-
|
|
34
|
+
// When autoAdjustAmount is enabled, estimate the fee against the final
|
|
35
|
+
// transaction shape and reduce the output amount whenever amount + fee
|
|
36
|
+
// would exceed the total input. This covers both the send-max case
|
|
37
|
+
// (amount == totalInput) and the near-total case where amount alone fits
|
|
38
|
+
// but leaves no room for the requested feeRate.
|
|
39
|
+
if (this.autoAdjustAmount) {
|
|
37
40
|
// Add temporary outputs matching the ACTUAL final transaction shape
|
|
38
41
|
// so the fee estimate accounts for the real vsize.
|
|
39
42
|
const numOutputs = this.splitInputsInto > 1 ? this.splitInputsInto : 1;
|
|
@@ -59,27 +62,32 @@ export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDI
|
|
|
59
62
|
}
|
|
60
63
|
}
|
|
61
64
|
|
|
62
|
-
// If a note is present, add a temporary OP_RETURN since it affects vsize.
|
|
63
65
|
if (this.note) {
|
|
64
66
|
this.addOPReturn(this.note);
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
if (this.anchor) {
|
|
70
|
+
this.addAnchor();
|
|
71
|
+
}
|
|
72
|
+
|
|
67
73
|
const estimatedFee = await this.estimateTransactionFees();
|
|
68
74
|
|
|
69
75
|
// Remove all temporary outputs.
|
|
70
|
-
const tempCount = numOutputs + (this.note ? 1 : 0);
|
|
76
|
+
const tempCount = numOutputs + (this.note ? 1 : 0) + (this.anchor ? 1 : 0);
|
|
71
77
|
for (let i = 0; i < tempCount; i++) {
|
|
72
78
|
this.outputs.pop();
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
if (this.amount + estimatedFee > this.totalInputAmount) {
|
|
82
|
+
const adjustedAmount = this.totalInputAmount - estimatedFee;
|
|
83
|
+
if (adjustedAmount < TransactionBuilder.MINIMUM_DUST) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Insufficient funds: after deducting fee of ${estimatedFee} sats, remaining amount ${adjustedAmount} sats is below minimum dust`,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
81
88
|
|
|
82
|
-
|
|
89
|
+
this.amount = adjustedAmount;
|
|
90
|
+
}
|
|
83
91
|
}
|
|
84
92
|
|
|
85
93
|
// Add the primary output(s) first
|
|
@@ -87,10 +95,7 @@ export class FundingTransaction extends TransactionBuilder<TransactionType.FUNDI
|
|
|
87
95
|
this.splitInputs(this.amount);
|
|
88
96
|
} else if (this.isPubKeyDestination) {
|
|
89
97
|
const toHexClean = this.to.startsWith('0x') ? this.to.slice(2) : this.to;
|
|
90
|
-
const pubKeyScript: Script = script.compile([
|
|
91
|
-
fromHex(toHexClean),
|
|
92
|
-
opcodes.OP_CHECKSIG,
|
|
93
|
-
]);
|
|
98
|
+
const pubKeyScript: Script = script.compile([fromHex(toHexClean), opcodes.OP_CHECKSIG]);
|
|
94
99
|
|
|
95
100
|
this.addOutput({
|
|
96
101
|
value: toSatoshi(this.amount),
|
|
@@ -46,6 +46,11 @@ export const MINIMUM_AMOUNT_REWARD: bigint = 330n; //540n;
|
|
|
46
46
|
export const MINIMUM_AMOUNT_CA: bigint = 297n;
|
|
47
47
|
export const ANCHOR_SCRIPT = fromHex('51024e73');
|
|
48
48
|
|
|
49
|
+
// Tolerance for fees
|
|
50
|
+
// Less than 1 is percentage: 0.10 equals +- 10%
|
|
51
|
+
// 1 or more is satoshis, 5 equals +- 5 sat
|
|
52
|
+
export const TOLERANCE = 1;
|
|
53
|
+
|
|
49
54
|
/**
|
|
50
55
|
* Allows to build a transaction like you would on Ethereum.
|
|
51
56
|
* @description The transaction builder class
|
|
@@ -387,11 +392,28 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
387
392
|
* @throws {Error} - If something went wrong
|
|
388
393
|
*/
|
|
389
394
|
public async signPSBT(): Promise<Psbt> {
|
|
390
|
-
if (
|
|
391
|
-
|
|
395
|
+
if (!this.utxos.length) throw new Error('No UTXOs specified');
|
|
396
|
+
|
|
397
|
+
if (
|
|
398
|
+
this.to &&
|
|
399
|
+
!this.isPubKeyDestination &&
|
|
400
|
+
!EcKeyPair.verifyContractAddress(this.to, this.network)
|
|
401
|
+
) {
|
|
402
|
+
throw new Error(
|
|
403
|
+
'Invalid contract address. The contract address must be a taproot address.',
|
|
404
|
+
);
|
|
392
405
|
}
|
|
393
406
|
|
|
394
|
-
throw new Error('
|
|
407
|
+
if (this.signed) throw new Error('Transaction is already signed');
|
|
408
|
+
this.signed = true;
|
|
409
|
+
|
|
410
|
+
await this.buildTransaction();
|
|
411
|
+
|
|
412
|
+
const built = await this.internalBuildTransaction(this.transaction);
|
|
413
|
+
if (!built) throw new Error('Could not sign transaction');
|
|
414
|
+
if (this.regenerated) throw new Error('Transaction was regenerated');
|
|
415
|
+
|
|
416
|
+
return this.transaction;
|
|
395
417
|
}
|
|
396
418
|
|
|
397
419
|
/**
|
|
@@ -804,19 +826,33 @@ export abstract class TransactionBuilder<T extends TransactionType> extends Twea
|
|
|
804
826
|
);
|
|
805
827
|
}
|
|
806
828
|
|
|
807
|
-
if (this.totalInputAmount <= amountSpent) {
|
|
808
|
-
throw new Error(
|
|
809
|
-
`Insufficient funds: need ${amountSpent + feeWithoutChange} sats but only have ${this.totalInputAmount} sats`,
|
|
810
|
-
);
|
|
811
|
-
}
|
|
812
|
-
|
|
813
829
|
if (expectRefund && sendBackAmount < 0n) {
|
|
814
830
|
throw new Error(
|
|
815
|
-
`Insufficient funds: need at least ${-sendBackAmount} more sats to cover fees.`,
|
|
831
|
+
`Insufficient funds for fee: need at least ${-sendBackAmount} more sats to cover fees.`,
|
|
816
832
|
);
|
|
817
833
|
}
|
|
818
834
|
}
|
|
819
835
|
|
|
836
|
+
const amountNeeded = amountSpent + this.transactionFee;
|
|
837
|
+
|
|
838
|
+
const tolerance: bigint =
|
|
839
|
+
TOLERANCE < 1
|
|
840
|
+
? BigInt(Math.ceil(Number(this.transactionFee) * TOLERANCE))
|
|
841
|
+
: BigInt(Math.ceil(TOLERANCE));
|
|
842
|
+
|
|
843
|
+
if (this.totalInputAmount < amountSpent) {
|
|
844
|
+
throw new Error(
|
|
845
|
+
`Insufficient funds: need ${amountNeeded} sats but only have ${this.totalInputAmount} sats`,
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
if (this.totalInputAmount < amountNeeded - tolerance) {
|
|
850
|
+
const missingAmount = amountNeeded - this.totalInputAmount - tolerance;
|
|
851
|
+
throw new Error(
|
|
852
|
+
`Insufficient funds for fee: need at least ${missingAmount} more sats to cover fees.`,
|
|
853
|
+
);
|
|
854
|
+
}
|
|
855
|
+
|
|
820
856
|
if (this.debugFees) {
|
|
821
857
|
this.log(
|
|
822
858
|
`Final fee: ${this.transactionFee} sats, Change output: ${this.feeOutput ? `${this.feeOutput.value} sats` : 'none'}`,
|