-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathdefault-did.ts
50 lines (43 loc) · 1.57 KB
/
default-did.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
import parse from 'url-parse'
import { IDIDManager, TAgent, TKeyType } from '@veramo/core'
interface CreateDefaultDidOptions {
agent: TAgent<IDIDManager>
baseUrl: string
messagingServiceEndpoint?: string
}
export async function createDefaultDid(options: CreateDefaultDidOptions) {
if (!options.agent) throw Error('[createDefaultDid] Agent is required')
if (!options.baseUrl) throw Error('[createDefaultDid] baseUrl is required')
const hostname = parse(options.baseUrl).hostname
const serverIdentifier = await options?.agent?.didManagerGetOrCreate({
provider: 'did:web',
alias: hostname,
options: {
keyType: <TKeyType>'Ed25519',
},
})
console.log('🆔', serverIdentifier?.did)
if (serverIdentifier && options.messagingServiceEndpoint) {
const messagingServiceEndpoint = options.baseUrl + options.messagingServiceEndpoint
console.log('📨 Messaging endpoint', messagingServiceEndpoint)
await options?.agent?.didManagerAddService({
did: serverIdentifier.did,
service: {
id: serverIdentifier.did + '#msg',
type: 'Messaging',
description: 'Handles incoming POST messages',
serviceEndpoint: messagingServiceEndpoint,
},
})
// list DIDCommMessaging service at the same endpoint
await options?.agent?.didManagerAddService({
did: serverIdentifier.did,
service: {
id: serverIdentifier.did + '#msg-didcomm',
type: 'DIDCommMessaging',
description: 'Handles incoming DIDComm messages',
serviceEndpoint: messagingServiceEndpoint,
},
})
}
}