decode
Inputs a Bitcoin transaction or a transaction id and returns its metadata if it is a Bitcoin Computer transition transaction (smart object create/update).
Type
;(tx: NakamotoJS.Transaction | string) =>
Promise<{
exp: string
env?: { [s: string]: string }
mod?: string
}>
Parameters
tx
A NakamotoJS transaction, or a string representing a transaction ID.
Return Value
An object containing the following properties:
Description
The decode function takes a Bitcoin transaction or a transaction ID as input and retrieves the associated transition metadata if the transaction is a Bitcoin Computer smart-object transaction. This metadata includes the JavaScript expression, any environment variables, and an optional module specifier (the module used when evaluating the expression—not the module source itself).
Module deploy transactions are not transitions. If tx is a module deploy (multisig cleartext { ept } in data outputs, or a taproot reveal with protocol id BC in the witness), decode throws ModuleDecodeError (import { ModuleDecodeError } from @bitcoin-computer/lib) instructing you to use computer.load instead. To inspect raw module payloads without evaluating them, see Transaction.onChainMetaData (multisig) or Computer.getInscription(rawTx, index) (taproot witness).
Inside smart contracts (InnerComputer)
Only confirmed transactions may be decoded. Unconfirmed or missing txIds invalidate the evaluation. See Contract – Querying.
Inside smart contracts (InnerComputer)
Only confirmed transactions may be decoded. Unconfirmed or missing txIds invalidate the evaluation. See Contract – Querying.
Example
import { Computer, Contract } from '@bitcoin-computer/lib'
import { chain, expect, network, url } from '../../utils/index.js'
describe('decode', () => {
it('Should decode a transaction', async () => {
// A smart contract
class C extends Contract {}
// Create and fund a wallet
const computer = new Computer({ chain, network, url })
await computer.faucet(1e8)
// A transition encodes an update to the on-chain state
const transition = {
exp: `${C} new C()`,
env: {},
mod: undefined,
}
// Encode the transition to a transaction
const { tx } = await computer.encode(transition)
// Decode transaction back into transition
const decoded = await computer.decode(tx!)
expect(decoded).to.deep.equal(transition)
})
it('Should decode a txId', async () => {
// A smart contract
class C extends Contract {}
// Create and fund a wallet
const computer = new Computer({ chain, network, url })
await computer.faucet(1e8)
// A transition encodes an update to the on-chain state
const transition = {
exp: `${C} new C()`,
env: {},
mod: undefined,
}
// Encode the transition to a transaction
const { tx } = await computer.encode(transition)
await computer.broadcast(tx!)
// Decode transaction back into transition
const decoded = await computer.decode(tx!.getId())
expect(decoded).to.deep.equal(transition)
})
})