Class: MTX

primitives.MTX

new MTX(options)

A mutable transaction object.

Extends:
  • TX
Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Description
version Number <nullable>
changeIndex Number <nullable>
inputs Array:.<Input:> <nullable>
outputs Array:.<Output:> <nullable>
Properties:
Name Type Description
version Number

Transaction version.

flag Number

Flag field for segregated witness. Always non-zero (1 if not present).

inputs Array:.<Input:>
outputs Array:.<Output:>
locktime Number

nLockTime

view CoinView
Source:

Extends

  • TX

Members

estimateSize

Estimate maximum possible size.

Source:

fund

Select coins and fill the inputs.

Source:

Methods

(static) fromJSON(json) → {MTX}

Instantiate a transaction from a jsonified transaction object.

Parameters:
Name Type Description
json Object

The jsonified transaction object.

Source:
Returns:
Type
MTX

(static) fromOptions(options) → {MTX}

Instantiate MTX from options.

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

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

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
MTX

(static) fromReader(br) → {MTX}

Instantiate a transaction from a buffer reader.

Parameters:
Name Type Description
br BufferReader
Source:
Returns:
Type
MTX

(static) fromTX(tx) → {MTX}

Instantiate MTX from TX.

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

(static) isMTX(obj) → {Boolean}

Test whether an object is an MTX.

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

addCoin(coin) → {Input}

Add a coin as an input. Note that this will add the coin to the internal coin viewpoint.

Parameters:
Name Type Description
coin Coin
Source:
Returns:
Type
Input
Example
mtx.addCoin(Coin.fromTX(tx, 0, -1));

addInput(options) → {Input}

Add an input to the transaction.

Parameters:
Name Type Description
options Input | Object
Source:
Returns:
Type
Input
Example
mtx.addInput({ prevout: { hash: ... }, script: ... });
mtx.addInput(new Input());

addOutpoint(outpoint) → {Input}

Add an outpoint as an input.

Parameters:
Name Type Description
outpoint Outpoint | Object
Source:
Returns:
Type
Input
Example
mtx.addOutpoint({ hash: ..., index: 0 });
mtx.addOutpoint(new Outpoint(hash, index));

addOutput(script, valuenullable) → {Output}

Add an output.

Parameters:
Name Type Attributes Description
script Address | Script | Output | Object

Script or output options.

value Amount <nullable>
Source:
Returns:
Type
Output
Example
mtx.addOutput(new Output());
mtx.addOutput({ address: ..., value: 100000 });
mtx.addOutput(address, 100000);
mtx.addOutput(script, 100000);

addTX(tx, index, heightnullable) → {Input}

Add a transaction as an input. Note that this will add the coin to the internal coin viewpoint.

Parameters:
Name Type Attributes Description
tx TX
index Number
height Number <nullable>
Source:
Returns:
Type
Input
Example
mtx.addTX(tx, 0);

avoidFeeSniping(height)

Avoid fee sniping.

Parameters:
Name Type Description
height Number

Current chain height.

Source:
See:
  • bitcoin/src/wallet/wallet.cpp

