Skip to content

Transactions API ​

The transaction module wraps Bitcoin transactions, txids, inputs, outpoints, outputs, UTXO coins, and spent-output undo collections.

Example ​

ts
import { Transaction } from "js-bitcoinkernel";

const tx = new Transaction(Buffer.from(rawTxHex, "hex"));

console.log(tx.txid.toString());
console.log(tx.inputs.length);
console.log(tx.outputs.get(0).amount);

Txid ​

Purpose: wrapper for a transaction identifier.

APIInputsReturnsErrors and caveats
toBytes()None.32-byte internal-order bytes.Throws if binding unavailable.
toString()None.64-character display-endian txid hex.Reverses native bytes.
equals(other)Any value.boolean.False for non-Txid; native compare for txids.
copy()None.Owned copy.Throws if disposed.
detach()None.Same instance, now owned.Useful when keeping a borrowed txid after parent disposal.

Usage:

ts
const txid = tx.txid.detach();
tx.dispose();
console.log(txid.toString());
txid.dispose();

Wtxid and WitnessStack ​

Wtxid has the same byte conversion, comparison, copy(), and detach() behaviour as Txid, but commits to witness data. For a non-witness transaction, its bytes equal its txid bytes; it remains a distinct wrapper type.

WitnessStack is a borrowed, read-only sequence of raw Uint8Array witness items. It supports length, get(index), negative indexes, slice(), iteration, copy(), and detach(). The stack is tied to its TransactionInput until detached or copied.

ts
using input = tx.inputs.get(0);
using witness = input.witnessStack.detach();
for (const item of witness) console.log(item.length);

TxValidationState ​

Transaction.check() runs Bitcoin Core's context-free CheckTransaction rules and returns an owned state. It can report only UNSET for valid transactions or CONSENSUS for invalid ones today; the remaining TxValidationResult values are exported for future kernel entry points.

ts
using state = tx.check();
if (state.validationMode === ValidationMode.INVALID) {
  console.log(state.txValidationResult);
}

TransactionOutPoint ​

Purpose: identifies the previous output spent by an input.

APIInputsReturnsErrors and caveats
indexNone.Output index as number.Also called vout.
txidNone.Borrowed Txid view.Keep outpoint alive.
toString()None.txid=<hex> index=<n>.Diagnostic format.
copy()None.Owned copy.Throws if disposed.

Usage:

ts
const input = tx.inputs.get(0);
console.log(input.outPoint.txid.toString(), input.outPoint.index);

TransactionInput ​

Purpose: wrapper for a transaction input.

APIInputsReturnsErrors and caveats
outPointNone.Borrowed TransactionOutPoint.Tied to input lifetime.
sequenceNone.32-bit sequence number.Used for RBF and relative locktime semantics.
scriptSigNone.Raw Uint8Array.Serialized script bytes, without a CompactSize prefix.
witnessStackNone.Borrowed WitnessStack.Tied to the input lifetime; detach or copy before retaining it.
toString()None.Outpoint summary.Diagnostic only.
copy()None.Owned copy.Throws if disposed.

Usage:

ts
for (const input of tx.inputs) {
  console.log(input.sequence);
}

TransactionOutput ​

Purpose: wrapper for a transaction output.

APIInputsReturnsErrors and caveats
new TransactionOutput(scriptPubkey, amount)ScriptPubkey, satoshi amount.Owned output.Throws if native allocation fails.
amountNone.bigint.Satoshis.
scriptPubkeyNone.Borrowed ScriptPubkey.Keep output alive.
toString()None.Diagnostic summary.Includes amount and script length.
copy()None.Owned copy.Throws if disposed.

Usage:

ts
const output = tx.outputs.get(0);
console.log(output.amount, output.scriptPubkey.toHex());

TransactionInputSequence ​

Purpose: lazy sequence of transaction inputs.

