Skip to content

Commit

Permalink
Migrate to WalletAccount api
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Oct 21, 2024
1 parent b64d007 commit db15d78
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 393 deletions.
4 changes: 2 additions & 2 deletions examples/starknet-react-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@cartridge/controller": "workspace:^",
"@cartridge/ui-next": "workspace:^",
"@starknet-react/chains": "^0.1.3",
"@starknet-react/core": "^2.1.5",
"@starknet-react/core": "^3.0.2",
"next": "^14.2.5",
"next-themes": "^0.3.0",
"prettier": "^2.7.1",
Expand All @@ -39,4 +39,4 @@
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5"
}
}
}
4 changes: 2 additions & 2 deletions packages/connector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
},
"dependencies": {
"@cartridge/controller": "workspace:^",
"@starknet-react/core": "^2.1.5",
"@starknet-react/core": "^3.0.2",
"@telegram-apps/sdk": "^2.4.0",
"starknet": "^6.11.0"
},
"devDependencies": {
"@cartridge/tsconfig": "workspace:^",
"typescript": "^5.4.5"
}
}
}
10 changes: 0 additions & 10 deletions packages/connector/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,4 @@ export default class ControllerConnector extends Connector {
async delegateAccount() {
return await this.controller.delegateAccount();
}

/**
* @deprecated Use controller.openSettings() instead.
*/
async openMenu() {
console.warn(
"openMenu() is deprecated. Please use controller.openSettings() instead.",
);
return await this.controller.openSettings();
}
}
3 changes: 2 additions & 1 deletion packages/controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"dependencies": {
"@cartridge/account-wasm": "workspace:^",
"@cartridge/penpal": "^6.2.3",
"@starknet-io/types-js": "^0.7.7",
"base64url": "^3.0.1",
"cbor-x": "^1.5.0",
"fast-deep-equal": "^3.1.3",
Expand All @@ -38,4 +39,4 @@
"@types/node": "^20.6.0",
"typescript": "^5.4.5"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,80 +1,43 @@
import {
Account,
Abi,
Call,
EstimateFeeDetails,
Signature,
InvokeFunctionResponse,
EstimateFee,
DeclareContractPayload,
RpcProvider,
TypedData,
InvocationsDetails,
WalletAccount,
Call,
AllowArray,
} from "starknet";

import { SPEC, StarknetWindowObject } from "@starknet-io/types-js";

import {
ConnectError,
Keychain,
KeychainOptions,
Modal,
ResponseCodes,
} from "./types";
import { Signer } from "./signer";
import { AsyncMethodReturns } from "@cartridge/penpal";

class DeviceAccount extends Account {
class ControllerAccount extends WalletAccount {
address: string;
private keychain: AsyncMethodReturns<Keychain>;
private modal: Modal;
private options?: KeychainOptions;

constructor(
rpcUrl: string,
provider: StarknetWindowObject,
address: string,
keychain: AsyncMethodReturns<Keychain>,
options: KeychainOptions,
modal: Modal,
) {
super(
new RpcProvider({ nodeUrl: rpcUrl }),
address,
new Signer(keychain, modal),
);
super({ nodeUrl: options.rpc }, provider);

this.address = address;
this.keychain = keychain;
this.options = options;
this.modal = modal;
}

/**
* 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
*/
async estimateInvokeFee(
calls: Call | Call[],
details?: EstimateFeeDetails,
): Promise<EstimateFee> {
return this.keychain.estimateInvokeFee(calls, {
...details,
});
}

async estimateDeclareFee(
payload: DeclareContractPayload,
details?: EstimateFeeDetails,
): Promise<EstimateFee> {
return this.keychain.estimateDeclareFee(payload, {
...details,
});
}

/**
* Invoke execute function in account contract
*
Expand All @@ -87,17 +50,10 @@ class DeviceAccount extends Account {
*
* @returns response from addTransaction
*/
// @ts-expect-error TODO: fix overload type mismatch
async execute(
calls: Call | Call[],
abis?: Abi[],
transactionsDetail: InvocationsDetails = {},
): Promise<InvokeFunctionResponse> {
async execute(calls: AllowArray<Call>): Promise<InvokeFunctionResponse> {
return new Promise(async (resolve, reject) => {
const sessionExecute = await this.keychain.execute(
calls,
abis,
transactionsDetail,
false,
this.options?.paymaster,
);
Expand All @@ -119,8 +75,6 @@ class DeviceAccount extends Account {
this.modal.open();
const manualExecute = await this.keychain.execute(
calls,
abis,
transactionsDetail,
true,
this.options?.paymaster,
(sessionExecute as ConnectError).error,
Expand All @@ -146,17 +100,22 @@ class DeviceAccount extends Account {
* @returns the signature of the JSON object
* @throws {Error} if the JSON object is not a valid JSON
*/
async signMessage(typedData: TypedData): Promise<Signature> {
async signMessage(typedData: TypedData): Promise<SPEC.SIGNATURE> {
try {
this.modal.open();
const res = await this.keychain.signMessage(typedData, this.address);
const res = await this.keychain.signMessage(typedData);
this.modal.close();
return res as Signature;

if ("code" in res) {
throw res;
}

return res;
} catch (e) {
console.error(e);
throw e;
}
}
}

export default DeviceAccount;
export default ControllerAccount;
Loading

0 comments on commit db15d78

Please sign in to comment.