-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathresolver.ts
93 lines (89 loc) · 2.75 KB
/
resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { AccountId, ChainIdParams } from 'caip'
import type {
DIDResolutionResult,
DIDResolutionOptions,
ResolverRegistry,
ParsedDID,
Resolver,
Resolvable
} from 'did-resolver'
const DID_LD_JSON = 'application/did+ld+json'
const DID_JSON = 'application/did+json'
const SECPK1_NAMESPACES = ['eip155', 'bip122']
const TZ_NAMESPACE = 'tezos'
function toDidDoc(did: string, accountId: string): any {
const { namespace } = AccountId.parse(accountId).chainId as ChainIdParams
const vmId = did + '#blockchainAccountId'
const doc = {
'@context': [
'https://www.w3.org/ns/did/v1',
{
blockchainAccountId: 'https://w3id.org/security#blockchainAccountId',
EcdsaSecp256k1RecoveryMethod2020:
'https://identity.foundation/EcdsaSecp256k1RecoverySignature2020#EcdsaSecp256k1RecoveryMethod2020',
Ed25519VerificationKey2018: 'https://w3id.org/security#Ed25519VerificationKey2018',
},
],
id: did,
verificationMethod: [
{
id: vmId,
type: 'EcdsaSecp256k1RecoveryMethod2020',
controller: did,
blockchainAccountId: accountId,
},
],
authentication: [vmId],
assertionMethod: [vmId],
}
if (SECPK1_NAMESPACES.includes(namespace)) {
// nothing to do here
} else if (namespace === TZ_NAMESPACE) {
(doc['@context'][1] as any).TezosMethod2021 = 'https://w3id.org/security#TezosMethod2021'
const tzId = did + '#TezosMethod2021'
doc.verificationMethod.push({
id: tzId,
type: 'TezosMethod2021',
controller: did,
blockchainAccountId: accountId,
})
doc.authentication.push(tzId)
doc.assertionMethod.push(tzId)
} else {
throw new Error(`chain namespace not supported ${namespace}`)
}
return doc
}
export function getResolver(): ResolverRegistry {
return {
pkh: async (
did: string,
parsed: ParsedDID,
r: Resolvable,
options: DIDResolutionOptions
): Promise<DIDResolutionResult> => {
const contentType = options.accept || DID_JSON
const response: DIDResolutionResult = {
didResolutionMetadata: { contentType },
didDocument: null,
didDocumentMetadata: {},
}
try {
const doc = toDidDoc(did, parsed.id)
if (contentType === DID_LD_JSON) {
response.didDocument = doc
} else if (contentType === DID_JSON) {
delete doc['@context']
response.didDocument = doc
} else {
delete response.didResolutionMetadata.contentType
response.didResolutionMetadata.error = 'representationNotSupported'
}
} catch (e) {
response.didResolutionMetadata.error = 'invalidDid'
response.didResolutionMetadata.message = e.toString()
}
return response
},
}
}