← Back to Pull requests

Add the factory that creates self-owned wallets (step 12) #14

Closed
unknown closed this pull request without merging on Oct 21, 2026 18:48 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 18:29 UTC
Author

<!-- claude-projects-attribution: {"github_login":"b3hr4d"} -->
_Requested by Behrad · project thread_

Before: there was no factory, only its draft interface. A wallet was installed by hand, so its deployer stayed its controller until someone added the wallet as a controller of itself.

After: the factory's create_wallet creates a wallet for its caller and leaves the wallet its own only controller, with no rights left to the factory. The creator pays: under an allowance the creator gave the factory, the cycles ledger's create_canister_from takes the wallet's cycles from the creator's account and has the cycles minting canister create the canister, so the factory never holds a creator's cycles. Before anyone pays, the factory checks the arguments and that two of the three signer keys it pins signed the channel's release. It then stages the release's chunks in the new canister's own chunk store, each checked against the signed manifest, installs the wallet with InitArgs naming the channel, the signer keys and the factory, empties the chunk store and hands the canister over. If the release does not install, the call fails with InstallFailed and the creator becomes the canister's only controller, with the cycles left in it. current_release shows the release a wallet created now gets.

How: until it hands a canister over, the factory keeps a record of it in stable memory 1, written in the same message that learns the canister's id. A hand-over that fails, or a creation whose callback traps, leaves the record for the IC's global timer (exported by hand, as in the wallet), which tries again five minutes later, a deadline later creations never push back: a canister whose module is its release's goes to itself, any other to its creator, and one the factory no longer controls (read with canister_info) only leaves the record. post_upgrade sets the timer again while a canister waits. The factory's interface types live in release::factory, shared with its clients; a test binary that linked two canister crates got duplicate exported symbols.

Worth your review

  • factory.did changes: FactoryArg (Init with the channel, signer keys and threshold; Upgrade keeps or replaces them), CreateWalletArgs gains claim_expires_at_ns, version and from_subaccount, CreatedWallet.cycles is what the creator paid (the wallet's status shows what is left), the errors are InvalidArgument, InsufficientAllowance, InsufficientFunds, ReleaseUnavailable, ReleaseRejected, CreateFailed and InstallFailed, and current_release is an update, since it reads the channel.

  • Minimums: at least 1T cycles per wallet and a claim window of at least an hour. The anonymous principal cannot create a wallet.

  • Cost, measured on PocketIC: of 3T paid, the wallet starts with 2.49T (0.5T of it went to creating the canister on a 13-node subnet). The factory spends about 8.5B cycles of its own per wallet on its calls, which the creator does not pay, and a refused call still costs it a channel read and the signature checks. The design's security table now says so. Charging the creator for it would need a fee model, still an open decision.

  • A factory whose controllers change its code could create wallets that run other code or that it still controls. Wallets it already handed over are out of its reach, and a wallet's module hash and controllers are public, but nothing checks them before the claim yet. That is a new row in the design's security table.

  • icp.yaml names the factory, and the README has a "Creating a wallet" section. scripts/try-local.sh still installs its wallet by hand, so its wallets still need the controller step to upgrade; moving it onto the factory would mean signing and publishing a local release in the script.
  • Tests. 5 new PocketIC tests in tests/integration/tests/factory.rs, with the real wallet, channel and factory Wasm on PocketIC's own cycles minting canister and cycles ledger: the factory creates a wallet that owns itself, is claimed, and upgrades itself to a release signed by keys 2 and 3, and creates wallets of the latest release or of the version asked for; it takes nothing for a wallet it cannot create (no release, unknown version, no or too small allowance, too few cycles, bad arguments, a release with too few signatures); a release that does not install leaves the canister to its creator with an empty chunk store; the timer hands over a wallet whose hand-over failed while the factory was frozen, though another wallet was created in between; and an upgrade keeps the configuration or replaces it, and refuses Init or a bad one. 10 new unit tests check the configuration and the arguments, who gets a canister, that a refused attempt to mark a busy canister leaves it busy, that a retry already set keeps its deadline, the cycles ledger's id, and the factory's interface and its wallet install argument against factory.did and wallet.did. The busy-canister and retry-deadline fixes answer the Codex review, and their tests fail without them.

    I injected 15 faults, one at a time, and each failed at least one test: signatures not checked, payment before the release check, the chunk store left full, the factory left a controller, a failed install handed to the wallet, a timer that does nothing, a timer never armed, no record before the install, no minimum cycles, no minimum claim window, anonymous creators, a wallet install argument that drifts from wallet.did, an upgrade that takes Init, an upgrade whose configuration goes unchecked, and an allowance error reported as another. Not covered by a test: the per-chunk check against the manifest and the check that the channel answered the version asked for, since the real channel never serves anything else; the IC also checks the module hash at install.

    The whole CI battery passes locally: 115 wallet unit tests, the interface check (18 of 23 methods), 10 factory unit tests, and 76 PocketIC tests (55 for the wallet, with the upgrade from main and regtest Bitcoin, 11 for the channel, 5 for the self-upgrade and the 5 above). I also ran the README's "Publishing a release" and "Creating a wallet" commands on a local icp-cli 1.7 network: the wallet came back as its own only controller, running the release's module, and took the claim.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_013Vgh2De8Zarye1iDyGSRbe

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: b3hr4d Review duration: 19m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #14"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%