decode
Inputs a Bitcoin transaction or a transaction id and returns its metadata if it is a Bitcoin Computer transaction.
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 metadata if the transaction is a Bitcoin Computer transaction. This metadata includes the JavaScript expression, any environment variables, and an optional module specifier.
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)
})
})