Class: TX

primitives~TX(optionsnullable)

TX A static transaction object.

Constructor

new TX(optionsnullable)

Create a transaction.

Parameters:
Name Type Attributes Description
options Object <nullable>
Properties:
Name Type Description
version Number
inputs Array.<Input>
outputs Array.<Output>
locktime Number
Source:

Methods

(static) fromJSON(json) → {TX}

Instantiate a transaction from a jsonified transaction object.

Parameters:
Name Type Description
json Object

The jsonified transaction object.

Source:
Returns:
Type
TX

(static) fromOptions(options) → {TX}

Instantiate TX from options object.

Parameters:
Name Type Description
options Object
Source:
Returns:
Type
TX

(static) fromRaw(data, encnullable) → {TX}

Instantiate a transaction from a serialized Buffer.

Parameters:
Name Type Attributes Description
data Buffer
enc String <nullable>

Encoding, can be 'hex' or null.

Source:
Returns:
Type
TX

(static) fromReader(br, block) → {TX}

Instantiate a transaction from a buffer reader.

Parameters:
Name Type Description
br BufferReader
block Boolean
Source:
Returns:
Type
TX

(static) isTX(obj) → {Boolean}

Test whether an object is a TX.

Parameters:
Name Type Description
obj Object
Source:
Returns:
Type
Boolean

(private) _getAddresses(view) → {Array}

Get all addresses.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

[addrs, table]

Type
Array

(private) _getInputAddresses(view) → {Array}

Get all input addresses.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

[addrs, table]

Type
Array

(private) _getOutputAddresses() → {Array}

Get all output addresses.

Source:
Returns:

[addrs, table]

Type
Array

check(view, flagsopt, nullable)

Verify all transaction inputs.

Parameters:
Name Type Attributes Default Description
view CoinView
flags VerifyFlags <optional>
<nullable>
STANDARD_VERIFY_FLAGS
Source:
Throws:

on invalid inputs

Type
ScriptError

(async) checkAsync(view, flagsopt, nullable, poolnullable) → {Promise}

Verify the transaction inputs on the worker pool (if workers are enabled).

Parameters:
Name Type Attributes Default Description
view CoinView
flags VerifyFlags <optional>
<nullable>
STANDARD_VERIFY_FLAGS
pool WorkerPool <nullable>
Source:
Returns:
Type
Promise

checkInput(index, coin, flagsopt)

Verify a transaction input.

Parameters:
Name Type Attributes Default Description
index Number

Index of output being verified.

coin Coin | Output

Previous output.

flags VerifyFlags <optional>
STANDARD_VERIFY_FLAGS
Source:
Throws:

on invalid input

Type
ScriptError

(async) checkInputAsync(index, coin, flagsopt, poolnullable) → {Promise}

Verify a transaction input asynchronously.

Parameters:
Name Type Attributes Default Description
index Number

Index of output being verified.

coin Coin | Output

Previous output.

flags VerifyFlags <optional>
STANDARD_VERIFY_FLAGS
pool WorkerPool <nullable>
Source:
Returns:
Type
Promise

checkInputs(view, height) → {Array}

