feat(identity): generate and persist Ed25519 keypair client-side
This commit is contained in:
1 parent
8724ff43b0
commit
ae93606ff8
8 files changed
+478
-1
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import Dexie, { type EntityTable } from "dexie";
|
||||
|
||||
/** A single, non-custodial local identity record. Exactly one row ever exists. */
|
||||
export interface IdentityRecord {
|
||||
/** Fixed primary key - there is only ever one local identity. */
|
||||
id: "local";
|
||||
/** 32-byte Ed25519 public key. */
|
||||
publicKey: Uint8Array;
|
||||
/** 32-byte Ed25519 secret-key seed. Never leaves this device. */
|
||||
secretKey: Uint8Array;
|
||||
createdAt: number;
|
||||
}
|
||||
|
||||
export class CommonsDatabase extends Dexie {
|
||||
identity!: EntityTable<IdentityRecord, "id">;
|
||||
|
||||
constructor() {
|
||||
super("the-commons-hollow");
|
||||
this.version(1).stores({
|
||||
identity: "id",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const db = new CommonsDatabase();
|
||||
@@ -0,0 +1,55 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { db } from "./db";
|
||||
import { getPublicKey, init, sign, verify } from "./identity";
|
||||
|
||||
beforeEach(async () => {
|
||||
indexedDB.deleteDatabase(db.name);
|
||||
db.close();
|
||||
await db.open();
|
||||
});
|
||||
|
||||
describe("identity", () => {
|
||||
it("generates and persists a 32-byte keypair on first init", async () => {
|
||||
const { publicKey } = await init();
|
||||
|
||||
expect(publicKey).toBeInstanceOf(Uint8Array);
|
||||
expect(publicKey.length).toBe(32);
|
||||
|
||||
const stored = await db.identity.get("local");
|
||||
expect(stored).toBeDefined();
|
||||
expect(stored!.publicKey.length).toBe(32);
|
||||
expect(stored!.secretKey.length).toBe(32);
|
||||
expect(await db.identity.count()).toBe(1);
|
||||
});
|
||||
|
||||
it("returns the same public key on re-init instead of regenerating", async () => {
|
||||
const first = await init();
|
||||
const second = await init();
|
||||
|
||||
expect(second.publicKey).toEqual(first.publicKey);
|
||||
expect(await db.identity.count()).toBe(1);
|
||||
});
|
||||
|
||||
it("signs a message and verifies it against the persisted public key", async () => {
|
||||
await init();
|
||||
const message = new TextEncoder().encode("the-commons-hollow");
|
||||
|
||||
const signature = await sign(message);
|
||||
const publicKey = await getPublicKey();
|
||||
const isValid = await verify(signature, message, publicKey);
|
||||
|
||||
expect(isValid).toBe(true);
|
||||
});
|
||||
|
||||
it("fails verification against a tampered message", async () => {
|
||||
await init();
|
||||
const message = new TextEncoder().encode("original payload");
|
||||
const tampered = new TextEncoder().encode("tampered payload");
|
||||
|
||||
const signature = await sign(message);
|
||||
const publicKey = await getPublicKey();
|
||||
const isValid = await verify(signature, tampered, publicKey);
|
||||
|
||||
expect(isValid).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,54 @@
|
||||
import * as ed from "@noble/ed25519";
|
||||
import { db, type IdentityRecord } from "./db";
|
||||
|
||||
export class IdentityMissingError extends Error {
|
||||
constructor() {
|
||||
super("No local identity found. Call identity.init() before signing.");
|
||||
this.name = "IdentityMissingError";
|
||||
}
|
||||
}
|
||||
|
||||
export interface LocalIdentity {
|
||||
publicKey: Uint8Array;
|
||||
}
|
||||
|
||||
async function loadExisting(): Promise<IdentityRecord | undefined> {
|
||||
return db.identity.get("local");
|
||||
}
|
||||
|
||||
export async function init(): Promise<LocalIdentity> {
|
||||
const existing = await loadExisting();
|
||||
if (existing) {
|
||||
return { publicKey: existing.publicKey };
|
||||
}
|
||||
|
||||
const { secretKey, publicKey } = await ed.keygenAsync();
|
||||
const record: IdentityRecord = {
|
||||
id: "local",
|
||||
secretKey,
|
||||
publicKey,
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
await db.identity.add(record);
|
||||
return { publicKey };
|
||||
}
|
||||
|
||||
export async function getPublicKey(): Promise<Uint8Array> {
|
||||
const record = await loadExisting();
|
||||
if (!record) throw new IdentityMissingError();
|
||||
return record.publicKey;
|
||||
}
|
||||
|
||||
export async function sign(message: Uint8Array): Promise<Uint8Array> {
|
||||
const record = await loadExisting();
|
||||
if (!record) throw new IdentityMissingError();
|
||||
return ed.signAsync(message, record.secretKey);
|
||||
}
|
||||
|
||||
export async function verify(
|
||||
signature: Uint8Array,
|
||||
message: Uint8Array,
|
||||
publicKey: Uint8Array,
|
||||
): Promise<boolean> {
|
||||
return ed.verifyAsync(signature, message, publicKey);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
import "fake-indexeddb/auto";
|
||||
Reference in new issue
Block a user