diff --git a/src/pds.js b/src/pds.js index ec13ceb..51c9fac 100644 --- a/src/pds.js +++ b/src/pds.js @@ -263,6 +263,24 @@ export class PersonalDataServer { `) } + async initIdentity(did, privateKeyHex) { + await this.state.storage.put('did', did) + await this.state.storage.put('privateKey', privateKeyHex) + } + + async getDid() { + if (!this._did) { + this._did = await this.state.storage.get('did') + } + return this._did + } + + async getSigningKey() { + const hex = await this.state.storage.get('privateKey') + if (!hex) return null + return importPrivateKey(hexToBytes(hex)) + } + async fetch(request) { const url = new URL(request.url) if (url.pathname === '/test/cbor') { @@ -296,6 +314,21 @@ export class PersonalDataServer { signature: bytesToHex(sig) }) } + if (url.pathname === '/init') { + const body = await request.json() + if (!body.did || !body.privateKey) { + return Response.json({ error: 'missing did or privateKey' }, { status: 400 }) + } + await this.initIdentity(body.did, body.privateKey) + return Response.json({ ok: true, did: body.did }) + } + if (url.pathname === '/status') { + const did = await this.getDid() + return Response.json({ + initialized: !!did, + did: did || null + }) + } return new Response('pds running', { status: 200 }) } }