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 pathexampleSingle.chain.ts
More file actions
147 lines (133 loc) · 4.2 KB
/
Copy pathexampleSingle.chain.ts
File metadata and controls
147 lines (133 loc) · 4.2 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable no-console */
import Blockchain from '../../src/blockchain/Blockchain'
import connect from '../../src/blockchainApiConnection/BlockchainApiConnection'
import { testEnv1, mnemonic } from './exampleConfig'
import actorProcessChain from './onchain/1_actorProcess'
import verificationProcessSingleChain from './onchain/3_verificationProcessSingle'
import issuanceProcess from './offchain/2_issuanceProcess'
import teardown from './offchain/4_teardown'
const { pubKey, privKey, disclosedAttributes, claim } = testEnv1
// all processes from attestation, to possibly revocation and final verification
async function completeProcessSingle({
blockchain,
expectedVerificationOutcome,
doRevocation = false,
reqUpdatedAfter,
}: {
blockchain: Blockchain
expectedVerificationOutcome: boolean
doRevocation: boolean
reqUpdatedAfter?: Date
}): Promise<boolean> {
console.group()
// create claimer and attester entities
// eslint-disable-next-line prefer-const
let { claimer, attester, accumulator } = await actorProcessChain({
blockchain,
claimerMnemonic: mnemonic,
claimerMnemonicPw: 'password',
attesterPubKey: pubKey,
attesterPrivKey: privKey,
attesterURI: '//Alice',
})
// issue credential (off-chain method)
const { credential, witness } = await issuanceProcess({
attester,
claimer,
accumulator,
claim,
})
// (optionally) revoke credentials
if (doRevocation) {
console.log(
'AccumulatorCount before revocation:',
await blockchain.getAccumulatorCount(attester.address)
)
accumulator = await attester.revokeAttestation({
accumulator,
witnesses: [witness],
})
const tx = await attester.buildUpdateAccumulatorTX(accumulator)
await blockchain.signAndSend(tx, attester.keyringPair)
console.log(
'AccumulatorCount after revocation:',
await blockchain.getAccumulatorCount(attester.address)
)
}
// verify credential with revocation check
const { verified } = await verificationProcessSingleChain({
claimer,
attester,
credential,
requestedAttributes: disclosedAttributes,
reqUpdatedAfter,
accumulator,
})
// check outcome
const achievedExpectedOutcome = expectedVerificationOutcome === verified
console.groupEnd()
console.log(`Expected outcome achieved? ${achievedExpectedOutcome}`)
return achievedExpectedOutcome
}
// all calls of completeProcessSingle should return true
async function completeProcessSingleExamples(): Promise<void> {
// connect to chain
const blockchain = await connect({
pgabiModName: 'portablegabiPallet',
})
console.log('Connected to chain')
// we accept every accumulator when requiring past in reqUpdatedAfter
const past = new Date(0)
// we only accept the newest accumulator
const future = new Date()
future.setDate(future.getDate() + 100)
// store outcomes here for CI check
const outcomes: boolean[] = []
// without credential revocation
outcomes.push(
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: false,
reqUpdatedAfter: past,
})
)
// without revocation but required date in future => should verify
outcomes.push(
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: false,
reqUpdatedAfter: future,
})
)
// with revocation and required date in future => should not verify
outcomes.push(
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: false,
doRevocation: true,
reqUpdatedAfter: future,
})
)
// with credential revocation but revocation not required in verification => should verify
outcomes.push(
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: true,
})
)
// with credential revocation but date in past => should verify
outcomes.push(
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: true,
reqUpdatedAfter: past,
})
)
// check whether outcome is true fall our instances and disconnect from chain
return teardown('onchain', outcomes)
}
completeProcessSingleExamples()