bitcoinjs-lib
- Version 6.1.5
- Published
- 239 kB
- 6 dependencies
- MIT license
Install
npm i bitcoinjs-lib
yarn add bitcoinjs-lib
pnpm add bitcoinjs-lib
Overview
Client-side Bitcoin JavaScript library
Index
Variables
Functions
Classes
Psbt
- addInput()
- addInputs()
- addOutput()
- addOutputs()
- addUnknownKeyValToGlobal()
- addUnknownKeyValToInput()
- addUnknownKeyValToOutput()
- clearFinalizedInput()
- clone()
- combine()
- data
- extractTransaction()
- finalizeAllInputs()
- finalizeInput()
- finalizeTaprootInput()
- fromBase64()
- fromBuffer()
- fromHex()
- getFee()
- getFeeRate()
- getInputType()
- inputCount
- inputHasHDKey()
- inputHasPubkey()
- locktime
- outputHasHDKey()
- outputHasPubkey()
- setInputSequence()
- setLocktime()
- setMaximumFeeRate()
- setVersion()
- signAllInputs()
- signAllInputsAsync()
- signAllInputsHD()
- signAllInputsHDAsync()
- signInput()
- signInputAsync()
- signInputHD()
- signInputHDAsync()
- signTaprootInput()
- signTaprootInputAsync()
- toBase64()
- toBuffer()
- toHex()
- txInputs
- txOutputs
- updateGlobal()
- updateInput()
- updateOutput()
- validateSignaturesOfAllInputs()
- validateSignaturesOfInput()
- version
Transaction
- addInput()
- addOutput()
- ADVANCED_TRANSACTION_FLAG
- ADVANCED_TRANSACTION_MARKER
- byteLength()
- clone()
- DEFAULT_SEQUENCE
- fromBuffer()
- fromHex()
- getHash()
- getId()
- hashForSignature()
- hashForWitnessV0()
- hashForWitnessV1()
- hasWitnesses()
- ins
- isCoinbase()
- isCoinbaseHash()
- locktime
- outs
- setInputScript()
- setWitness()
- SIGHASH_ALL
- SIGHASH_ANYONECANPAY
- SIGHASH_DEFAULT
- SIGHASH_INPUT_MASK
- SIGHASH_NONE
- SIGHASH_OUTPUT_MASK
- SIGHASH_SINGLE
- toBuffer()
- toHex()
- version
- virtualSize()
- weight()
Interfaces
Type Aliases
Namespaces
Variables
variable opcodes
const opcodes: { [key: string]: number };
Functions
function initEccLib
initEccLib: (eccLib: TinySecp256k1Interface | undefined) => void;
Classes
class Block
class Block {}
property bits
bits: number;
property merkleRoot
merkleRoot?: Buffer;
property nonce
nonce: number;
property prevHash
prevHash?: Buffer;
property timestamp
timestamp: number;
property transactions
transactions?: Transaction[];
property version
version: number;
property witnessCommit
witnessCommit?: Buffer;
method byteLength
byteLength: (headersOnly?: boolean, allowWitness?: boolean) => number;
method calculateMerkleRoot
static calculateMerkleRoot: ( transactions: Transaction[], forWitness?: boolean) => Buffer;
method calculateTarget
static calculateTarget: (bits: number) => Buffer;
method checkProofOfWork
checkProofOfWork: () => boolean;
method checkTxRoots
checkTxRoots: () => boolean;
method fromBuffer
static fromBuffer: (buffer: Buffer) => Block;
method fromHex
static fromHex: (hex: string) => Block;
method getHash
getHash: () => Buffer;
method getId
getId: () => string;
method getUTCDate
getUTCDate: () => Date;
method getWitnessCommit
getWitnessCommit: () => Buffer | null;
method hasWitness
hasWitness: () => boolean;
method hasWitnessCommit
hasWitnessCommit: () => boolean;
method toBuffer
toBuffer: (headersOnly?: boolean) => Buffer;
method toHex
toHex: (headersOnly?: boolean) => string;
method weight
weight: () => number;
class Psbt
class Psbt {}
Psbt class can parse and generate a PSBT binary based off of the BIP174. There are 6 roles that this class fulfills. (Explained in BIP174)
Creator: This can be done with
new Psbt()
Updater: This can be done withpsbt.addInput(input)
,psbt.addInputs(inputs)
,psbt.addOutput(output)
,psbt.addOutputs(outputs)
when you are looking to add new inputs and outputs to the PSBT, andpsbt.updateGlobal(itemObject)
,psbt.updateInput(itemObject)
,psbt.updateOutput(itemObject)
addInput requires hash: Buffer | string; and index: number; as attributes and can also include any attributes that are used in updateInput method. addOutput requires script: Buffer; and value: number; and likewise can include data for updateOutput. For a list of what attributes should be what types. Check the bip174 library. Also, check the integration tests for some examples of usage. Signer: There are a few methods. signAllInputs and signAllInputsAsync, which will search all input information for your pubkey or pubkeyhash, and only sign inputs where it finds your info. Or you can explicitly sign a specific input with signInput and signInputAsync. For the async methods you can create a SignerAsync object and use something like a hardware wallet to sign with. (You must implement this) Combiner: psbts can be combined easily withpsbt.combine(psbt2, psbt3, psbt4 ...)
the psbt calling combine will always have precedence when a conflict occurs. Combine checks if the internal bitcoin transaction is the same, so be sure that all sequences, version, locktime, etc. are the same before combining. Input Finalizer: This role is fairly important. Not only does it need to construct the input scriptSigs and witnesses, but it SHOULD verify the signatures etc. Before runningpsbt.finalizeAllInputs()
please runpsbt.validateSignaturesOfAllInputs()
Running any finalize method will delete any data in the input(s) that are no longer needed due to the finalized scripts containing the information. Transaction Extractor: This role will perform some checks before returning a Transaction object. Such as fee rate not being larger than maximumFeeRate etc.
constructor
constructor(opts?: PsbtOptsOptional, data?: PsbtBase);
property data
readonly data: PsbtBase;
property inputCount
readonly inputCount: number;
property locktime
locktime: number;
property txInputs
readonly txInputs: PsbtTxInput[];
property txOutputs
readonly txOutputs: PsbtTxOutput[];
property version
version: number;
method addInput
addInput: (inputData: PsbtInputExtended) => this;
method addInputs
addInputs: (inputDatas: PsbtInputExtended[]) => this;
method addOutput
addOutput: (outputData: PsbtOutputExtended) => this;
method addOutputs
addOutputs: (outputDatas: PsbtOutputExtended[]) => this;
method addUnknownKeyValToGlobal
addUnknownKeyValToGlobal: (keyVal: KeyValue) => this;
method addUnknownKeyValToInput
addUnknownKeyValToInput: (inputIndex: number, keyVal: KeyValue) => this;
method addUnknownKeyValToOutput
addUnknownKeyValToOutput: (outputIndex: number, keyVal: KeyValue) => this;
method clearFinalizedInput
clearFinalizedInput: (inputIndex: number) => this;
method clone
clone: () => Psbt;
method combine
combine: (...those: Psbt[]) => this;
method extractTransaction
extractTransaction: (disableFeeCheck?: boolean) => Transaction;
method finalizeAllInputs
finalizeAllInputs: () => this;
method finalizeInput
finalizeInput: ( inputIndex: number, finalScriptsFunc?: FinalScriptsFunc | FinalTaprootScriptsFunc) => this;
method finalizeTaprootInput
finalizeTaprootInput: ( inputIndex: number, tapLeafHashToFinalize?: Buffer, finalScriptsFunc?: FinalTaprootScriptsFunc) => this;
method fromBase64
static fromBase64: (data: string, opts?: PsbtOptsOptional) => Psbt;
method fromBuffer
static fromBuffer: (buffer: Buffer, opts?: PsbtOptsOptional) => Psbt;
method fromHex
static fromHex: (data: string, opts?: PsbtOptsOptional) => Psbt;
method getFee
getFee: () => number;
method getFeeRate
getFeeRate: () => number;
method getInputType
getInputType: (inputIndex: number) => AllScriptType;
method inputHasHDKey
inputHasHDKey: (inputIndex: number, root: HDSigner) => boolean;
method inputHasPubkey
inputHasPubkey: (inputIndex: number, pubkey: Buffer) => boolean;
method outputHasHDKey
outputHasHDKey: (outputIndex: number, root: HDSigner) => boolean;
method outputHasPubkey
outputHasPubkey: (outputIndex: number, pubkey: Buffer) => boolean;
method setInputSequence
setInputSequence: (inputIndex: number, sequence: number) => this;
method setLocktime
setLocktime: (locktime: number) => this;
method setMaximumFeeRate
setMaximumFeeRate: (satoshiPerByte: number) => void;
method setVersion
setVersion: (version: number) => this;
method signAllInputs
signAllInputs: (keyPair: Signer, sighashTypes?: number[]) => this;
method signAllInputsAsync
signAllInputsAsync: ( keyPair: Signer | SignerAsync, sighashTypes?: number[]) => Promise<void>;
method signAllInputsHD
signAllInputsHD: (hdKeyPair: HDSigner, sighashTypes?: number[]) => this;
method signAllInputsHDAsync
signAllInputsHDAsync: ( hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]) => Promise<void>;
method signInput
signInput: ( inputIndex: number, keyPair: Signer, sighashTypes?: number[]) => this;
method signInputAsync
signInputAsync: ( inputIndex: number, keyPair: Signer | SignerAsync, sighashTypes?: number[]) => Promise<void>;
method signInputHD
signInputHD: ( inputIndex: number, hdKeyPair: HDSigner, sighashTypes?: number[]) => this;
method signInputHDAsync
signInputHDAsync: ( inputIndex: number, hdKeyPair: HDSigner | HDSignerAsync, sighashTypes?: number[]) => Promise<void>;
method signTaprootInput
signTaprootInput: ( inputIndex: number, keyPair: Signer, tapLeafHashToSign?: Buffer, sighashTypes?: number[]) => this;
method signTaprootInputAsync
signTaprootInputAsync: ( inputIndex: number, keyPair: Signer | SignerAsync, tapLeafHash?: Buffer, sighashTypes?: number[]) => Promise<void>;
method toBase64
toBase64: () => string;
method toBuffer
toBuffer: () => Buffer;
method toHex
toHex: () => string;
method updateGlobal
updateGlobal: (updateData: PsbtGlobalUpdate) => this;
method updateInput
updateInput: (inputIndex: number, updateData: PsbtInputUpdate) => this;
method updateOutput
updateOutput: (outputIndex: number, updateData: PsbtOutputUpdate) => this;
method validateSignaturesOfAllInputs
validateSignaturesOfAllInputs: (validator: ValidateSigFunction) => boolean;
method validateSignaturesOfInput
validateSignaturesOfInput: ( inputIndex: number, validator: ValidateSigFunction, pubkey?: Buffer) => boolean;
class Transaction
class Transaction {}
property ADVANCED_TRANSACTION_FLAG
static readonly ADVANCED_TRANSACTION_FLAG: number;
property ADVANCED_TRANSACTION_MARKER
static readonly ADVANCED_TRANSACTION_MARKER: number;
property DEFAULT_SEQUENCE
static readonly DEFAULT_SEQUENCE: number;
property ins
ins: Input[];
property locktime
locktime: number;
property outs
outs: Output[];
property SIGHASH_ALL
static readonly SIGHASH_ALL: number;
property SIGHASH_ANYONECANPAY
static readonly SIGHASH_ANYONECANPAY: number;
property SIGHASH_DEFAULT
static readonly SIGHASH_DEFAULT: number;
property SIGHASH_INPUT_MASK
static readonly SIGHASH_INPUT_MASK: number;
property SIGHASH_NONE
static readonly SIGHASH_NONE: number;
property SIGHASH_OUTPUT_MASK
static readonly SIGHASH_OUTPUT_MASK: number;
property SIGHASH_SINGLE
static readonly SIGHASH_SINGLE: number;
property version
version: number;
method addInput
addInput: ( hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer) => number;
method addOutput
addOutput: (scriptPubKey: Buffer, value: number) => number;
method byteLength
byteLength: (_ALLOW_WITNESS?: boolean) => number;
method clone
clone: () => Transaction;
method fromBuffer
static fromBuffer: (buffer: Buffer, _NO_STRICT?: boolean) => Transaction;
method fromHex
static fromHex: (hex: string) => Transaction;
method getHash
getHash: (forWitness?: boolean) => Buffer;
method getId
getId: () => string;
method hashForSignature
hashForSignature: ( inIndex: number, prevOutScript: Buffer, hashType: number) => Buffer;
Hash transaction for signing a specific input.
Bitcoin uses a different hash for each signed transaction input. This method copies the transaction, makes the necessary changes based on the hashType, and then hashes the result. This hash can then be used to sign the provided transaction input.
method hashForWitnessV0
hashForWitnessV0: ( inIndex: number, prevOutScript: Buffer, value: number, hashType: number) => Buffer;
method hashForWitnessV1
hashForWitnessV1: ( inIndex: number, prevOutScripts: Buffer[], values: number[], hashType: number, leafHash?: Buffer, annex?: Buffer) => Buffer;
method hasWitnesses
hasWitnesses: () => boolean;
method isCoinbase
isCoinbase: () => boolean;
method isCoinbaseHash
static isCoinbaseHash: (buffer: Buffer) => boolean;
method setInputScript
setInputScript: (index: number, scriptSig: Buffer) => void;
method setWitness
setWitness: (index: number, witness: Buffer[]) => void;
method toBuffer
toBuffer: (buffer?: Buffer, initialOffset?: number) => Buffer;
method toHex
toHex: () => string;
method virtualSize
virtualSize: () => number;
method weight
weight: () => number;
Interfaces
interface HDSigner
interface HDSigner extends HDSignerBase {}
method derivePath
derivePath: (path: string) => HDSigner;
The path string must match /^m(/\d+'?)+$/ ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
method sign
sign: (hash: Buffer) => Buffer;
Input hash (the "message digest") for the signature algorithm Return a 64 byte signature (32 byte r and 32 byte s in that order)
interface HDSignerAsync
interface HDSignerAsync extends HDSignerBase {}
Same as above but with async sign method
method derivePath
derivePath: (path: string) => HDSignerAsync;
method sign
sign: (hash: Buffer) => Promise<Buffer>;
interface Network
interface Network {}
property bech32
bech32: string;
property bip32
bip32: Bip32;
property messagePrefix
messagePrefix: string;
property pubKeyHash
pubKeyHash: number;
property scriptHash
scriptHash: number;
property wif
wif: number;
interface Payment
interface Payment {}
property address
address?: string;
property data
data?: Buffer[];
property hash
hash?: Buffer;
property input
input?: Buffer;
property internalPubkey
internalPubkey?: Buffer;
property m
m?: number;
property n
n?: number;
property name
name?: string;
property network
network?: Network;
property output
output?: Buffer;
property pubkey
pubkey?: Buffer;
property pubkeys
pubkeys?: Buffer[];
property redeem
redeem?: Payment;
property redeemVersion
redeemVersion?: number;
property scriptTree
scriptTree?: Taptree;
property signature
signature?: Buffer;
property signatures
signatures?: Buffer[];
property witness
witness?: Buffer[];
interface PaymentOpts
interface PaymentOpts {}
property allowIncomplete
allowIncomplete?: boolean;
property validate
validate?: boolean;
interface PsbtTxInput
interface PsbtTxInput extends TransactionInput {}
property hash
hash: Buffer;
interface PsbtTxOutput
interface PsbtTxOutput extends TransactionOutput {}
property address
address: string | undefined;
interface Signer
interface Signer {}
property network
network?: any;
property publicKey
publicKey: Buffer;
method getPublicKey
getPublicKey: () => Buffer;
method sign
sign: (hash: Buffer, lowR?: boolean) => Buffer;
method signSchnorr
signSchnorr: (hash: Buffer) => Buffer;
interface SignerAsync
interface SignerAsync {}
property network
network?: any;
property publicKey
publicKey: Buffer;
method getPublicKey
getPublicKey: () => Buffer;
method sign
sign: (hash: Buffer, lowR?: boolean) => Promise<Buffer>;
method signSchnorr
signSchnorr: (hash: Buffer) => Promise<Buffer>;
interface TxInput
interface Input {}
Type Aliases
type PaymentCreator
type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
type Stack
type Stack = StackElement[];
type StackElement
type StackElement = Buffer | number;
type TaggedHashPrefix
type TaggedHashPrefix = (typeof TAGS)[number];
Namespaces
namespace address
module 'src/address.d.ts' {}
function fromBase58Check
fromBase58Check: (address: string) => Base58CheckResult;
function fromBech32
fromBech32: (address: string) => Bech32Result;
function fromOutputScript
fromOutputScript: (output: Buffer, network?: Network) => string;
function toBase58Check
toBase58Check: (hash: Buffer, version: number) => string;
function toBech32
toBech32: (data: Buffer, version: number, prefix: string) => string;
function toOutputScript
toOutputScript: (address: string, network?: Network) => Buffer;
interface Base58CheckResult
interface Base58CheckResult {}
interface Bech32Result
interface Bech32Result {}
namespace crypto
module 'src/crypto.d.ts' {}
An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)]
variable TAGGED_HASH_PREFIXES
const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)]
variable TAGS
const TAGS: readonly [ 'BIP0340/challenge', 'BIP0340/aux', 'BIP0340/nonce', 'TapLeaf', 'TapBranch', 'TapSighash', 'TapTweak', 'KeyAgg list', 'KeyAgg coefficient'];
function hash160
hash160: (buffer: Buffer) => Buffer;
function hash256
hash256: (buffer: Buffer) => Buffer;
function ripemd160
ripemd160: (buffer: Buffer) => Buffer;
function sha1
sha1: (buffer: Buffer) => Buffer;
function sha256
sha256: (buffer: Buffer) => Buffer;
function taggedHash
taggedHash: (prefix: TaggedHashPrefix, data: Buffer) => Buffer;
type TaggedHashPrefix
type TaggedHashPrefix = (typeof TAGS)[number];
namespace networks
module 'src/networks.d.ts' {}
variable bitcoin
const bitcoin: Network;
variable regtest
const regtest: Network;
variable testnet
const testnet: Network;
interface Network
interface Network {}
property bech32
bech32: string;
property bip32
bip32: Bip32;
property messagePrefix
messagePrefix: string;
property pubKeyHash
pubKeyHash: number;
property scriptHash
scriptHash: number;
property wif
wif: number;
namespace payments
module 'src/payments/index.d.ts' {}
function embed
embed: (a: Payment, opts?: PaymentOpts) => Payment;
function p2ms
p2ms: (a: Payment, opts?: PaymentOpts) => Payment;
function p2pk
p2pk: (a: Payment, opts?: PaymentOpts) => Payment;
function p2pkh
p2pkh: (a: Payment, opts?: PaymentOpts) => Payment;
function p2sh
p2sh: (a: Payment, opts?: PaymentOpts) => Payment;
function p2tr
p2tr: (a: Payment, opts?: PaymentOpts) => Payment;
function p2wpkh
p2wpkh: (a: Payment, opts?: PaymentOpts) => Payment;
function p2wsh
p2wsh: (a: Payment, opts?: PaymentOpts) => Payment;
interface Payment
interface Payment {}
property address
address?: string;
property data
data?: Buffer[];
property hash
hash?: Buffer;
property input
input?: Buffer;
property internalPubkey
internalPubkey?: Buffer;
property m
m?: number;
property n
n?: number;
property name
name?: string;
property network
network?: Network;
property output
output?: Buffer;
property pubkey
pubkey?: Buffer;
property pubkeys
pubkeys?: Buffer[];
property redeem
redeem?: Payment;
property redeemVersion
redeemVersion?: number;
property scriptTree
scriptTree?: Taptree;
property signature
signature?: Buffer;
property signatures
signatures?: Buffer[];
property witness
witness?: Buffer[];
interface PaymentOpts
interface PaymentOpts {}
property allowIncomplete
allowIncomplete?: boolean;
property validate
validate?: boolean;
type PaymentCreator
type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
type PaymentFunction
type PaymentFunction = () => Payment;
type Stack
type Stack = StackElement[];
type StackElement
type StackElement = Buffer | number;
type StackFunction
type StackFunction = () => Stack;
namespace script
module 'src/script.d.ts' {}
variable number
const number: typeof scriptNumber;
variable OPS
const OPS: { [key: string]: number };
variable signature
const signature: typeof scriptSignature;
function compile
compile: (chunks: Buffer | Stack) => Buffer;
function countNonPushOnlyOPs
countNonPushOnlyOPs: (value: Stack) => number;
function decompile
decompile: ( buffer: Buffer | Array<number | Buffer>) => Array<number | Buffer> | null;
function fromASM
fromASM: (asm: string) => Buffer;
function isCanonicalPubKey
isCanonicalPubKey: (buffer: Buffer) => boolean;
function isCanonicalScriptSignature
isCanonicalScriptSignature: (buffer: Buffer) => boolean;
function isDefinedHashType
isDefinedHashType: (hashType: number) => boolean;
function isPushOnly
isPushOnly: (value: Stack) => boolean;
function toASM
toASM: (chunks: Buffer | Array<number | Buffer>) => string;
function toStack
toStack: (chunks: Buffer | Array<number | Buffer>) => Buffer[];
Package Files (19)
- src/address.d.ts
- src/block.d.ts
- src/crypto.d.ts
- src/ecc_lib.d.ts
- src/index.d.ts
- src/networks.d.ts
- src/ops.d.ts
- src/payments/embed.d.ts
- src/payments/index.d.ts
- src/payments/p2ms.d.ts
- src/payments/p2pk.d.ts
- src/payments/p2pkh.d.ts
- src/payments/p2sh.d.ts
- src/payments/p2tr.d.ts
- src/payments/p2wpkh.d.ts
- src/payments/p2wsh.d.ts
- src/psbt.d.ts
- src/script.d.ts
- src/transaction.d.ts
Dependencies (6)
Dev Dependencies (31)
- @types/bs58
- @types/bs58check
- @types/mocha
- @types/node
- @types/proxyquire
- @types/randombytes
- @typescript-eslint/eslint-plugin
- @typescript-eslint/parser
- better-npm-audit
- bip32
- bip39
- bip65
- bip68
- bs58
- dhttp
- ecpair
- eslint
- eslint-config-prettier
- eslint-plugin-prettier
- hoodwink
- minimaldata
- mocha
- nyc
- prettier
- proxyquire
- randombytes
- regtest-client
- rimraf
- tiny-secp256k1
- ts-node
- typescript
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/bitcoinjs-lib
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/bitcoinjs-lib)
- HTML<a href="https://www.jsdocs.io/package/bitcoinjs-lib"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3233 ms. - Missing or incorrect documentation? Open an issue for this package.