-
Notifications
You must be signed in to change notification settings - Fork 20
/
dock-did.js
158 lines (127 loc) · 4.62 KB
/
dock-did.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
DockDid,
DIDDocument,
DidKey,
VerificationRelationship,
LinkedDomains,
ServiceEndpoint,
} from '@docknetwork/credential-sdk/types';
import { DockAPI } from '@docknetwork/dock-blockchain-api';
import { DockDIDModule } from '@docknetwork/dock-blockchain-modules';
// The following can be tweaked depending on where the node is running and what
// account is to be used for sending the transaction.
import {
Ed25519Keypair,
DidKeypair,
} from '@docknetwork/credential-sdk/keypairs';
// DID will be generated randomly
const dockDID = DockDid.random();
const dock = new DockAPI();
const didModule = new DockDIDModule(dock);
// Generate first key with this seed. The key type is Sr25519
const firstKeyPair = Ed25519Keypair.random();
// Generate second key (for update) with this seed. The key type is Ed25519
const secondKeyPair = Ed25519Keypair.random();
// This function assumes the DID has been written.
async function removeDID() {
console.log('Removing DID now.');
// Sign the DID removal with this key pair as this is the current key of the DID
const pair = new DidKeypair([dockDID, 1], firstKeyPair);
return await didModule.removeDocument(dockDID, pair);
}
// This function assumes the DID has been written.
async function addServiceEndpoint() {
console.log('Add new service endpoint now.');
// Sign key update with this key pair as this is the current key of the DID
const pair = new DidKeypair([dockDID, 1], firstKeyPair);
const newEndpoint = new ServiceEndpoint(new LinkedDomains(), [
'https://foo.example.com',
]);
const doc = await didModule.getDocument(dockDID);
doc.addServiceEndpoint(
`${dockDID}#linked-domain`,
newEndpoint,
dockDID,
pair,
);
return await didModule.updateDocument(doc, pair);
}
// This function assumes the DID has been written.
async function addController() {
console.log('Add new controller now.');
// Sign key update with this key pair as this is the current key of the DID
const pair = new DidKeypair([dockDID, 1], firstKeyPair);
const newController = DockDid.random();
const doc = await didModule.getDocument(dockDID);
doc.addController(newController);
return await didModule.updateDocument(doc, pair);
}
// This function assumes the DID has been written.
async function addKey() {
console.log('Add new key now.');
// Sign key update with this key pair as this is the current key of the DID
const pair = new DidKeypair([dockDID, 1], firstKeyPair);
// Update DID key to the following
const newPair = new DidKeypair([dockDID, 2], secondKeyPair);
// the following function will figure out the correct PublicKey type from the `type` property of `newPair`
const newPk = newPair.publicKey();
const vr = new VerificationRelationship();
vr.setAuthentication();
const newDidKey = new DidKey(newPk, vr);
const document = await didModule.getDocument(dockDID);
document.addKey([dockDID, 2], newDidKey);
return await didModule.updateDocument(document, pair);
}
async function getDIDDoc() {
console.log('Getting DID now.');
// Check if DID exists
const result = await didModule.getDocument(dockDID);
console.log('DID Document:', JSON.stringify(result, null, 2));
return result;
}
// Called when connected to the node
async function registerNewDID() {
// The controller is same as the DID
const didKey = new DidKey(
firstKeyPair.publicKey(),
new VerificationRelationship(),
);
console.log('Submitting new DID', dockDID, firstKeyPair.publicKey());
return await didModule.createDocument(DIDDocument.create(dockDID, [didKey]));
}
// Initialize Dock API, connect to the node and start working with it
// It will create a new DID with a key, then update the key to another one and then remove the DID
dock
.init({
address: [process.env.FullNodeEndpoint || 'ws://127.0.0.1:9944'],
})
.then(() => {
const account = dock.keyring.addFromUri(
process.env.TestAccountURI || '//Alice',
);
dock.setAccount(account);
return registerNewDID();
})
.then(getDIDDoc)
.then(addKey)
.then(getDIDDoc)
.then(addController)
.then(addServiceEndpoint)
.then(getDIDDoc)
.then(removeDID)
.then(async () => {
try {
await didModule.getDocument(dockDID);
throw new Error(
'The call to get the DID document should have failed but did not fail. This means the remove DID call has not worked.',
);
} catch (e) {
// The call to get the DID document has failed since the DID has been removed
console.log('Example ran successfully');
process.exit(0);
}
})
.catch((error) => {
console.error('Error occurred somewhere, it was caught!', error);
process.exit(1);
});