Skip to content

Commit

Permalink
Merge pull request #28 from bitcoinerlab/coinselect
Browse files Browse the repository at this point in the history
Add getScriptSatisfactionSize function and fix signature size in getTimeConstraints
  • Loading branch information
landabaso authored Nov 13, 2023
2 parents 1641233 + 899a820 commit 493a822
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@bitcoinerlab/descriptors",
"description": "This library parses and creates Bitcoin Miniscript Descriptors and generates Partially Signed Bitcoin Transactions (PSBTs). It provides PSBT finalizers and signers for single-signature, BIP32 and Hardware Wallets.",
"homepage": "https://github.com/bitcoinerlab/descriptors",
"version": "2.0.1",
"version": "2.0.2",
"author": "Jose-Luis Landabaso",
"license": "MIT",
"repository": {
Expand Down
47 changes: 46 additions & 1 deletion src/descriptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ export function DescriptorsFactory(ecc: TinySecp256k1Interface) {
//signatures don't matter
const fakeSignatures = signersPubKeys.map(pubkey => ({
pubkey,
signature: Buffer.alloc(64, 0)
// https://transactionfee.info/charts/bitcoin-script-ecdsa-length/
signature: Buffer.alloc(72, 0)
}));
const { nLockTime, nSequence } = satisfyMiniscript({
expandedMiniscript,
Expand All @@ -715,6 +716,50 @@ export function DescriptorsFactory(ecc: TinySecp256k1Interface) {
return { nLockTime, nSequence };
} else return undefined;
}

/**
* Retrieves the byte length of the script satisfaction for a Miniscript-based
* descriptor, using only the expression, signers' public keys, and preimages
* provided in the constructor.
*
* Useful in scenarios like coin selection algorithms for transaction creation,
* where signatures are not yet available. Since signatures are still to be
* computed, the function assigns a standard length of 72 bytes for each
* signature. However, note that this may not always be completely accurate,
* as approximately 50% of signatures are 71 bytes in length
* (source: https://transactionfee.info/charts/bitcoin-script-ecdsa-length/).
* The function returns the byte length for a worst-case scenario.
*
* @returns The byte length of the compiled script satisfaction, or `undefined`
* if this was not a miniscript-based descriptor.
*/
getScriptSatisfactionSize(): number | undefined {
const miniscript = this.#miniscript;
const preimages = this.#preimages;
const expandedMiniscript = this.#expandedMiniscript;
const expansionMap = this.#expansionMap;
const signersPubKeys = this.#signersPubKeys;
//Create a method. solvePreimages to solve them.
if (miniscript) {
if (expandedMiniscript === undefined || expansionMap === undefined)
throw new Error(
`Error: cannot get script satisfactions from not expanded miniscript ${miniscript}`
);
//We create some fakeSignatures since we may not have them yet.
const fakeSignatures = signersPubKeys.map(pubkey => ({
pubkey,
// https://transactionfee.info/charts/bitcoin-script-ecdsa-length/
signature: Buffer.alloc(72, 0)
}));
const { scriptSatisfaction } = satisfyMiniscript({
expandedMiniscript,
expansionMap,
signatures: fakeSignatures,
preimages
});
return scriptSatisfaction.length;
} else return undefined;
}
/**
* Creates and returns an instance of bitcoinjs-lib
* [`Payment`](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/ts_src/payments/index.ts)'s interface with the `scriptPubKey` of this `Output`.
Expand Down

0 comments on commit 493a822

Please sign in to comment.