Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add controller support & update dojo.c #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Assets/Dojo/Plugins/WebGL/controller.jslib
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
mergeInto(LibraryManager.library, {
NewController: (policies, options) => {
policies = JSON.parse(UTF8ToString(policies));
options = JSON.parse(UTF8ToString(options));

window.controller = controller = new Controller(policies, options);
},
ControllerConnect: async (cb) => {
let result = {
success: false,
};

try {
await controller.connect();
result.success = true;
}
catch (error) {
result.error = error;
}

const resultStr = JSON.stringify(result);
const bufferSize = lengthBytesUTF8(resultStr) + 1;
const buffer = _malloc(bufferSize);
stringToUTF8(resultStr, buffer, bufferSize);
dynCall_vi(cb, buffer);
},
ControllerDisconnect: async (cb) => {
let result = {
success: false,
};

try {
await controller.disconnect();
result.success = true;
}
catch (error) {
result.error = error;
}

const resultStr = JSON.stringify(result);
const bufferSize = lengthBytesUTF8(resultStr) + 1;
const buffer = _malloc(bufferSize);
stringToUTF8(resultStr, buffer, bufferSize);
dynCall_vi(cb, buffer);
},
ControllerRevoke: async (origin, policy, cb) => {
let result = {
success: false,
};

try {
await controller.revoke(origin, policy);
result.success = true;
} catch (error) {
result.error = error;
}

const resultStr = JSON.stringify(result);
const bufferSize = lengthBytesUTF8(resultStr) + 1;
const buffer = _malloc(bufferSize);
stringToUTF8(resultStr, buffer, bufferSize);
dynCall_vi(cb, buffer);
},
ControllerUsername: async (cb) => {
let result = {
success: false,
};

try {
const username = await controller.username();
result.success = true;
result.result = username;
} catch (error) {
result.error = error;
}

const resultStr = JSON.stringify(result);
const bufferSize = lengthBytesUTF8(resultStr) + 1;
const buffer = _malloc(bufferSize);
stringToUTF8(resultStr, buffer, bufferSize);
dynCall_vi(cb, buffer);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export declare const KEYCHAIN_URL = "https://x.cartridge.gg";
export declare const RPC_SEPOLIA = "https://api.cartridge.gg/x/starknet/sepolia";
export declare const RPC_MAINNET = "https://api.cartridge.gg/x/starknet/mainnet";
46 changes: 46 additions & 0 deletions Assets/WebGLTemplates/Dojo/TemplateData/controller/device.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Account, Abi, Call, EstimateFeeDetails, Signature, InvokeFunctionResponse, EstimateFee, DeclareContractPayload, TypedData, InvocationsDetails } from "starknet";
import { Keychain, Modal, PaymasterOptions } from "./types";
import { AsyncMethodReturns } from "@cartridge/penpal";
declare class DeviceAccount extends Account {
address: string;
private keychain;
private modal;
private paymaster?;
constructor(rpcUrl: string, address: string, keychain: AsyncMethodReturns<Keychain>, modal: Modal, paymaster?: PaymasterOptions);
/**
* Estimate Fee for a method on starknet
*
* @param calls the invocation object containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
* - signature - (defaults to []) the signature
*
* @returns response from addTransaction
*/
estimateInvokeFee(calls: Call | Call[], details?: EstimateFeeDetails): Promise<EstimateFee>;
estimateDeclareFee(payload: DeclareContractPayload, details?: EstimateFeeDetails): Promise<EstimateFee>;
/**
* Invoke execute function in account contract
*
* @param calls the invocation object or an array of them, containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
* - signature - (defaults to []) the signature
* @param abis (optional) the abi of the contract for better displaying
*
* @returns response from addTransaction
*/
execute(calls: Call | Call[], abis?: Abi[], transactionsDetail?: InvocationsDetails): Promise<InvokeFunctionResponse>;
/**
* Sign an JSON object for off-chain usage with the starknet private key and return the signature
* This adds a message prefix so it cant be interchanged with transactions
*
* @param json - JSON object to be signed
* @returns the signature of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
signMessage(typedData: TypedData): Promise<Signature>;
}
export default DeviceAccount;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Policy } from "./types";
export declare class MissingPolicys extends Error {
missing: Policy[];
constructor(missing: Policy[]);
}
export declare class NotReadyToConnect extends Error {
constructor();
}
29 changes: 29 additions & 0 deletions Assets/WebGLTemplates/Dojo/TemplateData/controller/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export * from "./types";
export { defaultPresets } from "./presets";
import { AccountInterface } from "starknet";
import { AsyncMethodReturns } from "@cartridge/penpal";
import { Keychain, Policy, ControllerOptions } from "./types";
declare class Controller {
private url;
private policies;
private paymaster?;
private connection?;
private modal?;
keychain?: AsyncMethodReturns<Keychain>;
rpc: URL;
account?: AccountInterface;
constructor(policies?: Policy[], options?: ControllerOptions);
private initModal;
private setTheme;
private setColorMode;
ready(): Promise<boolean>;
probe(): Promise<true | null | undefined>;
connect(): Promise<AccountInterface | undefined>;
disconnect(): Promise<void>;
revoke(origin: string, _policy: Policy[]): Promise<void> | null;
username(): Promise<string> | undefined;
delegateAccount(): Promise<import("starknet").CallContractResponse | null>;
}
export * from "./types";
export * from "./errors";
export default Controller;
Loading