APIInputsReturnsErrors and caveats
lengthNone.Input count.Cached after first lookup.
get(index)Positive or negative index.Borrowed TransactionInput.Throws out of bounds.
slice(start, end)Optional bounds.Array of input views.Views tied to transaction.
iteratorNone.Inputs in order.Keep parent transaction alive.

Usage:

ts
const lastInput = tx.inputs.get(-1);

TransactionOutputSequence ​

Purpose: lazy sequence of transaction outputs.

APIInputsReturnsErrors and caveats
lengthNone.Output count.Cached after first lookup.
get(index)Positive or negative index.Borrowed TransactionOutput.Throws out of bounds.
slice(start, end)Optional bounds.Array of output views.Views tied to transaction.
iteratorNone.Outputs in order.Keep parent transaction alive.

Usage:

ts
for (const output of tx.outputs) {
  console.log(output.amount);
}

Transaction ​

Purpose: wrapper for a full Bitcoin transaction.

APIInputsReturnsErrors and caveats
new Transaction(data)Raw transaction bytes.Owned transaction.Throws if native parsing fails.
inputsNone.TransactionInputSequence.Lazy native views.
outputsNone.TransactionOutputSequence.Lazy native views.
txidNone.Borrowed Txid view.Keep transaction alive or detach/copy txid.
wtxidNone.Borrowed Wtxid view.Includes witness data; detach or copy before retaining it.
versionNone.number.Raw transaction version.
hasWitnessNone.boolean.Whether witness data is present.
check()None.Owned TxValidationState.Context-free structural checks only; it does not verify scripts or UTXO availability.
locktimeNone.number.Raw nLockTime.
toBytes()None.Serialized transaction bytes.Uses native byte writer.
toString()None.Txid/input/output summary.Diagnostic only.
copy()None.Owned copy.Throws if disposed.

Usage:

ts
const tx = new Transaction(Buffer.from(rawTxHex, "hex"));
console.log(tx.toBytes().length);

Coin ​

Purpose: wrapper for a UTXO record read from undo data.

APIInputsReturnsErrors and caveats
confirmationHeightNone.Block height.Height where coin was created.
isCoinbaseNone.boolean.Coinbase outputs have maturity rules.
outputNone.Borrowed TransactionOutput.Keep coin alive.
toString()None.Diagnostic summary.Includes height, amount, coinbase.
copy()None.Owned copy.Throws if disposed.

Usage:

ts
for (const coin of spent.coins) {
  console.log(coin.confirmationHeight, coin.output.amount);
}

CoinSequence ​

Purpose: iterable sequence of coins spent by a transaction.

APIInputsReturnsErrors and caveats
lengthNone.Coin count.Cached after first lookup.
get(index)Zero-based index.Borrowed Coin.Throws RangeError out of bounds.
iteratorNone.Coins in order.Views tied to TransactionSpentOutputs.

Usage:

ts
const firstCoin = spent.coins.get(0);

TransactionSpentOutputs ​

Purpose: per-transaction undo data describing coins consumed by that transaction.

APIInputsReturnsErrors and caveats
coinsNone.CoinSequence.Ordered with transaction inputs.
toString()None.Diagnostic summary.Shows coin count.
copy()None.Owned copy.Usually obtained from BlockSpentOutputs.transactions.

Usage:

ts
const undo = chainman.blockSpentOutputs.get(entry);
const spent = undo.transactions.get(0);
console.log(spent.coins.length);

Numeric inputs and sequence boundaries ​

Amounts accept safe integer number values or signed 64-bit bigint values. Unsafe numbers, fractional values, and int64 overflow throw RangeError instead of being rounded or truncated by the FFI.

All lazy sequences, including CoinSequence, support negative indices through get(). Indices must be safe integers; NaN, infinities, and fractions throw. slice() follows JavaScript array boundary conversion and clamping. Cached sequence lengths still check that the native owner is alive.

Experimental software. Do not use for consensus-critical systems or funds handling.