Files
wasm/packages/quantus-codec
rob thijssen e6ff57a334 feat: storage addressing, nested calls, and a stated integer convention
Three things the tier-1 case matrix needed (quantus/extension#7).

**Storage keys and values (#4).** `storage_target` resolves a pallet and item to
`twox128(prefix) ‖ twox128(item)` plus each map key hashed by the hasher the
entry declares, and reports the value type and the entry's default.
`decode_storage_value` reads the result. Nothing here knows that `System::Account`
is a `Blake2_128Concat` map over an `AccountId32`; the hashers, both types and
the default all come out of the metadata.

The `Default` versus `Optional` distinction is carried deliberately. A `Default`
entry that the node returns nothing for means the declared default — an account
nobody has funded reads as a zero balance — where an `Optional` one means
nothing. A wallet that conflated them would report a failure for an account that
simply has no money in it.

**A call nests inside a call.** A multi-field variant with named fields only
accepted a positional array, so `Utility.batch_all` — whose `Vec<RuntimeCall>`
holds calls spelled exactly like top-level ones — could not be encoded at all. It
now takes the same object form at any depth.

**Every integer renders as a decimal string**, whatever its width, and that is
now stated rather than incidental. A u128 balance does not survive a JSON number
(12 decimal places puts ordinary amounts past 2^53) and `scale_value` widens
every unsigned integer to u128, so the width is not available to switch on.
Emitting a number when it happens to fit and a string when it does not would make
a consumer handle both shapes for the same field depending on the value.

All of it proven against Heisenberg: an ML-DSA-65 account funded by a `batch_all`
whose payload crossed the 256-byte BLAKE2b threshold, then signing and being
included itself.

Closes #4. Refs #3

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012uDUodEcRbBwNRi3UCmw8f
2026-09-15 15:15:35 +03:00
..

@quantus/codec

Metadata-driven SCALE encode and decode for the Quantus chain, compiled to WASM.

Nothing in this package names a pallet, a call, a signed extension or a signature scheme. Everything is read from the metadata the node produced by running Metadata_metadata against the runtime WASM in a given block's state, which makes the runtime the oracle rather than this package's author.

Why not @polkadot/api

Three reasons, in increasing order of importance — the evidence is on quantus/api#1.

  1. @polkadot/types refuses fixed arrays longer than 2048 bytes. ML-DSA signatures are [u8;5261] and [u8;7219], so every Quantus extrinsic trips it.
  2. api.rpc.chain.getBlock throws on every block of this chain, at the timestamp inherent. The extrinsic preamble byte's top two bits are a type tag (0b00 bare, 0b10 signed, 0b01 general) and the low six are the version; Quantus emits 0x84 — signed, v4 — and 0x05 — bare, v5 — in the same block while the metadata declares version 4. polkadot-js reads that byte as a version.
  3. It guesses that signed extensions it does not recognise contribute nothing to the signed payload, logging Unknown signed extensions … treating them as no-effect.

The third is why this package exists rather than a patch. The guess is correct only while every unrecognised extension happens to be zero-sized. This chain's encoding has already changed between runtimes — transactionVersion has gone 2 → 3 → 6 across four upgrades, each an extrinsic-format change — and when the guess stops being correct the wallet keeps signing. Those signatures are cryptographically valid, over a payload missing bytes the runtime put there, and the chain reports them as BadProof, which is also what it reports for a wrong key. Silent, remote, and indistinguishable from the one thing it is not.

Here the registry decides. An extension whose declared type encodes to nothing contributes nothing; anything else must be supplied by the caller or no payload is produced at all.

Use

import { Runtime } from '@quantus/codec';

const runtime = Runtime.fromMetadata(await fetchMetadata()); // state_getMetadata

const call = runtime.encodeCall('Balances', 'transfer_keep_alive', {
  dest: { Id: '0x…' },
  value: '1000000000'
});

const values = runtime.standardExtensions({
  blockHash: genesisHash, // immortal era
  genesisHash,
  nonce,
  specVersion,
  transactionVersion
});

const payload = runtime.signerPayload(call, values);
// sign `payload` with @quantus/crypto under the QUANTUS_EXTRINSIC context,
// hashing it first with BLAKE2b-256 if it is longer than 256 bytes
const extrinsic = runtime.encodeExtrinsic(
  { Id: accountId },
  signature,
  runtime.encodeExtra(values),
  call
);

standardExtensions fills in the extensions Substrate itself defines. Anything else this runtime declares as non-empty is refused by name — see above for why that is the desired behaviour rather than a limitation.

Build

./scripts/build-quantus.sh quantus-codec

Same constraints as @quantus/crypto: a modern toolchain (separate from wasm-crypto's 2022 nightly), initSync over base64+zlib for the MV3 CSP, wasm-bindgen's own glue rather than @polkadot/wasm-bridge, and binaryen 123 — version 105 silently corrupts the output. See quantus/wasm#1 and #3.