Skip to content

Commit

Permalink
Fix fee estimation when using ECDSA signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Oct 29, 2024
1 parent 188537d commit 6d7fccc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 13 additions & 4 deletions packages/cashscript/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isUtxoP2PKH,
TransactionDetails,
Unlocker,
SignatureAlgorithm,
} from './interfaces.js';
import {
createInputScript,
Expand Down Expand Up @@ -336,10 +337,18 @@ export class Transaction {
}
}

// Replace all SignatureTemplate with 65-length placeholder Uint8Arrays
const placeholderArgs = this.encodedFunctionArgs.map((arg) => (
arg instanceof SignatureTemplate ? placeholder(65) : arg
));
// Replace all SignatureTemplate with placeholder Uint8Arrays
const placeholderArgs = this.encodedFunctionArgs.map((arg) => {
if (!(arg instanceof SignatureTemplate)) return arg;

// Schnorr signatures are *always* 65 bytes: 64 for signature + 1 byte for hashtype.
if (arg.getSignatureAlgorithm() === SignatureAlgorithm.SCHNORR) return placeholder(65);

// ECDSA signatures are at least 71 bytes: 64 bytes for signature + 1 byte for hashtype + 6 bytes for encoding
// overhead. But it may have up to 2 extra bytes for padding, so we overestimate by 2 bytes.
// (see https://transactionfee.info/charts/bitcoin-script-ecdsa-length/)
return placeholder(73);
});

// Create a placeholder preimage of the correct size
const placeholderPreimage = this.abiFunction.covenant
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/bip68.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Code taken and adapted from https://github.com/bitcoinjs/bip68
// If we make signficant changes to this code, we should also take and adapt the tests from that repository.
// If we make significant changes to this code, we should also take and adapt the tests from that repository.

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

Expand Down

0 comments on commit 6d7fccc

Please sign in to comment.