This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreadme.ts
More file actions
131 lines (110 loc) · 7.84 KB
/
Copy pathreadme.ts
File metadata and controls
131 lines (110 loc) · 7.84 KB
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
/* eslint-disable no-console */
// eslint-disable-next-line @typescript-eslint/no-var-requires
import * as portablegabi from '../../src'
const privKey = new portablegabi.AttesterPrivateKey(
'{"XMLName":{"Space":"","Local":""},"Counter":0,"ExpiryDate":1610554062,"P":"iDYKxuFGt1Xv1aqMLaagjrOPX0hjkOlFrKOp4NPnSBHmQ9SFETUX1M43q3jLsGz+UEWFS3+SS9QpP4CTkl3p/w==","Q":"92MJOhwjESn7QohCCY1oBxsToAfccGoKtE3sBoaNxHWoowSiCy8fMG+B1sO5QU+bV3i1xwvVno9o30RcMoXEaw==","PPrime":"RBsFY3CjW6r36tVGFtNQR1nHr6QxyHSi1lHU8GnzpAjzIepCiJqL6mcb1bxl2DZ/KCLCpb/JJeoUn8BJyS70/w==","QPrime":"e7GEnQ4RiJT9oUQhBMa0A42J0APuODUFWib2A0NG4jrUUYJRBZePmDfA62HcoKfNq7xa44Xqz0e0b6IuGULiNQ==","ECDSA":"MHcCAQEEILO+g4uSDheZ6PSLxR7olFzUhZpeO9tQu84hX6UeIevaoAoGCCqGSM49AwEHoUQDQgAEKvmUz3HIZy890jE78CC9V9BuN8taO+L8GjAeS14v0CL7GCFZ1GMnaSZi4WG3mOjJlJ80CnMowIbUT3Fw1TluFw==","NonrevSk":null}'
)
const pubKey = new portablegabi.AttesterPublicKey(
'{"XMLName":{"Space":"","Local":""},"Counter":0,"ExpiryDate":1610554062,"N":"g6DWNN/cWep9/lCc6gg0tA8wS1y5LgQx2/fM/wMpYJE8MTZ9SJ3y9kjIBAeSb4aY3vsFhRp8aWsEZzAA0Qu0kW4bzyKN1RU7A0tlmkmDetCxu7Gy2zQMHlTg4YkAVxVYAIIIWhHKHrVLzH7zCsuXos1qm/sthByVdEXv4HPjCZU=","Z":"BiDMFSNGKLIcHJY3tmh2vgiW7D3f5g5b+6Bjf0ns3/rPOg8x0BJ+CzqOLQL+loNIomOzBm/Pk36q3pmPPFMfug80AwUlZOvKTrzj29Agq4DF7p4jruElRyZsdGNjlFkVzILFT/9yrXfjD/9DAHXGm6/4unVnwKP4I0j1r9sLYtg=","S":"Bxm9bNpNLZUM6gy74aR0HW2DadFuy/l+MOdZkG2BiFxbTEP24GXBYA3+d1xajplWEm2iLF4w2OeviIpr8VIzDNy6dXRyGcTnGzj6sVeGlR5u3N+8M2XNH1pNEymLQQbUAt3ogYSWiJW88bxHCf3AZiS91XT1Zh3ENCS9NsyGzt8=","G":"Angd7BuIjTeWGsVLGVCtv+5dx1TMEUr/Z5Fhk7OFUNBexY8fuNfzxfeclgSQpC+nyIAFHc3RB+3Fcs2vOSygopVfLEJo9h7dSjtlcxSZ1wE8YNgouHwfVuq4KWixzIk7Le+IeUzNaQNOL9SI3h5mlxJ5QOO2Src+BPQuFjXPSfI=","H":"U1MyQqwl1LrZY5G61Z2ZDM3zWQKv78HOluCrtxCDBsMvYNRLvhbppOhOdsnG3axN5NIH01/R6mlYojBDg9L7xSwR+1QpmHGUbwkemADlUZQ9c98Up1ORKxNW0asQJdPHV4NGqjQbDfJzejdGJwd95scmSpqLNvRTT+L0iW0ln4A=","T":"BEIUJ5pXzFZPeoB3us341EWxwE7HByM4NaPYRS6YVtDcJdz+H9EEKdUcXhUVrJAQ2OZy2FP0+SNv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+5oYSkzHiZTeJtLg0OVGZnka3pGxzg0ihkkT6Bdk8K2OicTNxlHzgI=","ZQ9/qIgvOx/8dyXlAFeZH+2lriSPaj/NDzPCxR9sXqBYJskSkSrdGogxP2RZeAGyDh7NvwUtvBDQ/vLKz/O3ANPUOnaRx1n4uBF+uBdt0h3Ml/DckhL5k2+nHQsnZWPFxkdpatCIFWcvYuldx+gXLePBaRmNnKMoxAgT+tJnJcw=","ZGfBOqHujseUhLZdfs8kq+/kmG3yMwUAmQrGgTdNej8npNsOyD/Am/SoPdSjpr1enuMgBzva/bjn3/z8nncpia65+v9Pn5831UuFp8h53/1WaEHvN/yctnIKb8k1IRtPlSvnfq7qwC/sIGvHq+ZTj3/ie57rTSkSMrmdFL8PMM0=","TM38T4ekWiNWICCgry7GsppfVt2ImPv4SL//f/J3beP34K1afJCsHk50XJwi8qyMz8HqEVK2sWvMQzJ8Amct4sAfRYIZNmqH7mSR7LwIXvihwv1dUlJv2R7MLTjEGkEnJHE5cCR0K5GxjeQSSgNHAu33MOth3ipsK9ZmF+slSkI=","YwMb/IVn2NsA4y8ZiiBxCWoOg0tsqyYKTakxDZnRhw+wHwhnA3+T87X4tOSAx+dYlmtj3UQzUAeFRYztr2YTrF2boS/YFeAiVh6swPgFOScvmOuf5O4fJn7z+iXr+ivgFccswxBhxqa9MdF8ReqHaVouj8LLyk33fZgWduwfnA=="],"EpochLength":432000,"Params":{"LePrime":120,"Lh":256,"Lm":256,"Ln":1024,"Lstatzk":80,"Le":597,"LeCommit":456,"LmCommit":592,"LRA":1104,"LsCommit":593,"Lv":1700,"LvCommit":2036,"LvPrime":1104,"LvPrimeCommit":1440},"Issuer":"","ECDSA":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEKvmUz3HIZy890jE78CC9V9BuN8taO+L8GjAeS14v0CL7GCFZ1GMnaSZi4WG3mOjJlJ80CnMowIbUT3Fw1TluFw==","NonrevPk":null}'
)
async function exec() {
/* (1) Claimer Setup */
// (1.1) Example claim
const claim = {
contents: {
name: 'Jasper',
age: '42',
city: 'Berlin',
id: 'ed638ndke92902n29',
},
}
// (1.2) Create the claimer identity (either from scratch or mnemonic seed).
const claimer = await portablegabi.Claimer.create()
/* (2) Attester Setup */
// (2.1) Create a key pair and attester entity.
// const attester = await portablegabi.Attester.create() // takes very long due to finding safe prime numbers (~10-20 minutes)
// (2.1.b) Alternatively, use a pre-compiled key pair from src/testSetup/testConfig.ts
const attester = new portablegabi.Attester(pubKey, privKey)
console.log('Public key:\n\t', attester.privateKey.toString())
console.log('Private key:\n\t', attester.privateKey.toString())
// (2.1) Create accumulator (for revocation)
const accumulator = await attester.createAccumulator()
/* (3) Attestation */
// (3.1) Attester sends two nonces to claimer
const {
message: startAttestationMsg,
session: attestationSession,
} = await attester.startAttestation()
// (3.2) Claimer requests attestation
const {
message: attestationRequest,
session: claimerSession,
} = await claimer.requestAttestation({
startAttestationMsg,
claim,
attesterPubKey: attester.publicKey,
})
console.log('Claimer requests attestation:\n\t', attestationRequest)
// (3.3) Attester issues requested attestation and generates a witness which can be used to revoke the attestation
// the attester might want to inspect the attributes he is about to sign
const checkClaim = attestationRequest.getClaim()
console.log('Attester checks claim :\n\t', checkClaim)
const { attestation, witness } = await attester.issueAttestation({
attestationSession,
attestationRequest,
accumulator,
})
console.log('Attester issues attestion:\n\t', attestation)
// (3.4) Claimer builds credential from attester's signature
const credential = await claimer.buildCredential({
claimerSession,
attestation,
})
console.log('Claimer builds credential:\n\t', credential)
/* (4) Verification */
// (4.1) Verifier sends two nonces to claimer
const {
session: verifierSession,
message: presentationReq,
} = await portablegabi.Verifier.requestPresentation({
requestedAttributes: ['contents.age', 'contents.city'],
reqUpdatedAfter: new Date(), // request that the nonrevocation proof contains an accumulator which was created after this date or that the accumulator is the newest available
})
console.log('Verifier starts verification session:\n\t', presentationReq)
// (4.2) Claimer reveals attributes
const proof = await claimer.buildPresentation({
credential,
presentationReq,
attesterPubKey: attester.publicKey,
})
console.log('Claimer builds zk-proof on requested attributes:\n\t', proof)
// (4.3) Verifier verifies attributes
const {
verified,
claim: verifiedClaim,
} = await portablegabi.Verifier.verifyPresentation({
proof,
verifierSession,
attesterPubKey: attester.publicKey,
latestAccumulator: accumulator, // the newest available accumulator
})
console.log('Verifier verifiers proof:\n\t', verified, verifiedClaim)
/* (5) Revocation */
// Revoke the witness of a credential.
const accumulatorAfterRevocation = await attester.revokeAttestation({
accumulator,
witnesses: [witness],
})
// Expect failure here due to prior revocation.
await credential
.updateSingle({
attesterPubKey: attester.publicKey,
accumulator: accumulatorAfterRevocation,
})
.catch((e) => {
if (e.message.includes('revoked')) {
console.log('Credential was revoked and cannot be updated')
} else throw e
})
}
exec()