# decode

Parses a Bitcoin transaction and returns its metadata if it is a Bitcoin Computer transaction.

# Type

;(tx: NakamotoJS.Transaction) =>
  Promise<{
    exp: string
    env?: { [s: string]: string }
    mod?: string
  }>

# Parameters

# tx

A NakamotoJS transaction.

# Return Value

An object containing the following properties:

Parameter Description
exp The JavaScript expression of the transaction
env A mapping from variable names to revisions
mod A module specifier

# Description

The function decode is the inverse of encode when the latter is called with exp, env, and mod.

# Example

import { Computer } from '@bitcoin-computer/lib'
import { chain, expect, network, url } from '../../utils'

class C extends Contract {}

describe('decode', () => {
  it('Should decode a transaction', async () => {
    // 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: '',
    }

    // 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)
  })
})

Sources