From 0f98fa749bf842b9637a0ff6a754c9f9b81aabd9 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Tue, 3 Dec 2024 15:19:34 +0000 Subject: [PATCH] Adapt lib to SessionPolicies type --- packages/controller/src/controller.ts | 5 +- packages/controller/src/iframe/base.ts | 6 +- packages/controller/src/index.ts | 2 +- packages/controller/src/presets.ts | 167 ------------------ packages/controller/src/session/account.ts | 1 - packages/controller/src/session/index.ts | 2 - packages/controller/src/session/provider.ts | 8 +- packages/controller/src/telegram/provider.ts | 8 +- packages/controller/src/types.ts | 3 +- packages/controller/src/utils.ts | 32 ++-- packages/controller/src/verified.ts | 9 + packages/keychain/src/hooks/theme.tsx | 6 +- .../profile/src/components/context/theme.tsx | 4 +- 13 files changed, 47 insertions(+), 206 deletions(-) delete mode 100644 packages/controller/src/presets.ts diff --git a/packages/controller/src/controller.ts b/packages/controller/src/controller.ts index b42385973..1756c06c3 100644 --- a/packages/controller/src/controller.ts +++ b/packages/controller/src/controller.ts @@ -5,7 +5,6 @@ import { KeychainIFrame, ProfileIFrame } from "./iframe"; import { NotReadyToConnect } from "./errors"; import { Keychain, - Policy, ResponseCodes, ConnectReply, ProbeReply, @@ -112,7 +111,7 @@ export default class ControllerProvider extends BaseProvider { try { let response = await this.keychain.connect( - this.options.policies || [], + this.options.policies || {}, this.rpc.toString(), ); if (response.code !== ResponseCodes.SUCCESS) { @@ -182,7 +181,7 @@ export default class ControllerProvider extends BaseProvider { return true; } - revoke(origin: string, _policy: Policy[]) { + revoke(origin: string) { if (!this.keychain) { console.error(new NotReadyToConnect().message); return null; diff --git a/packages/controller/src/iframe/base.ts b/packages/controller/src/iframe/base.ts index b43e88c94..80e591646 100644 --- a/packages/controller/src/iframe/base.ts +++ b/packages/controller/src/iframe/base.ts @@ -1,5 +1,5 @@ import { AsyncMethodReturns, connectToChild } from "@cartridge/penpal"; -import { defaultPresets } from "../presets"; +import { defaultTheme, verifiedConfigs } from "../verified"; import { ControllerOptions, Modal } from "../types"; export type IFrameOptions = Omit< @@ -39,7 +39,9 @@ export class IFrame implements Modal { "theme", encodeURIComponent( JSON.stringify( - defaultPresets[theme ?? "cartridge"] ?? defaultPresets.cartridge, + theme && theme in verifiedConfigs + ? verifiedConfigs[theme] + : defaultTheme, ), ), ); diff --git a/packages/controller/src/index.ts b/packages/controller/src/index.ts index a6b32f9fe..c36adeb67 100644 --- a/packages/controller/src/index.ts +++ b/packages/controller/src/index.ts @@ -2,4 +2,4 @@ export { default } from "./controller"; export * from "./errors"; export * from "./types"; -export { defaultPresets } from "./presets"; +export { defaultTheme } from "./verified"; diff --git a/packages/controller/src/presets.ts b/packages/controller/src/presets.ts deleted file mode 100644 index 89ee968ee..000000000 --- a/packages/controller/src/presets.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { ControllerThemePresets } from "./types"; - -export const defaultPresets: ControllerThemePresets = { - cartridge: { - id: "cartridge", - name: "Cartridge", - icon: "/whitelabel/cartridge/icon.svg", - cover: { - light: "/whitelabel/cartridge/cover-light.png", - dark: "/whitelabel/cartridge/cover-dark.png", - }, - }, - "force-prime": { - id: "force-prime", - name: "Force Prime", - icon: "/whitelabel/force-prime/icon.png", - cover: "/whitelabel/force-prime/cover.png", - colors: { - primary: "#E1CC89", - }, - }, - paved: { - id: "paved", - name: "Paved", - icon: "/whitelabel/paved/icon.svg", - cover: "/whitelabel/paved/cover.png", - colors: { - primary: "#B0CAF8", - }, - }, - eternum: { - id: "eternum", - name: "Eternum", - icon: "/whitelabel/eternum/icon.gif", - cover: "/whitelabel/eternum/cover.png", - colors: { - primary: "#CE9822", - }, - }, - pistols: { - id: "pistols", - name: "Pistols at Ten Blocks", - icon: "/whitelabel/pistols/icon.png", - cover: "/whitelabel/pistols/cover.png", - colors: { - primary: "#EF9758", - }, - }, - pixelaw: { - id: "pixelaw", - name: "Pixelaw", - icon: "/whitelabel/pixelaw/icon.svg", - cover: "/whitelabel/pixelaw/cover.png", - colors: { - primary: "#7C00B1", - primaryForeground: "white", - }, - }, - "dope-wars": { - id: "dope-wars", - name: "Dope Wars", - icon: "/whitelabel/dope-wars/icon.png", - cover: "/whitelabel/dope-wars/cover.png", - colors: { - primary: "#11ED83", - }, - }, - zkastle: { - id: "zkastle", - name: "zKastle", - icon: "/whitelabel/zkastle/icon.svg", - cover: "/whitelabel/zkastle/cover.png", - colors: { - primary: "#E50D2C", - }, - }, - "loot-survivor": { - id: "loot-survivor", - name: "Loot Survivor", - icon: "/whitelabel/loot-survivor/icon.png", - cover: "/whitelabel/loot-survivor/cover.png", - colors: { - primary: "#33FF33", - }, - }, - zktt: { - id: "zktt", - name: "zKTT", - icon: "/whitelabel/zktt/icon.png", - cover: "/whitelabel/zktt/cover.png", - colors: { - primary: "#FFFFFF", - }, - }, - "tale-weaver": { - id: "tale-weaver", - name: "Tale Weaver", - icon: "/whitelabel/tale-weaver/icon.png", - cover: "/whitelabel/tale-weaver/cover.png", - colors: { - primary: "#fce377", - }, - }, - "realm-of-ra": { - id: "realm-of-ra", - name: "Realm of Ra", - icon: "/whitelabel/realm-of-ra/icon.png", - cover: "/whitelabel/realm-of-ra/cover.png", - colors: { - primary: "#de9534", - }, - }, - "jokers-of-neon": { - id: "jokers-of-neon", - name: "Jokers of Neon", - icon: "/whitelabel/jokers-of-neon/icon.png", - cover: "/whitelabel/jokers-of-neon/cover.png", - colors: { - primary: "#A144B2", - }, - }, - flippyflop: { - id: "flippyflop", - name: "FlippyFlop", - icon: "/whitelabel/flippyflop/icon.png", - cover: "/whitelabel/flippyflop/cover.png", - colors: { - primary: "#F38332", - }, - }, - "savage-summit": { - id: "savage-summit", - name: "Savage Summit", - icon: "/whitelabel/savage-summit/icon.png", - cover: "/whitelabel/savage-summit/cover.png", - colors: { - primary: "#fbf7da", - }, - }, - "dark-shuffle": { - id: "dark-shuffle", - name: "Dark Shuffle", - icon: "/whitelabel/dark-shuffle/icon.svg", - cover: "/whitelabel/dark-shuffle/cover.png", - colors: { - primary: "#F59100", - }, - }, - "blob-arena": { - id: "blob-arena", - name: "Blob Arena", - icon: "/whitelabel/blob-arena/icon.png", - cover: "/whitelabel/blob-arena/cover.png", - colors: { - primary: "#980f06", - }, - }, - zkube: { - id: "zkube", - name: "zKube", - icon: "/whitelabel/zkube/icon.png", - cover: "/whitelabel/zkube/cover.png", - colors: { - primary: "#5bc3e6", - }, - }, -}; diff --git a/packages/controller/src/session/account.ts b/packages/controller/src/session/account.ts index a6753c791..239f3f879 100644 --- a/packages/controller/src/session/account.ts +++ b/packages/controller/src/session/account.ts @@ -7,7 +7,6 @@ import BaseProvider from "../provider"; export * from "../errors"; export * from "../types"; -export { defaultPresets } from "../presets"; export default class SessionAccount extends WalletAccount { public controller: CartridgeSessionAccount; diff --git a/packages/controller/src/session/index.ts b/packages/controller/src/session/index.ts index 871edb491..29d06e08c 100644 --- a/packages/controller/src/session/index.ts +++ b/packages/controller/src/session/index.ts @@ -2,5 +2,3 @@ export { default } from "./provider"; export * from "./provider"; export * from "../errors"; export * from "../types"; - -export { defaultPresets } from "../presets"; diff --git a/packages/controller/src/session/provider.ts b/packages/controller/src/session/provider.ts index ed46cb8ee..343e95c76 100644 --- a/packages/controller/src/session/provider.ts +++ b/packages/controller/src/session/provider.ts @@ -1,10 +1,10 @@ -import { Policy } from "../types"; +import { SessionPolicies } from "../types"; import { ec, stark, WalletAccount } from "starknet"; import SessionAccount from "./account"; import { KEYCHAIN_URL } from "../constants"; import BaseProvider from "../provider"; -import { toWasmPolicies } from "src/utils"; +import { toWasmPolicies } from "../utils"; interface SessionRegistration { username: string; @@ -17,7 +17,7 @@ interface SessionRegistration { export type SessionOptions = { rpc: string; chainId: string; - policies: Policy[]; + policies: SessionPolicies; redirectUrl: string; }; @@ -29,7 +29,7 @@ export default class SessionProvider extends BaseProvider { protected _username?: string; protected _redirectUrl: string; - protected _policies: Policy[]; + protected _policies: SessionPolicies; constructor({ rpc, chainId, policies, redirectUrl }: SessionOptions) { super({ rpc }); diff --git a/packages/controller/src/telegram/provider.ts b/packages/controller/src/telegram/provider.ts index bd198b6c2..58c5499d0 100644 --- a/packages/controller/src/telegram/provider.ts +++ b/packages/controller/src/telegram/provider.ts @@ -6,8 +6,8 @@ import { } from "@telegram-apps/sdk"; import { ec, stark, WalletAccount } from "starknet"; -import { KEYCHAIN_URL } from "src/constants"; -import { Policy } from "src/types"; +import { KEYCHAIN_URL } from "../constants"; +import { SessionPolicies } from "../types"; import SessionAccount from "src/session/account"; import BaseProvider from "src/provider"; import { toWasmPolicies } from "src/utils"; @@ -24,7 +24,7 @@ export default class TelegramProvider extends BaseProvider { private _tmaUrl: string; protected _chainId: string; protected _username?: string; - protected _policies: Policy[]; + protected _policies: SessionPolicies; constructor({ rpc, @@ -34,7 +34,7 @@ export default class TelegramProvider extends BaseProvider { }: { rpc: string; chainId: string; - policies: Policy[]; + policies: SessionPolicies; tmaUrl: string; }) { super({ diff --git a/packages/controller/src/types.ts b/packages/controller/src/types.ts index d495555a4..7423e2386 100644 --- a/packages/controller/src/types.ts +++ b/packages/controller/src/types.ts @@ -32,7 +32,6 @@ export type VerifiedConfig = { theme?: ControllerThemePreset; }; -/** It must contain one field */ export type SessionPolicies = { /** The key must be the contract address */ contracts?: ContractPolicies; @@ -176,7 +175,7 @@ export type ProviderOptions = { }; export type KeychainOptions = IFrameOptions & { - policies?: SessionPolicies[]; + policies?: SessionPolicies; /** The URL of keychain */ url?: string; /** The origin of keychain */ diff --git a/packages/controller/src/utils.ts b/packages/controller/src/utils.ts index ff728dd2d..c7d8ecffd 100644 --- a/packages/controller/src/utils.ts +++ b/packages/controller/src/utils.ts @@ -6,7 +6,7 @@ import { typedData, TypedDataRevision, } from "starknet"; -import { Policy } from "./types"; +import { SessionPolicies } from "./types"; import wasm from "@cartridge/account-wasm/controller"; export function normalizeCalls(calls: Call | Call[]) { @@ -19,29 +19,31 @@ export function normalizeCalls(calls: Call | Call[]) { }); } -export function toWasmPolicies(policies: Policy[]): wasm.Policy[] { - return policies.map((richPolicy) => { - if ("target" in richPolicy) { - return { - target: richPolicy.target, - method: richPolicy.method, - }; - } else { +export function toWasmPolicies(policies: SessionPolicies): wasm.Policy[] { + return [ + ...Object.entries(policies.contracts ?? {}).flatMap( + ([target, { methods }]) => + (Array.isArray(methods) ? methods : [methods]).map((m) => ({ + target, + method: m.name, + })), + ), + ...(policies.messages ?? []).map((p) => { const domainHash = typedData.getStructHash( - richPolicy.types, + p.types, "StarknetDomain", - richPolicy.domain, + p.domain, TypedDataRevision.ACTIVE, ); const typeHash = typedData.getTypeHash( - richPolicy.types, - richPolicy.primaryType, + p.types, + p.primaryType, TypedDataRevision.ACTIVE, ); return { scope_hash: hash.computePoseidonHash(domainHash, typeHash), }; - } - }); + }), + ]; } diff --git a/packages/controller/src/verified.ts b/packages/controller/src/verified.ts index 3fab2794c..d15ca6d9d 100644 --- a/packages/controller/src/verified.ts +++ b/packages/controller/src/verified.ts @@ -1,5 +1,14 @@ import { VerifiedConfigs } from "./types"; +export const defaultTheme = { + name: "Cartridge", + icon: "/whitelabel/cartridge/icon.svg", + cover: { + light: "/whitelabel/cartridge/cover-light.png", + dark: "/whitelabel/cartridge/cover-dark.png", + }, +}; + export const verifiedConfigs: VerifiedConfigs = { "force-prime": { origin: "", diff --git a/packages/keychain/src/hooks/theme.tsx b/packages/keychain/src/hooks/theme.tsx index 2f39cba93..37d6f68c4 100644 --- a/packages/keychain/src/hooks/theme.tsx +++ b/packages/keychain/src/hooks/theme.tsx @@ -2,7 +2,7 @@ import { ControllerColor, ControllerTheme, ControllerThemePreset, - defaultPresets, + defaultTheme, } from "@cartridge/controller"; import { CartridgeTheme } from "@cartridge/ui"; import { useThemeEffect } from "@cartridge/ui-next"; @@ -53,7 +53,7 @@ export function useControllerThemePreset() { return useMemo(() => { const q = router.query.theme; if (typeof q === "undefined") { - return defaultPresets.cartridge; + return defaultTheme; } const str = decodeURIComponent(Array.isArray(q) ? q[q.length - 1] : q); @@ -61,7 +61,7 @@ export function useControllerThemePreset() { try { return JSON.parse(str) as ControllerThemePreset; } catch { - return defaultPresets.cartridge; + return defaultTheme; } }, [router.query.theme]); } diff --git a/packages/profile/src/components/context/theme.tsx b/packages/profile/src/components/context/theme.tsx index 4ccd514a4..8e7f29223 100644 --- a/packages/profile/src/components/context/theme.tsx +++ b/packages/profile/src/components/context/theme.tsx @@ -1,4 +1,4 @@ -import { ControllerThemePreset, defaultPresets } from "@cartridge/controller"; +import { ControllerThemePreset, defaultTheme } from "@cartridge/controller"; import { useThemeEffect } from "@cartridge/ui-next"; import { createContext, useCallback, useEffect, useState } from "react"; import { useSearchParams } from "react-router-dom"; @@ -20,7 +20,7 @@ type ThemeProviderContextType = { const initialState: ThemeProviderContextType = { colorScheme: "system", setColorScheme: () => null, - theme: defaultPresets.cartridge as ControllerThemePreset, + theme: defaultTheme, }; export const ThemeContext =