← Back to Pull requests

Step 9: Bitcoin accounts signed with threshold ECDSA #10

Closed
unknown closed this pull request without merging on Oct 21, 2026 04:20 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 03:12 UTC
Author

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

Before: add_account refused Bitcoin, so the wallet held only ICP and ICRC tokens.

After: an owner adds Bitcoin accounts on mainnet, testnet or regtest. Each one holds its coins at the P2WPKH address of a threshold ECDSA key, balance reads its confirmed satoshis from the Bitcoin canister, and a Transfer from it builds, signs and sends a transaction through the Bitcoin canister, at most once.

Keys. Account index n on a Bitcoin network is the key of path wallet / bitcoin-<network> / n, with n as 4 big-endian bytes, as docs/design.md planned. The keys are key_1 on mainnet, test_key_1 on testnet and dfx_test_key on regtest. add_account asks the management canister for the public key once and stores it, so the address never needs another call. While it waits, other calls run, so it checks again that the caller is still an owner and that no other account took the index.

Transactions. A transfer is built on its first execute. It spends the account's confirmed outputs that no open transfer has reserved, those with 100 confirmations first and then the largest first, and pays the recipient in output 0 and the change back to the account in output 1. Change too small to relay goes to the fee. fee is the whole fee in satoshis; with none, the wallet pays the median rate the Bitcoin canister reports, at least 1 sat/vB, and 2 sat/vB when it reports none. A fee below 1 sat/vB of the transaction it pays for fails the transfer; one that covers an exact-balance payment without change passes, but change the network would relay never goes to the fee (Codex's first finding). Each input is signed in a bounded-wait sign_with_ecdsa call, and every signature is normalized to low-s and checked against the account's key before the transaction goes to the Bitcoin canister.

At most once. The transaction is stored once it is built, its outputs are reserved in a new memory 12, and every later execute sends that same transaction, signing it first if it is not signed yet. Its id covers no signature, so signing it again cannot make a second payment. One execution runs at a time, with a heartbeat after every await, and one stuck for an hour is taken over. Once a send may have reached the Bitcoin canister, the action stays ready, cannot be rejected and never expires: execute sends the same transaction again until the Bitcoin canister accepts it. As in step 8, only a reject by the IC before the Bitcoin canister ran counts as not sent. A transfer that ends without sending (failed, rejected or expired) releases its outputs.

