Skip to content

Commit

Permalink
Session error propagation toggle (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Sep 25, 2024
1 parent 340a71d commit c7c4a66
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
19 changes: 13 additions & 6 deletions packages/controller/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
import {
ConnectError,
Keychain,
KeychainOptions,
Modal,
PaymasterOptions,
ResponseCodes,
} from "./types";
import { Signer } from "./signer";
Expand All @@ -26,14 +26,14 @@ class DeviceAccount extends Account {
address: string;
private keychain: AsyncMethodReturns<Keychain>;
private modal: Modal;
private paymaster?: PaymasterOptions;
private options?: KeychainOptions;

constructor(
rpcUrl: string,
address: string,
keychain: AsyncMethodReturns<Keychain>,
options: KeychainOptions,
modal: Modal,
paymaster?: PaymasterOptions,
) {
super(
new RpcProvider({ nodeUrl: rpcUrl }),
Expand All @@ -42,8 +42,8 @@ class DeviceAccount extends Account {
);
this.address = address;
this.keychain = keychain;
this.options = options;
this.modal = modal;
this.paymaster = paymaster;
}

/**
Expand Down Expand Up @@ -99,14 +99,21 @@ class DeviceAccount extends Account {
abis,
transactionsDetail,
false,
this.paymaster,
this.options?.paymaster,
);

// Session call succeeded
if (sessionExecute.code === ResponseCodes.SUCCESS) {
resolve(sessionExecute as InvokeFunctionResponse);
return;
}

// Propagates session txn error back to caller
if (this.options?.propagateSessionErrors) {
reject((sessionExecute as ConnectError).error);
return;
}

// Session call or Paymaster flow failed.
// Session not avaialble, manual flow fallback
this.modal.open();
Expand All @@ -115,7 +122,7 @@ class DeviceAccount extends Account {
abis,
transactionsDetail,
true,
this.paymaster,
this.options?.paymaster,
(sessionExecute as ConnectError).error,
);

Expand Down
22 changes: 9 additions & 13 deletions packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import {
ConnectReply,
ProbeReply,
ControllerOptions,
PaymasterOptions,
ConnectError,
Profile,
IFrames,
ProfileOptions,
ProfileContextTypeVariant,
} from "./types";
import { KeychainIFrame, ProfileIFrame } from "./iframe";
Expand All @@ -27,13 +25,12 @@ import { RPC_SEPOLIA } from "./constants";

export default class Controller {
private policies: Policy[];
private paymaster?: PaymasterOptions;
private keychain?: AsyncMethodReturns<Keychain>;
private profile?: AsyncMethodReturns<Profile>;
private options: ControllerOptions;
private iframes: IFrames;
public rpc: URL;
public account?: AccountInterface;
private profileOptions: ProfileOptions;

constructor({
policies,
Expand All @@ -55,7 +52,6 @@ export default class Controller {
};

this.rpc = new URL(rpc || RPC_SEPOLIA);
this.paymaster = paymaster;

// TODO: remove this on the next major breaking change. pass everthing by url
this.policies =
Expand All @@ -64,7 +60,7 @@ export default class Controller {
target: addAddressPadding(policy.target),
})) || [];

this.profileOptions = options;
this.options = options;
}

async openSettings() {
Expand Down Expand Up @@ -105,23 +101,23 @@ export default class Controller {
this.rpc.toString(),
response.address,
this.keychain,
this.options,
this.iframes.keychain,
this.paymaster,
) as AccountInterface;
} catch (e) {
console.error(new NotReadyToConnect().message);
return;
}

if (
this.profileOptions.profileUrl &&
this.profileOptions.indexerUrl &&
this.options.profileUrl &&
this.options.indexerUrl &&
!this.iframes.profile
) {
const username = await this.keychain.username();
this.iframes.profile = new ProfileIFrame({
profileUrl: this.profileOptions.profileUrl,
indexerUrl: this.profileOptions.indexerUrl,
profileUrl: this.options.profileUrl,
indexerUrl: this.options.indexerUrl,
address: this.account.address,
username,
onConnect: (profile) => {
Expand Down Expand Up @@ -166,8 +162,8 @@ export default class Controller {
this.rpc.toString(),
response.address,
this.keychain,
this.options,
this.iframes.keychain,
this.paymaster,
) as AccountInterface;

return this.account;
Expand All @@ -179,7 +175,7 @@ export default class Controller {
}

openProfile(tab: ProfileContextTypeVariant = "inventory") {
if (!this.profileOptions.indexerUrl) {
if (!this.options.indexerUrl) {
console.error("`indexerUrl` option is required to open profile");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export type KeychainOptions = IFrameOptions & {
origin?: string;
/** Paymaster options for transaction fee management */
paymaster?: PaymasterOptions;
/** List of ERC20 tokens to pre-fund */
// prefunds?: Prefund[];
/** Propagate transaction errors back to caller instead of showing modal */
propagateSessionErrors?: boolean;
};

export type ProfileOptions = IFrameOptions & {
Expand Down

0 comments on commit c7c4a66

Please sign in to comment.