load

Loads a module from the blockchain and returns its exports.

Type

;(rev: string) => Promise<ModuleExportsNamespace>

Parameters

rev

A module specifier encoded as a string of the form <transaction-id>:<output-number> (typically <txId>:0 as returned by computer.deploy).

Return Value

A JavaScript module namespace (the exports of the deployed ES module).

Description

load fetches the module source for the given specifier and evaluates it in a SES compartment (with Contract and a restricted inner computer available as globals).

How the source is recovered depends on moduleStorageType (see deploy):

  • multisig — Reads cleartext { ept } from the deploy transaction’s data outputs (Transaction.onChainMetaData.ept).
  • taproot — Reads the module body from the reveal transaction’s input witness (BC protocol envelope, content type text/javascript).

Do not use computer.decode on a module specifier or deploy transaction; decode only handles transition metadata (exp / env / mod).

Module sources are cached client-side after the first successful fetch for a given deploy txId.

A Bitcoin Computer Node also indexes deploys in its Module table. To discover or inspect sources without evaluating them, use getModules / getModule (or the node modules / module HTTP endpoints).

Inside smart contracts (InnerComputer)

Module locations are revs of the form txId:outputIndex. Inside a contract, the module’s deploy transaction must be confirmed; an unconfirmed deploy invalidates the evaluation. Off-chain computer.load may still resolve mempool deploys for development. See Contract – Querying.

Inside smart contracts (InnerComputer)

Module locations are revs of the form txId:outputIndex. Inside a contract, the module’s deploy transaction must be confirmed; an unconfirmed deploy invalidates the evaluation. Off-chain computer.load may still resolve mempool deploys for development. See Contract – Querying.

Example

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

describe('load', () => {
  // A smart contract
  class C extends Contract {}

  it('Should load a module', async () => {
    // Create and fund wallet
    const computer = new Computer({ chain, network, url })
    await computer.faucet(1e8)

    // Deploy module
    const rev = await computer.deploy(`export ${C}`)

    // Load module
    const { C: Loaded } = await computer.load(rev)

    // The deployed module is always equal to loaded module
    // when white spaces are removed
    const trim = (Class: any) => Class.toString().replace(/\s+/g, '')
    expect(trim(Loaded)).eq(trim(C))
  })
})

Source