Daily limit. A Bitcoin transfer's fee counts toward its chain's limit, both when it is proposed (the explicit fee) and when it runs (the real fee, checked again once the transaction is built). It is checked once more just before each send until one may have reached the Bitcoin canister, so a policy executed while the transaction was being signed applies (Codex's third finding); a transaction that may have been sent still goes. A transfer stored in memory 6 now carries its fee. Ledger fees still do not count.

How: btc.rs (addresses, checks, coin selection, the transaction and its signatures) and the Bitcoin attempt state in actions.rs are pure; signer.rs makes the threshold ECDSA calls and bitcoin_canister.rs the Bitcoin canister's; calls.rs is the bounded-wait call that ledger.rs now uses too, with step 8's error texts unchanged. lib.rs makes add_account async and gives execute its Bitcoin branch.

Worth your review

  • The key names and the path bytes. Changing either later moves every Bitcoin address, so this is the one to check before anything holds coins.

  • Only P2WPKH addresses, and only confirmed outputs are spent, so a transfer's change can be spent once it confirms.

  • Mining rewards (Codex's second finding, and the decision card in the project thread). The Bitcoin canister lists a reward from its first confirmation without marking it, and the network refuses to spend one for 100 blocks. Outputs with 100 confirmations are now spent first, so only a transfer that needs a younger reward sends a transaction the network refuses; it reads Executed and the reward stays reserved. You chose this over holding every incoming output for 100 confirmations; decision 0003 records both.

  • Fee semantics above, and the Bitcoin fee counting toward the daily limit while ledger fees do not.

  • A transaction the network drops keeps its outputs reserved and its action open for good; execute rebroadcasts it. Telling a dropped transaction from a slow one needs the outputs read again, which a later step can add. For the same reason, the outputs of a sent transaction stay in memory 12 for good, one small entry per output spent.

  • New dependencies: bitcoin 0.32.102 with no default features, DFINITY's ic-cdk-bitcoin-canister 0.2.0 for the Bitcoin canister's types and prices, and secp256k1 0.29.1, already under bitcoin, named only to build it with its lowmemory tables.

  • Stored format: StoredAccount.public_key, StoredPending.bitcoin and StoredTransfer.fee are new optional fields, and memory 12 is new. The upgrade test carries step 8's state across.

  • add_account keeps its Candid type but now awaits the management canister for a Bitcoin account.

  • The Wasm is 3.70 MB, or 894 KB gzipped (step 8: 3.10 MB and 715 KB). With secp256k1's default precomputed tables it was 4.78 MB and 1.97 MB gzipped, just under the 2 MB install limit, because those tables do not compress.

  • CI now downloads Bitcoin Core 31.1 from bitcoincore.org, pinned to the checksum all 16 builders signed in bitcoin-core/guix.sigs.
  • Tests. Unit tests cover addresses against the BIP-173 vector, the checks at propose, the fee rate, coin selection and its maturity boundary, dust change, the fee floor, the 32-input cap, and signing with a known key, including a high-s signature. The attempt state has tests for the execution lock and its takeover, a kept transaction, sends that did not run or may have, and a property test (1,024 cases) against a model Bitcoin canister that takes a send, takes it unheard, loses it, is full, or stalls, with the wallet's code sometimes trapping after it: it checks that at most one transaction is sent, that a transfer ends Executed only if it was, and that one maybe sent stays open. Six regtest tests start Bitcoin Core and a PocketIC instance whose Bitcoin canister follows it. They check that an account receives at its key's address, sends once with the change back, and still spends after an upgrade; the checks at propose; two execute calls at once; two transfers that never spend the same output, even unconfirmed; failed transfers that reserve nothing; the daily limit with the fee; a send whose outcome is unknown, which keeps its output reserved, cannot be rejected, and later sends the same transaction; a transfer that spends a change output 100 blocks old rather than a larger mining reward 1 block old; and a policy lowered while a transfer waits for its signatures, which fails that transfer and frees its outputs, while one that may have been sent still sends.

    Faults. Injected one at a time, each made at least one test fail: a lost execution not marking its send as maybe sent; an unknown send not marked; no execution lock; a maybe-sent transfer expiring; resume ignoring a takeover; outputs released after a maybe-sent transaction; fees not counted as spent; a little-endian index in the path; no low-s normalization; the change output dropped; dust change kept; the fee floor ignored, or checked only on the transaction with change; the signature check skipped; no reserved outputs filtered out; no outputs reserved; the transaction rebuilt on every execute; no daily-limit check with the real fee; signing with the next account's key; maturity ignored in the coin order, every coin taken as mature, or maturity counted one block late; no daily-limit check before the send, or that check or the one at the start of execute made even after a send that may have reached the Bitcoin canister. The fourth regtest test is the only one that catches the rebuilt transaction, the fifth every coin taken as mature, and the sixth the three faults around the check before the send.

    Checks run locally: cargo fmt --all --check, clippy native and wasm32 with -D warnings, unit and interface tests, the release Wasm build, and the PocketIC tests against server 16.1.0 with Bitcoin Core 31.1, including the upgrade from step 8.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_013Vgh2De8Zarye1iDyGSRbe

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: b3hr4d Review duration: 68m
    All Vouch review confidence checks passed
    Residual risk 0.25 = Change Risk (0.57) × (1 − Review Confidence (0.57)). 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%
    56%
    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 #10"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 24.7% · Safe to merge
    24.7%