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

support rendering DID docs for unrecognized key formats

Changed files
+17 -5
packages
lib
+17 -5
packages/lib/src/document.ts
···
const verificationMethods: VerificationMethod[] = []
for (const [keyid, key] of Object.entries(data.verificationMethods)) {
const info = formatKeyAndContext(key)
-
if (!context.includes(info.context)) {
context.push(info.context)
}
verificationMethods.push({
···
}
type KeyAndContext = {
-
context: string
type: string
-
publicKeyMultibase
}
const formatKeyAndContext = (key: string): KeyAndContext => {
···
try {
keyInfo = crypto.parseDidKey(key)
} catch (err) {
-
throw new UnsupportedKeyError(key, err)
}
const { jwtAlg } = keyInfo
···
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
}
}
-
throw new UnsupportedKeyError(key, `Unsupported key type: ${jwtAlg}`)
}
···
const verificationMethods: VerificationMethod[] = []
for (const [keyid, key] of Object.entries(data.verificationMethods)) {
const info = formatKeyAndContext(key)
+
if (info.context && !context.includes(info.context)) {
context.push(info.context)
}
verificationMethods.push({
···
}
type KeyAndContext = {
+
context?: string
type: string
+
publicKeyMultibase: string
}
const formatKeyAndContext = (key: string): KeyAndContext => {
···
try {
keyInfo = crypto.parseDidKey(key)
} catch (err) {
+
return {
+
// we can't specify a context for a key type we don't recognize
+
type: 'Multikey',
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
+
}
}
const { jwtAlg } = keyInfo
···
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
}
}
+
+
// this codepath might seem unreachable/redundant, but it's possible
+
// parseDidKey() supports more key formats in future, before this function
+
// can be updated likewise
+
return {
+
// we can't specify a context for a key type we don't recognize
+
type: 'Multikey',
+
publicKeyMultibase: key.replace(/^(did:key:)/, ''),
+
}
}