Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion app-kit-bridge-evm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest run",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "~7.0.2",
"vite": "^8.2.1"
"vite": "^8.2.1",
"vitest": "^4.1.9"
},
"dependencies": {
"@circle-fin/app-kit": "^1.12.0",
Expand Down
69 changes: 69 additions & 0 deletions app-kit-bridge-evm/src/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2026 Circle Internet Group, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, it, vi } from "vitest";
import { connectEvmProvider } from "./connect.ts";
import type { BrowserWalletProvider } from "./connect.ts";

describe("connectEvmProvider", () => {
it("prefers accounts returned by eth_requestAccounts", async () => {
const provider = {
request: vi
.fn()
.mockResolvedValueOnce(["0x1111111111111111111111111111111111111111"]),
} as unknown as BrowserWalletProvider;

await expect(connectEvmProvider(provider)).resolves.toEqual({
provider,
account: "0x1111111111111111111111111111111111111111",
});
expect(provider.request).toHaveBeenCalledTimes(1);
expect(provider.request).toHaveBeenCalledWith({
method: "eth_requestAccounts",
params: undefined,
});
});

it("falls back to eth_accounts when requestAccounts returns empty", async () => {
const provider = {
request: vi
.fn()
.mockResolvedValueOnce([])
.mockResolvedValueOnce(["0x2222222222222222222222222222222222222222"]),
} as unknown as BrowserWalletProvider;

await expect(connectEvmProvider(provider)).resolves.toEqual({
provider,
account: "0x2222222222222222222222222222222222222222",
});
expect(provider.request).toHaveBeenNthCalledWith(2, {
method: "eth_accounts",
params: undefined,
});
});

it("throws when no account is available so callers stay disconnected", async () => {
const provider = {
request: vi.fn().mockResolvedValueOnce([]).mockResolvedValueOnce([]),
} as unknown as BrowserWalletProvider;

await expect(connectEvmProvider(provider)).rejects.toThrow(
"No wallet account available",
);
});
});
49 changes: 49 additions & 0 deletions app-kit-bridge-evm/src/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Copyright 2026 Circle Internet Group, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import type { CreateViemAdapterFromProviderParams } from "@circle-fin/adapter-viem-v2";

export type BrowserWalletProvider =
CreateViemAdapterFromProviderParams["provider"];

/**
* Request wallet permission and resolve a concrete account.
* Prefers accounts from eth_requestAccounts; falls back to eth_accounts.
* Throws when no account is available so callers stay disconnected.
*/
export async function connectEvmProvider(provider: BrowserWalletProvider) {
const requestedAccounts = (await provider.request({
method: "eth_requestAccounts",
params: undefined,
})) as string[] | undefined;

let account = requestedAccounts?.[0];
if (!account) {
const accounts = (await provider.request({
method: "eth_accounts",
params: undefined,
})) as string[];
account = accounts[0];
}

if (!account) {
throw new Error("No wallet account available");
}

return { provider, account };
}
22 changes: 8 additions & 14 deletions app-kit-bridge-evm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

import { AppKit } from "@circle-fin/app-kit";
import { createViemAdapterFromProvider } from "@circle-fin/adapter-viem-v2";
import type { CreateViemAdapterFromProviderParams } from "@circle-fin/adapter-viem-v2";

type BrowserWalletProvider = CreateViemAdapterFromProviderParams["provider"];
import { connectEvmProvider } from "./connect.ts";
import type { BrowserWalletProvider } from "./connect.ts";

