|
| 1 | +import type { TurnkeySDKClientBase } from '@turnkey/core' |
| 2 | +import type { TurnkeyClient as TurnkeyHttpClient } from '@turnkey/http' |
| 3 | +import type { TurnkeyServerClient } from '@turnkey/sdk-server' |
| 4 | +import { createAccount } from '@turnkey/viem' |
| 5 | +import type { LocalAccount, WalletClient } from 'viem' |
| 6 | +import { createWalletClient } from 'viem' |
| 7 | +import { unichain } from 'viem/chains' |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +import type { ChainManager } from '@/services/ChainManager.js' |
| 11 | +import { MockChainManager } from '@/test/MockChainManager.js' |
| 12 | +import { getRandomAddress } from '@/test/utils.js' |
| 13 | +import { TurnkeyWallet } from '@/wallet/TurnkeyWallet.js' |
| 14 | + |
| 15 | +vi.mock('viem', async () => ({ |
| 16 | + // @ts-ignore - importActual returns unknown |
| 17 | + ...(await vi.importActual('viem')), |
| 18 | + createWalletClient: vi.fn(), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@turnkey/viem', async () => ({ |
| 22 | + createAccount: vi.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +const mockAddress = getRandomAddress() |
| 26 | +const mockChainManager = new MockChainManager({ |
| 27 | + supportedChains: [unichain.id], |
| 28 | +}) as unknown as ChainManager |
| 29 | + |
| 30 | +function createMockTurnkeyClient(): |
| 31 | + | TurnkeyHttpClient |
| 32 | + | TurnkeyServerClient |
| 33 | + | TurnkeySDKClientBase { |
| 34 | + return { |
| 35 | + // minimal shape for typing; createAccount uses this via @turnkey/viem |
| 36 | + } as unknown as TurnkeyHttpClient |
| 37 | +} |
| 38 | + |
| 39 | +describe('TurnkeyWallet', () => { |
| 40 | + beforeEach(() => { |
| 41 | + vi.clearAllMocks() |
| 42 | + }) |
| 43 | + |
| 44 | + it('should initialize signer and address from Turnkey account', async () => { |
| 45 | + const mockLocalAccount = { address: mockAddress } as unknown as LocalAccount |
| 46 | + vi.mocked(createAccount).mockResolvedValue(mockLocalAccount) |
| 47 | + |
| 48 | + const wallet = await TurnkeyWallet.create({ |
| 49 | + client: createMockTurnkeyClient(), |
| 50 | + organizationId: 'org_123', |
| 51 | + signWith: 'key_abc', |
| 52 | + chainManager: mockChainManager, |
| 53 | + }) |
| 54 | + |
| 55 | + expect(wallet.address).toBe(mockAddress) |
| 56 | + expect(wallet.signer).toBe(mockLocalAccount) |
| 57 | + expect(createAccount).toHaveBeenCalledOnce() |
| 58 | + const args = vi.mocked(createAccount).mock.calls[0][0] |
| 59 | + expect(args.client).toEqual(createMockTurnkeyClient()) |
| 60 | + expect(args.organizationId).toBe('org_123') |
| 61 | + expect(args.signWith).toBe('key_abc') |
| 62 | + expect(args.ethereumAddress).toBeUndefined() |
| 63 | + }) |
| 64 | + |
| 65 | + it('takes ethereumAddress', async () => { |
| 66 | + const mockLocalAccount = { address: mockAddress } as unknown as LocalAccount |
| 67 | + vi.mocked(createAccount).mockResolvedValue(mockLocalAccount) |
| 68 | + |
| 69 | + await TurnkeyWallet.create({ |
| 70 | + client: createMockTurnkeyClient(), |
| 71 | + organizationId: 'org_123', |
| 72 | + signWith: 'key_abc', |
| 73 | + ethereumAddress: '0x123', |
| 74 | + chainManager: mockChainManager, |
| 75 | + }) |
| 76 | + |
| 77 | + const args = vi.mocked(createAccount).mock.calls[0][0] |
| 78 | + expect(args.ethereumAddress).toBe('0x123') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should create a wallet client with correct configuration', async () => { |
| 82 | + const mockLocalAccount = { address: mockAddress } as unknown as LocalAccount |
| 83 | + vi.mocked(createAccount).mockResolvedValue(mockLocalAccount) |
| 84 | + const wallet = await TurnkeyWallet.create({ |
| 85 | + client: createMockTurnkeyClient(), |
| 86 | + organizationId: 'org_123', |
| 87 | + signWith: 'key_abc', |
| 88 | + chainManager: mockChainManager, |
| 89 | + }) |
| 90 | + const mockWalletClient = { |
| 91 | + account: mockLocalAccount, |
| 92 | + address: mockAddress, |
| 93 | + } as unknown as WalletClient |
| 94 | + vi.mocked(createWalletClient).mockResolvedValue(mockWalletClient) |
| 95 | + |
| 96 | + const walletClient = await wallet.walletClient(unichain.id) |
| 97 | + |
| 98 | + expect(createWalletClient).toHaveBeenCalledOnce() |
| 99 | + const args = vi.mocked(createWalletClient).mock.calls[0][0] |
| 100 | + expect(args.account).toBe(mockLocalAccount) |
| 101 | + expect(args.chain).toBe(mockChainManager.getChain(unichain.id)) |
| 102 | + expect(walletClient).toBe(mockWalletClient) |
| 103 | + }) |
| 104 | +}) |
0 commit comments