automatically restore from seed if a seed exists
This commit is contained in:
parent
e563e746b9
commit
319cc2e6af
1 changed files with 6 additions and 2 deletions
8
utils.ts
8
utils.ts
|
@ -40,9 +40,13 @@ export function randomTimeUpTo2DaysInThePast() {
|
||||||
|
|
||||||
export async function getCCNPubkey(): Promise<string> {
|
export async function getCCNPubkey(): Promise<string> {
|
||||||
const ccnPubPath = await getEveFilePath("ccn.pub");
|
const ccnPubPath = await getEveFilePath("ccn.pub");
|
||||||
|
const seedPath = await getEveFilePath("ccn.seed");
|
||||||
const doWeHaveKey = await exists(ccnPubPath);
|
const doWeHaveKey = await exists(ccnPubPath);
|
||||||
if (doWeHaveKey) return Deno.readTextFileSync(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 ccnPrivateKey = nip06.privateKeyFromSeedWords(ccnSeed);
|
||||||
const ccnPublicKey = nostrTools.getPublicKey(ccnPrivateKey);
|
const ccnPublicKey = nostrTools.getPublicKey(ccnPrivateKey);
|
||||||
const encryptedPrivateKey = encryptUint8Array(ccnPrivateKey, encryptionKey);
|
const encryptedPrivateKey = encryptUint8Array(ccnPrivateKey, encryptionKey);
|
||||||
|
@ -52,7 +56,7 @@ export async function getCCNPubkey(): Promise<string> {
|
||||||
await getEveFilePath("ccn.priv"),
|
await getEveFilePath("ccn.priv"),
|
||||||
encodeBase64(encryptedPrivateKey),
|
encodeBase64(encryptedPrivateKey),
|
||||||
);
|
);
|
||||||
Deno.writeTextFileSync(await getEveFilePath("ccn.seed"), ccnSeed);
|
Deno.writeTextFileSync(seedPath, ccnSeed);
|
||||||
|
|
||||||
return ccnPublicKey;
|
return ccnPublicKey;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue