Class: TX

primitives.TX(optionsopt, nullable)

TX A static transaction object.

Constructor

new TX(optionsopt, nullable)

Create a transaction.

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

Members

(nullable) _hash :Hash

Type:
Source:

(nullable) _hashOutputs :Hash

Type:
Source:

(nullable) _hashPrevouts :Hash

Type:
Source:

(nullable) _hashSequence :Hash

Type:
Source:

(nullable) _raw :Buffer

Type:
  • Buffer
Source:

(nullable) _sizes :Sizes

Type:
  • Sizes
Source:

(nullable) _wdhash :Hash

Type:
Source:

(nullable) _whash :Hash

Type:
Source:

inputs :Array.<Input>

Type:
  • Array.<Input>
Source:

outputs :Array.<Output>

Type:
  • Array.<Output>
Source:

Methods

(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, poolopt, nullable) → {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 <optional>
<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, poolopt, nullable) → {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 <optional>
<nullable>
Source:
Returns:
Type
Promise

checkInputs(view, height, network) → {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.

network Network
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) → {Boolean}

Verify signature.

Parameters:
Name Type Description
index Number
prev Script
value AmountValue
sig Buffer
key Buffer
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

covenant(index) → {Covenant|null}

Get covenant from index.

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Covenant | null

encode() → {Buffer}

Serialize transaction.

Source:
Returns:
Type
Buffer

format(viewopt, nullable, entryopt, nullable, indexopt, nullable) → {Object}

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

Parameters:
Name Type Attributes Description
view CoinView <optional>
<nullable>
entry ChainEntry <optional>
<nullable>
index Number <optional>
<nullable>
Source:
Returns:
Type
Object

fromJSON(json)

Inject properties from a json object.

Parameters:
Name Type Description
json Object
Source:

fromOptions(options)

Inject properties from options object.

Parameters:
Name Type Description
options Object
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) → {AmountValue}

Calculate the fee for the transaction.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

fee (zero if not all coins are available).

Type
AmountValue

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) → {AmountValue}

Calculate the total input value.

Parameters:
Name Type Description
view CoinView
Source:
Returns:

value

Type
AmountValue

getJSON(networkopt, viewopt, entryopt, indexopt) → {Object}

Convert the transaction to an object suitable for JSON serialization.

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

getMinFee(sizeopt, nullable, rateopt, nullable) → {AmountValue}

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

Parameters:
Name Type Attributes Description
size Number <optional>
<nullable>

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

rate Rate <optional>
<nullable>

Rate of dollarydoo per kB.

Source:
Returns:

fee

Type
AmountValue

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

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() → {AmountValue}

Calculate the total output value.

Source:
Returns:

value

Type
AmountValue

getPrevout() → {Array.<Hash>}

Get all unique outpoint hashes.

Source:
Returns:

Outpoint hashes.

Type
Array.<Hash>

getPriority(view, height, sizeopt, nullable) → {Number}

Calculate the transaction priority.

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

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

Source:
Returns:
Type
Number

getRate(view, sizeopt, nullable) → {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 <optional>
<nullable>
Source:
Returns:
Type
Rate

getRoundFee(sizeopt, nullable, rateopt, nullable) → {AmountValue}

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 <optional>
<nullable>

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

rate Rate <optional>
<nullable>

Rate of dollarydoo per kB.

Source:
Returns:

fee

Type
AmountValue

getSigops(view) → {Number}

Calculate sigops.

Parameters:
Name Type Description
view CoinView
Source:
Returns:
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() → {Sizes}

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

Source:
Returns:
Type
Sizes

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

hasCoins(view) → {Boolean}

Test whether the transaction has all coins available.

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

hash() → {Hash}

Hash the transaction with the non-witness serialization.

Source:
Returns:
Type
Hash

(private) hashes() → {Array.<Buffer>}

Calculate all three transaction hashes.

Source:
Returns:
Type
Array.<Buffer>

hasSaneCovenants() → {Boolean}

Test whether the transaction violates any basic covenants rules.

Source:
Returns:
Type
Boolean

hasStandardInputs(view) → {Boolean}

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

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

hasWitness() → {Boolean}

Test whether the transaction has a non-empty witness.

Source:
Returns:
Type
Boolean

inject(tx) → {this}

Inject properties from tx. Used for cloning.

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

input(index) → {Input|null}

Get input from index.

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Input | null

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, height, sizeopt, nullable) → {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

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

size Number <optional>
<nullable>

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

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

left() → {Buffer}

Calculate left hash.

Source:
Returns:
Type
Buffer

outpoint(index) → {Outpoint}

Create outpoint from output index.

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Outpoint

output(index) → {Output|null}

Get output from index.

Parameters:
Name Type Description
index Number
Source:
Returns:
Type
Output | null

read(br)

Inject properties from serialized buffer reader (witness serialization).

Parameters:
Name Type Description
br bio.BufferReader
Source:

refresh()

Clear any cached values.

Source:

Calculate right hash.

Source:
Returns:
Type
Buffer

root() → {Buffer}

Calculate root hash.

Source:
Returns:
Type
Buffer

signature(index, prev, value, key, type) → {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 AmountValue

Previous output value.

key Buffer
type SighashType
Source:
Returns:

Signature in DER format.

Type
Buffer

signatureHash(index, prev, value, type) → {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 AmountValue

Previous output value.

type SighashType

Sighash type.

Source:
Returns:

Signature hash.

Type
Buffer

test(filter) → {Boolean}

Test a transaction against a bloom filter.

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

testAndMaybeUpdate(filter) → {Boolean}

Test a transaction against a bloom filter.

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

toInv() → {InvItem}

Convert the tx to an inv item.

Source:
Returns:
Type
InvItem

txid() → {HexHash}

Get little-endian tx hash.

Source:
Returns:
Type
HexHash

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, poolopt, nullable) → {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 <optional>
<nullable>
Source:
Returns:
Type
Promise

verifyCovenants(view, height, network) → {Number}

Test whether the transaction violates any contextual covenants rules.

Parameters:
Name Type Description
view CoinView
height Number
network Network
Source:
Returns:
Type
Number

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, poolopt, nullable) → {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 <optional>
<nullable>
Source:
Returns:
Type
Promise

verifyInputs(view, height, network) → {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.

network Network
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() → {Hash}

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

Source:
Returns:

hash

Type
Hash

write(bw) → {BufioWriter}

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

Parameters:
Name Type Description
bw BufioWriter
Source:
Returns:
Type
BufioWriter

wtxid() → {HexHash}

Get little-endian wtx hash.

Source:
Returns:
Type
HexHash