|
| 1 | +import { withTestEnvOverride } from '@test/utils/test-env-override.js' |
1 | 2 | import { describe, expect, it } from 'vitest' |
| 3 | +import type { CustodialWallet } from '../db/schema/index.js' |
2 | 4 | import { |
3 | 5 | decryptPrivateKey, |
4 | 6 | encryptPrivateKey, |
5 | 7 | generateCustodialWallet, |
6 | 8 | getAddress, |
| 9 | + signWalletMessage, |
7 | 10 | } from './custodial-wallet.js' |
8 | 11 |
|
| 12 | +function createMockWallet(overrides: Partial<CustodialWallet> = {}): CustodialWallet { |
| 13 | + return { |
| 14 | + id: 'test', |
| 15 | + userId: 'test', |
| 16 | + address: '0x0000000000000000000000000000000000000001', |
| 17 | + encryptedPrivateKey: 'x', |
| 18 | + chainId: 11155111, |
| 19 | + createdAt: new Date(), |
| 20 | + updatedAt: new Date(), |
| 21 | + ...overrides, |
| 22 | + } |
| 23 | +} |
| 24 | + |
9 | 25 | describe('custodial-wallet', () => { |
10 | 26 | describe('generateCustodialWallet', () => { |
11 | 27 | it('should produce address and privateKey', async () => { |
@@ -52,4 +68,64 @@ describe('custodial-wallet', () => { |
52 | 68 | if (enc2) expect(decryptPrivateKey(enc2)).toBe(privateKey) |
53 | 69 | }) |
54 | 70 | }) |
| 71 | + |
| 72 | + describe('signWalletMessage', () => { |
| 73 | + it('should return hex signature for valid wallet', async () => { |
| 74 | + const { address, privateKey } = await generateCustodialWallet() |
| 75 | + const encrypted = encryptPrivateKey(privateKey) |
| 76 | + if (!encrypted) throw new Error('encrypt failed') |
| 77 | + |
| 78 | + const wallet = createMockWallet({ address, encryptedPrivateKey: encrypted }) |
| 79 | + const signed = await signWalletMessage(wallet, 'Hello') |
| 80 | + expect(signed).toMatch(/^0x[a-fA-F0-9]+$/) |
| 81 | + expect(signed.length).toBeGreaterThan(130) |
| 82 | + }) |
| 83 | + |
| 84 | + it('should throw when encrypted key fails to decrypt', async () => { |
| 85 | + const wallet = createMockWallet({ encryptedPrivateKey: 'invalid' }) |
| 86 | + await expect(signWalletMessage(wallet, 'x')).rejects.toThrow('Failed to decrypt') |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + describe('getWalletBalance', () => { |
| 91 | + it('should return balance string when TEST_BALANCE_OVERRIDE is set', async () => { |
| 92 | + await withTestEnvOverride('TEST_BALANCE_OVERRIDE', '1000000000000000000', async () => { |
| 93 | + const { getWalletBalance } = await import('./custodial-wallet.js') |
| 94 | + const wallet = createMockWallet() |
| 95 | + const balance = await getWalletBalance(wallet) |
| 96 | + expect(balance).toBe('1000000000000000000') |
| 97 | + }) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + describe('sendWalletTransaction', () => { |
| 102 | + it('should return hash when TEST_SEND_HASH_OVERRIDE is set', async () => { |
| 103 | + await withTestEnvOverride('TEST_SEND_HASH_OVERRIDE', '0xabcd1234', async () => { |
| 104 | + const { sendWalletTransaction } = await import('./custodial-wallet.js') |
| 105 | + const { address, privateKey } = await generateCustodialWallet() |
| 106 | + const encrypted = encryptPrivateKey(privateKey) |
| 107 | + if (!encrypted) throw new Error('encrypt failed') |
| 108 | + |
| 109 | + const wallet = createMockWallet({ address, encryptedPrivateKey: encrypted }) |
| 110 | + const hash = await sendWalletTransaction( |
| 111 | + wallet, |
| 112 | + '0x0000000000000000000000000000000000000001', |
| 113 | + '0.001', |
| 114 | + ) |
| 115 | + expect(hash).toBe('0xabcd1234') |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should throw INSUFFICIENT_FUNDS when RPC returns insufficient funds', async () => { |
| 120 | + const { sendWalletTransaction } = await import('./custodial-wallet.js') |
| 121 | + const { address, privateKey } = await generateCustodialWallet() |
| 122 | + const encrypted = encryptPrivateKey(privateKey) |
| 123 | + if (!encrypted) throw new Error('encrypt failed') |
| 124 | + |
| 125 | + const wallet = createMockWallet({ address, encryptedPrivateKey: encrypted }) |
| 126 | + await expect( |
| 127 | + sendWalletTransaction(wallet, '0x0000000000000000000000000000000000000001', '1'), |
| 128 | + ).rejects.toMatchObject({ code: 'INSUFFICIENT_FUNDS' }) |
| 129 | + }) |
| 130 | + }) |
55 | 131 | }) |
0 commit comments