@anywalletkit/bunker
@anywalletkit/bunker
Section titled “@anywalletkit/bunker”Experimental. The first step of the post-quantum roadmap proposed in #82. The API may change in any release.
Bunker mode: keep the public keys of large accounts unseen until their funds are behind a post-quantum key.
An attacker who can turn a public key into a private key needs the public key first. On most chains an address is only a hash of it, so the key stays hidden until the account signs something. This package tracks that, and stops a protected account from giving it away.
sealability(chain, address): can this address hide its key at all? EVM, Tron, Bitcoin SegWit v0, Zcash transparent and TON can. Solana, NEAR and Taproot cannot.ExposureLedger: what this device has signed. The chain shows transactions; only the wallet knows about a login signature, which exposes the key while the nonce stays 0.scanExposure: address type, ledger and chain together, per account:sealed,exposed,unsealableorunknown, with codes that say why.bunkerPolicies: policies for@anywalletkit/kit, composed over your own. A protected account can do one thing: sweep everything to a sealed address of the same wallet.nextSealedIndex: the address to sweep to. It stops with aSealedIndexErrorwhen it cannot tell whether an address was used.judgeBunker,bunkerGate: the same rules and the same write-ahead ledger without a kitWriteIntent, for a wallet that does not sign through the kit’s policies.bunkerPoliciesjudges through them.
npm install @anywalletkit/bunker@anywalletkit/kit is an optional peer (>=0.9.0 <1.0.0). Only bunkerPolicies is made for it, and the package uses its types alone: the ledger, the scan, nextSealedIndex, judgeBunker and bunkerGate run without it. If the kit is not installed, TypeScript needs skipLibCheck (most app templates set it), because the type declarations name the kit’s WriteIntent.
import { ExposureLedger, bunkerPolicies, evmNonceProbe, nextSealedIndex, scanExposure } from '@anywalletkit/bunker'
const ledger = await ExposureLedger.load(storage) // public data, plain storage is fineconst reports = await scanExposure(accounts, { ledger, probes: { eip155: evmNonceProbe(chains, rpc) } })
const wallet = new Wallet(signer, { policies: bunkerPolicies(ledger, { protect: (intent) => inBunker.has(intent.from), isSealedDestination: (chain, address) => mySealedAddresses.has(address), isFullSweep: (intent) => leavesNothingBehind(intent), inner: appPolicies, // your lists, fee caps and confirm screen }),})| A protected account asks to… | Result |
|---|---|
| sign a message or typed data | refused (off-chain-signature) |
| approve or revoke | refused (not-a-sweep) |
| call a contract | refused (unsealed-destination): to the kit a call is a send to the contract, and a contract is not a sealed address of the wallet |
| send to an address that is not sealed and its own | refused (unsealed-destination) |
| send somewhere the kit could not read | refused (unknown-destination) |
| send part of its balance | refused (partial-spend) |
| sweep everything to a sealed address | allowed; written to the ledger before the wallet signs |
Every confirmed write of every account, protected or not, is recorded. mode: 'warn' reports instead of refusing.
isSealedDestination and isFullSweep are optional. Without isSealedDestination no address counts as sealed, so every send of a protected account is an unsealed-destination: refused in enforce, reported in warn. An app that runs warn only passes protect and onWarning, nothing else.
The ledger
Section titled “The ledger”- One entry per key and reason, the earliest. A key is one whatever the network or the spelling: an EVM address on every EVM network, a TON address raw (
0:ab…) or friendly (UQ…), a Tron address in base58 or hex (exposureKey). - Written before the signature, and never taken back. The record is made when the write is confirmed. If the signature then never happens (the node refuses the transaction, the app crashes, the user’s device rejects it), the key stays marked exposed. That is on purpose: a key wrongly called exposed costs a sweep to another address, a key wrongly called sealed costs the protection.
- What the policies record:
transaction,message,typed-data. A scan addson-chain.dapp-sessionis yours to record: the policies see writes, not connections. When your app answers a connection that hands out the public key (TON Connect), callledger.record({ chain, address, reason: 'dapp-session', at: Date.now() })first, orgate.admit({ kind: 'session', … }). The@anywalletkit/extension-kit/bunkeradapter does it for an extension. - When it cannot be saved, the write is refused by default.
ledgerFailure: 'continue'lets a write this package does not guard go on and tellsonLedgerError. A protected account inenforcemode is always refused.
Scan reports
Section titled “Scan reports”scanExposure returns one report per account:
{ chain, address, seedRooted, status: 'exposed', codes: ['key-hash', 'on-chain', 'message'], reasons: ['The address is a hash…', 'The chain shows…', 'This wallet signed a message…'], firstExposedAt: 1764850000000 }codes is for your own text and styling; reasons[i] is the default English sentence for codes[i].
| Code | Meaning |
|---|---|
key-hash, key-or-script-hash, code-and-key-hash |
The address type can hide its key (EVM and Tron; Bitcoin SegWit v0, legacy and Zcash transparent; TON). Always the first code; the same value as sealability(chain, address).code |
taproot-output-key, address-is-key, keys-on-chain |
It cannot (Taproot; Solana; NEAR): unsealable |
unknown-address-type, unknown-chain |
Not an address this package knows: unknown, and no probe is asked |
imported-key |
seedRooted: false: no seed stands behind the key |
transaction, message, typed-data |
exposed: this wallet signed one with the key |
dapp-session |
exposed: a dApp connection handed out the public key |
on-chain |
exposed: a scan found a transaction of the key on the chain, signed by this wallet or by another copy of the key |
no-probe |
unknown: no probe was given for this chain family, so the chain was not asked |
probe-failed |
unknown: the probe was asked and could not tell (an RPC error, a network that did not answer) |
An exposed key lists one code for each reason the ledger holds, oldest first, the same on every scan. firstExposedAt (Unix milliseconds) is the earliest of them; for on-chain it is when the scan found the transaction, not when it was sent.
The next sealed address
Section titled “The next sealed address”import { SealedIndexError, nextSealedIndex } from '@anywalletkit/bunker'
try { const { index, address } = await nextSealedIndex({ chain: 'eip155:1', addressAt, isUsed, ledger })} catch (error) { if (error instanceof SealedIndexError && error.code === 'cannot-tell') showRetry(error.address)}isUsed(address) answers true, false, or undefined when it could not tell (the RPC is down). An address is taken only on a plain false.
error.code |
Meaning |
|---|---|
cannot-tell |
isUsed could not say. The search stops at error.index / error.address: the index is not skipped (that would leave a gap) and not taken (it may be used) |
none-found |
Every index tried (from … from + limit - 1) is used or in the ledger |
unsealable |
Addresses of this chain or path cannot hide their key |
An error your isUsed throws is passed on as it is.
Without the kit’s policies
Section titled “Without the kit’s policies”A browser extension signs dApp requests in its request router, not through Wallet. judgeBunker reads a small request instead of a WriteIntent, and bunkerGate adds the protect question, enforce / warn and the write-ahead record:
import { bunkerGate } from '@anywalletkit/bunker'
const gate = bunkerGate(ledger, { protect: (request) => inBunker.has(request.from), isSealedDestination, isFullSweep })await gate.admit({ kind: 'send', chain: 'eip155:1', from, destinations: [to] }) // right before the key signsA request is message, typed-data, send (with its destinations), other (an approval, a contract call, a swap) or session (a dApp connection). One rule exists only here, because only the caller knows who asked: a request marked dapp: true, and every session, is refused for a protected account with dapp-request, a full sweep included. bunkerRequestOf(intent) gives the request for a kit write.
For an extension built on @anywalletkit/extension-kit, the @anywalletkit/extension-kit/bunker entry wires all of this: the keyring, the sign handler, what sites see, a signed ledger store and the scan over the wallet’s networks.
Limits
Section titled “Limits”- The sweep transaction itself shows the key while it waits in the mempool.
- Solana, NEAR and Taproot accounts cannot be sealed. They need a post-quantum vault or the chain’s own recovery keys (RFC 0001 in #82, sections 7 and 8).
- Only an EVM probe ships. Bitcoin, Zcash, Tron and TON probes are planned.
- A scan asks an RPC about every address, which links them at that provider.
Classes
Section titled “Classes”Interfaces
Section titled “Interfaces”- BunkerGate
- BunkerGateOptions
- BunkerJudgeOptions
- BunkerOptions
- BunkerRefusal
- BunkerRequest
- ExposureEntry
- ExposureReport
- LedgerStorage
- ScanAccount
- SealabilityInfo
Type Aliases
Section titled “Type Aliases”- BunkerRequestKind
- BunkerRule
- ExposureCode
- ExposureProbe
- ExposureReason
- Sealability
- SealabilityCode
- SealedIndexErrorCode