FiloSottile / FiloSottile/typage
Fails on react-native android, wors on web
- Dominant language
- TypeScript
- Stars
- 481
- Forks
- 29
- Avg merge
- 9m
- Merged PRs (30d)
- 6
Description
/**
* Encrypts an Ed25519 seed with a password and returns an ASCII-armored
* age-encrypted string, suitable for clipboard copy, file export, or QR code.
*
* The output looks like:
* -----BEGIN AGE ENCRYPTED FILE-----
* ...base64...
* -----END AGE ENCRYPTED FILE-----
*
* @param accountSecret - The 32-byte Ed25519 seed to export.
* @param password - The user-supplied password to encrypt with.
* @returns - The armored age-encrypted string.
*/
export async function exportAccountSecret(
accountSecret: AccountSecret,
password: string,
): Promise {
const seed = hexToBytes(accountSecret);
console.log("seed.length:", seed.length); // should be 32
console.log("seed.constructor.name:", seed.constructor.name); // should be Uint8Array
const encrypter = new Encrypter();
encrypter.setPassphrase(password);
const ciphertext = await encrypter.encrypt(seed);
console.log("ciphertext.length:", ciphertext.length); // should be same on both
const armored = armor.encode(ciphertext);
console.log("armored.length:", armored.length); // should be same on both
return armored;
}
/**
* Decrypts an armored age-encrypted string back into the Ed25519 seed.
*
* @param armoredCiphertext - The string produced by exportAccountSecret.
* @param password - The password used during export.
* @returns - The original 32-byte Ed25519 seed.
*
* @throws If the password is wrong, or the input is malformed or tampered with.
*/
export async function importAccountSecret(
armoredCiphertext: string,
password: string,
): Promise {
const decrypter = new Decrypter();
decrypter.addPassphrase(password);
const ciphertext = armor.decode(armoredCiphertext);
const seed = await decrypter.decrypt(ciphertext);
return AccountSecretSchema.parse(bytesToHex(seed));
}
Contributor guide
Assessment
This issue has not been assessed yet.