-
Notifications
You must be signed in to change notification settings - Fork 28
/
example.ts
307 lines (271 loc) · 8.74 KB
/
example.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* eslint-disable no-console */
import Kilt, {
AttesterIdentity,
Claim,
Credential,
CType,
CTypeUtils,
ICType,
Identity,
IRevocationHandle,
PublicAttesterIdentity,
} from '../src'
import constants from '../src/test/constants'
const NODE_URL = 'ws://127.0.0.1:9944'
const SEP = '_'
async function setup(): Promise<{
claimer: Identity
attester: AttesterIdentity
claim: Claim
ctype: CType
}> {
console.log(
((s) => s.padEnd(40 + s.length / 2, SEP).padStart(80, SEP))(' SETUP ')
)
// ------------------------- Attester ----------------------------------------
// To get an attestation, we need an Attester
// we can generate a new keypair, which will take about 20 minutes:
// const attester = await Kilt.AttesterIdentity.buildFromMnemonic("...")
// or we just use unsafe precalculated keys (just for demo purposes!):
const attester = await Kilt.AttesterIdentity.buildFromMnemonic(
'receive clutch item involve chaos clutch furnace arrest claw isolate okay together',
{
key: {
publicKey: constants.PUBLIC_KEY.toString(),
privateKey: constants.PRIVATE_KEY.toString(),
},
}
)
console.log(
'Attester balance is:',
await Kilt.Balance.getBalance(attester.address)
)
// TODO: how to handle instantiation? We cannot always upload the accumulator...
await attester.updateAccumulator(attester.getAccumulator())
// for privacy enhanced attestations the attester has to initiate the attestation process
// ------------------------- CType ----------------------------------------
// First build a schema
const ctypeSchema: ICType['schema'] = {
$id:
'kilt:ctype:0x3b53bd9a535164136d2df46d0b7146b17b9821490bc46d4dfac7e06811631803',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
properties: {
name: {
type: 'string',
},
age: {
type: 'integer',
},
},
type: 'object',
title: 'title',
}
// Generate the Hash for it
const ctypeHash = CTypeUtils.getHashForSchema(ctypeSchema)
// Put everything together
const rawCtype: ICType = {
schema: ctypeSchema,
hash: ctypeHash,
owner: attester.address,
}
// Build the CType object
const ctype = new Kilt.CType(rawCtype)
// Store ctype on blockchain
// ! This costs tokens !
// Also note, that the completely same ctype can only be stored once on the blockchain.
try {
await ctype.store(attester)
} catch (e) {
console.log(
'Error while storing CType. Probably either insufficient funds or ctype does already exist.',
e
)
}
// ------------------------- Claimer ----------------------------------------
// How to generate an Identity
// const mnemonic = Kilt.Identity.generateMnemonic()
const claimer = await Kilt.Identity.buildFromMnemonic(
'wish rather clinic rather connect culture frown like quote effort cart faculty',
{ peEnabled: true }
)
// const address = claimer.address
// At this point the generated Identity has no tokens.
// If you want to interact with the blockchain, you will have to get some.
// Contact [email protected] and provide the address of the identity
const rawClaim = {
name: 'Alice',
age: 29,
}
const claim = new Kilt.Claim({
cTypeHash: ctypeHash,
contents: rawClaim,
owner: claimer.address,
})
console.log('Claimer', claimer.address, '\n')
console.log('Attester', attester.address, '\n')
console.log('Ctype', ctype, '\n')
console.log('Claim', claim, '\n')
return {
claimer,
attester,
ctype,
claim,
}
}
async function doAttestation(
claimer: Identity,
attester: AttesterIdentity,
claim: Claim
): Promise<{
credential: Credential
revocationHandle: IRevocationHandle
}> {
console.log(
((s) => s.padEnd(40 + s.length / 2, SEP).padStart(80, SEP))(' ATTESTATION ')
)
// ------------------------- Attester ----------------------------------------
const {
message: initiateAttestationMessage,
session: attestersSession,
} = await Kilt.Attester.initiateAttestation(
attester,
claimer.getPublicIdentity()
)
// ------------------------- CLAIMER -----------------------------------------
// And we need to build a request for an attestation
const {
message: reqAttestation,
session: claimerSession,
} = await Kilt.Claimer.requestAttestation(
claim,
claimer,
attester.getPublicIdentity(),
{
initiateAttestationMsg: initiateAttestationMessage,
}
)
// The message can be encrypted as follows
const reqAttestationEnc = reqAttestation.encrypt()
// claimer sends [[encrypted]] to the attester
// ------------------------- Attester ----------------------------------------
// Check the validity of the message
Kilt.Message.ensureHashAndSignature(reqAttestationEnc, claimer.address)
// When the Attester receives the message, she can decrypt it
const reqAttestationDec = Kilt.Message.decrypt(reqAttestationEnc, attester)
// And make sure, that the sender is the owner of the identity
Kilt.Message.ensureOwnerIsSender(reqAttestationDec)
const {
revocationHandle,
message: submitAttestation,
} = await Kilt.Attester.issueAttestation(
attester,
reqAttestationDec,
claimer.getPublicIdentity(),
attestersSession
)
console.log(
'revocationHandle should be stored for revocation: ',
revocationHandle
)
// And send a message back
const submitAttestationEnc = submitAttestation.encrypt()
// ------------------------- CLAIMER -----------------------------------------
Kilt.Message.ensureHashAndSignature(
submitAttestationEnc,
attester.address
)
const submitAttestationDec = Kilt.Message.decrypt(
submitAttestationEnc,
claimer
)
const credential = await Kilt.Claimer.buildCredential(
claimer,
submitAttestationDec,
claimerSession
)
console.log('RFO Message', reqAttestation.body, '\n')
console.log('Submit attestation:', submitAttestation.body, '\n')
console.log('AttestedClaim', credential, '\n')
return {
credential,
revocationHandle,
}
}
async function doVerification(
claimer: Identity,
attesterPub: PublicAttesterIdentity,
credential: Credential,
privacyEnhanced: boolean
): Promise<void> {
console.log(
((s) => s.padEnd(40 + s.length / 2, SEP).padStart(80, SEP))(
' VERIFICATION '
)
)
const verifierMnemonic = Identity.generateMnemonic()
const verifier = await Kilt.Identity.buildFromMnemonic(verifierMnemonic, {
peEnabled: true,
})
// ------------------------- Verifier ----------------------------------------
const { session, message: request } = await Kilt.Verifier.newRequestBuilder()
.requestPresentationForCtype({
ctypeHash: credential.attestation.cTypeHash,
requestUpdatedAfter: new Date(), // request accumulator newer than NOW or the latest available
properties: ['age'],
})
.finalize(privacyEnhanced, verifier, claimer.getPublicIdentity())
// ------------------------- Claimer -----------------------------------------
const presentation = await Kilt.Claimer.createPresentation(
claimer,
request,
verifier.getPublicIdentity(),
[credential],
[attesterPub],
privacyEnhanced
)
// ------------------------- Verifier ----------------------------------------
// The verifier needs the public identity of the attester. Either he already has a list of trusted
// attesters or he needs to resolve them differently. A Decentralized Identity (DID) would be an
// option for that.
const { verified, claims } = await Kilt.Verifier.verifyPresentation(
presentation,
session,
[await Kilt.Attester.getLatestAccumulator(attesterPub)],
[attesterPub]
)
console.log('Received claims: ', JSON.stringify(claims))
console.log('All valid? ', verified)
}
// do an attestation and a verification
async function example(): Promise<boolean> {
const { claimer, attester, claim } = await setup()
const { credential, revocationHandle } = await doAttestation(
claimer,
attester,
claim
)
// should succeed
await doVerification(claimer, attester.getPublicIdentity(), credential, true)
await doVerification(claimer, attester.getPublicIdentity(), credential, false)
// revoke
await Kilt.Attester.revokeAttestation(attester, revocationHandle)
// should fail
await doVerification(claimer, attester.getPublicIdentity(), credential, true)
await doVerification(claimer, attester.getPublicIdentity(), credential, false)
return true
}
// connect to the blockchain, execute the examples and then disconnect
;(async () => {
await Kilt.connect(NODE_URL)
const done = await example()
if (!done) {
throw new Error('Example did not finish')
}
})()
.finally(() => Kilt.disconnect(NODE_URL))
.catch((e) => {
console.error('Error Error Error!\n')
setTimeout(() => {
throw e
}, 1)
})