+33
src/pds.js
+33
src/pds.js
···
263
263
`)
264
264
}
265
265
266
+
async initIdentity(did, privateKeyHex) {
267
+
await this.state.storage.put('did', did)
268
+
await this.state.storage.put('privateKey', privateKeyHex)
269
+
}
270
+
271
+
async getDid() {
272
+
if (!this._did) {
273
+
this._did = await this.state.storage.get('did')
274
+
}
275
+
return this._did
276
+
}
277
+
278
+
async getSigningKey() {
279
+
const hex = await this.state.storage.get('privateKey')
280
+
if (!hex) return null
281
+
return importPrivateKey(hexToBytes(hex))
282
+
}
283
+
266
284
async fetch(request) {
267
285
const url = new URL(request.url)
268
286
if (url.pathname === '/test/cbor') {
···
294
312
return Response.json({
295
313
publicKey: bytesToHex(kp.publicKey),
296
314
signature: bytesToHex(sig)
315
+
})
316
+
}
317
+
if (url.pathname === '/init') {
318
+
const body = await request.json()
319
+
if (!body.did || !body.privateKey) {
320
+
return Response.json({ error: 'missing did or privateKey' }, { status: 400 })
321
+
}
322
+
await this.initIdentity(body.did, body.privateKey)
323
+
return Response.json({ ok: true, did: body.did })
324
+
}
325
+
if (url.pathname === '/status') {
326
+
const did = await this.getDid()
327
+
return Response.json({
328
+
initialized: !!did,
329
+
did: did || null
297
330
})
298
331
}
299
332
return new Response('pds running', { status: 200 })