|
| 1 | +import { type Address, type Hash, type TransactionReceipt } from "viem"; |
| 2 | +import { waitForTransactionReceipt, writeContract } from "viem/actions"; |
| 3 | +import { describe, expect, test, vi } from "vitest"; |
| 4 | + |
| 5 | +import { deployAccount } from "./account.js"; |
| 6 | + |
| 7 | +// Mock the passkey utils |
| 8 | +vi.mock("../../../utils/passkey.js", () => ({ |
| 9 | + getPublicKeyBytesFromPasskeySignature: vi.fn().mockReturnValue([ |
| 10 | + Buffer.from("0000000000000000000000000000000000000000000000000000000000000001", "hex"), |
| 11 | + Buffer.from("0000000000000000000000000000000000000000000000000000000000000002", "hex"), |
| 12 | + ]), |
| 13 | +})); |
| 14 | + |
| 15 | +// Mock viem actions |
| 16 | +vi.mock("viem/actions", () => ({ |
| 17 | + writeContract: vi.fn(), |
| 18 | + waitForTransactionReceipt: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +// Add FactoryAbi mock at the top with other mocks |
| 22 | +vi.mock("../../../abi/Factory.js", () => ({ |
| 23 | + FactoryAbi: [ |
| 24 | + { |
| 25 | + inputs: [ |
| 26 | + { type: "bytes32", name: "_salt" }, |
| 27 | + { type: "string", name: "_uniqueAccountId" }, |
| 28 | + { type: "bytes[]", name: "_initialValidators" }, |
| 29 | + { type: "address[]", name: "_initialK1Owners" }, |
| 30 | + ], |
| 31 | + name: "deployProxySsoAccount", |
| 32 | + outputs: [{ type: "address", name: "accountAddress" }], |
| 33 | + stateMutability: "nonpayable", |
| 34 | + type: "function", |
| 35 | + }, |
| 36 | + ], |
| 37 | +})); |
| 38 | + |
| 39 | +describe("deployAccount", () => { |
| 40 | + // Setup common test data |
| 41 | + const mockSalt = new Uint8Array([ |
| 42 | + 213, 36, 52, 69, 251, 82, 199, 45, 113, 6, 20, 213, 78, 47, 165, |
| 43 | + 164, 106, 221, 105, 67, 247, 47, 200, 167, 137, 64, 151, 12, 179, |
| 44 | + 74, 90, 23, |
| 45 | + ]); |
| 46 | + |
| 47 | + // CBOR-encoded COSE key with known x,y coordinates |
| 48 | + const mockCredentialPublicKey = new Uint8Array([ |
| 49 | + 0xa5, // map of 5 pairs |
| 50 | + 0x01, // key 1 (kty) |
| 51 | + 0x02, // value 2 (EC2) |
| 52 | + 0x03, // key 3 (alg) |
| 53 | + 0x26, // value -7 (ES256) |
| 54 | + 0x20, // key -1 (crv) |
| 55 | + 0x01, // value 1 (P-256) |
| 56 | + 0x21, // key -2 (x coordinate) |
| 57 | + 0x58, 0x20, // bytes(32) |
| 58 | + ...new Uint8Array(32).fill(0x01), // x coordinate filled with 0x01 |
| 59 | + 0x22, // key -3 (y coordinate) |
| 60 | + 0x58, 0x20, // bytes(32) |
| 61 | + ...new Uint8Array(32).fill(0x02), // y coordinate filled with 0x02 |
| 62 | + ]); |
| 63 | + |
| 64 | + const mockClient = { |
| 65 | + account: "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", |
| 66 | + chain: { id: 1 }, |
| 67 | + } as any; |
| 68 | + const mockContracts = { |
| 69 | + accountFactory: "0x1234567890123456789012345678901234567890" as Address, |
| 70 | + passkey: "0x2234567890123456789012345678901234567890" as Address, |
| 71 | + session: "0x3234567890123456789012345678901234567890" as Address, |
| 72 | + }; |
| 73 | + |
| 74 | + const mockTransactionHash = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef" as Hash; |
| 75 | + const mockTransactionReceipt: TransactionReceipt = { |
| 76 | + status: "success", |
| 77 | + contractAddress: "0x4234567890123456789012345678901234567890", |
| 78 | + blockNumber: 1n, |
| 79 | + blockHash: "0x5e1d3a76f1b1c3a2b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7" as Hash, |
| 80 | + transactionHash: mockTransactionHash, |
| 81 | + logs: [], |
| 82 | + logsBloom: "0x", |
| 83 | + cumulativeGasUsed: 0n, |
| 84 | + effectiveGasPrice: 0n, |
| 85 | + gasUsed: 0n, |
| 86 | + type: "eip1559", |
| 87 | + from: "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266", |
| 88 | + to: "0x1234567890123456789012345678901234567890", |
| 89 | + transactionIndex: 0, |
| 90 | + }; |
| 91 | + |
| 92 | + test("deploys account successfully", async () => { |
| 93 | + // Setup mocks |
| 94 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 95 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue(mockTransactionReceipt); |
| 96 | + |
| 97 | + const result = await deployAccount(mockClient, { |
| 98 | + credentialPublicKey: mockCredentialPublicKey, |
| 99 | + contracts: mockContracts, |
| 100 | + expectedOrigin: "https://example.com", |
| 101 | + salt: mockSalt, |
| 102 | + }); |
| 103 | + |
| 104 | + // Verify the result |
| 105 | + expect(result).toEqual({ |
| 106 | + address: "0x4234567890123456789012345678901234567890", |
| 107 | + transactionReceipt: mockTransactionReceipt, |
| 108 | + }); |
| 109 | + |
| 110 | + // Verify writeContract was called with correct parameters |
| 111 | + expect(writeContract).toHaveBeenCalledWith( |
| 112 | + mockClient, |
| 113 | + expect.objectContaining({ |
| 114 | + address: mockContracts.accountFactory, |
| 115 | + functionName: "deployProxySsoAccount", |
| 116 | + }), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + test("handles transaction failure", async () => { |
| 121 | + // Setup mock for failed transaction |
| 122 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 123 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue({ |
| 124 | + ...mockTransactionReceipt, |
| 125 | + status: "reverted", |
| 126 | + }); |
| 127 | + |
| 128 | + await expect( |
| 129 | + deployAccount(mockClient, { |
| 130 | + credentialPublicKey: mockCredentialPublicKey, |
| 131 | + contracts: mockContracts, |
| 132 | + expectedOrigin: "https://example.com", |
| 133 | + salt: mockSalt, |
| 134 | + }), |
| 135 | + ).rejects.toThrow("Account deployment transaction reverted"); |
| 136 | + }); |
| 137 | + |
| 138 | + test("handles missing contract address in receipt", async () => { |
| 139 | + // Setup mock for missing contract address |
| 140 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 141 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue({ |
| 142 | + ...mockTransactionReceipt, |
| 143 | + contractAddress: null, |
| 144 | + }); |
| 145 | + |
| 146 | + await expect( |
| 147 | + deployAccount(mockClient, { |
| 148 | + credentialPublicKey: mockCredentialPublicKey, |
| 149 | + contracts: mockContracts, |
| 150 | + expectedOrigin: "https://example.com", |
| 151 | + salt: mockSalt, |
| 152 | + }), |
| 153 | + ).rejects.toThrow("No contract address in transaction receipt"); |
| 154 | + }); |
| 155 | + |
| 156 | + test("calls onTransactionSent callback when provided", async () => { |
| 157 | + const onTransactionSent = vi.fn(); |
| 158 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 159 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue(mockTransactionReceipt); |
| 160 | + |
| 161 | + await deployAccount(mockClient, { |
| 162 | + credentialPublicKey: mockCredentialPublicKey, |
| 163 | + contracts: mockContracts, |
| 164 | + expectedOrigin: "https://example.com", |
| 165 | + salt: mockSalt, |
| 166 | + onTransactionSent, |
| 167 | + }); |
| 168 | + |
| 169 | + expect(onTransactionSent).toHaveBeenCalledWith(mockTransactionHash); |
| 170 | + }); |
| 171 | + |
| 172 | + test("uses window.location.origin when expectedOrigin is not provided", async () => { |
| 173 | + // Mock window.location |
| 174 | + const originalWindow = global.window; |
| 175 | + global.window = { |
| 176 | + ...originalWindow, |
| 177 | + location: { |
| 178 | + ...originalWindow?.location, |
| 179 | + origin: "https://example.com", |
| 180 | + }, |
| 181 | + } as any; |
| 182 | + |
| 183 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 184 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue(mockTransactionReceipt); |
| 185 | + |
| 186 | + const writeContractSpy = vi.mocked(writeContract); |
| 187 | + await deployAccount(mockClient, { |
| 188 | + credentialPublicKey: mockCredentialPublicKey, |
| 189 | + contracts: mockContracts, |
| 190 | + salt: mockSalt, |
| 191 | + }); |
| 192 | + |
| 193 | + // Simpler assertion that just checks the key parts |
| 194 | + const lastCall = writeContractSpy.mock.lastCall; |
| 195 | + expect(lastCall?.[0]).toBe(mockClient); |
| 196 | + expect(lastCall?.[1]).toMatchObject({ |
| 197 | + address: mockContracts.accountFactory, |
| 198 | + functionName: "deployProxySsoAccount", |
| 199 | + }); |
| 200 | + |
| 201 | + // Restore window |
| 202 | + global.window = originalWindow; |
| 203 | + }); |
| 204 | + |
| 205 | + test("handles paymaster configuration", async () => { |
| 206 | + vi.mocked(writeContract).mockResolvedValue(mockTransactionHash); |
| 207 | + vi.mocked(waitForTransactionReceipt).mockResolvedValue(mockTransactionReceipt); |
| 208 | + |
| 209 | + const paymasterAddress = "0x5234567890123456789012345678901234567890" as Address; |
| 210 | + const paymasterInput = "0x1234" as const; |
| 211 | + |
| 212 | + await deployAccount(mockClient, { |
| 213 | + credentialPublicKey: mockCredentialPublicKey, |
| 214 | + contracts: mockContracts, |
| 215 | + expectedOrigin: "https://example.com", |
| 216 | + paymasterAddress, |
| 217 | + paymasterInput, |
| 218 | + }); |
| 219 | + |
| 220 | + expect(writeContract).toHaveBeenCalledWith( |
| 221 | + mockClient, |
| 222 | + expect.objectContaining({ |
| 223 | + paymaster: paymasterAddress, |
| 224 | + paymasterInput, |
| 225 | + }), |
| 226 | + ); |
| 227 | + }); |
| 228 | +}); |
0 commit comments