checkInputs(spendHeight, retnullable) → {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 Attributes Description
spendHeight 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.

ret Object <nullable>

Return object, may be set with properties reason and score.

Source:
Returns:
Type
Boolean

clone() → {MTX}

Clone the transaction. Note that this will not carry over the view.

Source:
Returns:
Type
MTX

format() → {Object}

Inspect the transaction.

Source:
Returns:
Type
Object

(private) fromOptions(options)

Inject properties from options object.

Parameters:
Name Type Description
options Object
Source:

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

Get all addresses.

Source:
Returns:

addresses

Type
Array:.<Address:>

getFee() → {Amount}

Calculate the fee for the transaction.

Source:
Returns:

fee (zero if not all coins are available).

Type
Amount

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

Get all address hashes.

Source:
Returns:

hashes

Type
Array:.<Hash:>

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

Get all input addresses.

Source:
Returns:

addresses

Type
Array:.<Address:>

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

Get all input address hashes.

Source:
Returns:

hashes

Type
Array:.<Hash:>

getInputValue() → {Amount}

Calculate the total input value.

Source:
Returns:

value

Type
Amount

getJSON(network) → {Object}

Convert transaction to JSON.

Parameters:
Name Type Description
network Network
Source:
Returns:
Type
Object

getSigops(flagsnullable) → {Number}

Calculate virtual sigop count.

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

sigop count

Type
Number

getSigopsCost(flagsnullable) → {Number}

Calculate sigops weight, taking into account witness programs.

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

sigop weight

Type
Number

getSigopsSize() → {Number}

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

Source:
Returns:

vsize

Type
Number

hasCoins() → {Boolean}

Test whether the transaction has all coins available/filled.

Source:
Returns:
Type
Boolean

inspect() → {Object}

Inspect the transaction.

Source:
Returns:
Type
Object

isInputSigned(index, coin) → {Boolean}

Test whether an input is fully-signed.

Parameters:
Name Type Description
index Number
coin Coin | Output
Source:
Returns:
Type
Boolean

isSigned() → {Boolean}

Test whether the transaction is fully-signed.

Source:
Returns:
Type
Boolean

isVectorSigned(prev, vector) → {Boolean}

Test whether a vector is fully-signed.

Parameters:
Name Type Description
prev Script
vector Script | Witness
Source:
Returns:
Type
Boolean

scriptInput(index, coin, ring) → {Boolean}

Build input script (or witness) templates (with OP_0 in place of signatures).

Parameters:
Name Type Description
index Number

Input index.

coin Coin | Output
ring KeyRing
Source:
Returns:

Whether the script was able to be built.

Type
Boolean

scriptVector(prev, vector, ring) → {Boolean}

Build script for a single vector based on a previous script.

Parameters:
Name Type Description
prev Script
vector Witness | Script
ring Buffer
Source:
Returns:
Type
Boolean

selectCoins(coins, optionsnullable) → {CoinSelection}

Select necessary coins based on total output value.

Parameters:
Name Type Attributes Description
coins Array:.<Coin:>
options Object <nullable>
Source:
Throws:

on not enough funds available.

Returns:
Type
CoinSelection

setLocktime(locktime)

Set locktime and sequences appropriately.

Parameters:
Name Type Description
locktime Number
Source:

setSequence(index, locktime, secondsnullable)

Set sequence locktime.

Parameters:
Name Type Attributes Description
index Number

Input index.

locktime Number
seconds Boolean <nullable>
Source:

sign(ring, type) → {Number}

Built input scripts (or witnesses) and sign the inputs.

Parameters:
Name Type Description
ring KeyRing

Address used to sign. The address must be able to redeem the coin.

type SighashType
Source:
Returns:

Number of inputs signed.

Type
Number

signAsync(ring, typenullable) → {Promise}

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

Parameters:
Name Type Attributes Description
ring KeyRing
type SighashType <nullable>
Source:
Returns:
Type
Promise

signature(index, prev, value, 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.

type SighashType
version Number

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

Source:
Returns:

Signature in DER format.

Type
Buffer

signInput(index, coin, ring, type) → {Boolean}

Sign an input.

Parameters:
Name Type Description
index Number

Index of input being signed.

coin Coin | Output
ring KeyRing

Private key.

type SighashType
Source:
Returns:

Whether the input was able to be signed.

Type
Boolean

signInputAsync(index, coin, ring, typenullable) → {Promise}

Sign a transaction input on the worker pool (if workers are enabled).

Parameters:
Name Type Attributes Description
index Number
coin Coin | Output
ring KeyRing
type SighashType <nullable>
Source:
Returns:
Type
Promise

signVector(prev, vector, sig, ring) → {Boolean}

Add a signature to a vector based on a previous script.

Parameters:
Name Type Description
prev Script
vector Witness | Script
sig Buffer
ring KeyRing
Source:
Returns:
Type
Boolean

sortMembers()

Sort inputs and outputs according to BIP69.

Source:
See:

subtractFee(fee, indexnullable)

Attempt to subtract a fee from outputs.

Parameters:
Name Type Attributes Description
fee Amount
index Number <nullable>
Source:

template(ring) → {Number}

Build input scripts (or witnesses).

Parameters:
Name Type Description
ring KeyRing

Address used to sign. The address must be able to redeem the coin.

Source:
Returns:

Number of inputs templated.

Type
Number

toJSON() → {Object}

Convert transaction to JSON.

Source:
Returns:
Type
Object

toTX() → {TX}

Convert the MTX to a TX.

Source:
Returns:
Type
TX

verify(flagsopt) → {Boolean}

Verify all transaction inputs.

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

Whether the inputs are valid.

Type
Boolean

verifyAsync(flagsopt, nullable) → {Promise}

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

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