Repository navigation
feat(core): App Connect v2 client library for wallet sign-in - #656
Conversation
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]>
|
Warning Review limit reachedYou'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. View limit details
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. |
PastaPastaPasta
left a comment
There was a problem hiding this comment.
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. TheCargo.lockdiff only adds thebs58andripemdedges 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, documentTypea_B-9with 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→encoderound-trips the TS bytes exactly. - An appended unknown odd TLV 0x07 is skipped.
- The request hash and URI body match.
crypto::openrecovers the 3 TS-sealed slots.
- Rust
- Crypto matches
crypto.ts.- The KDF is the ECDH x-coordinate (
shared_secret_point[..32]) fed to HKDF-SHA256 with saltdash:key-exchange:v1and infoapp-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_withexists only undercfg(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, andaes-gcmhas itszeroizefeature.SecretKeytemporaries go throughnon_secure_erase. SecretBytes's Debug is redacted, andLoginRequest,Grant,GrantedKeyandWalletGrantonly print a secret through it.
- The KDF is the ECDH x-coordinate (
- 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 TSe - 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
matchesindex 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. TSgrantOfre-reads the key throughfindWalletKey. Rust checks the same properties on the verifier's proved read from the same round, which I consider equivalent. - The poll loop is
awaitV2Grant.respondersis 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 typeloginKeyResponse, andappEphemeralPubKeyHash == 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::fetchandDocument::fetch_manyare 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)
crates/forge-core/src/app_connect/mod.rs:122, nit.SecretBytesderivesPartialEq, a non-constant-time comparison, and that carries over intoGrant,GrantedKeyandWalletGrant. Nothing compares secrets today, but nothing stops a later caller (e.g. a dg "same key as stored?" check) from doing so. Consider droppingPartialEqfrom the secret-holding types, or comparing withsubtle.crates/forge-core/src/platform/identity.rs:1281, nit.wif_from_bytesbuilds asecp256k1::SecretKeythat is never erased, unlikewith_secret_keyinapp_connect/mod.rs.random_secret's validity probe (app_connect/mod.rs:184) also leaves an un-erasedSecretKeytemporary. Minor, given that the WIF string itself is a long-lived copy.crates/forge-core/src/app_connect/poll.rs:70, note. When a port ignores the cursor, the loop stops withtruncated: false, so the round counts as a complete read. This matchespoller.ts, and it is safe with the provedPlatformClientport. A test fake or a future unproved port would quietly turn "misbehaving" into "complete". Returning an error there would be stricter.- Follow-up already noted in the PR: wallet grants are
ECDSA_HASH160, andWriteEngine::select_matching_keyonly picksECDSA_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.
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_connectmodule 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 thedash-key:<base58>?n=…&v=2URI builder (nocb).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 smallIdentityPorttrait.poll: theResponsePorttrait, paginated candidate listing and the poll cadence.forge:await_forge_grant, a port ofawaitV2Grant: a 3 s settle window, ambiguity when a second wallet answers, a not-an-answer cache, final renewal refusals, and expiry after 10 minutes.WalletGrantthat exports as WIF or dfk1.platform/app_connect.rs: both ports implemented onPlatformClient. Identity keys and remaining budgets are proved reads.loginKeyResponseis queried on the(appEphemeralPubKeyHash, $ownerId)index of contractH8F9mP1BM55TE1ShsxPZHzhyinaMdY9bMmP85mkDhcJJ.No new crates:
bs580.5 andripemd0.1 were already inCargo.lock, so the lock diff only adds edges.cargo deny --locked checkpasses.Out of scope / follow-ups
WriteEngine::select_matching_keyaccepts only ECDSA_SECP256K1. That must be widened before item 8 can sign with a wallet grant. This is noted onWalletGrant::key_type.IdentityPort::contract_keys(ENC bindings) returns "not supported". Forge requests no encryption key, and the SDK has no proved contract-keys fetch.chain_tip(),refresh_protocol_version()). Not ported:PollTrigger,cbcallbacks and URI parsing.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
vectors.jsonfrom platform-auth a627708d is loaded directly fromforge-web/lib/vendor. The Rust output matches it byte for byte: request bytes, URIs, request hash, session key, AAD, sealed payload and slot keys.app_connect/forge_vectors.jsonwas generated byforge_vectors.tswith 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.cargo test -p forge-core --lib app_connect: 67 pass.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 --allandcargo clippy -p forge-core --all-targets -- -D warningsare clean.🤖 Generated with Claude Code