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.mashnet.ts
More file actions
129 lines (116 loc) · 3.51 KB
/
Copy pathexampleSingle.mashnet.ts
File metadata and controls
129 lines (116 loc) · 3.51 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
/* eslint-disable prefer-const */
/* eslint-disable no-console */
import Blockchain from '../../src/blockchain/Blockchain'
import connect, {
disconnect,
} from '../../src/blockchainApiConnection/BlockchainApiConnection'
import { testEnv1 } from './exampleConfig'
import verificationProcessSingleChain from './onchain/3_verificationProcessSingle'
import issuanceProcess from './offchain/2_issuanceProcess'
import { actorSetupChain } from '../../src/testSetup/testSetup.chain'
import { chainCfg } from '../../src/testSetup/testConfig'
import { PgabiModName } from '../../src/types/Chain'
const { disclosedAttributes, claim } = testEnv1
const pgabiModName: PgabiModName = 'portablegabi'
// 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
let {
attesters: [attester],
claimers: [claimer],
accumulators: [accumulator],
} = await actorSetupChain({
pgabiModName,
mnemonics: [chainCfg.mnemonic, chainCfg.mnemonic],
keypairTypes: ['ed25519', 'ed25519'],
})
// 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],
})
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, // require accumulator's revocation index of 0 or greater
accumulator,
})
console.groupEnd()
console.log(
// eslint-disable-next-line eqeqeq
`Expected outcome achieved? ${expectedVerificationOutcome == verified}`
)
return expectedVerificationOutcome === verified
}
// all calls of completeProcessSingle should return true
async function completeProcessSingleExamples(): Promise<void> {
// connect to chain
const blockchain = await connect({
pgabiModName,
types: {
DelegationNodeId: 'Hash',
PublicSigningKey: 'Hash',
PublicBoxKey: 'Hash',
Permissions: 'u32',
ErrorCode: 'u16',
},
})
console.log('Connected to chain')
const past = new Date()
const future = new Date()
future.setDate(past.getDate() + 100)
// without credential revocation
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: false,
reqUpdatedAfter: past,
})
// with credential revocation
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: false,
doRevocation: true,
reqUpdatedAfter: future,
})
// with credential revocation but revocation not required in verification
await completeProcessSingle({
blockchain,
expectedVerificationOutcome: true,
doRevocation: true,
})
// disconnect from chain
await disconnect().finally(() => process.exit())
}
completeProcessSingleExamples()