Skip to content

feat(core): App Connect v2 client library for wallet sign-in - #656

Merged
PastaPastaPasta merged 2 commits into
masterfrom
feat/app-connect-rust
Nov 22, 2026
Merged

PastaPastaPasta merged 2 commits into
masterfrom
feat/app-connect-rust

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Owner

Problem

The wallet-primary plan (docs/design/wallet-primary.md, item 7) needs dg to offer "sign in with DashPay". The terminal would show a QR code, the phone wallet would approve it, and the CLI would read the answer from Platform. forge-web already does this with the vendored TypeScript App Connect v2 module. The Rust side had nothing: it could not build the request, open the wallet's encrypted answer, or apply the verifier and Forge rules to the granted key.

What this adds

A library-only forge_core::app_connect module that ports the vendored TS (forge-web/lib/vendor/platform-auth/app-connect, platform-auth a627708d) and Forge's use of it (forge-web/lib/auth/app-connect-v2.ts):

  • request: canonical encoding and decoding of the v2 request: version, app ephemeral key, appId, label, and TLVs in ascending order. Unknown even TLVs are refused, unknown odd TLVs are skipped, and millisecond values above 2^53−1 are refused. Also the request hash, the request-age check, and the dash-key:<base58>?n=…&v=2 URI builder (no cb).
  • crypto: ephemeral keys; the HKDF-SHA256 session key over ECDH; the AAD (dash:app-connect:v2 ‖ hash160(appEphPub) ‖ ownerId); and AES-256-GCM opening of the slot payload with its 28+32n framing. Secrets are zeroized and erased after use.
  • verify: the verifier rules: bounds, purpose, level, limits, primary binding, the one-hour expiry margin, budgets, and the retryable set. On-chain reads sit behind the small IdentityPort trait.
  • poll: the ResponsePort trait, paginated candidate listing and the poll cadence.
  • forge:
    • Forge's request: one contractGroup AUTHENTICATION/HIGH binding on the dash-forge group, with a budget and expiry; appId forge-core; label "Dash Forge"; a CONNECTION_ID TLV and no ENC binding.
    • await_forge_grant, a port of awaitV2Grant: a 3 s settle window, ambiguity when a second wallet answers, a not-an-answer cache, final renewal refusals, and expiry after 10 minutes.
    • The Forge grant rules: exactly one group key, AUTH/HIGH, group bounds, limits, budget left, and a valid secret.
    • The result as a WalletGrant that exports as WIF or dfk1.
  • platform/app_connect.rs: both ports implemented on PlatformClient. Identity keys and remaining budgets are proved reads. loginKeyResponse is queried on the (appEphemeralPubKeyHash, $ownerId) index of contract H8F9mP1BM55TE1ShsxPZHzhyinaMdY9bMmP85mkDhcJJ.

No new crates: bs58 0.5 and ripemd 0.1 were already in Cargo.lock, so the lock diff only adds edges. cargo deny --locked check passes.

Out of scope / follow-ups

  • No dg command and no CLI behaviour change. The dg sign-in flow is plan item 8.
  • Signing with a wallet key: released wallets grant ECDSA_HASH160 auth keys, but WriteEngine::select_matching_key accepts only ECDSA_SECP256K1. That must be widened before item 8 can sign with a wallet grant. This is noted on WalletGrant::key_type.
  • IdentityPort::contract_keys (ENC bindings) returns "not supported". Forge requests no encryption key, and the SDK has no proved contract-keys fetch.
  • Left to dg: the chain clock and the protocol-version gate (chain_tip(), refresh_protocol_version()). Not ported: PollTrigger, cb callbacks and URI parsing.
  • Divergence from the TS: an answer with more slots than bindings is refused as ExtraKeys, and it still counts as a responder for ambiguity. The TS verifier lets it fall through, so a wallet can answer twice without the client noticing. This is worth fixing upstream in platform-auth.

Testing

  • TS vectors: vectors.json from platform-auth a627708d is loaded directly from forge-web/lib/vendor. The Rust output matches it byte for byte: request bytes, URIs, request hash, session key, AAD, sealed payload and slot keys.
  • Forge vectors: app_connect/forge_vectors.json was generated by forge_vectors.ts with the vendored TS module. It covers Forge's own login and renewal requests (bytes, URIs, request hash) and a one-key answer, which the Rust code decodes, opens and accepts.
  • Ported TS cases: request.test, the verifier tables and poller.test, plus the poll-loop scenarios (settle, ambiguity, copied answers, renewal, expiry, read errors), all on paused tokio time. cargo test -p forge-core --lib app_connect: 67 pass.
  • Live testnet: tests/live_app_connect.rs (ignored by default) reads real wallet answers by request hash and reads the deployer's keys and budgets through the port. It passed locally.
  • cargo fmt --all and cargo clippy -p forge-core --all-targets -- -D warnings are clean.

