This repository was archived by the owner on Dec 19, 2025. It is now read-only.
Improve Bitcoin TxBuilder API #157
bmancini55
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The existing
TxBuilderwas built to be functional first to ensure all the required pieces are covered. Because scriptSig and witness data must be constructed after the inputs and outputs are defined (for many SIGHASH_ALL at least), we need defer construction of the scriptSig for inputs until after the inputs and outputs are defined. The existing API is clunky and requires spelunking into the inputs via index.One nuance here is that the signatures must match the final ordering of inputs. The
.inputsproperty goes through theInputSorterand thus matches the final ordering. This means that the index used to assign scriptSig MAY not be in the same index that the input was added under.So without further ado, here are some attempts to improve this API, added here to a discussion to be maintained in posterity!
scriptSig setter function
This version is a mild improvement over the original and hides
txb.inputs[0].scriptSig = <something>behind a function. The benefit here is thatinputsandoutputscan now be private which would simplify the API a bit.The downside is that it still has the same input sorting issue as described above.
scriptSig as Lambda
This version adds a lambda that takes an index and returns a Script,
(index: number) => Script.This method would allow us to couple the logic of the scriptSig with the rest of the input logic. Because it's a lambda, it also allows deferment of scriptSig building until some finalization function is called,
.serialize()or.toTx()and pass in the final sorted index to the signature creation.Fluent builder using lamba for scoping
This uses a fluent builder pattern. The nested builders uses lambda's to control scoping of the object being built. This can internally resolve the input sorting/signature issue.
Fluent with scope finalizer
This example also uses a fluent builder. Instead of using lambdas to control scope we use a scope finalizer that kicks us back to the parent. This can internally resolve the input sorting/signature issue.
Beta Was this translation helpful? Give feedback.
All reactions