import { type CoinSpent, type IssuedCoin } from "./client.js"; import { denomById, type Keyset } from "./denom.js"; /** * §6.10's reserve gap limit: stop after this many consecutive indices with no * on-chain activity. */ export declare const RESERVE_GAP_LIMIT = 20; /** * §6.10's coin gap limit: stop after this many consecutive counters with no * issuance record. * * Wider than the reserve limit because counters are consumed by things that * never produce a coin — an abandoned withdrawal, a `BlindingExhaustedError` * that forces the wallet to advance (§6.11) — while a reserve index is only * consumed deliberately. */ export declare const COIN_GAP_LIMIT = 100; /** * How deep the `/link` walk follows change-of-change before giving up. * * A bound rather than an assumption: the melt records come from an untrusted * log, and a log that pointed a coin's melt record at itself would otherwise * spin. Honest chains are short — every level is a real refresh the wallet * performed. */ export declare const MAX_REFRESH_DEPTH = 32; /** * How many registry spent totals recovery reads at once (§6.10's last * step). One per coin the seed ever held, so a wallet that has paid for a * while makes dozens on every start: 57 one by one took 12.7 seconds, * measured. */ export declare const SPENT_LOOKUP_CONCURRENCY = 8; /** * How many reserve indices recovery reads ahead of its walk. The walk itself * stays in order and stops where it did (after {@link RESERVE_GAP_LIMIT} * never funded in a row); only the reads overlap. */ export declare const RESERVE_LOOKAHEAD = 8; /** What the chain shows for one reserve address (§5.2.1, §5.2.4, §5.2.5). */ export interface ReserveActivity { /** `Σ` USDC deposited to this reserve, in base units. */ readonly deposits: bigint; /** `Σ` reclaimed out of it, in base units. */ readonly reclaims: bigint; } /** One §8 issuance record, as a recovering wallet needs it. */ export interface IssuanceRecord { /** `σ_blind`, exactly `L = bitlen(N)/8` bytes. */ readonly sigmaBlind: Uint8Array; /** Unix seconds the record was published, when the log carries it. */ readonly issuedAt?: number; } /** One §8 melt record `⟨C⁰_p, T_γ, {D_j, σ_blind}, s_L⟩`, minus what recovery does not need. */ export interface MeltRecord { /** `T_γ`, the published transfer key of the row the issuer signed. */ readonly tGamma: Uint8Array; /** `{D_j, σ_blind}` in index order — `j` is positional and must not be re-sorted. */ readonly targets: readonly { readonly denomId: string; readonly sigmaBlind: Uint8Array; }[]; readonly issuedAt?: number; } /** * The three read-only views recovery needs, as ports. * * They are ports rather than HTTP calls because two of the three services * that would answer them do not exist yet (§6.4.1's registry has no HTTP * surface), and because a wallet may well read the chain through its own * node. Nothing here writes, and nothing here transmits a coin public key * that is not already public — see the module docs on why * {@link RecoverySources.issuance} is keyed on `z`. */ export interface RecoverySources { /** On-chain `Deposit` / `WithdrawAuthorized` for a reserve. `undefined` = never used. */ reserveActivity(reserveAddr: Uint8Array): Promise; /** * The §8 issuance record for one blinded message under one denomination. * * `z` arrives as `pad(z, L)` — §6.12's fixed-width encoding, the same bytes * the withdrawal put on the wire. */ issuance(denomId: string, z: Uint8Array): Promise; /** Every §8 melt record naming this coin as `C⁰_p`. */ melts(coinPub: Uint8Array): Promise; /** The registry's `spentTotal(C_p)`, in base units. Read only when {@link coinSpent} is absent. */ spentTotal(coinPub: Uint8Array): Promise; /** * The registry's whole answer for one coin, `GET /spent/:C_p`: its spent * total and the refund credit a melt has not yet collected (§6.7). Read in * place of {@link spentTotal} when present -- without it a refunded coin * reads as spent, and its refunded value is not recovered. */ coinSpent?(coinPub: Uint8Array): Promise; } /** One coin recovered, with where it came from and what is left of it. */ export interface RecoveredCoin { readonly coin: IssuedCoin; /** `withdraw` came out of a counter; `refresh` came out of a melt record. */ readonly origin: "withdraw" | "refresh"; /** The reserve index this coin's derivation chain starts at. */ readonly reserveIndex: number; /** The withdraw counter, for `origin: "withdraw"`. */ readonly counter: bigint | undefined; /** Unix seconds, from the log; `0` when the log did not date the record. */ readonly issuedAt: number; /** `spentTotal(C_p)` as the registry has it. */ readonly spent: bigint; /** * Refunded value (§6.7) the registry still credits this coin, at most * {@link spent}: reachable only by melting the coin (§6.6), never by * offering it again (§7). */ readonly refundCredit: bigint; /** `value − spent + refundCredit`: what a melt of this coin locks. */ readonly residual: bigint; } /** One live reserve found by the scan. */ export interface RecoveredReserve extends ReserveActivity { readonly index: number; /** The 20-byte EVM address. */ readonly address: Uint8Array; /** The first counter that is safe to withdraw at (§6.13: never reuse one). */ readonly nextCounter: bigint; } /** Everything §6.10's walk produced. */ export interface RecoveryResult { readonly reserves: readonly RecoveredReserve[]; /** Every coin found, spent or not. */ readonly coins: readonly RecoveredCoin[]; /** Those with residual value. */ readonly spendable: readonly RecoveredCoin[]; /** `Σ` residual across {@link spendable}. */ readonly totalValue: bigint; /** * The counter a resumed wallet must start at: above every counter this * pass saw, across every reserve. */ readonly nextCounter: bigint; /** Records the log served that did not verify and were skipped. */ readonly rejected: number; /** `Σ deposits` and `Σ reclaims` across the reserves found, for reconciliation (§7). */ readonly deposits: bigint; readonly reclaims: bigint; } /** * One step of a recovery, for a progress display. * * Recovery reads public data for as long as the chain and the wallet's history * are long: the issuer's whole log, a `reserves(addr)` call per reserve index, * every `eth_getLogs` window from the reserve's deployment block (or from a * saved checkpoint, `sources.ts`), and one registry lookup per coin found. On * a used seed that is a minute or more of reads, so an agent that boots with * it should say what it is doing. Each phase reports as it goes; `done` (or * `blocksDone === blocksTotal`, `done === total`) marks its end. * * Never key material: phase names, counts, block numbers, reserve indices, * and the reserve address the deposit-log scan filters on, which the chain * already shows. */ export type RecoveryProgress = { /** The issuer's public `/log`, read whole, page by page (`sources.ts`). */ readonly phase: "issuer-log"; /** Entries read so far. */ readonly entries: number; /** The log's size as the issuer reports it, when it does. */ readonly logSize: number | undefined; readonly done: boolean; } | { /** * One reserve index about to be checked on chain (`reserves(addr)`, one * `eth_call`, then the deposit-log scan when it was funded); the walk * stops after {@link RESERVE_GAP_LIMIT} in a row were never funded. * The `done` event names the last index checked. */ readonly phase: "reserve-discovery"; readonly index: number; /** Funded reserves found before this index (all of them, on `done`). */ readonly funded: number; readonly done: boolean; } | { /** The `Deposit` and `BalanceReclaimed` log scan of one funded reserve, window by window. */ readonly phase: "deposit-logs"; /** The reserve address, `0x` lowercase. */ readonly reserve: string; /** The first block this scan reads: the deployment block, or a checkpoint's block + 1. */ readonly fromBlock: bigint; /** The chain head when the scan began: its last block. */ readonly toBlock: bigint; readonly blocksDone: bigint; readonly blocksTotal: bigint; /** `"checkpoint"` when a saved checkpoint let the scan start late. */ readonly startedFrom: "deployment" | "checkpoint"; } | { /** Withdraw counters searched for coins under one funded reserve. */ readonly phase: "coin-search"; readonly reserveIndex: number; /** The counter just searched. */ readonly counter: bigint; /** Coins found so far, every reserve counted. */ readonly found: number; } | { /** The registry's spent total for each coin found (`GET /spent/{coin}`). */ readonly phase: "spent-lookups"; readonly done: number; readonly total: number; }; /** * A caller's progress hook, made safe to call from inside a recovery: a * display that throws is ignored, never the reason a recovery stops. */ export declare function progressReporter(hook: ((progress: RecoveryProgress) => void) | undefined): (progress: RecoveryProgress) => void; /** Arguments to {@link recoverFromSeed}. */ export interface RecoverParams { readonly seed: Uint8Array; /** * The keyset to derive candidates under — already verified against the * on-chain `commitKeyset` value by the caller (§4). */ readonly keyset: Keyset; /** * Every other keyset the wallet held coins of (K1), verified the same way. * A counter's coin was withdrawn under one epoch, and recovery does not * know which, so it derives one candidate per rung of every keyset given; * change is resolved by its `denomId` among all of them. */ readonly keysets?: readonly Keyset[]; readonly sources: RecoverySources; /** Defaults to {@link RESERVE_GAP_LIMIT}. */ readonly reserveGapLimit?: number; /** Defaults to {@link COIN_GAP_LIMIT}. */ readonly coinGapLimit?: number; /** Defaults to {@link MAX_REFRESH_DEPTH}. */ readonly maxRefreshDepth?: number; /** Defaults to {@link SPENT_LOOKUP_CONCURRENCY}. */ readonly spentLookupConcurrency?: number; /** Defaults to {@link RESERVE_LOOKAHEAD}; 1 reads one index at a time. */ readonly reserveLookahead?: number; /** Told about every record skipped. Never given key material. */ readonly onWarning?: (message: string) => void; /** * Told how far the walk has got: each reserve index checked, each counter * searched, each spent total read ({@link RecoveryProgress}). The sources * report their own reads (`publicRecoverySources`, `JsonRpcChain`). A hook * that throws is ignored. */ readonly onProgress?: (progress: RecoveryProgress) => void; } /** * Rebuild a wallet from its seed (§6.10). * * Converges on registry truth: every coin it returns carries the registry's * own `spentTotal` and refund credit, so a coin that was already spent comes * back marked spent rather than as recovered value, and a refunded one with * the value its melt will collect. Re-running it is safe and produces the * same answer. */ export declare function recoverFromSeed(params: RecoverParams): Promise; /** * The keyset lookup a caller holding only a `denomId` needs, re-exported so * consumers of a {@link RecoveryResult} need not import `denom.ts` to resolve * one. Never parse a value out of the id string (see `denom.ts`). */ export { denomById };