🤖 Generated with Claude Code

PastaPastaPasta and others added 2 commits November 22, 2026 17:28
A Rust port of the App Connect v2 module forge-web vendors from
platform-auth (a627708d), so dg can later sign in with a phone wallet:
the request codec and dash-key: URI, the response crypto (ECDH, HKDF,
AES-256-GCM), the verifier, candidate paging and the poll loop, plus
Forge's own request (one AUTHENTICATION/HIGH key bound to the dash-forge
group, with a budget and an expiry) and rules. The result is a
WalletGrant that dg can store as a dfk1: limited key.

Chain reads sit behind two small ports; PlatformClient implements both.
Tests reproduce platform-auth's vectors.json and a Forge-shaped vector
made by the TypeScript module byte for byte, and port the TS verifier
and poller cases. An ignored live test reads real testnet answers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Count an answer with more keys than bindings as a responder, so it
  cannot be dropped silently while a second answer settles.
- Erase ephemeral and granted secret keys after use.
- Report the last read error when a login expires.
- Refuse a granted key whose secret is not a valid secp256k1 key.
- Make LoginRequest read-only and note that released wallets grant
  ECDSA_HASH160 keys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 22, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 48 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 42055b64-a271-44c5-a510-f9895f9fb032
Commits

Reviewing files that changed from the base of the PR and between 2a865f3 and 7bd9492.

