Skip to content

fix(trade): check the signed transaction Privy or WalletConnect returns before broadcasting it - #849

Open
kome12 wants to merge 6 commits into
mainfrom
fix/privy-signed-tx-check
Open

kome12 wants to merge 6 commits into
mainfrom
fix/privy-signed-tx-check

Conversation

@kome12

@kome12 kome12 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Privy and WalletConnect sign remotely and hand back signed bytes, which the CLI then broadcast as they came back. This PR checks that those bytes are the transaction the CLI sent, signed by the wallet, on every remote-signer path. A mismatch fails with the new error SIGNED_TX_MISMATCH and nothing is broadcast. Execute treats it as terminal: it doesn't try the next quote, which would go to the same signer.

The checks (src/signed-tx-binding.js):

  • EVM:
    • Decodes the returned transaction: replay-protected legacy, type 1 or type 2.
    • Refuses blob and set-code transactions and non-canonical integers.
    • Recovers the sender (secp256k1) and compares the fields with what was sent.
  • Solana:
    • The message bytes must be identical, with the same signature count.
    • The wallet must be a required signer, and its signature slot must verify (ed25519).
    • The check uses the wallet's own slot: in an x402 payment the facilitator is the fee payer and the wallet signs slot 1.

Privy (src/privy.js): signEvmTransaction and signSolanaTransaction take the wallet address and check the response.

  • EVM: every field sent must match (chain, nonce, gas limit, fees, recipient, value, calldata, type 2, empty access list).
  • Solana: the same message, with the wallet's valid signature.
  • Callers:
    • trade execute swaps, approvals and allowance revokes
    • wallet send on Solana
    • limit-order deposit and cancel
    • x402 payments

WalletConnect (src/walletconnect-trading.js): when the wallet returns signed bytes, the send functions check them against the connected wallet.

  • EVM: chain, recipient, value, calldata and gas limit must match. The wallet still chooses nonce and fees.
  • Solana: the same message. A signature-only reply must verify against the transaction the CLI built.
  • The error says the wallet may have sent the transaction itself, so check its activity before retrying. In a swap the code is kept, not replaced by the generic ambiguous-send error.
  • Unchanged: a reply carrying only a hash, where the wallet broadcast itself.
  • Callers: swaps, approvals, allowance revokes, wallet send and limit orders.

Why

Every pre-sign check (target, intent binding, value, fee cap, simulation) runs on the transaction sent for signing. A signer response encoding a different transaction would have been broadcast unchecked. On WalletConnect, a gas limit lower than requested would make a swap revert and still burn fees.

Before / after

Signer returns a different transaction before after
Privy EVM swap, recipient and calldata swapped broadcast, reported as success refused, nothing broadcast
Privy EVM swap signed by another key broadcast refused
Privy approval with the spender swapped broadcast refused before any broadcast
Privy Solana swap, different message broadcast, reported as success refused
WalletConnect EVM swap, recipient swapped broadcast, reported as success refused
WalletConnect approval, spender swapped broadcast refused
WalletConnect EVM, different gas limit broadcast refused
WalletConnect Solana, different message broadcast refused
WalletConnect Solana signature that doesn't verify injected and broadcast refused

Why this isn't split

