-
Notifications
You must be signed in to change notification settings - Fork 20
/
schema.js
165 lines (142 loc) · 4.51 KB
/
schema.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
159
160
161
162
163
164
165
import { randomAsHex } from '@docknetwork/credential-sdk/utils';
import { DockAPI } from '@docknetwork/dock-blockchain-api';
import {
DockDid,
DIDDocument,
DidKey,
VerificationRelationship,
DockBlobId,
} from '@docknetwork/credential-sdk/types';
import { Schema } from '@docknetwork/credential-sdk/modules';
import { DockCoreModules } from '@docknetwork/dock-blockchain-modules';
import {
VerifiableCredential,
getKeyDoc,
} from '@docknetwork/credential-sdk/vc';
import {
Ed25519Keypair,
DidKeypair,
} from '@docknetwork/credential-sdk/keypairs';
import {
UniversalResolver,
CoreResolver,
WildcardResolverRouter,
} from '@docknetwork/credential-sdk/resolver';
const dock = new DockAPI();
const modules = new DockCoreModules(dock);
const blobModule = modules.blob;
const didModule = modules.did;
async function createDID(did, pair) {
console.log('Creating new author DID', did);
// Create an author DID to write with
const publicKey = pair.publicKey();
const didKey = new DidKey(publicKey, new VerificationRelationship());
await didModule.createDocument(DIDDocument.create(did, [didKey]));
}
async function main() {
console.log('Connecting to the node...');
await dock.init({
address: process.env.FullNodeEndpoint || 'ws://127.0.0.1:9944',
});
console.log('Setting sdk account...');
const account = dock.keyring.addFromUri(
process.env.TestAccountURI || '//Alice',
);
dock.setAccount(account);
const keySeed = randomAsHex(32);
const subjectKeySeed = randomAsHex(32);
// Generate a DID to be used as author
const dockDID = DockDid.random();
// Generate first key with this seed. The key type is Ed25519
const pair = new DidKeypair([dockDID, 1], new Ed25519Keypair(keySeed));
await createDID(dockDID, pair);
// Properly format a keyDoc to use for signing
const keyDoc = getKeyDoc(dockDID, pair);
const subjectDID = DockDid.random();
const subjectPair = new DidKeypair(
[subjectDID, 1],
new Ed25519Keypair(subjectKeySeed),
);
await createDID(subjectDID, subjectPair);
console.log('Creating a new schema...');
const schema = new Schema(DockBlobId.random());
await schema.setJSONSchema({
$schema: 'http://json-schema.org/draft-07/schema#',
description: 'Dock Schema Example',
type: 'object',
properties: {
id: {
type: 'string',
},
emailAddress: {
type: 'string',
format: 'email',
},
alumniOf: {
type: 'string',
},
},
required: ['emailAddress', 'alumniOf'],
additionalProperties: false,
});
console.log('The schema is:', JSON.stringify(schema.toJSON(), null, 2));
console.log('Writing schema to the chain with blob id of', schema.id, '...');
await schema.writeToChain(blobModule, dockDID, pair);
console.log(`Schema written, reading from chain (${schema.id})...`);
const result = await Schema.get(schema.id, blobModule);
console.log('Result from chain:', result);
const universalResolverUrl = 'https://uniresolver.io';
const resolver = new WildcardResolverRouter([
new CoreResolver(modules),
new UniversalResolver(universalResolverUrl),
]);
console.log('Creating a verifiable credential and assigning its schema...');
const vc = new VerifiableCredential('https://example.com/credentials/187');
vc.setSchema(result.id, 'JsonSchemaValidator2018');
vc.addContext('https://www.w3.org/2018/credentials/examples/v1');
vc.addContext({
emailAddress: 'https://schema.org/email',
alumniOf: 'https://schema.org/alumniOf',
});
vc.addType('AlumniCredential');
vc.addSubject({
id: String(subjectDID),
alumniOf: 'Example University',
emailAddress: '[email protected]',
});
await vc.sign(keyDoc);
console.log('Verifying the credential:', vc);
const { verified, error } = await vc.verify({
resolver,
compactProof: false,
});
if (!verified) {
throw error || new Error('Verification failed');
}
console.log('Credential verified, mutating the subject and trying again...');
vc.addSubject({
id: 'uuid:0x0',
thisWillFail: true,
});
try {
await vc.verify({
resolver,
compactProof: false,
});
throw new Error(
"Verification succeeded, but it shouldn't have. This is a bug.",
);
} catch (e) {
console.log('Verification failed as expected:', e);
}
console.log('All done, disconnecting...');
await dock.disconnect();
}
main()
.then(() => {
process.exit(0);
})
.catch((error) => {
console.error('Error occurred somewhere, it was caught!', error);
process.exit(1);
});