|
| 1 | +import type { ProviderInterface } from "@base-org/account"; |
| 2 | +import * as ox__Hex from "ox/Hex"; |
| 3 | +import * as ox__TypedData from "ox/TypedData"; |
| 4 | +import { beforeEach, describe, expect, test, vi } from "vitest"; |
| 5 | +import { TEST_WALLET_A } from "~test/addresses.js"; |
| 6 | +import { BASE_ACCOUNT } from "../constants.js"; |
| 7 | +import type { Wallet } from "../interfaces/wallet.js"; |
| 8 | +import { |
| 9 | + autoConnectBaseAccountSDK, |
| 10 | + connectBaseAccountSDK, |
| 11 | + isBaseAccountSDKWallet, |
| 12 | +} from "./base-account-web.js"; |
| 13 | + |
| 14 | +// Mock dependencies |
| 15 | +vi.mock("@base-org/account", () => ({ |
| 16 | + createBaseAccountSDK: vi.fn(() => ({ |
| 17 | + getProvider: () => ({ |
| 18 | + disconnect: vi.fn(), |
| 19 | + emit: vi.fn(), |
| 20 | + off: vi.fn(), |
| 21 | + on: vi.fn(), |
| 22 | + request: vi.fn(), |
| 23 | + }), |
| 24 | + })), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("../../utils/address.js", () => ({ |
| 28 | + getAddress: vi.fn((address) => address), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("../../chains/utils.js", () => ({ |
| 32 | + getCachedChain: vi.fn((chainId) => ({ id: chainId })), |
| 33 | + getChainMetadata: vi.fn(async (_chain) => ({ |
| 34 | + explorers: [{ url: "https://explorer.test" }], |
| 35 | + name: "Test Chain", |
| 36 | + nativeCurrency: { decimals: 18, name: "Test Coin", symbol: "TC" }, |
| 37 | + })), |
| 38 | +})); |
| 39 | + |
| 40 | +vi.mock("../utils/normalizeChainId.js", () => ({ |
| 41 | + normalizeChainId: vi.fn((chainId) => Number(chainId)), |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock("ox/Hex", async () => { |
| 45 | + const actualModule = await vi.importActual("ox/Hex"); |
| 46 | + return { |
| 47 | + ...actualModule, |
| 48 | + toNumber: vi.fn((hex) => Number.parseInt(hex, 16)), |
| 49 | + validate: vi.fn(() => true), |
| 50 | + }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock("ox/TypedData", () => ({ |
| 54 | + extractEip712DomainTypes: vi.fn(() => []), |
| 55 | + serialize: vi.fn(() => "serializedData"), |
| 56 | + validate: vi.fn(), |
| 57 | +})); |
| 58 | + |
| 59 | +describe("Base Account Web", () => { |
| 60 | + let provider: ProviderInterface; |
| 61 | + |
| 62 | + beforeEach(async () => { |
| 63 | + // Reset module to clear cached provider |
| 64 | + vi.resetModules(); |
| 65 | + const module = await import("./base-account-web.js"); |
| 66 | + provider = await module.getBaseAccountWebProvider(); |
| 67 | + }); |
| 68 | + |
| 69 | + test("getBaseAccountWebProvider initializes provider", async () => { |
| 70 | + expect(provider).toBeDefined(); |
| 71 | + expect(provider.request).toBeInstanceOf(Function); |
| 72 | + }); |
| 73 | + |
| 74 | + test("isBaseAccountSDKWallet returns true for Base Account wallet", () => { |
| 75 | + const wallet: Wallet = { id: BASE_ACCOUNT } as Wallet; |
| 76 | + expect(isBaseAccountSDKWallet(wallet)).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + test("isBaseAccountSDKWallet returns false for non-Base Account wallet", () => { |
| 80 | + const wallet: Wallet = { id: "other" } as unknown as Wallet; |
| 81 | + expect(isBaseAccountSDKWallet(wallet)).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + test("connectBaseAccountSDK connects to the wallet", async () => { |
| 85 | + provider.request = vi |
| 86 | + .fn() |
| 87 | + .mockResolvedValueOnce([TEST_WALLET_A]) |
| 88 | + .mockResolvedValueOnce("0x1"); |
| 89 | + const emitter = { emit: vi.fn() }; |
| 90 | + const options = { client: {} }; |
| 91 | + |
| 92 | + const [account, chain] = await connectBaseAccountSDK( |
| 93 | + // biome-ignore lint/suspicious/noExplicitAny: Inside tests |
| 94 | + options as any, |
| 95 | + // biome-ignore lint/suspicious/noExplicitAny: Inside tests |
| 96 | + emitter as any, |
| 97 | + provider, |
| 98 | + ); |
| 99 | + |
| 100 | + expect(account.address).toBe(TEST_WALLET_A); |
| 101 | + expect(chain.id).toBe(1); |
| 102 | + }); |
| 103 | + |
| 104 | + test("autoConnectBaseAccountSDK auto-connects to the wallet", async () => { |
| 105 | + provider.request = vi |
| 106 | + .fn() |
| 107 | + .mockResolvedValueOnce([TEST_WALLET_A]) |
| 108 | + .mockResolvedValueOnce("0x1"); |
| 109 | + const emitter = { emit: vi.fn() }; |
| 110 | + const options = { client: {} }; |
| 111 | + |
| 112 | + const [account, chain] = await autoConnectBaseAccountSDK( |
| 113 | + // biome-ignore lint/suspicious/noExplicitAny: Inside tests |
| 114 | + options as any, |
| 115 | + // biome-ignore lint/suspicious/noExplicitAny: Inside tests |
| 116 | + emitter as any, |
| 117 | + provider, |
| 118 | + ); |
| 119 | + |
| 120 | + expect(account.address).toBe(TEST_WALLET_A); |
| 121 | + expect(chain.id).toBe(1); |
| 122 | + }); |
| 123 | + |
| 124 | + test("signMessage uses ox__Hex for validation", async () => { |
| 125 | + const account = { |
| 126 | + address: TEST_WALLET_A, |
| 127 | + signMessage: async ({ message }: { message: string }) => { |
| 128 | + const messageToSign = `0x${ox__Hex.fromString(message)}`; |
| 129 | + const res = await provider.request({ |
| 130 | + method: "personal_sign", |
| 131 | + params: [messageToSign, account.address], |
| 132 | + }); |
| 133 | + expect(ox__Hex.validate(res)).toBe(true); |
| 134 | + return res; |
| 135 | + }, |
| 136 | + }; |
| 137 | + |
| 138 | + provider.request = vi.fn().mockResolvedValue("0xsignature"); |
| 139 | + const signature = await account.signMessage({ message: "hello" }); |
| 140 | + expect(signature).toBe("0xsignature"); |
| 141 | + }); |
| 142 | + |
| 143 | + test("signTypedData uses ox__TypedData for serialization", async () => { |
| 144 | + const account = { |
| 145 | + address: TEST_WALLET_A, |
| 146 | + // biome-ignore lint/suspicious/noExplicitAny: Inside tests |
| 147 | + signTypedData: async (typedData: any) => { |
| 148 | + const { domain, message, primaryType } = typedData; |
| 149 | + const types = { |
| 150 | + EIP712Domain: ox__TypedData.extractEip712DomainTypes(domain), |
| 151 | + ...typedData.types, |
| 152 | + }; |
| 153 | + ox__TypedData.validate({ domain, message, primaryType, types }); |
| 154 | + const stringifiedData = ox__TypedData.serialize({ |
| 155 | + domain: domain ?? {}, |
| 156 | + message, |
| 157 | + primaryType, |
| 158 | + types, |
| 159 | + }); |
| 160 | + const res = await provider.request({ |
| 161 | + method: "eth_signTypedData_v4", |
| 162 | + params: [account.address, stringifiedData], |
| 163 | + }); |
| 164 | + expect(ox__Hex.validate(res)).toBe(true); |
| 165 | + return res; |
| 166 | + }, |
| 167 | + }; |
| 168 | + |
| 169 | + provider.request = vi.fn().mockResolvedValue("0xsignature"); |
| 170 | + const signature = await account.signTypedData({ |
| 171 | + domain: {}, |
| 172 | + message: {}, |
| 173 | + primaryType: "EIP712Domain", |
| 174 | + types: {}, |
| 175 | + }); |
| 176 | + expect(signature).toBe("0xsignature"); |
| 177 | + }); |
| 178 | +}); |
0 commit comments