Skip to content

Commit

Permalink
Adapt lib to SessionPolicies type
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Dec 3, 2024
1 parent 4c8b22a commit 0f98fa7
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 206 deletions.
5 changes: 2 additions & 3 deletions packages/controller/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { KeychainIFrame, ProfileIFrame } from "./iframe";
import { NotReadyToConnect } from "./errors";
import {
Keychain,
Policy,
ResponseCodes,
ConnectReply,
ProbeReply,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions packages/controller/src/iframe/base.ts
Original file line number Diff line number Diff line change
@@ -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<CallSender> = Omit<
Expand Down Expand Up @@ -39,7 +39,9 @@ export class IFrame<CallSender extends {}> implements Modal {
"theme",
encodeURIComponent(
JSON.stringify(
defaultPresets[theme ?? "cartridge"] ?? defaultPresets.cartridge,
theme && theme in verifiedConfigs
? verifiedConfigs[theme]
: defaultTheme,
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { default } from "./controller";
export * from "./errors";
export * from "./types";

export { defaultPresets } from "./presets";
export { defaultTheme } from "./verified";
167 changes: 0 additions & 167 deletions packages/controller/src/presets.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/controller/src/session/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions packages/controller/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ export { default } from "./provider";
export * from "./provider";
export * from "../errors";
export * from "../types";

export { defaultPresets } from "../presets";
8 changes: 4 additions & 4 deletions packages/controller/src/session/provider.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +17,7 @@ interface SessionRegistration {
export type SessionOptions = {
rpc: string;
chainId: string;
policies: Policy[];
policies: SessionPolicies;
redirectUrl: string;
};

Expand All @@ -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 });
Expand Down
8 changes: 4 additions & 4 deletions packages/controller/src/telegram/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -34,7 +34,7 @@ export default class TelegramProvider extends BaseProvider {
}: {
rpc: string;
chainId: string;
policies: Policy[];
policies: SessionPolicies;
tmaUrl: string;
}) {
super({
Expand Down
3 changes: 1 addition & 2 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
32 changes: 17 additions & 15 deletions packages/controller/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) {
Expand All @@ -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),
};
}
});
}),
];
}
9 changes: 9 additions & 0 deletions packages/controller/src/verified.ts
Original file line number Diff line number Diff line change
@@ -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: "",
Expand Down
Loading

0 comments on commit 0f98fa7

Please sign in to comment.