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

Configure Feed

Select the types of activity you want to include in your feed.

Improve get data/doc (#13)

* serve doc data off of last op

* bump version

authored by

Daniel Holmgren and committed by
GitHub
1abf995a cee78371

+23 -10
+1 -1
packages/lib/package.json
··· 1 1 { 2 2 "name": "@did-plc/lib", 3 - "version": "0.0.3", 3 + "version": "0.0.4", 4 4 "main": "src/index.ts", 5 5 "license": "MIT", 6 6 "scripts": {
+16 -3
packages/lib/src/data.ts
··· 110 110 throw new MisorderedOperationError() 111 111 } 112 112 await assureValidSig(doc.rotationKeys, op) 113 - if (check.is(op, t.def.tombstone)) { 113 + const data = opToData(did, op) 114 + // if tombstone & last op, return null. else throw 115 + if (data === null) { 114 116 if (i === rest.length - 1) { 115 117 return null 116 118 } else { 117 119 throw new MisorderedOperationError() 118 120 } 119 121 } 120 - const { verificationMethods, rotationKeys, alsoKnownAs, services } = op 121 - doc = { did, verificationMethods, rotationKeys, alsoKnownAs, services } 122 + doc = data 122 123 prev = await cidForCbor(op) 123 124 } 124 125 125 126 return doc 127 + } 128 + 129 + export const opToData = ( 130 + did: string, 131 + op: t.CompatibleOpOrTombstone, 132 + ): t.DocumentData | null => { 133 + if (check.is(op, t.def.tombstone)) { 134 + return null 135 + } 136 + const { verificationMethods, rotationKeys, alsoKnownAs, services } = 137 + normalizeOp(op) 138 + return { did, verificationMethods, rotationKeys, alsoKnownAs, services } 126 139 } 127 140 128 141 export const getLastOpWithCid = async (
+6 -6
packages/server/src/routes.ts
··· 42 42 // Get data for a DID document 43 43 router.get('/:did', async function (req, res) { 44 44 const { did } = req.params 45 - const log = await ctx.db.opsForDid(did) 46 - if (log.length === 0) { 45 + const last = await ctx.db.lastOpForDid(did) 46 + if (!last) { 47 47 throw new ServerError(404, `DID not registered: ${did}`) 48 48 } 49 - const data = await plc.validateOperationLog(did, log) 49 + const data = plc.opToData(did, last) 50 50 if (data === null) { 51 51 throw new ServerError(404, `DID not available: ${did}`) 52 52 } ··· 58 58 // Get data for a DID document 59 59 router.get('/:did/data', async function (req, res) { 60 60 const { did } = req.params 61 - const log = await ctx.db.opsForDid(did) 62 - if (log.length === 0) { 61 + const last = await ctx.db.lastOpForDid(did) 62 + if (!last) { 63 63 throw new ServerError(404, `DID not registered: ${did}`) 64 64 } 65 - const data = await plc.validateOperationLog(did, log) 65 + const data = plc.opToData(did, last) 66 66 if (data === null) { 67 67 throw new ServerError(404, `DID not available: ${did}`) 68 68 }