Files ignored due to path filters: 1

  • Cargo.lock is excluded by !**/*.lock
Files selected for processing: 14

  • crates/forge-core/Cargo.toml
  • crates/forge-core/src/app_connect/crypto.rs
  • crates/forge-core/src/app_connect/forge.rs
  • crates/forge-core/src/app_connect/forge_vectors.json
  • crates/forge-core/src/app_connect/forge_vectors.ts
  • crates/forge-core/src/app_connect/mod.rs
  • crates/forge-core/src/app_connect/poll.rs
  • crates/forge-core/src/app_connect/request.rs
  • crates/forge-core/src/app_connect/verify.rs
  • crates/forge-core/src/lib.rs
  • crates/forge-core/src/platform/app_connect.rs
  • crates/forge-core/src/platform/identity.rs
  • crates/forge-core/src/platform/mod.rs
  • crates/forge-core/tests/live_app_connect.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PastaPastaPasta PastaPastaPasta left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve

I reviewed this independently at 7bd94925, checking it against the vendored TS (app-connect/{request,crypto,verify,poller}.ts) and forge-web/lib/auth/app-connect-v2.ts. I found nothing blocking. Everything below is a nit or a note.

What I checked

  • cargo test -p forge-core --lib app_connect: 67 pass. cargo clippy -p forge-core --all-targets -- -D warnings: clean. cargo deny --locked check: advisories, bans, licenses and sources all ok. The Cargo.lock diff only adds the bs58 and ripemd edges to forge-core; both crates were already locked.
  • An independent vector. I generated it myself with the vendored TS (npx tsx, @noble/hashes 2.4.0, @noble/secp256k1 3.2.0) and ran it against the Rust through a scratch integration test, since deleted. The request has 3 auth bindings (group CRITICAL with budget 2^64−1 and expiry 2^53−1, singleContract, documentType a_B-9 with expiry 1), 2 enc bindings (ENC and DEC), IDENTITY_HINT, CONNECTION_ID, MODE=renew and a non-ASCII label (Revü ✓ 🚀). The answer is a 3-slot payload. Results:
    • Rust decode→encode round-trips the TS bytes exactly.
    • An appended unknown odd TLV 0x07 is skipped.
    • The request hash and URI body match.
    • crypto::open recovers the 3 TS-sealed slots.
  • Crypto matches crypto.ts.
    • The KDF is the ECDH x-coordinate (shared_secret_point[..32]) fed to HKDF-SHA256 with salt dash:key-exchange:v1 and info app-connect:v2.
    • The AAD is dash:app-connect:v2 ‖ hash160(appEphPub) ‖ ownerId.
    • Framing (28+32n, 1≤n≤17) is checked before decryption.
    • The app side only decrypts. seal_with exists only under cfg(test)/feature = "vectors", so the app never encrypts and cannot reuse a nonce.
    • The ephemeral key comes from getrandom, with rejection sampling.
    • The session key, ECDH point and plaintext are Zeroizing, and aes-gcm has its zeroize feature. SecretKey temporaries go through non_secure_erase.
    • SecretBytes's Debug is redacted, and LoginRequest, Grant, GrantedKey and WalletGrant only print a secret through it.
  • The verifier is rule-for-rule verify.ts. It checks:
    • the hint;
    • the key on the answering identity, with no ambiguous key-data match;
    • not disabled, not MASTER, purpose, bounded;
    • exact bounds equality against a binding;
    • the level rule (group: CRITICAL or HIGH, never more privileged than asked; everything else exactly HIGH);
    • the key type;
    • that a limit is never dropped (group, or a requested budget, needs a budget; a requested expiry needs an expiry);
    • the expiry margin, as e <= now + margin (equivalent to TS e - now <= margin);
    • duplicate key or duplicate binding;
    • that the primary binding is granted;
    • that the encryption key is current;
    • budget unknown or exhausted;
    • the same retryable set.
    • Using the matches index as the slot index is correct, because every accepted slot is pushed.
  • The Forge rules (forge_grant) require exactly one auth key, no enc keys, the group role, AUTHENTICATION/HIGH exactly, bounds equal to the dash-forge group, a total budget and an expiry, and a remaining budget > 0. TS grantOf re-reads the key through findWalletKey. Rust checks the same properties on the verifier's proved read from the same round, which I consider equivalent.
  • The poll loop is awaitV2Grant.
    • responders is cumulative.
    • An answer that opens counts as a responder, whether it is accepted, refused, or hits a read error past decryption.
    • The not-answer set is Decrypt, Framing and HintMismatch, cached by content hash.
    • Only complete rounds count towards the settle window.
    • A truncated read is TooManyAnswers.
    • With a hint, a refusal is final at once.
    • chain_now = chain time at creation + local elapsed time.
  • Request binding and response source. The query is pinned to contract H8F9…, doc type loginKeyResponse, and appEphemeralPubKeyHash == hash160(appEphPub). The AAD binds the ciphertext to that hash and to $ownerId, so:
    • a copied answer fails to open;
    • a node that knows only the hash cannot forge an answer that opens.
      Identity::fetch, IdentityKeysRemainingBudgets::fetch and Document::fetch_many are SDK proved reads, so a node can neither invent key state nor drop a second answerer from the page.

On ExtraKeys (stricter than the TS): sound. Such an answer has already opened under this request's key and its owner's AAD, so it can only come from someone who saw the QR code. Counting it as a responder only makes the client more conservative. A QR viewer could already force Ambiguous with any answer that opens, so this gives an attacker nothing new, and it closes the TS gap where such an answer is dropped as framing/not-an-answer. One behaviour change from the TS: with a hint, an ExtraKeys answer from the hinted identity is a final Refused instead of being ignored. That is reasonable. +1 on fixing it upstream in platform-auth.

Nits (none blocking)

  1. crates/forge-core/src/app_connect/mod.rs:122, nit. SecretBytes derives PartialEq, a non-constant-time comparison, and that carries over into Grant, GrantedKey and WalletGrant. Nothing compares secrets today, but nothing stops a later caller (e.g. a dg "same key as stored?" check) from doing so. Consider dropping PartialEq from the secret-holding types, or comparing with subtle.
  2. crates/forge-core/src/platform/identity.rs:1281, nit. wif_from_bytes builds a secp256k1::SecretKey that is never erased, unlike with_secret_key in app_connect/mod.rs. random_secret's validity probe (app_connect/mod.rs:184) also leaves an un-erased SecretKey temporary. Minor, given that the WIF string itself is a long-lived copy.
  3. crates/forge-core/src/app_connect/poll.rs:70, note. When a port ignores the cursor, the loop stops with truncated: false, so the round counts as a complete read. This matches poller.ts, and it is safe with the proved PlatformClient port. A test fake or a future unproved port would quietly turn "misbehaving" into "complete". Returning an error there would be stricter.
  4. Follow-up already noted in the PR: wallet grants are ECDSA_HASH160, and WriteEngine::select_matching_key only picks ECDSA_SECP256K1. Item 8 must widen that before dg can sign with a grant.

🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@PastaPastaPasta
PastaPastaPasta merged commit b1b82c5 into master Nov 22, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant