Fork of github.com/did-method-plc/did-method-plc

Improve get data/doc (#13)

* serve doc data off of last op

* bump version

Changed files
+23 -10
packages
lib
server
src
+1 -1
packages/lib/package.json
···
{
"name": "@did-plc/lib",
-
"version": "0.0.3",
+
"version": "0.0.4",
"main": "src/index.ts",
"license": "MIT",
"scripts": {
+16 -3
packages/lib/src/data.ts
···
throw new MisorderedOperationError()
}
await assureValidSig(doc.rotationKeys, op)
-
if (check.is(op, t.def.tombstone)) {
+
const data = opToData(did, op)
+
// if tombstone & last op, return null. else throw
+
if (data === null) {
if (i === rest.length - 1) {
return null
} else {
throw new MisorderedOperationError()
}
}
-
const { verificationMethods, rotationKeys, alsoKnownAs, services } = op
-
doc = { did, verificationMethods, rotationKeys, alsoKnownAs, services }
+
doc = data
prev = await cidForCbor(op)
}
return doc
+
}
+
+
export const opToData = (
+
did: string,
+
op: t.CompatibleOpOrTombstone,
+
): t.DocumentData | null => {
+
if (check.is(op, t.def.tombstone)) {
+
return null
+
}
+
const { verificationMethods, rotationKeys, alsoKnownAs, services } =
+
normalizeOp(op)
+
return { did, verificationMethods, rotationKeys, alsoKnownAs, services }
}
export const getLastOpWithCid = async (
+6 -6
packages/server/src/routes.ts
···
// Get data for a DID document
router.get('/:did', async function (req, res) {
const { did } = req.params
-
const log = await ctx.db.opsForDid(did)
-
if (log.length === 0) {
+
const last = await ctx.db.lastOpForDid(did)
+
if (!last) {
throw new ServerError(404, `DID not registered: ${did}`)
}
-
const data = await plc.validateOperationLog(did, log)
+
const data = plc.opToData(did, last)
if (data === null) {
throw new ServerError(404, `DID not available: ${did}`)
}
···
// Get data for a DID document
router.get('/:did/data', async function (req, res) {
const { did } = req.params
-
const log = await ctx.db.opsForDid(did)
-
if (log.length === 0) {
+
const last = await ctx.db.lastOpForDid(did)
+
if (!last) {
throw new ServerError(404, `DID not registered: ${did}`)
}
-
const data = await plc.validateOperationLog(did, log)
+
const data = plc.opToData(did, last)
if (data === null) {
throw new ServerError(404, `DID not available: ${did}`)
}