Skip to content

feat(trade): add checks that a remote signer returned the transaction it was sent - #848

Open
kome12 wants to merge 2 commits into
mainfrom
feat/remote-signer-tx-checks
Open

kome12 wants to merge 2 commits into
mainfrom
feat/remote-signer-tx-checks

Conversation

@kome12

@kome12 kome12 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

Adds src/signed-tx-binding.js. It checks that the signed bytes a remote signer (Privy or WalletConnect) returns are the transaction the CLI sent it, signed by the expected wallet.

  • Solana (assertSolanaSignedMatches): the message bytes after the signature section are identical, the signature count is the same, the wallet is one of the required signers, and the wallet's signature slot verifies (ed25519). The check uses the wallet's slot, not slot 0, because in an x402 payment the facilitator is the fee payer and the wallet signs a later slot.
  • EVM (decodeSignedEvmTransaction, assertEvmSignedMatches):
    • 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 it, and each given field, with what was sent.

A mismatch throws the new error code SIGNED_TX_MISMATCH (added to src/error-codes.js, with docs/error-codes.md regenerated). trade execute treats it as terminal: it does not move on to the next quote, which would go to the same signer.

Also adds shared test helpers for the follow-up PRs:

  • fixtures/remote-signers.js: fixed test keys and signers that sign what a stub receives.
  • fixtures/execute-harness.js: quote builders, a network stub that records what reaches the execute endpoint, and an execute() wrapper.

Why

On the Privy and WalletConnect paths, every pre-sign check (target, intent binding, value, fee cap, simulation) runs on the unsigned transaction. The signed bytes the signer returns were then broadcast as they came back. A response encoding a different transaction would have been broadcast unchecked.

Part 1 of 3

Nothing calls these checks yet, so this PR changes no behaviour. The next two PRs wire them in and open once this one merges:

  • Privy: every Privy caller checks the returned transaction.
  • WalletConnect: every WalletConnect caller that receives signed bytes checks them.

Testing

  • src/__tests__/signed-tx-binding.test.js: 32 unit tests.
    • EVM accepts: type 2, type 1, replay-protected legacy.
    • EVM refuses: pre-155 legacy, blob and set-code transactions, non-canonical integers, 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, a changed signature count.
  • npm test: 6368 passed. npm run lint and npm run error-codes:check are clean.
  • Live check against the real Privy API (sign-only, nothing broadcast), via the full branch:
    • Accepted: EVM, Solana legacy, Solana v0, and a two-signer x402-style transaction.
    • Refused: a wrong-signer control.

No changeset in this PR. The user-facing changesets come with the Privy and WalletConnect PRs.

🤖 Generated with Claude Code

… 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>
Comment thread src/signed-tx-binding.js
throw new Error(`unsupported transaction type 0x${first.toString(16).padStart(2, '0')}`);
}

const UINT_FIELDS = ['chainId', 'nonce', 'gasLimit', 'value', 'maxFeePerGas', 'maxPriorityFeePerGas', 'gasPrice'];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔴 - Critical | Remote signer binding is never enforced before broadcast

The new assertEvmSignedMatches and assertSolanaSignedMatches functions are not imported or called from src/trading.js. The Privy branches accept signed_transaction and the WalletConnect branches accept signedTransaction/signature data, then proceed to executeTransaction or wallet broadcast without comparing the returned bytes to the transaction that was preflighted and sent for signing. A compromised, buggy, or malicious remote signer could therefore substitute a different recipient, calldata, value, nonce, or Solana message and the CLI would broadcast it. The tests only validate the standalone helpers and do not cover their integration into the real signing paths.

Comment thread src/error-codes.js
BROADCAST_FAILED: { group: 'trade', description: 'The transaction could not be broadcast.' },
TX_REVERTED: { group: 'trade', description: 'The transaction reverted or failed on chain.' },
TXHASH_MISMATCH: { group: 'trade', description: 'The RPC returned a different transaction hash than the one signed.' },
SIGNED_TX_MISMATCH: { group: 'trade', description: 'The remote signer (Privy or WalletConnect) returned a transaction that differs from the one checked; the CLI did not broadcast it.' },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 - Low | Missing changeset for a user-facing trading security change

This PR changes trade execution behavior and adds a new public SIGNED_TX_MISMATCH error code, but no .changeset file is included. Per the repository instructions, user-facing bug fixes and output/error changes require a changeset so the published package and changelog reflect the change.

@nansen-pr-reviewer

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

Copy link
Copy Markdown

nansen-pr-reviewer summary

The change adds signed-transaction binding helpers and a new error code, but the helpers are not integrated into any Privy or WalletConnect signing path. Remote signers can still return substituted transactions that proceed to broadcast, leaving the central security objective unenforced. The user-facing error-code change also lacks the required changeset.

Findings by Severity

Severity Count
🔴 Critical 2
🔵 Low 2

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

Risk: 5/5 (Critical) — raised by: complexity: 787 added lines

Findings not posted inline


Token usage: 81,001 input, 1,530 output, 13,398 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.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/error-codes.js
BROADCAST_FAILED: { group: 'trade', description: 'The transaction could not be broadcast.' },
TX_REVERTED: { group: 'trade', description: 'The transaction reverted or failed on chain.' },
TXHASH_MISMATCH: { group: 'trade', description: 'The RPC returned a different transaction hash than the one signed.' },
SIGNED_TX_MISMATCH: { group: 'trade', description: 'The remote signer (Privy or WalletConnect) returned a transaction that differs from the one checked; the CLI did not broadcast it.' },

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 - Low | Missing changeset for the user-facing trading security change

This change adds the public SIGNED_TX_MISMATCH error code and changes trade execution safety behavior, but no .changeset file is included. Repository instructions require a changeset for user-facing bug fixes and output/error changes, so the published package changelog will not describe this change.

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.

2 participants