Skip to content

Commit

Permalink
fix: no session connection (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 authored Dec 5, 2024
1 parent 7d62bf9 commit 7b2521c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
8 changes: 6 additions & 2 deletions packages/auth-server/components/views/confirmation/Send.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ const { getClient } = useClientStore();
const transactionParams = computed(() => {
const params = request.value!.content.action.params as ExtractParams<"eth_sendTransaction">;
// convert rpc formatted data to actual values (e.g. convert hex to BigInt)
// Convert RPC formatted data to actual values (e.g. convert hex to BigInt)
const formatted = chainConfig.formatters.transaction.format(params[0] as ZksyncRpcTransaction);
return formatted as SendTransactionRequest;
if (!formatted.gas) {
formatted.gas = 100_000_000n; // TODO: remove when gas estimation for passkey client is fixed
}
// Remove undefined and null values
return Object.fromEntries(Object.entries(formatted).filter(([_, val]) => val !== undefined && val !== null)) as SendTransactionRequest;
});
const advancedInfoOpened = ref(false);
Expand Down
61 changes: 41 additions & 20 deletions packages/sdk/src/client-auth-server/Signer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Address, type Chain, type Hash, hexToNumber, http, type RpcSchema as RpcSchemaGeneric, type SendTransactionParameters, type Transport } from "viem";
import { type Address, type Chain, createWalletClient, custom, type Hash, http, type RpcSchema as RpcSchemaGeneric, type SendTransactionParameters, type Transport, type WalletClient } from "viem";

import { createZksyncSessionClient, type ZksyncSsoSessionClient } from "../client/index.js";
import type { Communicator } from "../communicator/index.js";
Expand All @@ -20,7 +20,7 @@ type Account = {
interface SignerInterface {
accounts: Address[];
chain: Chain;
getClient(parameters?: { chainId?: number }): ZksyncSsoSessionClient;
getClient(parameters?: { chainId?: number }): ZksyncSsoSessionClient | WalletClient;
handshake(): Promise<Address[]>;
request<TMethod extends Method>(request: RequestArguments<TMethod>): Promise<ExtractReturnType<TMethod>>;
disconnect: () => Promise<void>;
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Signer implements SignerInterface {

private _account: StorageItem<Account | null>;
private _chainsInfo = new StorageItem<ChainsInfo>(StorageItem.scopedStorageKey("chainsInfo"), []);
private walletClient: ZksyncSsoSessionClient | undefined;
private client: { instance: ZksyncSsoSessionClient; type: "session" } | { instance: WalletClient; type: "auth-server" } | undefined;

constructor({ metadata, communicator, updateListener, session, chains, transports }: SignerConstructorParams) {
if (!chains.length) throw new Error("At least one chain must be included in the config");
Expand Down Expand Up @@ -84,6 +84,10 @@ export class Signer implements SignerInterface {
}
}

get walletClient() {
return this.client?.instance as WalletClient | undefined;
}

getClient(parameters?: { chainId?: number }) {
const chainId = parameters?.chainId || this.chain.id;
const chain = this.chains.find((e) => e.id === chainId);
Expand Down Expand Up @@ -123,16 +127,29 @@ export class Signer implements SignerInterface {
if (!this.account) throw new Error("Account is not set");
if (!chainInfo) throw new Error(`Chain info for ${chain} wasn't set during handshake`);
if (session) {
this.walletClient = createZksyncSessionClient({
address: this.account.address,
sessionKey: session.sessionKey,
sessionConfig: session.sessionConfig,
contracts: chainInfo.contracts,
chain,
transport: this.transports[chain.id] || http(),
});
this.client = {
type: "session",
instance: createZksyncSessionClient({
address: this.account.address,
sessionKey: session.sessionKey,
sessionConfig: session.sessionConfig,
contracts: chainInfo.contracts,
chain,
transport: this.transports[chain.id] || http(),
}),
};
} else {
this.walletClient = undefined;
this.client = {
type: "auth-server",
instance: createWalletClient({
key: "zksync-sso-auth-server-wallet",
account: this.account.address,
chain,
transport: custom({
request: this.request.bind(this),
}),
}),
};
}
}

Expand Down Expand Up @@ -205,25 +222,29 @@ export class Signer implements SignerInterface {
}

private async tryLocalHandling<TMethod extends Method>(request: RequestArguments<TMethod>): Promise<ExtractReturnType<TMethod> | undefined> {
const client = this.walletClient;
const originalClient = this.client;

switch (request.method) {
case "eth_estimateGas": {
if (!this.walletClient || !this.session) return undefined;
if (!client) return undefined;
const params = request.params as ExtractParams<"eth_estimateGas">;
const res = await this.walletClient.request({ method: request.method, params: params });
const res = await client.request({ method: request.method, params: params });
return res as ExtractReturnType<TMethod>;
}
case "eth_sendTransaction": {
if (!this.walletClient || !this.session) return undefined;
if (originalClient?.type !== "session") return undefined;
const params = request.params as ExtractParams<"eth_sendTransaction">;
const transactionRequest = params[0];
const res = await this.walletClient.sendTransaction(transactionRequest as unknown as SendTransactionParameters);
const res = await originalClient.instance.sendTransaction(transactionRequest as unknown as SendTransactionParameters);
return res as ExtractReturnType<TMethod>;
}
case "wallet_switchEthereumChain": {
const params = request.params as ExtractParams<"wallet_switchEthereumChain">;
const chainId = params[0].chainId;
const switched = this.switchChain(typeof chainId === "string" ? hexToNumber(chainId as Hash) : chainId);
return switched ? (null as ExtractReturnType<TMethod>) : undefined;
throw new Error("Chain switching is not supported yet");
// const params = request.params as ExtractParams<"wallet_switchEthereumChain">;
// const chainId = params[0].chainId;
// const switched = this.switchChain(typeof chainId === "string" ? hexToNumber(chainId as Hash) : chainId);
// return switched ? (null as ExtractReturnType<TMethod>) : undefined;
}
case "wallet_getCapabilities": {
const chainInfo = this.chainsInfo.find((e) => e.id === this.chain.id);
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/client-auth-server/interface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from "eventemitter3";
import type { RpcSchema as RpcSchemaGeneric } from "viem";
import type { RpcSchema as RpcSchemaGeneric, WalletClient } from "viem";

import type { ZksyncSsoSessionClient } from "../client/index.js";
import type { ExtractParams, ExtractReturnType, Method, RpcSchema } from "./rpc.js";
Expand Down Expand Up @@ -27,7 +27,7 @@ interface ProviderConnectInfo {
export interface ProviderInterface extends EventEmitter {
request<M extends Method>(args: RequestArguments<M>): Promise<ExtractReturnType<M>>;
disconnect(): Promise<void>;
getClient(parameters?: { chainId?: number }): Promise<ZksyncSsoSessionClient> | ZksyncSsoSessionClient;
getClient(parameters?: { chainId?: number }): Promise<ZksyncSsoSessionClient | WalletClient> | (ZksyncSsoSessionClient | WalletClient);
on(event: "connect", listener: (info: ProviderConnectInfo) => void): this;
on(event: "disconnect", listener: (error: ProviderRpcError) => void): this;
on(event: "chainChanged", listener: (chainId: string) => void): this;
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/connector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const isSsoSessionClientConnected = async<
return isSsoSessionClient(connectorClient);
};

export const getConnectedSsoClient = async<
export const getConnectedSsoSessionClient = async<
config extends Config,
chainId extends config["chains"][number]["id"],
>(
Expand Down

0 comments on commit 7b2521c

Please sign in to comment.