Skip to content

Commit

Permalink
Move policies to ControllerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Aug 16, 2024
1 parent 2cea4df commit fbd94c1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const ETH_TOKEN_ADDRESS =
// const PAPER_TOKEN_ADDRESS =
// "0x0410466536b5ae074f7fea81e5533b8134a9fa08b3dd077dd9db08f64997d113";

const cartridge = new CartridgeConnector(
[
const cartridge = new CartridgeConnector({
policies: [
{
target: ETH_TOKEN_ADDRESS,
method: "approve",
Expand All @@ -50,35 +50,33 @@ const cartridge = new CartridgeConnector(
method: "allowance",
},
],
{
url:
!process.env.NEXT_PUBLIC_VERCEL_BRANCH_URL ||
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",
// },
// ],
"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",
// },
// ],
});

function provider(chain: Chain) {
switch (chain) {
Expand Down
6 changes: 3 additions & 3 deletions packages/connector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Connector } from "@starknet-react/core";
import Controller, { Policy, ControllerOptions } from "@cartridge/controller";
import Controller, { ControllerOptions } from "@cartridge/controller";
import { AccountInterface } from "starknet";

class ControllerConnector extends Connector {
public controller: Controller;
private _account: AccountInterface | undefined;

constructor(policies?: Policy[], options?: ControllerOptions) {
constructor(options?: ControllerOptions) {
super();
this.controller = new Controller(policies, options);
this.controller = new Controller(options);
}

readonly id = "cartridge";
Expand Down
48 changes: 30 additions & 18 deletions packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,41 @@ import { KEYCHAIN_URL, RPC_SEPOLIA } from "./constants";

class Controller {
private url: URL;
private policies: Policy[];
private policies?: Policy[];
private paymaster?: PaymasterOptions;
private connection?: Connection<Keychain>;
private modal?: Modal;
public keychain?: AsyncMethodReturns<Keychain>;
public rpc: URL;
public account?: AccountInterface;

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),
}));
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),
}));
}

this.setTheme(options?.theme, options?.config?.presets);
if (options?.colorMode) {
this.setColorMode(options.colorMode);
this.setTheme(theme, config?.presets);
if (colorMode) {
this.setColorMode(colorMode);
}
if (options?.prefunds?.length) {
this.setPrefunds(options.prefunds);
if (prefunds?.length) {
this.setPrefunds(prefunds);
}

this.initModal();
Expand Down Expand Up @@ -177,10 +189,10 @@ class Controller {
this.modal.open();

try {
let response = await this.keychain.connect(
this.policies,
this.rpc.toString(),
);
let response = await this.keychain.connect({
rpcUrl: this.rpc.toString(),
policies: this.policies,
});
if (response.code !== ResponseCodes.SUCCESS) {
throw new Error(response.message);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export type ProbeReply = {

export interface Keychain {
probe(rpcUrl?: string): Promise<ProbeReply | ConnectError>;
connect(
policies: Policy[],
rpcUrl: string,
): Promise<ConnectReply | ConnectError>;
connect(args: {
rpcUrl: string;
policies?: Policy[];
}): Promise<ConnectReply | ConnectError>;
disconnect(): void;

reset(): void;
Expand Down Expand Up @@ -119,6 +119,7 @@ export interface Modal {
* Options for configuring the controller
*/
export type ControllerOptions = {
policies?: Policy[];
/** The URL of keychain */
url?: string;
/** The URL of the RPC */
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (!context.policies?.length || controller.account.sessionJson()) {
context.resolve({
code: ResponseCodes.SUCCESS,
address: controller.address,
Expand Down
13 changes: 11 additions & 2 deletions packages/keychain/src/utils/connection/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ export function connectFactory({
setContext: (context: ConnectionCtx) => void;
}) {
return (origin: string) =>
(policies: Policy[], rpcUrl: string): Promise<ConnectReply> => {
({
rpcUrl,
policies,
}: {
rpcUrl: string;
policies?: Policy[];
}): Promise<ConnectReply> => {
setOrigin(origin);
setRpcUrl(rpcUrl);
setPolicies(policies);

if (policies?.length) {
setPolicies(policies);
}

return new Promise((resolve, reject) => {
setContext({
Expand Down
2 changes: 1 addition & 1 deletion packages/keychain/src/utils/connection/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down

0 comments on commit fbd94c1

Please sign in to comment.