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

tidy

dholms 255d430f b0a55dab

Changed files
+42 -7
packages
lib
server
+5
packages/lib/src/data.ts
···
import { didForCreateOp, normalizeOp } from './operations'
import {
GenesisHashError,
+
ImproperlyFormattedDidError,
ImproperOperationError,
InvalidSignatureError,
LateRecoveryError,
···
const normalized = normalizeOp(op)
await assureValidSig(normalized.rotationKeys, op)
const expectedDid = await didForCreateOp(op, 64)
+
// id must be >=24 chars & prefix is 8chars
+
if (did.length < 32) {
+
throw new ImproperlyFormattedDidError('too short')
+
}
if (!expectedDid.startsWith(did)) {
throw new GenesisHashError(expectedDid)
}
+25 -6
packages/lib/src/error.ts
···
-
export class ImproperOperationError extends Error {
+
export class PlcError extends Error {
+
plcError = true
+
constructor(msg: string) {
+
super(msg)
+
}
+
+
static is(obj: unknown): obj is PlcError {
+
if (obj && typeof obj === 'object' && obj['plcError'] === true) {
+
return true
+
}
+
return false
+
}
+
}
+
export class ImproperOperationError extends PlcError {
constructor(public reason: string, public op: unknown) {
super(`Improperly formatted operation, ${reason}: ${op}`)
}
}
-
export class MisorderedOperationError extends Error {
+
export class MisorderedOperationError extends PlcError {
constructor() {
super('Operations not correctly ordered')
}
}
-
export class LateRecoveryError extends Error {
+
export class LateRecoveryError extends PlcError {
constructor(public timeLapsed: number) {
super(
`Recovery operation occured outside of the allowed 72 hr recovery window. Time lapsed: ${timeLapsed}`,
···
}
}
-
export class GenesisHashError extends Error {
+
export class GenesisHashError extends PlcError {
constructor(public expected: string) {
super(
`Hash of genesis operation does not match DID identifier: ${expected}`,
···
}
}
-
export class InvalidSignatureError extends Error {
+
export class InvalidSignatureError extends PlcError {
constructor(public op: unknown) {
super(`Invalid signature on op: ${JSON.stringify(op)}`)
}
}
-
export class UnsupportedKeyError extends Error {
+
export class UnsupportedKeyError extends PlcError {
constructor(public key: string, public err: unknown) {
super(`Unsupported key type ${key}: ${err}`)
}
}
+
+
export class ImproperlyFormattedDidError extends PlcError {
+
constructor(public reason: string) {
+
super(`Improperly formatted did: ${reason}`)
+
}
+
}
+10
packages/server/src/error.ts
···
+
import { PlcError } from '@did-plc/lib'
import { ErrorRequestHandler } from 'express'
export const handler: ErrorRequestHandler = (err, req, res, next) => {
+
// normalize our PLC errors to server errors
+
if (PlcError.is(err)) {
+
err = ServerError.fromPlcError(err)
+
}
+
req.log.info(
err,
ServerError.is(err)
···
typeof (obj as Record<string, unknown>).message === 'string' &&
typeof (obj as Record<string, unknown>).status === 'number'
)
+
}
+
+
static fromPlcError(err: PlcError): ServerError {
+
return new ServerError(400, err.message)
}
}
+2 -1
packages/server/src/routes.ts
···
res.send({ version })
})
+
// @TODO paginate & test this
router.get('/export', async function (req, res) {
const fullExport = await ctx.db.fullExport()
res.setHeader('content-type', 'application/jsonlines')
···
res.json({ log })
})
-
// Get operation log for a DID
+
// Get the most recent operation in the log for a DID
router.get('/last/:did', async function (req, res) {
const { did } = req.params
const log = await ctx.db.opsForDid(did)