Skip to content

Commit

Permalink
Probe checks rpc (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody authored Jul 18, 2024
1 parent 83006a6 commit 76509c2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 47 deletions.
6 changes: 1 addition & 5 deletions packages/controller/src/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@ class DeviceAccount extends Account {
async execute(
calls: Call | Call[],
abis?: Abi[],
transactionsDetail?: InvocationsDetails,
transactionsDetail: InvocationsDetails = {},
): Promise<InvokeFunctionResponse> {
if (!transactionsDetail) {
transactionsDetail = {};
}

try {
let res = await this.keychain.execute(
calls,
Expand Down
12 changes: 5 additions & 7 deletions packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,19 @@ class Controller {
}

try {
const res = await this.keychain.probe();
if (res.code !== ResponseCodes.SUCCESS) {
return;
}
const response = (await this.keychain.probe(
this.rpc.toString(),
)) as ProbeReply;

const { address } = res as ProbeReply;
this.account = new DeviceAccount(
this.rpc.toString(),
address,
response.address,
this.keychain,
this.modal,
this.paymaster,
) as AccountInterface;
} catch (e) {
console.error(e);
console.error(new NotReadyToConnect().message);
return;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ export type ExecuteReply = InvokeFunctionResponse & {
export type ProbeReply = {
code: ResponseCodes.SUCCESS;
address: string;
policies: Policy[];
};

export interface Keychain {
probe(): Promise<ProbeReply | ConnectError>;
probe(rpcUrl?: string): Promise<ProbeReply | ConnectError>;
connect(
policies: Policy[],
rpcUrl: string,
Expand Down
20 changes: 14 additions & 6 deletions packages/keychain/src/hooks/connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,22 @@ export function ConnectionProvider({ children }: PropsWithChildren) {

useEffect(() => {
if (rpcUrl) {
new RpcProvider({ nodeUrl: rpcUrl })
.getChainId()
.then(setChainId)
.catch(() => {
const update = async () => {
try {
let provider = new RpcProvider({ nodeUrl: rpcUrl });
let chainId = await provider.getChainId();
setChainId(chainId);

controller?.updateChain(rpcUrl, chainId);
} catch (e) {
console.error(e);
setError(new Error("Unable to fetch Chain ID from provided RPC URL"));
});
}
};

update();
}
}, [rpcUrl]);
}, [rpcUrl, controller]);

const logout = useCallback((context: ConnectionCtx) => {
setContext({
Expand Down
7 changes: 6 additions & 1 deletion packages/keychain/src/utils/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ export function connectToController({
return connectToParent({
methods: {
connect: normalize(
connectFactory({ setOrigin, setRpcUrl, setPolicies, setContext }),
connectFactory({
setOrigin,
setRpcUrl,
setPolicies,
setContext,
}),
),
disconnect: normalize(validate(disconnectFactory(setController))),
execute: normalize(validate(executeFactory({ setContext }))),
Expand Down
16 changes: 10 additions & 6 deletions packages/keychain/src/utils/connection/probe.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ProbeReply, ResponseCodes } from "@cartridge/controller";
import Controller from "utils/controller";

export function probe(controller: Controller, origin: string) {
return (): ProbeReply => {
const session = controller.session(origin);
return {
export function probe(controller: Controller, _origin: string) {
return (rpcUrl?: string): Promise<ProbeReply> => {
if (rpcUrl && rpcUrl !== controller.rpcUrl) {
return Promise.reject({
code: ResponseCodes.NOT_CONNECTED,
});
}

return Promise.resolve({
code: ResponseCodes.SUCCESS,
address: controller.address,
policies: session?.policies || [],
};
});
};
}
31 changes: 11 additions & 20 deletions packages/keychain/src/utils/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import Storage from "utils/storage";
import Account from "./account";
import { selectors, VERSION } from "./selectors";
import migrations from "./migrations";
import { AccountInfoDocument } from "generated/graphql";
import { client } from "./graphql";

type SerializedController = {
publicKey: string;
Expand Down Expand Up @@ -63,24 +61,6 @@ export default class Controller {
);
}

async getUser() {
const res = await client.request(AccountInfoDocument, {
id: this.address,
});

// @ts-expect-error TODO: fix type error
const account = res.accounts?.edges?.[0]?.node;
if (!account) {
throw new Error("User not found");
}

return {
address: this.address,
name: account.id,
profileUri: `https://cartridge.gg/profile/${this.address}`,
};
}

delete() {
return Storage.clear();
}
Expand Down Expand Up @@ -153,6 +133,17 @@ export default class Controller {
});
}

updateChain(rpcUrl: string, chainId: string) {
this.rpcUrl = rpcUrl;
this.chainId = chainId;

Storage.set(selectors[VERSION].active(), {
address: this.address,
rpcUrl,
chainId,
});
}

static fromStore() {
try {
const version = Storage.get("version");
Expand Down

0 comments on commit 76509c2

Please sign in to comment.