|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { WalletSDKError } from './errors'; |
| 3 | +import { encryptSeedPhrase, decryptSeedPhrase } from './backup'; |
| 4 | + |
| 5 | +// The Wallet class constructor is private and factories make API calls, |
| 6 | +// so we test the backup methods by: |
| 7 | +// 1. Testing the static decryptBackup independently (it's a thin wrapper) |
| 8 | +// 2. Testing that exportEncryptedBackup throws READ_ONLY when no mnemonic |
| 9 | +// 3. Round-trip testing using the underlying backup module functions |
| 10 | + |
| 11 | +// We need to import Wallet to test its static method and instance behavior |
| 12 | +// We'll mock the API client to avoid network calls |
| 13 | +vi.mock('./client', () => ({ |
| 14 | + WalletAPIClient: vi.fn().mockImplementation(() => ({ |
| 15 | + request: vi.fn().mockResolvedValue({ |
| 16 | + wallet_id: 'test-wallet-id', |
| 17 | + created_at: new Date().toISOString(), |
| 18 | + addresses: [], |
| 19 | + }), |
| 20 | + setSignatureAuth: vi.fn(), |
| 21 | + setJWTToken: vi.fn(), |
| 22 | + clearAuth: vi.fn(), |
| 23 | + })), |
| 24 | + hexToUint8Array: vi.fn((hex: string) => new Uint8Array(hex.match(/.{1,2}/g)?.map(b => parseInt(b, 16)) || [])), |
| 25 | + uint8ArrayToHex: vi.fn((arr: Uint8Array) => Array.from(arr).map(b => b.toString(16).padStart(2, '0')).join('')), |
| 26 | +})); |
| 27 | + |
| 28 | +// Mock the key derivation modules to avoid heavy crypto during tests |
| 29 | +vi.mock('../web-wallet/keys', () => ({ |
| 30 | + generateMnemonic: vi.fn().mockReturnValue( |
| 31 | + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about' |
| 32 | + ), |
| 33 | + isValidMnemonic: vi.fn().mockReturnValue(true), |
| 34 | + deriveWalletBundle: vi.fn().mockResolvedValue({ |
| 35 | + publicKeySecp256k1: 'mock-pub-key-secp', |
| 36 | + publicKeyEd25519: 'mock-pub-key-ed', |
| 37 | + privateKeySecp256k1: '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', |
| 38 | + addresses: [], |
| 39 | + }), |
| 40 | + deriveKeyForChain: vi.fn(), |
| 41 | +})); |
| 42 | + |
| 43 | +vi.mock('../web-wallet/signing', () => ({ |
| 44 | + signTransaction: vi.fn(), |
| 45 | +})); |
| 46 | + |
| 47 | +vi.mock('../web-wallet/identity', () => ({ |
| 48 | + buildDerivationPath: vi.fn().mockReturnValue("m/44'/60'/0'/0/0"), |
| 49 | +})); |
| 50 | + |
| 51 | +import { Wallet } from './wallet'; |
| 52 | + |
| 53 | +describe('Wallet backup methods', () => { |
| 54 | + const testMnemonic = |
| 55 | + 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'; |
| 56 | + const testPassword = 'Str0ng!P@ssword'; |
| 57 | + |
| 58 | + describe('Wallet.decryptBackup (static)', () => { |
| 59 | + it('should decrypt data encrypted by encryptSeedPhrase', async () => { |
| 60 | + const { data } = await encryptSeedPhrase(testMnemonic, testPassword, 'test-wallet'); |
| 61 | + const decrypted = await Wallet.decryptBackup(data, testPassword); |
| 62 | + |
| 63 | + expect(decrypted).toBe(testMnemonic); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return null for wrong password', async () => { |
| 67 | + const { data } = await encryptSeedPhrase(testMnemonic, testPassword, 'test-wallet'); |
| 68 | + const decrypted = await Wallet.decryptBackup(data, 'wrong-password'); |
| 69 | + |
| 70 | + expect(decrypted).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return null for garbage data', async () => { |
| 74 | + const garbage = new Uint8Array([0xff, 0xfe, 0xfd, 0xfc]); |
| 75 | + const decrypted = await Wallet.decryptBackup(garbage, testPassword); |
| 76 | + |
| 77 | + expect(decrypted).toBeNull(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('wallet.exportEncryptedBackup (instance)', () => { |
| 82 | + it('should throw WalletSDKError with READ_ONLY code for read-only wallets', async () => { |
| 83 | + // fromWalletId creates a read-only wallet (no mnemonic) |
| 84 | + const readOnlyWallet = Wallet.fromWalletId('test-id', { |
| 85 | + baseUrl: 'https://test.api.com', |
| 86 | + apiKey: 'test-key', |
| 87 | + }); |
| 88 | + |
| 89 | + await expect(readOnlyWallet.exportEncryptedBackup(testPassword)) |
| 90 | + .rejects.toThrow(WalletSDKError); |
| 91 | + |
| 92 | + try { |
| 93 | + await readOnlyWallet.exportEncryptedBackup(testPassword); |
| 94 | + } catch (err) { |
| 95 | + expect(err).toBeInstanceOf(WalletSDKError); |
| 96 | + expect((err as WalletSDKError).code).toBe('READ_ONLY'); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + it('should produce encrypted data when wallet has mnemonic (via create)', async () => { |
| 101 | + const wallet = await Wallet.create({ |
| 102 | + baseUrl: 'https://test.api.com', |
| 103 | + apiKey: 'test-key', |
| 104 | + chains: ['BTC'], |
| 105 | + }); |
| 106 | + |
| 107 | + const backup = await wallet.exportEncryptedBackup(testPassword); |
| 108 | + |
| 109 | + expect(backup.data).toBeInstanceOf(Uint8Array); |
| 110 | + expect(backup.data.length).toBeGreaterThan(0); |
| 111 | + expect(backup.filename).toMatch(/^wallet_.*_seedphrase\.txt\.gpg$/); |
| 112 | + expect(backup.walletId).toBeTruthy(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should produce data that Wallet.decryptBackup can decrypt', async () => { |
| 116 | + const wallet = await Wallet.create({ |
| 117 | + baseUrl: 'https://test.api.com', |
| 118 | + apiKey: 'test-key', |
| 119 | + chains: ['BTC'], |
| 120 | + }); |
| 121 | + |
| 122 | + const backup = await wallet.exportEncryptedBackup(testPassword); |
| 123 | + const decrypted = await Wallet.decryptBackup(backup.data, testPassword); |
| 124 | + |
| 125 | + // The mock generates the known test mnemonic |
| 126 | + expect(decrypted).toBe(testMnemonic); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should not decrypt with a wrong password', async () => { |
| 130 | + const wallet = await Wallet.create({ |
| 131 | + baseUrl: 'https://test.api.com', |
| 132 | + apiKey: 'test-key', |
| 133 | + chains: ['BTC'], |
| 134 | + }); |
| 135 | + |
| 136 | + const backup = await wallet.exportEncryptedBackup(testPassword); |
| 137 | + const decrypted = await Wallet.decryptBackup(backup.data, 'wrong-pw'); |
| 138 | + |
| 139 | + expect(decrypted).toBeNull(); |
| 140 | + }); |
| 141 | + }); |
| 142 | +}); |
0 commit comments