Skip to content

feat(turnkey): sync the deposit policy of Turnkey accounts - #2146

Merged
jaredvu merged 2 commits into
mainfrom
kefan/deposit-policy-enforce-lifecycle
Nov 25, 2026
Merged

jaredvu merged 2 commits into
mainfrom
kefan/deposit-policy-enforce-lifecycle

Conversation

@Kefancao

@Kefancao Kefancao commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Changes

Adds setUpDepositPolicyEnforceLifecycle, which makes sure the deposits of a Turnkey account are bridged to the Injective account the app holds the key for.

Why. Deposits to a Turnkey account's deposit addresses are bridged by the indexer to Injective, and the app sends them on to dYdX from its Injective account. The indexer can only bridge within a policy the account has approved, and the policy names the Injective recipient. The indexer cannot work out the app's Injective address (it is a separate key from the same mnemonic), so the app has to name it. Only the user can approve the policy.

Needs dydxprotocol/v4-chain#3490, which makes the indexer take the address from the app.

What it does. For a connected Turnkey user whose wallets the app holds, once the indexer is ready:

  1. GET /v4/turnkey/depositPolicy/{dydxAddress}?injectiveAddress=…, with the address of the Injective wallet in localWalletManager. If nothing is required, it stops. This needs no Turnkey session.
  2. Checks that what the indexer asks to approve is exactly the expected policy for that Injective address (see below). If not, it stops and asks the user for nothing.
  3. With the user's Turnkey session:
    • EVM (Arbitrum, Ethereum, Optimism, Base, Avalanche): signs the typed data for each chain that needs it and sends the signatures, with the Injective address, to POST /v4/turnkey/depositPolicy. The indexer stores the address and bridges to it from then on.
    • Solana: creates the policy in the user's Turnkey organization and deletes policies for the earlier route or for another Injective account.
  4. Without a session, it sets a flag that shows a notification asking the user to sign in again. A Turnkey session lasts 15 minutes from sign-in, so most returning users will see this once. After they sign in, the sync runs with no further input.

This applies to existing accounts and to new ones: with the indexer change, sign-up no longer sets a policy, so a new account is synced here right after sign-up, while its session is live. The indexer does not know a new account until its address is uploaded, so an account that is not found, or a request that fails, is looked at again after 5, 15 and 60 seconds. An account is otherwise checked once per page load.

The app does not sign what the indexer sends as it is. The approvals are made without asking the user, so depositPolicyExpectations.ts builds the policy the app expects from its own Injective address and compares:

  • The response must be for the user's dYdX address, the Injective address the app named, and the EVM address of the user's Turnkey wallet.
  • EVM: the typed data must be a Kernel Enable for the user's own account on that chain, with no hook, the standard execute action, and validator data that is byte-for-byte the expected encoding: one call policy with exactly two permissions (approve USDC to Skip's CCTP relayer; bridge USDC through it to CCTP domain 29, to the app's Injective address, with the relayer's fee capped at 5 USDC) and one ECDSA session key.
  • Solana: the policy's condition must be exactly the expected string for that address, with effect ALLOW and a single approver.

If any part differs, nothing is signed or created for any chain. These values mirror the indexer's (call-policies.ts, policy-engine.ts) and have to change together with it.

Files

  • src/bonsai/lifecycles/depositPolicyEnforceLifecycle.ts: the lifecycle, registered in storeLifecycles.ts.
  • src/lib/turnkey/depositPolicy.ts: fetch, check, sign, submit.
  • src/lib/turnkey/depositPolicyExpectations.ts: the expected policy and the checks.
  • src/lib/turnkey/turnkeySession.ts: the signed-in user's Turnkey session for code outside React.
  • src/state/wallet.ts, src/constants/notifications.ts, src/hooks/useNotificationTypes.tsx: the flag and the sign-in notification (NotificationType.DepositPolicyUpdate).
  • src/lib/hdKeyManager.ts: localWalletManager's second wallet is the Injective one since deposits moved to Injective; its field and getter are renamed from …NobleWallet to …InjectiveWallet. The getter had no callers.

Notes for review

  • Injective address. The earlier version of this PR derived the recipient from the dYdX address, mirroring the indexer. That function is gone: the recipient is the address of the Injective wallet in localWalletManager, and the lifecycle only runs when the dYdX wallet held there is the account being shown.
  • Order of release. The indexer change has to be live first. Against an indexer without it, the app refuses the response (it is for another Injective address) and does nothing, without prompting the user.
  • Policy sync only. Nothing here moves funds on Injective.
  • Not yet run in a browser against a real Turnkey session. The app's fetch, check, sign and submit code was run against the indexer's HTTP app and policy engine locally, for an account set up for the earlier route and for a new one, with Turnkey stood in for. Signing with the IndexedDB session and createPolicy/deletePolicy from the browser client have only been exercised with a stand-in. Worth a pass on staging before release.
  • Copy is English only. The notification's title and body are hardcoded with a TODO: localize, as the Injective deposit notification is; the button uses the existing REAUTHENTICATE string. It opens the sign-out dialog, the same way revealing the recovery phrase does with an expired session.
  • Signature format. Turnkey returns the recovery value as 0/1; it is sent as 27/28, which is what the smart account's signature check accepts.

Testing

