feat(client): add keypair generation
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
||||
import { dirname } from "node:path";
|
||||
import { generateKeyPair, type KeyPair } from "../crypto/keypair.js";
|
||||
|
||||
export const CLIENT_STATE_VERSION = 1;
|
||||
|
||||
@@ -132,6 +133,31 @@ export function hasClientKeyPair(state: YonexusClientState): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the client state has a valid key pair.
|
||||
* If no key pair exists, generates a new Ed25519 key pair.
|
||||
* Returns the (possibly updated) state and whether a new key was generated.
|
||||
*/
|
||||
export async function ensureClientKeyPair(
|
||||
state: YonexusClientState,
|
||||
stateStore: YonexusClientStateStore
|
||||
): Promise<{ state: YonexusClientState; generated: boolean }> {
|
||||
if (hasClientKeyPair(state)) {
|
||||
return { state, generated: false };
|
||||
}
|
||||
|
||||
const keyPair = await generateKeyPair();
|
||||
const updatedState: YonexusClientState = {
|
||||
...state,
|
||||
privateKey: keyPair.privateKey,
|
||||
publicKey: keyPair.publicKey,
|
||||
updatedAt: Math.floor(Date.now() / 1000)
|
||||
};
|
||||
|
||||
await stateStore.save(updatedState);
|
||||
return { state: updatedState, generated: true };
|
||||
}
|
||||
|
||||
function assertClientStateShape(
|
||||
value: unknown,
|
||||
filePath: string
|
||||
|
||||
Reference in New Issue
Block a user