Skip to content

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

1.4 KB
Raw Blame
import {
  deriveRecoveryMaterial,
  setupLocalWallet,
  type WalletSetupSource,
} from '../../services/walletApi';
import { deriveAesKeyFromSeed } from '../../utils/cryptoKeys';
import { openhumanEncryptSecret } from '../../utils/tauriCommands/auth';

export async function persistLocalWalletFromMnemonic(args: {
  mnemonic: string;
  source: WalletSetupSource;
  setEncryptionKey: (value: string | null) => Promise<void>;
  /**
   * Set to `true` only when the user has explicitly confirmed wallet replacement
   * through the double-confirmation dialog. Defaults to `false`.
   */
  force?: boolean;
}): Promise<void> {
  const { mnemonic, source, setEncryptionKey, force } = args;
  const words = mnemonic.trim().split(/\s+/).filter(Boolean);
  if (words.length === 0) {
    throw new Error('Recovery phrase is required.');
  }
  const normalizedMnemonic = words.join(' ');
  const recoveryMaterial = await deriveRecoveryMaterial(normalizedMnemonic);
  const aesKey = deriveAesKeyFromSeed(recoveryMaterial.seed);
  const encryptedMnemonic = (await openhumanEncryptSecret(normalizedMnemonic)).result?.trim();
  if (!encryptedMnemonic) {
    throw new Error('Failed to secure recovery phrase. Please try again.');
  }

  await setEncryptionKey(aesKey);
  await setupLocalWallet({
    consentGranted: true,
    source,
    mnemonicWordCount: words.length,
    encryptedMnemonic,
    accounts: recoveryMaterial.accounts,
    force,
  });
}