pnpm tsc, pnpm lint (no errors, no new warnings) and pnpm test (320 tests, 210 of them new) pass. The new tests cover:

  • the checks accepting the indexer's real request on all five chains and rejecting altered variants on each (other recipient, extra permission, other target or function, higher or uncapped fee, hook, other action, other domain or types, and so on), requests for another Injective address, plus the Solana policy. The fixture is a response generated by the indexer's own code;
  • the sync: what is signed and sent, the Solana policy calls, refusing everything when one request is unexpected, and error handling;
  • the lifecycle: syncing on entry, naming the app's Injective address, waiting for the account, its wallets and the indexer, ignoring other wallet types and accounts that are only being viewed, the sign-in flag being set and cleared, refusing before prompting, and the retries.

Issue

Deposits to a Turnkey account's deposit addresses are bridged by the indexer,
within a policy the account has approved. Accounts set up before deposits were
routed to Injective only permit the earlier route, so their deposits are not
bridged until the user approves the new one.

Add setUpDepositPolicyEnforceLifecycle, which checks the policy of a Turnkey
user when they enter the app and brings it up to date: it signs the approval
for each EVM chain and hands the signatures to the indexer, creates the Solana
policy in the user's Turnkey organization, and removes the policy of the
earlier route.

The approvals are made without asking the user, so nothing the indexer sends
is signed as it is. The app builds the policy it expects from the user's own
dYdX address and refuses the whole sync if a request differs from it.

Signing needs a Turnkey session, which lasts 15 minutes from sign-in. A user
who enters without one is shown a notification asking them to sign in again.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Kefancao
Kefancao requested a review from a team as a code owner November 23, 2026 21:47
@vercel

vercel Bot commented Nov 23, 2026

Copy link
Copy Markdown

You must have Developer access to commit code to dYdX Trading on Vercel. If you contact an administrator and receive Developer access, commit again to see your changes.

Learn more: https://vercel.com/docs/accounts/team-members-and-roles/access-roles#team-level-roles

Comment on lines +95 to +109
const selectDepositPolicyAccount = createAppSelector(
[
selectIsTurnkeyConnected,
getUserWalletAddress,
selectIndexerUrl,
selectIndexerReady,
getTurnkeyPrimaryWallet,
],
(
isTurnkeyConnected,
dydxAddress,
indexerUrl,
indexerReady,
primaryTurnkeyWallet
): DepositPolicyAccount | undefined => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you double check if there is also a selector somewhere to determine if the user can sign/actually connected vs impersonation mode.

i.e. should we be using something like selectTxAuthorizedAccount as well?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Kefancao did we see if this was necessary or not?

* Where an account's deposits are bridged to: the Injective account that shares its 20 address
* bytes with the dYdX account, as an `inj` address and as the 32-byte CCTP mint recipient.
*/
export function getInjectiveDepositRecipient(dydxAddress: string): {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this function won't work due to how Injective address derivation is done.

Using my test account as an example it converts dydx107a68…mcankv3clc -> inj107a68…mcan4ugcdh. The expected should be inj12x0u5s87…gkk2qvy

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we can do without using this function entirely. We have injectiveAddress derived in useAccounts and set in localWalletManager. Is there a way you can use localWalletManager as the piece to confirm account ownership and grab injectiveAddress?

Comment on lines +1429 to +1430
title: 'Sign in again to keep deposits working',
body: 'Your account needs a one-time update before new deposits to your deposit address can be processed. Sign in again to apply it.',

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

add TODO for localization

The deposit policy named the Injective account with the address bytes of the
dYdX account as the recipient of bridged deposits. That is not the account the
app holds the key for on Injective, so deposits would have arrived where the
app does not look.

The app now tells the indexer its own Injective address, taken from
localWalletManager, and approves bridging to that address only:

- The address is sent when the policy is fetched and again with the signatures,
  which is what makes the indexer bridge to it.
- The check of what the indexer asks for is built from that address. A response
  for another address is refused, including one from an indexer that derives the
  address itself.
- The lifecycle only runs for the account whose wallets the app holds, so not
  while another account is being viewed.
- The policy is checked before the user is asked to sign in, so a request that
  would be refused never prompts them.
- A new account is no longer given a policy at sign-up, so the lifecycle looks
  again shortly when the indexer does not know the account yet or a request
  fails.

localWalletManager's second wallet has been the Injective one since deposits
moved to Injective; its field and getter are renamed to say so.

Co-authored-by: Cursor <cursoragent@cursor.com>
// How long to wait before looking again at an account that could not be checked. A new account
// is one of these for a moment: the indexer does not know it until its address is uploaded,
// right after sign-up.
const RETRY_DELAYS_MS = [5_000, 15_000, 60_000];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: do we have exponential backoff or is that something that needs to be implemented (can pass on this). use TimeUnits

non-blocking

// An account the indexer does not know yet, or a request that failed, is looked at
// again shortly. A missing session waits for the user to sign in, and a refusal is not
// going to change.
const retryDelay = RETRY_DELAYS_MS[attempt];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

so we are using the length RETRY_DELAY_MS as the exit condition/max retry attempts as well?

@vercel

vercel Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
v4-staging Ready Ready Preview Nov 25, 2026 6:26pm UTC
v4-testnet Ready Ready Preview Nov 25, 2026 6:26pm UTC

Request Review

@jaredvu
jaredvu merged commit 6f74c55 into main Nov 25, 2026
11 checks passed
@jaredvu
jaredvu deleted the kefan/deposit-policy-enforce-lifecycle branch November 25, 2026 18:31

This branch was successfully deployed

2 active deployments
Preview – v4-staging — e37a570e Deployed Nov 25, 2026 by vercel[bot]
Preview – v4-testnet — e37a570e Deployed Nov 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants