@pwngh/economy-lab
    Preparing search index...

    Function chainHash

    The account's next chain head, as 64 lowercase hex characters: the digest (SHA-256 in every shipped runtime) over a fixed byte layout of four length-prefixed frames, each frame preceded by its 4-byte big-endian length. The frames, in order: the previous head's raw bytes (GENESIS for an account's first posting), the UTF-8 transaction id, this account's own legs — legs naming other accounts are excluded — as CURRENCY:decimal wire strings (debit-positive, credit-negative, fixed two decimals) sorted as strings and joined by a NUL byte, and the posting meta serialized as JSON with object keys sorted at every depth. Amounts inside meta must already be wire strings; a raw bigint throws rather than guess a format. The layout is fixed so the same posting reproduces the same bytes, and hash, on later verification — a store that persists heads computed this way re-derives byte-identically under offline verification.

    const amount = toAmount('CREDIT', 120_000n); // a $10 top-up at 120 CREDIT per dollar
    const head = await chainHash(systemDigest, {
    accountPrevHash: GENESIS,
    txnId: 'txn_01',
    account: spendable('usr_9'),
    legs: [credit(spendable('usr_9'), amount), debit(SYSTEM.STORED_VALUE, amount)],
    meta: { source: 'card' },
    });
    • The account's new chain head: the chainPreimage bytes through the digest, as lowercase hex.

      Parameters

      • digest: Digest
      • input: {
            account: AccountRef;
            accountPrevHash: Uint8Array;
            legs: readonly Leg[];
            meta: Record<string, unknown>;
            txnId: string;
        }

      Returns Promise<string>