Skip to content

Commit 6d7fccc

Browse files
committed
Fix fee estimation when using ECDSA signatures
1 parent 188537d commit 6d7fccc

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/cashscript/src/Transaction.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
isUtxoP2PKH,
2222
TransactionDetails,
2323
Unlocker,
24+
SignatureAlgorithm,
2425
} from './interfaces.js';
2526
import {
2627
createInputScript,
@@ -336,10 +337,18 @@ export class Transaction {
336337
}
337338
}
338339

339-
// Replace all SignatureTemplate with 65-length placeholder Uint8Arrays
340-
const placeholderArgs = this.encodedFunctionArgs.map((arg) => (
341-
arg instanceof SignatureTemplate ? placeholder(65) : arg
342-
));
340+
// Replace all SignatureTemplate with placeholder Uint8Arrays
341+
const placeholderArgs = this.encodedFunctionArgs.map((arg) => {
342+
if (!(arg instanceof SignatureTemplate)) return arg;
343+
344+
// Schnorr signatures are *always* 65 bytes: 64 for signature + 1 byte for hashtype.
345+
if (arg.getSignatureAlgorithm() === SignatureAlgorithm.SCHNORR) return placeholder(65);
346+
347+
// ECDSA signatures are at least 71 bytes: 64 bytes for signature + 1 byte for hashtype + 6 bytes for encoding
348+
// overhead. But it may have up to 2 extra bytes for padding, so we overestimate by 2 bytes.
349+
// (see https://transactionfee.info/charts/bitcoin-script-ecdsa-length/)
350+
return placeholder(73);
351+
});
343352

344353
// Create a placeholder preimage of the correct size
345354
const placeholderPreimage = this.abiFunction.covenant

packages/utils/src/bip68.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Code taken and adapted from https://github.com/bitcoinjs/bip68
2-
// If we make signficant changes to this code, we should also take and adapt the tests from that repository.
2+
// If we make significant changes to this code, we should also take and adapt the tests from that repository.
33

44
// see https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki#compatibility
55

0 commit comments

Comments
 (0)