Skip to content
Merged
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
115 changes: 96 additions & 19 deletions packages/thirdweb/src/wallets/wallet-connect/controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { UniversalProvider } from "@walletconnect/universal-provider";
import type {
RequestArguments,
UniversalProvider,
} from "@walletconnect/universal-provider";
import type { Address } from "abitype";
import {
getTypesForEIP712Domain,
Expand Down Expand Up @@ -39,6 +42,7 @@
import { getDefaultAppMetadata } from "../utils/defaultDappMetadata.js";
import { normalizeChainId } from "../utils/normalizeChainId.js";
import type { WalletEmitter } from "../wallet-emitter.js";
import type { WalletInfo } from "../wallet-info.js";
import type { WalletId } from "../wallet-types.js";
import { DEFAULT_PROJECT_ID, NAMESPACE } from "./constants.js";
import type { WCAutoConnectOptions, WCConnectOptions } from "./types.js";
Expand Down Expand Up @@ -78,16 +82,16 @@
emitter: WalletEmitter<WCSupportedWalletIds>,
walletId: WCSupportedWalletIds | "walletConnect",
storage: AsyncStorage,
sessionHandler?: (uri: string) => void,
sessionHandler?: (uri: string) => void | Promise<void>,

Check warning on line 85 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L85

Added line #L85 was not covered by tests
): Promise<ReturnType<typeof onConnect>> {
const provider = await initProvider(options, walletId, sessionHandler);
const wcOptions = options.walletConnect;

let { onDisplayUri } = wcOptions || {};
const walletInfo = await getWalletInfo(walletId);

Check warning on line 91 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L91

Added line #L91 was not covered by tests

// use default sessionHandler unless onDisplayUri is explicitly provided
if (!onDisplayUri && sessionHandler) {
const walletInfo = await getWalletInfo(walletId);
const deeplinkHandler = (uri: string) => {
const appUrl = walletInfo.mobile.native || walletInfo.mobile.universal;
if (!appUrl) {
Expand Down Expand Up @@ -185,7 +189,16 @@
provider.events.removeListener("display_uri", wcOptions.onDisplayUri);
}

return onConnect(address, chain, provider, emitter, storage, options.client);
return onConnect(
address,
chain,
provider,
emitter,
storage,
options.client,
walletInfo,
sessionHandler,
);

Check warning on line 201 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L192-L201

Added lines #L192 - L201 were not covered by tests
}

/**
Expand All @@ -197,12 +210,14 @@
emitter: WalletEmitter<WCSupportedWalletIds>,
walletId: WCSupportedWalletIds | "walletConnect",
storage: AsyncStorage,
sessionHandler?: (uri: string) => void,
sessionHandler?: (uri: string) => void | Promise<void>,

Check warning on line 213 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L213

Added line #L213 was not covered by tests
): Promise<ReturnType<typeof onConnect>> {
const savedConnectParams: SavedConnectParams | null = storage
? await getSavedConnectParamsFromStorage(storage, walletId)
: null;

const walletInfo = await getWalletInfo(walletId);

Check warning on line 219 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L219

Added line #L219 was not covered by tests

const provider = await initProvider(
savedConnectParams
? {
Expand Down Expand Up @@ -243,7 +258,16 @@
? options.chain
: getCachedChain(providerChainId);

return onConnect(address, chain, provider, emitter, storage, options.client);
return onConnect(
address,
chain,
provider,
emitter,
storage,
options.client,
walletInfo,
sessionHandler,
);

Check warning on line 270 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L261-L270

Added lines #L261 - L270 were not covered by tests
}

// Connection utils -----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -284,6 +308,10 @@
],
name: wcOptions?.appMetadata?.name || getDefaultAppMetadata().name,
url: wcOptions?.appMetadata?.url || getDefaultAppMetadata().url,
redirect: {
native: walletInfo.mobile.native || undefined,
universal: walletInfo.mobile.universal || undefined,
},

Check warning on line 314 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L311-L314

Added lines #L311 - L314 were not covered by tests
},
projectId: wcOptions?.projectId || DEFAULT_PROJECT_ID,
});
Expand Down Expand Up @@ -321,17 +349,22 @@
address,
client,
chain,
sessionRequestHandler,
walletInfo,

Check warning on line 353 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L352-L353

Added lines #L352 - L353 were not covered by tests
}: {
provider: WCProvider;
address: string;
client: ThirdwebClient;
chain: Chain;
sessionRequestHandler?: (uri: string) => void | Promise<void>;
walletInfo: WalletInfo;
}) {
const account: Account = {
address: getAddress(address),
async sendTransaction(tx: SendTransactionOption) {
const transactionHash = (await provider.request(
{
const transactionHash = (await requestAndOpenWallet({
provider,
payload: {

Check warning on line 367 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L365-L367

Added lines #L365 - L367 were not covered by tests
method: "eth_sendTransaction",
params: [
{
Expand All @@ -343,8 +376,10 @@
},
],
},
`eip155:${tx.chainId}`,
)) as Hex;
chain: `eip155:${tx.chainId}`,
walletInfo,
sessionRequestHandler,
})) as Hex;

Check warning on line 382 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L379-L382

Added lines #L379 - L382 were not covered by tests

trackTransaction({
chainId: tx.chainId,
Expand All @@ -370,13 +405,16 @@
}
return message.raw;
})();
return provider.request(
{
return requestAndOpenWallet({
provider,
payload: {

Check warning on line 410 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L408-L410

Added lines #L408 - L410 were not covered by tests
method: "personal_sign",
params: [messageToSign, this.address],
},
`eip155:${chain.id}`,
);
chain: `eip155:${chain.id}`,
walletInfo,
sessionRequestHandler,
});

Check warning on line 417 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L414-L417

Added lines #L414 - L417 were not covered by tests
},
async signTypedData(_data) {
const data = parseTypedData(_data);
Expand All @@ -399,28 +437,65 @@
types,
});

return await provider.request(
{
return await requestAndOpenWallet({
provider,
payload: {

Check warning on line 442 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L440-L442

Added lines #L440 - L442 were not covered by tests
method: "eth_signTypedData_v4",
params: [this.address, typedData],
},
`eip155:${chain.id}`,
);
chain: `eip155:${chain.id}`,
walletInfo,
sessionRequestHandler,
});

Check warning on line 449 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L446-L449

Added lines #L446 - L449 were not covered by tests
},
};

return account;
}

async function requestAndOpenWallet(args: {

Check warning on line 456 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L456

Added line #L456 was not covered by tests
provider: WCProvider;
payload: RequestArguments;
chain?: string;
walletInfo: WalletInfo;
sessionRequestHandler?: (uri: string) => void | Promise<void>;
}) {
const { provider, payload, chain, walletInfo, sessionRequestHandler } = args;
const resultPromise: Promise<`0x${string}`> = provider.request(
payload,
chain,
);

Check warning on line 467 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L462-L467

Added lines #L462 - L467 were not covered by tests

const walletLinkToOpen =
provider.session?.peer?.metadata?.redirect?.native ||
walletInfo.mobile.native ||
walletInfo.mobile.universal;

Check warning on line 472 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L469-L472

Added lines #L469 - L472 were not covered by tests

if (sessionRequestHandler && walletLinkToOpen) {
await sessionRequestHandler(walletLinkToOpen);
}

Check warning on line 476 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L474-L476

Added lines #L474 - L476 were not covered by tests

return resultPromise;
}

Check warning on line 479 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L478-L479

Added lines #L478 - L479 were not covered by tests

function onConnect(
address: string,
chain: Chain,
provider: WCProvider,
emitter: WalletEmitter<WCSupportedWalletIds>,
storage: AsyncStorage,
client: ThirdwebClient,
walletInfo: WalletInfo,
sessionRequestHandler?: (uri: string) => void | Promise<void>,

Check warning on line 489 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L488-L489

Added lines #L488 - L489 were not covered by tests
): [Account, Chain, DisconnectFn, SwitchChainFn] {
const account = createAccount({ address, chain, client, provider });
const account = createAccount({
address,
chain,
client,
provider,
sessionRequestHandler,
walletInfo,
});

Check warning on line 498 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L491-L498

Added lines #L491 - L498 were not covered by tests

async function disconnect() {
provider.removeListener("accountsChanged", onAccountsChanged);
Expand All @@ -444,6 +519,8 @@
chain,
client,
provider,
sessionRequestHandler,
walletInfo,

Check warning on line 523 in packages/thirdweb/src/wallets/wallet-connect/controller.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/wallet-connect/controller.ts#L522-L523

Added lines #L522 - L523 were not covered by tests
});
emitter.emit("accountChanged", newAccount);
emitter.emit("accountsChanged", accounts);
Expand Down
Loading