type EIP6963ProviderDetail = {
info: {
Expand Down Expand Up @@ -71,19 +70,14 @@ async function handleEvmConnect() {
try {
connectEvmButton.disabled = true;

evmProvider = await getProvider();
await evmProvider.request({
method: "eth_requestAccounts",
params: undefined,
});
const accounts = (await evmProvider.request({
method: "eth_accounts",
params: undefined,
})) as string[];
const connection = await connectEvmProvider(await getProvider());
evmProvider = connection.provider;

walletInfo.textContent = accounts[0] ?? "Connected";
bridgeButton.disabled = !evmProvider;
walletInfo.textContent = connection.account;
bridgeButton.disabled = false;
} catch (error) {
evmProvider = null;
bridgeButton.disabled = true;
render({ error: error instanceof Error ? error.message : "Unknown error" });
} finally {
connectEvmButton.disabled = Boolean(evmProvider);
Expand Down
4 changes: 3 additions & 1 deletion app-kit-bridge-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest run",
"preview": "vite preview"
},
"devDependencies": {
"typescript": "~7.0.2",
"vite": "^8.2.1"
"vite": "^8.2.1",
"vitest": "^4.1.9"
},
"dependencies": {
"@circle-fin/adapter-solana": "^1.6.5",
Expand Down
77 changes: 77 additions & 0 deletions app-kit-bridge-solana/src/connect.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright 2026 Circle Internet Group, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, it, vi } from "vitest";
import { connectEvmProvider, connectSolanaProvider } from "./connect.ts";
import type {
BrowserWalletProvider,
SolanaWalletProvider,
} from "./connect.ts";

describe("connectEvmProvider", () => {
it("prefers accounts returned by eth_requestAccounts", async () => {
const provider = {
request: vi
.fn()
.mockResolvedValueOnce(["0x1111111111111111111111111111111111111111"]),
} as unknown as BrowserWalletProvider;

await expect(connectEvmProvider(provider)).resolves.toEqual({
provider,
account: "0x1111111111111111111111111111111111111111",
});
expect(provider.request).toHaveBeenCalledTimes(1);
});

it("throws when no account is available so callers stay disconnected", async () => {
const provider = {
request: vi.fn().mockResolvedValueOnce([]).mockResolvedValueOnce([]),
} as unknown as BrowserWalletProvider;

await expect(connectEvmProvider(provider)).rejects.toThrow(
"No wallet account available",
);
});
});

describe("connectSolanaProvider", () => {
it("returns the connected address when present", async () => {
const provider = {
connect: vi.fn().mockResolvedValue({
publicKey: { toString: () => "So11111111111111111111111111111111111111112" },
}),
publicKey: null,
} as unknown as SolanaWalletProvider;

await expect(connectSolanaProvider(provider)).resolves.toEqual({
provider,
address: "So11111111111111111111111111111111111111112",
});
});

it("throws when address is missing so callers stay disconnected", async () => {
const provider = {
connect: vi.fn().mockResolvedValue({ publicKey: null }),
publicKey: null,
} as unknown as SolanaWalletProvider;

await expect(connectSolanaProvider(provider)).rejects.toThrow(
"No Solana wallet address available",
);
});
});
68 changes: 68 additions & 0 deletions app-kit-bridge-solana/src/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2026 Circle Internet Group, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

import type { CreateSolanaAdapterFromProviderParams } from "@circle-fin/adapter-solana";
import type { CreateViemAdapterFromProviderParams } from "@circle-fin/adapter-viem-v2";

export type BrowserWalletProvider =
CreateViemAdapterFromProviderParams["provider"];
export type SolanaWalletProvider =
CreateSolanaAdapterFromProviderParams["provider"];

/**
* Request wallet permission and resolve a concrete account.
* Prefers accounts from eth_requestAccounts; falls back to eth_accounts.
* Throws when no account is available so callers stay disconnected.
*/
export async function connectEvmProvider(provider: BrowserWalletProvider) {
const requestedAccounts = (await provider.request({
method: "eth_requestAccounts",
params: undefined,
})) as string[] | undefined;

let account = requestedAccounts?.[0];
if (!account) {
const accounts = (await provider.request({
method: "eth_accounts",
params: undefined,
})) as string[];
account = accounts[0];
}

if (!account) {
throw new Error("No wallet account available");
}

return { provider, account };
}

/**
* Connect a Solana provider and require a resolved address.
* Throws when the address is missing so callers stay disconnected.
*/
export async function connectSolanaProvider(provider: SolanaWalletProvider) {
const connection = await provider.connect();
const address =
connection.publicKey?.toString() ?? provider.publicKey?.toString();

if (!address) {
throw new Error("No Solana wallet address available");
}

return { provider, address };
}
Loading