This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
235 lines (221 loc) · 7.95 KB
/
index.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
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
/**
* Aabo Technologies 2021
* Maintained by Fernando Martin Garcia Del Angel
* Built on: May 9th, 2021
*/
'use strict'
const shim = require('fabric-shim')
const util = require('util')
const VaccinationStatus = {
notVaccinated = "NOTVACCINATED",
firstDose = "FIRSTDOSE",
fullyVaccinated = "FULLYVACCINATED",
reinforcement = "REINFORCEMENT"
}
const ValidationStatus = {
notValidated = "NOTVALIDATED",
inValidation = "INVALIDATION",
invalid = "INVALID",
valid = "VALID"
}
let Chaincode = class {
/**
* Chaincode Init
* @param {Object} stub Instantiation object
* @returns {Boolean} Execution status
*/
async Init(stub) {
let ret = stub.getFunctionAndParameters()
console.info(ret)
console.info(' ======= Instantiated Health Passport with SUCCESS =======')
console.info(' ======= Aabo Technologies 2021 ====== ')
return shim.success()
}
/**
* Invocation mehtod
* @param {Object} stub Instantiation object
* @returns {Boolean} Execution Status
*/
async Invoke(stub) {
console.info('Transaction ID:', stub.getTxId())
console.info(util.format('Args: %j', stub.getArgs()))
// Get the expected method and execute it
let ret = stub.getFunctionAndParameters()
let methodToExecute = this[ret.fcn]
if (!methodToExecute) { throw new Error('Received unknown method with name:', methodToExecute) }
try {
let payload = await methodToExecute(stub, ret.params, this)
return shim.success(payload)
} catch (error) {
console.error(error)
return shim.error(err)
}
}
/**
* Creates a vaccination card on the ledger
* @param {Objecg} stub chaincode stub
* @param {Object} args initial arguments
* @param {Class} thisClass verbatim
*/
async createPassport(stub, args, thisClass) {
console.info(' --- Started createPassport --- ')
let passport = {}
passport.id = args[0]
// Check if the vaccination card already exists
let vaccinationCardState = await stub.getState(passport.id)
if (vaccinationCardState.toString()) {
throw new Error(' --- Card already exists ---')
}
passport.firstName = args[1]
passport.lastName = args[2]
passport.dob = args[3]
passport.s3URL = args[4]
passport.patientNumber = args[5]
passport.country = args[6]
passport.dynamoHash = args[7]
passport.validated = ValidationStatus.notValidated
passport.vaxStatus = VaccinationStatus.notVaccinated
// Save to state
await stub.putState(passport.id, Buffer.from(JSON.stringify(passport)))
// Save an index for faster retrieval
let indexName = 'country~id'
let countryIdIndexKey = await stub.createCompositeKey(indexName, [passport.country, passport.id])
await stub.putState(countryIdIndexKey, Buffer.from('\u0000'))
console.info(' --- Ended createVaccinationPassport ---')
}
/**
* Reads Passport state
* @param {*} stub chaincode stub
* @param {*} args arguments
* @param {*} thisClass verbatim
*/
async readPassport(stub, args, thisClass) {
console.info(' --- Started readPassport --- ')
// Input Validation
if (args.length != 1) {
throw new Error('Incorrect number of arguments, expecting ID')
}
// Get and Validate ID
let ID = args[0]
if (!ID) { throw new Error('ID must not be empty') }
// Query the ledger
let passportAsBytes = await stub.getState(ID)
if (!passportAsBytes.toString()) {
throw new Error('Passport does not exist')
}
console.info(' --- Ended readPassport --- ')
return passportAsBytes
}
/**
* Mutates validation status
* @param {Object} stub shim
* @param {Object} args arguments
* @param {Class} thisClass verbatim
*/
async changeValidationStatus(stub, args, thisClass) {
console.info(' --- Started changeValidationStatus --- ')
let ID = args[0]
let newStatus = args[2]
// Query for passport
let passportAsBytes = await stub.getState(ID)
if (!passportAsBytes || !passportAsBytes.toString()) {
throw new Error(' Passport does not exist')
}
// Create passport object
let passport = {}
try {
passport = JSON.parse(passportAsBytes.toString())
} catch (error) {
throw new Error(error)
}
// Change its state
passport.validated = newStatus
// Write it back
let passportJSONAsBytes = Buffer.from(JSON.stringify(passport))
await stub.putState(ID, passportJSONAsBytes)
console.info(' --- Ended changeValidationStatus --- ')
}
/**
* Changes vaccination status
* @param {Object} stub shim
* @param {Object} args arguments
* @param {Class} thisClass verbatim
*/
async changeVaxStatus(stub, args, thisClass) {
console.info(' --- Started changeVaxStatus --- ')
let ID = args[0]
let newStatus = args[2]
// Query for passport
let passportAsBytes = await stub.getState(ID)
if (!passportAsBytes || !passportAsBytes.toString()) {
throw new Error(' Passport does not exist')
}
// Create passport object
let passport = {}
try {
passport = JSON.parse(passportAsBytes.toString())
} catch (error) {
throw new Error(error)
}
// Change its state
passport.vaxStatus = newStatus
// Write it back
let passportJSONAsBytes = Buffer.from(JSON.stringify(passport))
await stub.putState(ID, passportJSONAsBytes)
console.info(' --- Ended changeVaxStatus --- ')
}
async getAllResults(iterator, isHistory) {
let allResults = []
while (true) {
let res = await iterator.next()
if (res.value && res.value.value.toString()) {
let jsonRes = {}
console.log(res.value.value.toString('utf8'))
if (isHistory && isHistory === true) {
jsonRes.TxId = res.value.tx_id
jsonRes.Timestamp = res.value.timestamp
jsonRes.IsDelete = res.value.is_delete.toString()
try {
jsonRes.Value = JSON.parse(res.value.value.toString('utf8'))
} catch (err) {
console.error(err)
jsonRes.Value = res.value.value.toString('utf8')
}
} else {
jsonRes.Key = res.value.key
try {
jsonRes.Record = JSON.parse(res.value.value.toString('utf8'))
} catch (err) {
console.error(err)
jsonRes.Value = res.value.value.toString('utf8')
}
}
allResults.push(jsonRes)
}
if (res.done) {
console.log('end of data')
await iterator.close()
return allResults
}
}
}
/**
* Gets complete history for passport
* @param {Object} stub Shim
* @param {Object} args Method's Argument
* @param {Class} thisClass verbatim
*/
async getHistoryForPassport(stub, args, thisClass) {
if (args.length < 1) {
throw new Error('Incorrect number of arguments, expected one')
}
// Get the passport
let ID = args[0]
console.info(' --- Started getHistoryForPassport --- ')
let resultsIterator = await stub.getHistoryForKey(ID)
let method = thisClass['getAllResults']
let results = await method(resultsIterator, true)
return Buffer.from(JSON.stringify(results))
}
}
shim.start(new Chaincode())