Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

4.3 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import { persistLocalWalletFromMnemonic } from './setupLocalWalletFromMnemonic';

const mockSetupLocalWallet = vi.fn();
const mockEncryptSecret = vi.fn();
const mockDeriveRecoveryMaterial = vi.fn();

const canonicalSeed = Uint8Array.from(
  '5eb00bbddcf069084889a8ab9155568165f5c453ccb85e70811aaed6f6da5fc19a5ac40b389cd370d086206dec8aa6c43daea6690f20ad3d8d48b2d2ce9e38e4'.match(
    /.{2}/g
  ) ?? [],
  byte => Number.parseInt(byte, 16)
);
const canonicalAccounts = [
  {
    chain: 'evm' as const,
    address: '0x9858EfFD232B4033E47d90003D41EC34EcaEda94',
    derivationPath: "m/44'/60'/0'/0/0",
  },
  {
    chain: 'btc' as const,
    address: '1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA',
    derivationPath: "m/44'/0'/0'/0/0",
  },
  {
    chain: 'solana' as const,
    address: 'HAgk14JpMQLgt6rVgv7cBQFJWFto5Dqxi472uT3DKpqk',
    derivationPath: "m/44'/501'/0'/0'",
  },
  {
    chain: 'tron' as const,
    address: 'TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH',
    derivationPath: "m/44'/195'/0'/0/0",
  },
];

vi.mock('../../services/walletApi', () => ({
  deriveRecoveryMaterial: (...args: unknown[]) => mockDeriveRecoveryMaterial(...args),
  setupLocalWallet: (...args: unknown[]) => mockSetupLocalWallet(...args),
}));

vi.mock('../../utils/tauriCommands/auth', () => ({
  openhumanEncryptSecret: (...args: unknown[]) => mockEncryptSecret(...args),
}));

describe('persistLocalWalletFromMnemonic', () => {
  it('uses module-derived seed and accounts before persisting the wallet', async () => {
    const setEncryptionKey = vi.fn(async () => undefined);
    mockDeriveRecoveryMaterial.mockResolvedValueOnce({
      seed: new Uint8Array(canonicalSeed),
      accounts: canonicalAccounts,
    });
    mockSetupLocalWallet.mockResolvedValueOnce({
      configured: true,
      onboardingCompleted: true,
      consentGranted: true,
      secretStored: true,
      source: 'generated',
      mnemonicWordCount: 12,
      accounts: [],
      updatedAtMs: 123,
    });
    mockEncryptSecret.mockResolvedValueOnce({ result: 'enc2:wallet-secret' });

    const mnemonic =
      'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';

    await persistLocalWalletFromMnemonic({ mnemonic, source: 'generated', setEncryptionKey });

    expect(mockDeriveRecoveryMaterial).toHaveBeenCalledWith(
      'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
    );

    expect(setEncryptionKey).toHaveBeenCalledWith(
      'dce707ee483afb0a70cb2e076295f9f914e0c62cc097895eabda1c0c1f2f0cb1'
    );
    expect(mockSetupLocalWallet).toHaveBeenCalledWith({
      consentGranted: true,
      source: 'generated',
      mnemonicWordCount: 12,
      encryptedMnemonic: 'enc2:wallet-secret',
      accounts: canonicalAccounts,
    });
  });

  it('rejects whitespace-only input without touching encryption key or wallet store', async () => {
    const setEncryptionKey = vi.fn(async () => undefined);
    mockSetupLocalWallet.mockReset();
    mockEncryptSecret.mockReset();

    await expect(
      persistLocalWalletFromMnemonic({ mnemonic: '   \t  ', source: 'imported', setEncryptionKey })
    ).rejects.toThrow(/recovery phrase is required/i);
    expect(setEncryptionKey).not.toHaveBeenCalled();
    expect(mockEncryptSecret).not.toHaveBeenCalled();
    expect(mockSetupLocalWallet).not.toHaveBeenCalled();
    expect(mockDeriveRecoveryMaterial).not.toHaveBeenCalled();
  });

  it('rejects empty encrypted mnemonic output before persisting wallet state', async () => {
    const setEncryptionKey = vi.fn(async () => undefined);
    mockSetupLocalWallet.mockReset();
    mockEncryptSecret.mockReset();
    mockDeriveRecoveryMaterial.mockReset();
    mockDeriveRecoveryMaterial.mockResolvedValueOnce({
      seed: new Uint8Array(canonicalSeed),
      accounts: canonicalAccounts,
    });
    mockEncryptSecret.mockResolvedValueOnce({ result: '   ' });

    await expect(
      persistLocalWalletFromMnemonic({
        mnemonic:
          'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
        source: 'imported',
        setEncryptionKey,
      })
    ).rejects.toThrow(/failed to secure recovery phrase/i);

    expect(setEncryptionKey).not.toHaveBeenCalled();
    expect(mockSetupLocalWallet).not.toHaveBeenCalled();
  });
});