-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.js
108 lines (84 loc) · 2.71 KB
/
example.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
const util = require('util')
const Issuer = require('./issuer')
const User = require('./prover')
const Verifier = require('./verifier')
const curve = require('./lib/curve')
const schema = {
'drivers licence': 'boolean',
age: 'number',
nationality: 'string',
gender: 'string',
residence: 'string',
employed: 'boolean'
}
const application = {
gender: 'male',
age: 66,
'drivers licence': true,
residence: 'austria',
nationality: 'italy',
employed: true
}
let org = new Issuer('./storage/org')
const users = []
let verifier = new Verifier('./storage/verifier')
// register the certification
org.addCertification(schema, function (certId) {
// add the cert to the verifiers recognised certifications
verifier.registerCertification(org.getPublicCert(certId), function () {
newUsers(3, () => {
// user selects which attributes to show
let index = Math.floor(Math.random() * users.length)
let user = users[index]
// test user encoding
user = User.decode(user.encode())
// 512ms
const present = user.present(['age', 'drivers licence', 'gender'])
// test verifier encoding
verifier = Verifier.decode(verifier.encode())
// 1130ms
verifier.validate(present, function (err, identifier) {
if (err) throw err
console.log('credential has been accepted.')
// test issuer encoding
org = Issuer.decode(org.encode())
// 390ms
org.revokeCredential(identifier, function (err, revinfo) {
verifier.updateCertifications(revinfo)
users.forEach(u => u.updateNonRevocationWitnesses(revinfo))
const presentRevoked = user.present(['age', 'drivers licence'])
verifier.validate(presentRevoked, function (err) {
if (err) console.error(err)
while (true) {
const newIndex = Math.floor(Math.random() * users.length)
if (newIndex === index) continue
index = newIndex
break
}
const newUser = users[index]
verifier.validate(newUser.present(['age', 'nationality', 'drivers licence', 'employed']), (err) => {
if (err) console.log(err)
else console.log('success')
})
})
})
})
})
function newUsers (i, cb) {
if (i === 0) return cb()
const user = new User()
users.push(user)
// ~0.3ms
const app = user.apply(application, certId)
// ~24ms
const setup = org.addIssuance(app)
// ~67ms
const obtain = user.obtain(setup)
// ~188ms
const granted = org.grantCredential(obtain)
// ~216ms
user.store(granted)
newUsers(--i, cb)
}
})
})