forked from cartridge-gg/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: expose signExecuteFromOutside via keychain RPC + ControllerAccount.getOutsideTransaction #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
loothero
wants to merge
1
commit into
main
Choose a base branch
from
feat/expose-sign-execute-from-outside
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
198 changes: 198 additions & 0 deletions
198
packages/controller/src/__tests__/getOutsideTransaction.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| import { AsyncMethodReturns } from "@cartridge/penpal"; | ||
| import { Call } from "starknet"; | ||
|
|
||
| import ControllerAccount from "../account"; | ||
| import { ControllerOutsideExecutionUnsupported } from "../errors"; | ||
| import { | ||
| Keychain, | ||
| Modal, | ||
| ResponseCodes, | ||
| SignedOutsideExecution, | ||
| } from "../types"; | ||
| import type BaseProvider from "../provider"; | ||
| import SessionAccount from "../session/account"; | ||
|
|
||
| // The session WASM is only consumed at construction time; mock it (jest | ||
| // hoists this above the imports) so importing SessionAccount in the node | ||
| // test environment does not try to instantiate WebAssembly. | ||
| jest.mock("@cartridge/controller-wasm/session", () => ({ | ||
| CartridgeSessionAccount: { newAsRegistered: jest.fn() }, | ||
| })); | ||
|
|
||
| const ADDRESS = | ||
| "0x05f1f0a38429dcab9ffd8a786c0d827e84c1cbd8f60243e6d25d26589bea4189"; | ||
| const RPC_URL = "https://api.cartridge.gg/x/starknet/mainnet/rpc/v0_10"; | ||
|
|
||
| // Envelope exactly as the controller WASM serializes it: ANY_CALLER short | ||
| // string felt, execute_after 0, execute_before now+600 (numbers), a random | ||
| // [channel, 0x1] nonce pair, fixed-length hex felts, and the call selector | ||
| // rendered as a decimal string. | ||
| const WASM_SIGNED: SignedOutsideExecution = { | ||
| outside_execution: { | ||
| caller: | ||
| "0x00000000000000000000000000000000000000000000414e595f43414c4c4552", | ||
| execute_after: 0, | ||
| execute_before: 1765000600, | ||
| calls: [ | ||
| { | ||
| contractAddress: | ||
| "0x02d970e9ae65ff80ff3e39268279e6bb271fa3c50c025c6b66c032728b065125", | ||
| entrypoint: | ||
| "1488470508959902113554091917598462812858669094441227922119121426064847696246", | ||
| calldata: [ | ||
| "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
| ], | ||
| }, | ||
| ], | ||
| nonce: [ | ||
| "0x06d8e1c4f7c1a90e0a4f5a9bf26c0a6e2f9c87bb39c0a45f02c6f6a0e6f0a111", | ||
| "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
| ], | ||
| }, | ||
| signature: [ | ||
| "0x0000000000000000000000000000000000000000000000000000000000000aaa", | ||
| "0x0000000000000000000000000000000000000000000000000000000000000bbb", | ||
| ], | ||
| }; | ||
|
|
||
| const CALLS: Call[] = [ | ||
| { | ||
| contractAddress: | ||
| "0x02d970e9ae65ff80ff3e39268279e6bb271fa3c50c025c6b66c032728b065125", | ||
| entrypoint: "request_random", | ||
| calldata: ["0x1"], | ||
| }, | ||
| ]; | ||
|
|
||
| const OPTIONS = { | ||
| caller: "0x123", | ||
| execute_after: 100, | ||
| execute_before: 160, | ||
| }; | ||
|
|
||
| function makeAccount(keychain: Partial<Record<keyof Keychain, unknown>>) { | ||
| // WalletAccount's constructor only consumes `walletProvider.on`. | ||
| const provider = { on: jest.fn(), off: jest.fn() } as unknown as BaseProvider; | ||
| const modal = { open: jest.fn(), close: jest.fn() } as unknown as Modal; | ||
| return new ControllerAccount( | ||
| provider, | ||
| RPC_URL, | ||
| ADDRESS, | ||
| keychain as unknown as AsyncMethodReturns<Keychain>, | ||
| {}, | ||
| modal, | ||
| ); | ||
| } | ||
|
|
||
| describe("ControllerAccount.getOutsideTransaction", () => { | ||
| test("returns a contract-shaped V3 outside transaction", async () => { | ||
| const signExecuteFromOutside = jest.fn().mockResolvedValue(WASM_SIGNED); | ||
| const account = makeAccount({ signExecuteFromOutside }); | ||
|
|
||
| const result = await account.getOutsideTransaction(OPTIONS, CALLS); | ||
|
|
||
| expect(signExecuteFromOutside).toHaveBeenCalledTimes(1); | ||
| expect(signExecuteFromOutside).toHaveBeenCalledWith(CALLS); | ||
|
|
||
| expect(result).toEqual({ | ||
| outsideExecution: { | ||
| caller: WASM_SIGNED.outside_execution.caller, | ||
| nonce: WASM_SIGNED.outside_execution.nonce, | ||
| execute_after: 0, | ||
| execute_before: 1765000600, | ||
| calls: [ | ||
| { | ||
| to: WASM_SIGNED.outside_execution.calls[0].contractAddress, | ||
| selector: WASM_SIGNED.outside_execution.calls[0].entrypoint, | ||
| calldata: WASM_SIGNED.outside_execution.calls[0].calldata, | ||
| }, | ||
| ], | ||
| }, | ||
| signature: WASM_SIGNED.signature, | ||
| signerAddress: ADDRESS, | ||
| version: "3", | ||
| }); | ||
|
|
||
| // The V3 nonce pair must be preserved verbatim, not flattened. | ||
| expect(result.outsideExecution.nonce).toEqual( | ||
| WASM_SIGNED.outside_execution.nonce, | ||
| ); | ||
| // Window fields stay numbers, exactly as the WASM returned them. | ||
| expect(typeof result.outsideExecution.execute_after).toBe("number"); | ||
| expect(typeof result.outsideExecution.execute_before).toBe("number"); | ||
| }); | ||
|
|
||
| test("normalizes a single call to an array", async () => { | ||
| const signExecuteFromOutside = jest.fn().mockResolvedValue(WASM_SIGNED); | ||
| const account = makeAccount({ signExecuteFromOutside }); | ||
|
|
||
| await account.getOutsideTransaction(OPTIONS, CALLS[0]); | ||
|
|
||
| expect(signExecuteFromOutside).toHaveBeenCalledWith([CALLS[0]]); | ||
| }); | ||
|
|
||
| test("throws the typed error when the keychain lacks signExecuteFromOutside", async () => { | ||
| // Penpal proxies only contain methods the connected keychain registered; | ||
| // a pre-upgrade keychain (e.g. production x.cartridge.gg) omits it. | ||
| const account = makeAccount({ execute: jest.fn() }); | ||
|
|
||
| const promise = account.getOutsideTransaction(OPTIONS, CALLS); | ||
|
|
||
| await expect(promise).rejects.toMatchObject({ | ||
| name: "ControllerOutsideExecutionUnsupported", | ||
| }); | ||
| await expect(promise).rejects.toBeInstanceOf( | ||
| ControllerOutsideExecutionUnsupported, | ||
| ); | ||
| await expect(promise).rejects.toThrow( | ||
| /does not expose signExecuteFromOutside/, | ||
| ); | ||
| }); | ||
|
|
||
| test("maps a keychain NOT_CONNECTED reply to the typed error", async () => { | ||
| const signExecuteFromOutside = jest.fn().mockResolvedValue({ | ||
| code: ResponseCodes.NOT_CONNECTED, | ||
| message: "Controller not connected", | ||
| }); | ||
| const account = makeAccount({ signExecuteFromOutside }); | ||
|
|
||
| await expect( | ||
| account.getOutsideTransaction(OPTIONS, CALLS), | ||
| ).rejects.toMatchObject({ | ||
| name: "ControllerOutsideExecutionUnsupported", | ||
| }); | ||
| }); | ||
|
|
||
| test("surfaces other keychain errors as plain errors", async () => { | ||
| const signExecuteFromOutside = jest.fn().mockResolvedValue({ | ||
| code: ResponseCodes.ERROR, | ||
| message: "signing failed", | ||
| error: { code: 69, message: "signing failed", data: {} }, | ||
| }); | ||
| const account = makeAccount({ signExecuteFromOutside }); | ||
|
|
||
| const promise = account.getOutsideTransaction(OPTIONS, CALLS); | ||
|
|
||
| await expect(promise).rejects.toThrow("signing failed"); | ||
| await expect(promise).rejects.not.toMatchObject({ | ||
| name: "ControllerOutsideExecutionUnsupported", | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("SessionAccount.getOutsideTransaction", () => { | ||
| test("always throws the typed error (no recursion into starknet.js)", async () => { | ||
| // Invoke via the prototype: the override must fail fast regardless of | ||
| // instance state, so no constructed session (or WASM) is required. | ||
| const promise = SessionAccount.prototype.getOutsideTransaction.call( | ||
| Object.create(SessionAccount.prototype), | ||
| ); | ||
|
|
||
| await expect(promise).rejects.toMatchObject({ | ||
| name: "ControllerOutsideExecutionUnsupported", | ||
| }); | ||
| await expect(promise).rejects.toThrow( | ||
| /cannot sign SNIP-9 outside executions/, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
responseis null or undefined, or if the returned object does not containoutside_execution, destructuring or accessing its properties will throw aTypeErrorat runtime. Adding defensive checks forresponseandoutside_executionensures the application handles unexpected or malformed RPC responses gracefully.