This PR is 1711 lines, over the usual 1000. It was split into three PRs: the shared checks (#848), Privy and WalletConnect (#850). Separately, each earlier part left the next part's paths open, so the review bot rated each one Critical or High. Together they close every remote-signer path in one step.

Of the 1711 lines, about 960 are tests:

  • unit tests for the checks
  • execute-level tests for each signer
  • existing Privy stubs changed to sign what they receive with a fixed test key, instead of returning placeholder bytes

Testing

  • signed-tx-binding.test.js: 32 unit tests.
    • EVM accepts: each supported type.
    • EVM refuses: the unsupported types, a wrong signer, each substituted field.
    • Solana accepts: legacy and v0 messages, a two-signer transaction where the wallet signs slot 1.
    • Solana refuses: substituted messages, a wrong signer, a missing or invalid signature.
  • privy-signed-tx.test.js and walletconnect-signed-tx.test.js: execute-level tests with honest controls and substituted responses (table above). The refusal tests fail without the fix.
  • Live, sign-only, against the real Privy API (nothing broadcast):
    • Accepted: EVM, Solana legacy, Solana v0, and a two-signer x402-style transaction.
    • Refused: a wrong-signer control.
  • npm test: 6399 passed. Lint and the error-code docs check are clean.
  • Not tested live: WalletConnect (no wallet connected). It's covered by the stubbed tests.

🤖 Generated with Claude Code

kome12 and others added 4 commits November 17, 2026 14:46
… it was sent

Adds src/signed-tx-binding.js, which compares the signed bytes a remote
signer (Privy, WalletConnect) returns with the transaction the CLI sent it:

- Solana: identical message bytes and signature count, the wallet is one of
  the required signers, and its signature slot verifies (ed25519).
- EVM: decode the signed transaction (replay-protected legacy, type 1 or
  type 2; blob and set-code transactions are refused), recover the sender,
  and compare it and the given fields with what was sent.

A mismatch is a new error code, SIGNED_TX_MISMATCH, which execute treats as
terminal. Nothing calls the checks yet; the Privy and WalletConnect changes
that use them follow separately. Also adds the shared test helpers they use:
honest test signers with fixed keys, and an execute harness.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Privy signs remotely and returns signed bytes, which the CLI broadcast as
they came back. Every pre-sign check had run on the transaction sent for
signing, so a response encoding a different transaction would have been
broadcast unchecked.

signEvmTransaction and signSolanaTransaction now take the wallet address
and refuse a response that is not the transaction sent, signed by that
wallet (SIGNED_TX_MISMATCH; nothing is broadcast). EVM: every field sent
must match. Solana: same message, and the wallet's signature slot verifies
(an x402 payment has the facilitator as fee payer, so the wallet signs a
later slot). Every caller passes the wallet: swaps, approvals and allowance
revokes, wallet send (Solana), limit orders and x402 payments.

Tests sign with fixed test keys instead of returning placeholder bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kome12 kome12 self-assigned this Nov 17, 2026
@kome12
kome12 marked this pull request as ready for review November 17, 2026 08:23
@kome12
kome12 changed the base branch from feat/remote-signer-tx-checks to main November 17, 2026 08:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/trading.js
}, { from: walletAddress, label: 'swap transaction' });
signedTransaction = signResult.data?.signed_transaction || signResult.signed_transaction;

} else if (chainType === 'solana') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 - High | WalletConnect signed transactions are not bound to the preflight-checked transaction

The new binding logic is only invoked from PrivyClient. In the WalletConnect trading path, the CLI validates txBase64 before asking the wallet to sign, but then directly assigns the returned signedTransaction and sends it to the Trading API without decoding it, comparing its message/fields, or verifying the expected wallet signature. A malicious, compromised, or buggy WalletConnect adapter can therefore substitute a different recipient, calldata, amount, or Solana message after the CLI's checks and before broadcast. The same gap exists for WalletConnect-backed wallet sends in transfer.js. This contradicts the changeset and error-code documentation claiming the protection covers WalletConnect.

@nansen-pr-reviewer

nansen-pr-reviewer Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

nansen-pr-reviewer summary

The diff adds strong binding checks for returned signed transaction bytes from Privy and WalletConnect, and covers the tested signing paths. However, WalletConnect still accepts hash-only responses from its send-and-broadcast path without verifying that the hash identifies the transaction the CLI preflighted, so a compromised or buggy adapter can still substitute a transaction while the CLI treats it as successful.

Findings by Severity

Severity Count
🟠 High 2

Deterministic check: failure — Found 2 high severity finding(s) (max: 0)

Risk: 4/5 (High) — raised by: complexity: 1522 added lines


Token usage: 124,705 input, 2,043 output, 40,436 cache read | Usage Guide

Cooldown: for the next 10 minutes (counting from when this review finished), new pushes to this PR will not trigger another review — the next push after the window expires will. Need a fresh review sooner? Comment @nansen-pr-reviewer[bot] re-review.

…ore broadcasting it

When a WalletConnect wallet returns signed bytes instead of broadcasting
itself, the CLI broadcast them as they came back, although every pre-sign
check had run on the transaction it sent the wallet.

The send functions now take the wallet address and refuse returned bytes
that are not signed by it or differ from what was sent (EVM: chain,
recipient, value, calldata and gas limit; the wallet still picks nonce and
fees. Solana: the same message). A signature-only Solana reply must verify.
The error is SIGNED_TX_MISMATCH and says the wallet may have sent the
transaction itself. A reply carrying only a hash is unchanged. Covers
swaps, approvals, allowance revokes, wallet send and limit orders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kome12 kome12 changed the title fix(privy): check the signed transaction Privy returns before using it fix(trade): check the signed transaction Privy or WalletConnect returns before broadcasting it Nov 17, 2026
if (parsed.signedTransaction) return { signedTransaction: parsed.signedTransaction };

throw new Error('Unexpected response from walletconnect send-transaction');
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 - High | WalletConnect hash-only responses remain unbound to the preflight-checked transaction

The new validation only runs when WalletConnect returns signed transaction bytes. When the wallet returns transactionHash or txHash, the function returns that hash immediately and never fetches or decodes the corresponding transaction, verifies its sender, or compares its chain, recipient, value, calldata, and gas limit with the transaction the CLI checked. Because send-transaction may broadcast through the wallet before returning, a compromised or buggy WalletConnect adapter can substitute a different transaction and report its hash; the CLI then marks the quote executed and proceeds as though the checked transaction was broadcast. The same unverified hash path is used by WalletConnect-backed EVM transfers and approvals.

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