Perform contextual checks to verify input, output, and fee values, as well as coinbase spend maturity (coinbases can only be spent 100 blocks or more after they're created). Note that this function is consensus critical.

Parameters:
Name Type Description
view CoinView
height Number

Height at which the transaction is being spent. In the mempool this is the chain height plus one at the time it entered the pool.

Source:
Returns:

[fee, reason, score]

Type
Array

checkSanity() → {Array}

Non-contextual sanity checks for the transaction. Will mostly verify coin and output values.

Source:
See:
  • CheckTransaction()
Returns:

[valid, reason, score]

Type
Array

checksig(index, prev, value, sig, key, version) → {Boolean}

Verify signature.

Parameters:
Name Type Description
index Number
prev Script
value Amount
sig Buffer
key Buffer
version Number
Source:
Returns:
Type
Boolean

checkStandard() → {Array}

Non-contextual checks to determine whether the transaction has all standard output script types and standard input script size with only pushdatas in the code. Will mostly verify coin and output values.

Source:
See:
  • IsStandardTx()
Returns:

[valid, reason, score]

Type
Array

clone() → {TX}

Clone the transaction.

Source:
Returns:
Type
TX

format(view, entry, index) → {Object}

Inspect the transaction and return a more user-friendly representation of the data.

Parameters:
Name Type Description
view CoinView
entry ChainEntry
index Number
Source:
Returns:
Type
Object

(private) frame() → {RawTX}

Serialize the transaction. Note that this is cached. This will use the witness serialization if a witness is present.

Source:
Returns:
Type
RawTX

(private) frameNormal() → {RawTX}

Serialize transaction without witness.

Source:
Returns:
Type
RawTX

(private) frameWitness() → {RawTX}

Serialize transaction with witness. Calculates the witness size as it is framing (exposed on return value as witness).

Source:
Returns:
Type
RawTX

(private) fromJSON(json)

Inject properties from a json object.

Parameters:
Name Type Description
json Object
Source:

(private) fromOptions(options)

Inject properties from options object.

Parameters:
Name Type Description
options Object
Source:

(private) fromRaw(data)

Inject properties from serialized data.

Parameters:
Name Type Description
data Buffer
Source:

(private) fromReader(br, block)

Inject properties from buffer reader.

Parameters:
Name Type Description
br BufferReader
block Boolean
Source:

(private) fromWitnessReader(br, block)

Inject properties from serialized buffer reader (witness serialization).

Parameters:
Name Type Description
br BufferReader
block Boolean
Source:

getAddresses(view) → {Array.<Address>}

Get all addresses.

Parameters:
Name Type Description
view CoinView | null
Source:
Returns:

addresses

Type
Array.<Address>

getBaseSize() → {Number}

Calculate the size of the transaction without the witness. with the witness included.

Source:
Returns:

size

Type
Number

getChainValue(view) → {Number}

Calculate the transaction's on-chain value.

Parameters:
Name Type Description
view CoinView
Source:
Returns:
Type
Number

getFee(view) → {Amount}

Calculate the fee for the transaction.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

fee (zero if not all coins are available).

Type
Amount

getHashes(view) → {Array.<Hash>}

Get all address hashes.

Parameters:
Name Type Description
view CoinView | null
Source:
Returns:

hashes

Type
Array.<Hash>

getInputAddresses(view) → {Array.<Address>}

Get all input addresses.

Parameters:
Name Type Description
view CoinView | null
Source:
Returns:

addresses

Type
Array.<Address>

getInputHashes(view) → {Array.<Hash>}

Get all input address hashes.

Parameters:
Name Type Description
view CoinView | null
Source:
Returns:

hashes

Type
Array.<Hash>

getInputValue(view) → {Amount}

Calculate the total input value.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

value

Type
Amount

getJSON(network, view, entry, index) → {Object}

Convert the transaction to an object suitable for JSON serialization. Note that the hashes will be reversed to abide by bitcoind's legacy of little-endian uint256s.

Parameters:
Name Type Description
network Network
view CoinView
entry ChainEntry
index Number
Source:
Returns:
Type
Object

getLegacySigops() → {Number}

Calculate legacy (inaccurate) sigop count.

Source:
Returns:

sigop count

Type
Number

getMinFee(sizenullable, ratenullable) → {Amount}

Calculate minimum fee in order for the transaction to be relayable (not the constant min relay fee).

Parameters:
Name Type Attributes Description
size Number <nullable>

If not present, max size estimation will be calculated and used.

rate Rate <nullable>

Rate of satoshi per kB.

Source:
Returns:

fee

Type
Amount

getModifiedSize(sizenullable) → {Number}

Calculate the modified size of the transaction. This is used in the mempool for calculating priority.

Parameters:
Name Type Attributes Description
size Number <nullable>

The size to modify. If not present, virtual size will be used.

Source:
Returns:

Modified size.

Type
Number

getNormalSizes() → {RawTX}

Calculate the real size of the transaction without the witness vector.

Source:
Returns:
Type
RawTX

getOutputAddresses() → {Array.<Address>}

Get all output addresses.

Source:
Returns:

addresses

Type
Array.<Address>

getOutputHashes() → {Array.<Hash>}

Get all output address hashes.

Source:
Returns:

hashes

Type
Array.<Hash>

getOutputValue() → {Amount}

Calculate the total output value.

Source:
Returns:

value

Type
Amount

getPosition() → {Object}

Return the offset and size of the transaction. Useful when the transaction is deserialized within a block.

Source:
Returns:

Contains size and offset.

Type
Object

getPrevout() → {Array.<Hash>}

Get all unique outpoint hashes.

Source:
Returns:

Outpoint hashes.

Type
Array.<Hash>

getPriority(view, height, sizenullable) → {Number}

Calculate the transaction priority.

Parameters:
Name Type Attributes Description
view CoinView
height Number
size Number <nullable>

Size to calculate priority based on. If not present, virtual size will be used.

Source:
Returns:
Type
Number

getRate(view, sizenullable) → {Rate}

Calculate the transaction's rate based on size and fees. Size will be calculated if not present.

Parameters:
Name Type Attributes Description
view CoinView
size Number <nullable>
Source:
Returns:
Type
Rate

getRoundFee(sizenullable, ratenullable) → {Amount}

Calculate the minimum fee in order for the transaction to be relayable, but _round to the nearest kilobyte when taking into account size.

Parameters:
Name Type Attributes Description
size Number <nullable>

If not present, max size estimation will be calculated and used.

rate Rate <nullable>

Rate of satoshi per kB.

Source:
Returns:

fee

Type
Amount

getScripthashSigops(view) → {Number}

Calculate accurate sigop count, taking into account redeem scripts.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

sigop count

Type
Number

getSigops(view, flagsnullable) → {Number}

Calculate virtual sigop count.

Parameters:
Name Type Attributes Description
view CoinView
flags VerifyFlags <nullable>
Source:
Returns:

sigop count

Type
Number

getSigopsCost(view, flagsnullable) → {Number}

Calculate sigops cost, taking into account witness programs.

Parameters:
Name Type Attributes Description
view CoinView
flags VerifyFlags <nullable>
Source:
Returns:

sigop weight

Type
Number

getSigopsSize(sigops) → {Number}

Calculate the virtual size of the transaction (weighted against bytes per sigop cost).

Parameters:
Name Type Description
sigops Number

Sigops cost.

Source:
Returns:

vsize

Type
Number

getSize() → {Number}

Calculate the real size of the transaction with the witness included.

Source:
Returns:

size

Type
Number

getSizes() → {Object}

Calculate total size and size of the witness bytes.

Source:
Returns:

Contains size and witness.

Type
Object

getVirtualSize() → {Number}

Calculate the virtual size of the transaction. Note that this is cached.

Source:
Returns:

vsize

Type
Number

getWeight() → {Number}

Calculate the weight of the transaction. Note that this is cached.

Source:
Returns:

weight

Type
Number

getWitnessSigops(view) → {Number}

Calculate accurate sigop count, taking into account redeem scripts.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

sigop count

Type
Number

getWitnessSizes() → {RawTX}

Calculate the real size of the transaction with the witness included.

Source:
Returns:
Type
RawTX

hasCoins(view) → {Boolean}

Test whether the transaction has all coins available.

Parameters:
Name Type Description
view CoinView
Source:
Returns:
Type
Boolean

hash(encnullable) → {Hash|Buffer}

Hash the transaction with the non-witness serialization.

Parameters:
Name Type Attributes Description
enc String <nullable>

Can be 'hex' or null.

Source:
Returns:

hash

Type
Hash | Buffer

(private) hashSize(index, prev, type) → {Number}

Calculate sighash size.

Parameters:
Name Type Description
index Number
prev Script
type Number
Source:
Returns:
Type
Number

hasStandardInputs(view, flagsnullable) → {Boolean}

Perform contextual checks to verify coin and input script standardness (including the redeem script).

Parameters:
Name Type Attributes Description
view CoinView
flags VerifyFlags <nullable>
Source:
See:
  • AreInputsStandard()
Returns:
Type
Boolean

hasStandardWitness(view) → {Boolean}

Perform contextual checks to verify coin and witness standardness.

Parameters:
Name Type Description
view CoinView
Source:
See:
  • IsBadWitness()
Returns:
Type
Boolean

hasWitness() → {Boolean}

Test whether the transaction has a non-empty witness.

Source:
Returns:
Type
Boolean

(private) inject(tx) → {TX}

Inject properties from tx. Used for cloning.

Parameters:
Name Type Description
tx TX
Source:
Returns:
Type
TX

inspectSymbol() → {Object}

Inspect the transaction and return a more user-friendly representation of the data.

Source:
Returns:
Type
Object

isCoinbase() → {Boolean}

Test whether the transaction is a coinbase by examining the inputs.

Source:
Returns:
Type
Boolean

isFinal(height, time) → {Boolean}

Check finality of transaction by examining nLocktime and nSequence values.

Parameters:
Name Type Description
height Number

Height at which to test. This is usually the chain height, or the chain height + 1 when the transaction entered the mempool.

time Number

Time at which to test. This is usually the chain tip's parent's median time, or the time at which the transaction entered the mempool. If MEDIAN_TIME_PAST is enabled this will be the median time of the chain tip's previous entry's median time.

Source:
Returns:
Type
Boolean
Example
tx.isFinal(chain.height + 1, network.now());

isFree(view, heightnullable, sizenullable) → {Boolean}

Determine whether the transaction is above the free threshold in priority. A transaction which passed this test is most likely relayable without a fee.

Parameters:
Name Type Attributes Description
view CoinView
height Number <nullable>

If not present, tx height or network height will be used.

size Number <nullable>

If not present, modified size will be calculated and used.

Source:
Returns:
Type
Boolean

isRBF() → {Boolean}

Test whether the transaction is replaceable.

Source:
Returns:
Type
Boolean

isSane() → {Array}

Non-contextual sanity checks for the transaction. Will mostly verify coin and output values.

Source:
See:
  • CheckTransaction()
Returns:

[result, reason, score]

Type
Array

isStandard() → {Array}

Non-contextual checks to determine whether the transaction has all standard output script types and standard input script size with only pushdatas in the code. Will mostly verify coin and output values.

Source:
See:
  • IsStandardTx()
Returns:

[valid, reason, score]

Type
Array

isWatched(filter) → {Boolean}

Test a transaction against a bloom filter using the BIP37 matching algorithm. Note that this may update the filter depending on what the update value is.

Parameters:
Name Type Description
filter BloomFilter
Source:
See:
Returns:

True if the transaction matched.

Type
Boolean

refresh()

Clear any cached values.

Source:

rhash() → {Hash}

Get little-endian tx hash.

Source:
Returns:
Type
Hash

rwhash() → {Hash}

Get little-endian wtx hash.

Source:
Returns:
Type
Hash

signature(index, prev, value, key, type, version) → {Buffer}

Create a signature suitable for inserting into scriptSigs/witnesses.

Parameters:
Name Type Description
index Number

Index of input being signed.

prev Script

Previous output script or redeem script (in the case of witnesspubkeyhash, this should be the generated p2pkh script).

value Amount

Previous output value.

key Buffer
type SighashType
version Number

Sighash version (0=legacy, 1=segwit).

Source:
Returns:

Signature in DER format.

Type
Buffer

signatureHash(index, prev, value, type, version) → {Buffer}

Get the signature hash of the transaction for signing verifying.

Parameters:
Name Type Description
index Number

Index of input being signed/verified.

prev Script

Previous output script or redeem script (in the case of witnesspubkeyhash, this should be the generated p2pkh script).

value Amount

Previous output value.

type SighashType

Sighash type.

version Number

Sighash version (0=legacy, 1=segwit).

Source:
Returns:

Signature hash.

Type
Buffer

(private) signatureHashV0(index, prev, type) → {Buffer}

Legacy sighashing -- O(n^2).

Parameters:
Name Type Description
index Number
prev Script
type SighashType
Source:
Returns:
Type
Buffer

(private) signatureHashV1(index, prev, value, type) → {Buffer}

Witness sighashing -- O(n).

Parameters:
Name Type Description
index Number
prev Script
value Amount
type SighashType
Source:
Returns:
Type
Buffer

toInv() → {InvItem}

Convert the tx to an inv item.

Source:
Returns:
Type
InvItem

toJSON() → {Object}

Convert the transaction to an object suitable for JSON serialization.

Source:
Returns:
Type
Object

toNormal() → {Buffer}

Serialize the transaction without the witness vector, regardless of whether it is a witness transaction or not.

Source:
Returns:

Serialized transaction.

Type
Buffer

toNormalWriter(bw)

Write the transaction to a buffer writer. Uses non-witness serialization.

Parameters:
Name Type Description
bw BufferWriter
Source:

toRaw() → {Buffer}

Serialize the transaction. Note that this is cached. This will use the witness serialization if a witness is present.

Source:
Returns:

Serialized transaction.

Type
Buffer

toWriter(bw, block)

Write the transaction to a buffer writer.

Parameters:
Name Type Description
bw BufferWriter
block Boolean
Source:

txid() → {Hash}

Get little-endian tx hash.

Source:
Returns:
Type
Hash

verify(view, flagsopt, nullable) → {Boolean}

Verify all transaction inputs.

Parameters:
Name Type Attributes Default Description
view CoinView
flags VerifyFlags <optional>
<nullable>
STANDARD_VERIFY_FLAGS
Source:
Returns:

Whether the inputs are valid.

Type
Boolean

(async) verifyAsync(view, flagsopt, nullable, poolnullable) → {Promise}

Verify the transaction inputs on the worker pool (if workers are enabled).

Parameters:
Name Type Attributes Default Description
view CoinView
flags VerifyFlags <optional>
<nullable>
STANDARD_VERIFY_FLAGS
pool WorkerPool <nullable>
Source:
Returns:
Type
Promise

verifyInput(index, coin, flagsopt) → {Boolean}

Verify a transaction input.

Parameters:
Name Type Attributes Default Description
index Number

Index of output being verified.

coin Coin | Output

Previous output.

flags VerifyFlags <optional>
STANDARD_VERIFY_FLAGS
Source:
Returns:

Whether the input is valid.

Type
Boolean

(async) verifyInputAsync(index, coin, flagsopt, poolnullable) → {Promise}

Verify a transaction input asynchronously.

Parameters:
Name Type Attributes Default Description
index Number

Index of output being verified.

coin Coin | Output

Previous output.

flags VerifyFlags <optional>
STANDARD_VERIFY_FLAGS
pool WorkerPool <nullable>
Source:
Returns:
Type
Promise

verifyInputs(view, height) → {Boolean}

Perform contextual checks to verify input, output, and fee values, as well as coinbase spend maturity (coinbases can only be spent 100 blocks or more after they're created). Note that this function is consensus critical.

Parameters:
Name Type Description
view CoinView
height Number

Height at which the transaction is being spent. In the mempool this is the chain height plus one at the time it entered the pool.

Source:
Returns:
Type
Boolean

verifyLocktime(index, predicate) → {Boolean}

Verify the absolute locktime of a transaction. Called by OP_CHECKLOCKTIMEVERIFY.

Parameters:
Name Type Description
index Number

Index of input being verified.

predicate Number

Locktime to verify against.

Source:
Returns:
Type
Boolean

verifySequence(index, predicate) → {Boolean}

Verify the relative locktime of an input. Called by OP_CHECKSEQUENCEVERIFY.

Parameters:
Name Type Description
index Number

Index of input being verified.

predicate Number

Relative locktime to verify against.

Source:
Returns:
Type
Boolean

witnessHash(encnullable) → {Hash|Buffer}

Hash the transaction with the witness serialization, return the wtxid (normal hash if no witness is present, all zeroes if coinbase).

Parameters:
Name Type Attributes Description
enc String <nullable>

Can be 'hex' or null.

Source:
Returns:

hash

Type
Hash | Buffer

(private) writeNormal(bw) → {RawTX}

Serialize transaction without witness.

Parameters:
Name Type Description
bw BufferWriter
Source:
Returns:
Type
RawTX

(private) writeWitness(bw) → {RawTX}

Serialize transaction with witness. Calculates the witness size as it is framing (exposed on return value as witness).

Parameters:
Name Type Description
bw BufferWriter
Source:
Returns:
Type
RawTX

wtxid() → {Hash}

Get little-endian wtx hash.

Source:
Returns:
Type
Hash