From dfff5826002c2a862429842ced6c8b4501a04494 Mon Sep 17 00:00:00 2001 From: broody Date: Tue, 20 Aug 2024 08:38:56 -0700 Subject: [PATCH] Revert "Skip CreateSession if no policy is provided (#592)" This reverts commit a91431d493390d17e5d2c8162e20d6222d509d80. --- .../components/providers/StarknetProvider.tsx | 59 ++++++++++--------- packages/connector/src/index.ts | 6 +- packages/controller/src/index.ts | 48 ++++++--------- packages/controller/src/types.ts | 9 ++- .../keychain/src/components/connect/Login.tsx | 4 +- packages/keychain/src/hooks/connection.tsx | 2 +- packages/keychain/src/pages/index.tsx | 2 +- .../keychain/src/utils/connection/connect.ts | 13 +--- .../keychain/src/utils/connection/types.ts | 2 +- 9 files changed, 62 insertions(+), 83 deletions(-) diff --git a/examples/starknet-react-next/src/components/providers/StarknetProvider.tsx b/examples/starknet-react-next/src/components/providers/StarknetProvider.tsx index 1c0bb841b..ca8268c0e 100644 --- a/examples/starknet-react-next/src/components/providers/StarknetProvider.tsx +++ b/examples/starknet-react-next/src/components/providers/StarknetProvider.tsx @@ -25,8 +25,8 @@ const ETH_TOKEN_ADDRESS = // const PAPER_TOKEN_ADDRESS = // "0x0410466536b5ae074f7fea81e5533b8134a9fa08b3dd077dd9db08f64997d113"; -const cartridge = new CartridgeConnector({ - policies: [ +const cartridge = new CartridgeConnector( + [ { target: ETH_TOKEN_ADDRESS, method: "approve", @@ -50,34 +50,35 @@ const cartridge = new CartridgeConnector({ method: "allowance", }, ], - url: - !process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL || - process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL.split(".")[0] === - "cartridge-starknet-react-next" - ? process.env.XFRAME_URL - : "https://" + - (process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL ?? "").replace( - "cartridge-starknet-react-next", - "keychain", - ), - - rpc: process.env.NEXT_PUBLIC_RPC_SEPOLIA, - paymaster: { - caller: shortString.encodeShortString("ANY_CALLER"), + { + url: + !process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL || + process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL.split(".")[0] === + "cartridge-starknet-react-next" + ? process.env.XFRAME_URL + : "https://" + + (process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL ?? "").replace( + "cartridge-starknet-react-next", + "keychain", + ), + rpc: process.env.NEXT_PUBLIC_RPC_SEPOLIA, + paymaster: { + caller: shortString.encodeShortString("ANY_CALLER"), + }, + // theme: "dope-wars", + // colorMode: "light" + // prefunds: [ + // { + // address: ETH_TOKEN_ADDRESS, + // min: "300000000000000", + // }, + // { + // address: PAPER_TOKEN_ADDRESS, + // min: "100", + // }, + // ], }, - // theme: "dope-wars", - // colorMode: "light" - // prefunds: [ - // { - // address: ETH_TOKEN_ADDRESS, - // min: "300000000000000", - // }, - // { - // address: PAPER_TOKEN_ADDRESS, - // min: "100", - // }, - // ], -}); +); function provider(chain: Chain) { switch (chain) { diff --git a/packages/connector/src/index.ts b/packages/connector/src/index.ts index f9c54984a..e0693ea89 100644 --- a/packages/connector/src/index.ts +++ b/packages/connector/src/index.ts @@ -1,14 +1,14 @@ import { Connector } from "@starknet-react/core"; -import Controller, { ControllerOptions } from "@cartridge/controller"; +import Controller, { Policy, ControllerOptions } from "@cartridge/controller"; import { AccountInterface } from "starknet"; class ControllerConnector extends Connector { public controller: Controller; private _account: AccountInterface | undefined; - constructor(options?: ControllerOptions) { + constructor(policies?: Policy[], options?: ControllerOptions) { super(); - this.controller = new Controller(options); + this.controller = new Controller(policies, options); } readonly id = "cartridge"; diff --git a/packages/controller/src/index.ts b/packages/controller/src/index.ts index 10f808fe7..9afc97f27 100644 --- a/packages/controller/src/index.ts +++ b/packages/controller/src/index.ts @@ -30,7 +30,7 @@ import { KEYCHAIN_URL, RPC_SEPOLIA } from "./constants"; class Controller { private url: URL; - private policies?: Policy[]; + private policies: Policy[]; private paymaster?: PaymasterOptions; private connection?: Connection; private modal?: Modal; @@ -38,33 +38,21 @@ class Controller { public rpc: URL; public account?: AccountInterface; - constructor({ - policies, - url, - rpc, - paymaster, - theme, - config, - colorMode, - prefunds, - }: ControllerOptions = {}) { - this.url = new URL(url || KEYCHAIN_URL); - this.rpc = new URL(rpc || RPC_SEPOLIA); - this.paymaster = paymaster; - - if (policies?.length) { - this.policies = policies.map((policy) => ({ - ...policy, - target: addAddressPadding(policy.target), - })); - } + constructor(policies: Policy[] = [], options: ControllerOptions = {}) { + this.url = new URL(options?.url || KEYCHAIN_URL); + this.rpc = new URL(options?.rpc || RPC_SEPOLIA); + this.paymaster = options.paymaster; + this.policies = policies.map((policy) => ({ + ...policy, + target: addAddressPadding(policy.target), + })); - this.setTheme(theme, config?.presets); - if (colorMode) { - this.setColorMode(colorMode); + this.setTheme(options?.theme, options?.config?.presets); + if (options?.colorMode) { + this.setColorMode(options.colorMode); } - if (prefunds?.length) { - this.setPrefunds(prefunds); + if (options?.prefunds?.length) { + this.setPrefunds(options.prefunds); } this.initModal(); @@ -189,10 +177,10 @@ class Controller { this.modal.open(); try { - let response = await this.keychain.connect({ - rpcUrl: this.rpc.toString(), - policies: this.policies, - }); + let response = await this.keychain.connect( + this.policies, + this.rpc.toString(), + ); if (response.code !== ResponseCodes.SUCCESS) { throw new Error(response.message); } diff --git a/packages/controller/src/types.ts b/packages/controller/src/types.ts index 1f9c6dcb1..af13f5d32 100644 --- a/packages/controller/src/types.ts +++ b/packages/controller/src/types.ts @@ -61,10 +61,10 @@ export type ProbeReply = { export interface Keychain { probe(rpcUrl?: string): Promise; - connect(args: { - rpcUrl: string; - policies?: Policy[]; - }): Promise; + connect( + policies: Policy[], + rpcUrl: string, + ): Promise; disconnect(): void; reset(): void; @@ -119,7 +119,6 @@ export interface Modal { * Options for configuring the controller */ export type ControllerOptions = { - policies?: Policy[]; /** The URL of keychain */ url?: string; /** The URL of the RPC */ diff --git a/packages/keychain/src/components/connect/Login.tsx b/packages/keychain/src/components/connect/Login.tsx index cd4272bf2..a47d163e3 100644 --- a/packages/keychain/src/components/connect/Login.tsx +++ b/packages/keychain/src/components/connect/Login.tsx @@ -86,8 +86,8 @@ function Form({ await doLogin(usernameField.value, credentialId); break; case LoginMode.Controller: - if (policies?.length === 0) { - break; + if (policies.length === 0) { + throw new Error("Policies required for controller "); } await controller.approve(origin, expiresAt, policies); diff --git a/packages/keychain/src/hooks/connection.tsx b/packages/keychain/src/hooks/connection.tsx index 65d022b48..8aec2ace1 100644 --- a/packages/keychain/src/hooks/connection.tsx +++ b/packages/keychain/src/hooks/connection.tsx @@ -33,7 +33,7 @@ type ConnectionContextValue = { rpcUrl: string; chainId: string; chainName: string; - policies?: Policy[]; + policies: Policy[]; prefunds: Prefund[]; hasPrefundRequest: boolean; error: Error; diff --git a/packages/keychain/src/pages/index.tsx b/packages/keychain/src/pages/index.tsx index b5e3e197e..e28065414 100644 --- a/packages/keychain/src/pages/index.tsx +++ b/packages/keychain/src/pages/index.tsx @@ -40,7 +40,7 @@ function Home() { switch (context.type) { case "connect": { // TODO: show missing policies if mismatch - if (!context.policies?.length || controller.account.sessionJson()) { + if (controller.account.sessionJson()) { context.resolve({ code: ResponseCodes.SUCCESS, address: controller.address, diff --git a/packages/keychain/src/utils/connection/connect.ts b/packages/keychain/src/utils/connection/connect.ts index c415ffbf4..0b0afb6b7 100644 --- a/packages/keychain/src/utils/connection/connect.ts +++ b/packages/keychain/src/utils/connection/connect.ts @@ -14,19 +14,10 @@ export function connectFactory({ setContext: (context: ConnectionCtx) => void; }) { return (origin: string) => - ({ - rpcUrl, - policies, - }: { - rpcUrl: string; - policies?: Policy[]; - }): Promise => { + (policies: Policy[], rpcUrl: string): Promise => { setOrigin(origin); setRpcUrl(rpcUrl); - - if (policies?.length) { - setPolicies(policies); - } + setPolicies(policies); return new Promise((resolve, reject) => { setContext({ diff --git a/packages/keychain/src/utils/connection/types.ts b/packages/keychain/src/utils/connection/types.ts index 4e820133f..14736619e 100644 --- a/packages/keychain/src/utils/connection/types.ts +++ b/packages/keychain/src/utils/connection/types.ts @@ -26,7 +26,7 @@ export type ConnectionCtx = export type ConnectCtx = { origin: string; type: "connect"; - policies?: Policy[]; + policies: Policy[]; resolve: (res: ConnectReply | ConnectError) => void; reject: (reason?: unknown) => void; };