automatically restore from seed if a seed exists

This commit is contained in:
Danny Morabito 2025-02-20 17:54:13 +01:00
parent e563e746b9
commit 319cc2e6af
Signed by: dannym
GPG key ID: 7CC8056A5A04557E

View file

@ -40,9 +40,13 @@ export function randomTimeUpTo2DaysInThePast() {
export async function getCCNPubkey(): Promise<string> {
const ccnPubPath = await getEveFilePath("ccn.pub");
const seedPath = await getEveFilePath("ccn.seed");
const doWeHaveKey = await exists(ccnPubPath);
if (doWeHaveKey) return Deno.readTextFileSync(ccnPubPath);
const ccnSeed = Deno.env.get("CCN_SEED") || nip06.generateSeedWords();
const ccnSeed = Deno.env.get("CCN_SEED") ||
((await exists(seedPath))
? Deno.readTextFileSync(seedPath)
: nip06.generateSeedWords());
const ccnPrivateKey = nip06.privateKeyFromSeedWords(ccnSeed);
const ccnPublicKey = nostrTools.getPublicKey(ccnPrivateKey);
const encryptedPrivateKey = encryptUint8Array(ccnPrivateKey, encryptionKey);
@ -52,7 +56,7 @@ export async function getCCNPubkey(): Promise<string> {
await getEveFilePath("ccn.priv"),
encodeBase64(encryptedPrivateKey),
);
Deno.writeTextFileSync(await getEveFilePath("ccn.seed"), ccnSeed);
Deno.writeTextFileSync(seedPath, ccnSeed);
return ccnPublicKey;
}