Repository navigation
feat(turnkey): sync the deposit policy of Turnkey accounts - #2146
Conversation
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>
|
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 |
| const selectDepositPolicyAccount = createAppSelector( | ||
| [ | ||
| selectIsTurnkeyConnected, | ||
| getUserWalletAddress, | ||
| selectIndexerUrl, | ||
| selectIndexerReady, | ||
| getTurnkeyPrimaryWallet, | ||
| ], | ||
| ( | ||
| isTurnkeyConnected, | ||
| dydxAddress, | ||
| indexerUrl, | ||
| indexerReady, | ||
| primaryTurnkeyWallet | ||
| ): DepositPolicyAccount | undefined => { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
@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): { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
| 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.', |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
so we are using the length RETRY_DELAY_MS as the exit condition/max retry attempts as well?
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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:
GET /v4/turnkey/depositPolicy/{dydxAddress}?injectiveAddress=…, with the address of the Injective wallet inlocalWalletManager. If nothing is required, it stops. This needs no Turnkey session.POST /v4/turnkey/depositPolicy. The indexer stores the address and bridges to it from then on.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.tsbuilds the policy the app expects from its own Injective address and compares:Enablefor the user's own account on that chain, with no hook, the standardexecuteaction, 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.ALLOWand 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 instoreLifecycles.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…NobleWalletto…InjectiveWallet. The getter had no callers.Notes for review
localWalletManager, and the lifecycle only runs when the dYdX wallet held there is the account being shown.createPolicy/deletePolicyfrom the browser client have only been exercised with a stand-in. Worth a pass on staging before release.TODO: localize, as the Injective deposit notification is; the button uses the existingREAUTHENTICATEstring. It opens the sign-out dialog, the same way revealing the recovery phrase does with an expired session.Testing
pnpm tsc,pnpm lint(no errors, no new warnings) andpnpm test(320 tests, 210 of them new) pass. The new tests cover:Issue