@node-lightning/bitcoin
This package provides common Bitcoin functionality and does not rely on third party libraries.
Transaction Building
Transaction building uses the TxBuilder
class. This class allows modification of the version
and locktime
. It also allows addition of inputs and outputs via the addInput
and addOutput
methods.
A typical workflow for transaction building looks like:
- Create the builder
- Set the version
- Set the locktime
- Add inputs via their outpoint
- Add outputs by specifying a value in Bitcoin and including a locking script
- Sign your inputs and assign the signature to corresponding input's ScriptSig
A simple example looks like:
const privA;
const pubkeyA;
const pubkeyhashB;
const txb = new TxBuilder();
txb.addInput("9d0e63ad73020a9fad0106b6727e31d36e3ab4b9a01451233926d4759569de68:0");
txb.addOutput(1, Script.p2pkhLock(pubkeyhashB));
txb.addOutput(48.9999, Script.p2pkhLock(pubkeyA));
const commitScript = Script.p2pkhLock(pubkeyA);
const sig = txb.sign(0, commitScript, privA);
txb.inputs[0].scriptSig = Script.p2pkhUnlock(sig, pubkeyA);
const tx = tx.toTx();
Legacy
Special Transactions
Native P2WPKH
P2SH-P2WPKH
Native P2WSH
P2SH-P2WSH