Skip to content

Commit

Permalink
Update Controller opts to only accept SessionPolicies (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev authored Jan 8, 2025
1 parent da06ad3 commit 88e0435
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 93 deletions.
157 changes: 73 additions & 84 deletions examples/next/src/components/providers/StarknetProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "@starknet-react/core";
import { PropsWithChildren } from "react";
import ControllerConnector from "@cartridge/connector/controller";
import { Policy } from "@cartridge/controller";
import { SessionPolicies } from "@cartridge/controller";

const rpc = process.env.NEXT_PUBLIC_RPC_SEPOLIA!;

Expand All @@ -17,83 +17,82 @@ export const ETH_CONTRACT_ADDRESS =
export const STRK_CONTRACT_ADDRESS =
"0x04718f5a0Fc34cC1AF16A1cdee98fFB20C31f5cD61D6Ab07201858f4287c938D";

const policies: Policy[] = [
{
target: ETH_CONTRACT_ADDRESS,
method: "approve",
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
},
{
target: ETH_CONTRACT_ADDRESS,
method: "transfer",
},
{
target: ETH_CONTRACT_ADDRESS,
method: "mint",
},
{
target: ETH_CONTRACT_ADDRESS,
method: "burn",
},
{
target: ETH_CONTRACT_ADDRESS,
method: "allowance",
},
{
target: STRK_CONTRACT_ADDRESS,
method: "approve",
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
},
{
target: STRK_CONTRACT_ADDRESS,
method: "transfer",
},
{
target: STRK_CONTRACT_ADDRESS,
method: "mint",
},
{
target: STRK_CONTRACT_ADDRESS,
method: "burn",
},
{
target: STRK_CONTRACT_ADDRESS,
method: "allowance",
},
{
target:
"0x0305f26ad19e0a10715d9f3137573d3a543de7b707967cd85d11234d6ec0fb7e",
method: "new_game",
},
{
types: {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
Person: [
{ name: "name", type: "felt" },
{ name: "wallet", type: "felt" },
const policies: SessionPolicies = {
contracts: {
[ETH_CONTRACT_ADDRESS]: {
methods: [
{
name: "approve",
entrypoint: "approve",
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
},
{ name: "transfer", entrypoint: "transfer" },
{ name: "mint", entrypoint: "mint" },
{ name: "burn", entrypoint: "burn" },
{ name: "allowance", entrypoint: "allowance" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "felt" },
},
[STRK_CONTRACT_ADDRESS]: {
methods: [
{
name: "approve",
entrypoint: "approve",
description:
"Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
},
{ name: "transfer", entrypoint: "transfer" },
{ name: "mint", entrypoint: "mint" },
{ name: "burn", entrypoint: "burn" },
{ name: "allowance", entrypoint: "allowance" },
],
},
primaryType: "Mail",
domain: {
name: "StarkNet Mail",
version: "1",
revision: "1",
chainId: "SN_SEPOLIA",
"0x0305f26ad19e0a10715d9f3137573d3a543de7b707967cd85d11234d6ec0fb7e": {
methods: [{ name: "new_game", entrypoint: "new_game" }],
},
},
];
messages: [
{
types: {
StarknetDomain: [
{ name: "name", type: "shortstring" },
{ name: "version", type: "shortstring" },
{ name: "chainId", type: "shortstring" },
{ name: "revision", type: "shortstring" },
],
Person: [
{ name: "name", type: "felt" },
{ name: "wallet", type: "felt" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "felt" },
],
},
primaryType: "Mail",
domain: {
name: "StarkNet Mail",
version: "1",
revision: "1",
chainId: "SN_SEPOLIA",
},
},
],
};

// Configure RPC provider
const provider = jsonRpcProvider({
rpc: (chain: Chain) => {
switch (chain) {
case mainnet:
return { nodeUrl: "https://api.cartridge.gg/x/starknet/mainnet" };
case sepolia:
default:
return { nodeUrl: "https://api.cartridge.gg/x/starknet/sepolia" };
}
},
});

export function StarknetProvider({ children }: PropsWithChildren) {
return (
Expand All @@ -102,17 +101,7 @@ export function StarknetProvider({ children }: PropsWithChildren) {
chains={[sepolia]}
connectors={[controller]}
explorer={starkscan}
provider={jsonRpcProvider({
rpc: (chain: Chain) => {
switch (chain) {
case mainnet:
return { nodeUrl: process.env.NEXT_PUBLIC_RPC_MAINNET };
case sepolia:
default:
return { nodeUrl: process.env.NEXT_PUBLIC_RPC_SEPOLIA };
}
},
})}
provider={provider}
>
{children}
</StarknetConfig>
Expand Down
2 changes: 1 addition & 1 deletion packages/controller/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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
11 changes: 3 additions & 8 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ import {
TypedData,
} from "@starknet-io/types-js";
import { KeychainIFrame, ProfileIFrame } from "./iframe";
import {
ColorMode,
Policies,
Policy,
SessionPolicies,
} from "@cartridge/presets";
import { ColorMode, Policy, SessionPolicies } from "@cartridge/presets";

export type Session = {
chainId: constants.StarknetChainId;
Expand Down Expand Up @@ -100,7 +95,7 @@ export type ControllerAccounts = Record<ContractAddress, CartridgeID>;
export interface Keychain {
probe(rpcUrl: string): Promise<ProbeReply | ConnectError>;
connect(
policies: Policies,
policies: SessionPolicies,
rpcUrl: string,
): Promise<ConnectReply | ConnectError>;
disconnect(): void;
Expand Down Expand Up @@ -167,7 +162,7 @@ export type ProviderOptions = {
};

export type KeychainOptions = IFrameOptions & {
policies?: Policies;
policies?: SessionPolicies;
/** The URL of keychain */
url?: string;
/** The origin of keychain */
Expand Down

0 comments on commit 88e0435

Please sign in to comment.