You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@node-lightning/bitcoin

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-lightning/bitcoin

Bitcoin utilities

0.21.1
Source
npmnpm
Version published
Maintainers
1
Created
Source

@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:

// here are some dummy values that are needed
const privA; // 32-byte private key
const pubkeyA; // 33-byte compressed public key
const pubkeyhashB; // 20-byte hash of B's public key

// construct a builder
const txb = new TxBuilder();

// attach a single input that has 50 bitcoin in it
txb.addInput("9d0e63ad73020a9fad0106b6727e31d36e3ab4b9a01451233926d4759569de68:0");

// attach a spending output that pays to B's pubkeyhash
txb.addOutput(1, Script.p2pkhLock(pubkeyhashB));

// attach a change output that pays back to A's pubkey
txb.addOutput(48.9999, Script.p2pkhLock(pubkeyA));

// sign the input using the prior locking script
const commitScript = Script.p2pkhLock(pubkeyA);
const sig = txb.sign(0, commitScript, privA);

// create the scriptSig for the input that includes the signature and pubkey
// corresponding to the signature
txb.inputs[0].scriptSig = Script.p2pkhUnlock(sig, pubkeyA);

// convert the transaction into an immutable transaction
const tx = tx.toTx();

Legacy

Special Transactions

Native P2WPKH

P2SH-P2WPKH

Native P2WSH

P2SH-P2WSH

Keywords

bitcoin

FAQs

Package last updated on 18 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts