-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
293 lines (265 loc) · 8.72 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
var config = require('./examples/config.js');
var hfc = require('hfc');
var WebHooks = require('node-webhooks');
var Types = require('./types.js');
var RegisterThingTX = Types.RegisterThingTX;
var CreateRegistrantTX = Types.CreateRegistrantTX;
var RegisterSpecTX = Types.RegisterSpecTX;
var USER;
var MEMBER_SERVICES;
var HYPERLEDGER_PEER;
var CHAINCODENAME;
var HL_EVENTS;
var chain = hfc.newChain('doc_proof_chain');
chain.setECDSAModeForGRPC(true);
var webHooks = null;
var webhookname = "DefaultWebhook";
console.log('config', config);
chain.setKeyValStore(hfc.newFileKeyValStore(config.store.path));
console.log(Types)
// Call this function after importing the docproofware library Arguments are optional and overrride the defaults.
var initialize = function (ca, peer, hl_events, user, pem, chaincodeName, cb) {
console.log('init');
MEMBER_SERVICES = ca ? ca : MEMBER_SERVICES;
HYPERLEDGER_PEER = peer ? peer : HYPERLEDGER_PEER;
USER = user ? user : USER;
CHAINCODENAME = chaincodeName ? chaincodeName : CHAINCODENAME;
HL_EVENTS = hl_events ? hl_events : HL_EVENTS;
if (!cb) {
cb = function (err, user) {
if (err)
console.log('You may need fresh credentials from the CA' + err);
if (user)
console.log('Registration Okay');
};
}
configChain(pem);
getUser(USER, cb);
webHooks = new WebHooks({
db: './webHooksDB.json'
});
var eh = chain.getEventHub();
if(!CHAINCODENAME) throw new Error('missing chaincode name');
var regid = eh.registerChaincodeEvent(CHAINCODENAME, "evtsender", function (event) {
console.log("Custom event received, payload: " + event.payload.toString() + "\n");
console.log("Firing webhook: " + webhookname);
webHooks.trigger(webhookname, { uuid: event.payload.toString() });
});
};
function configChain(pem) {
chain.setMemberServicesUrl(MEMBER_SERVICES, { pem: pem });
chain.addPeer(HYPERLEDGER_PEER, { pem: pem });
// chain.eventHubConnect(HL_EVENTS, { pem: pem });
}
function getUser(creds, cb) {
chain.getUser(creds.name, function (err, user) {
console.log(err, user);
if (err)
return cb(err);
if (user.isEnrolled())
return cb(null, user);
// User is not enrolled yet, so perform both registration and enrollment
// Enrollement is stored in the local key value store. Enrollment credentials
// are signel use. You may need a new credential from the CA
console.log('Try to Enroll');
chain.enroll(creds.name, creds.password, function (err) {
if (err) {
console.log('enroll error', err);
return cb(err);
}
chain.setRegistrar(user);
console.log('got user', user);
return cb(null, user);
});
});
}
function deployBracketStore(user, cb) {
user.getUserCert(null, function (err, userCert) {
if (err)
return cb(err);
var deployRequest = {
// Function to trigger
fcn: 'init',
// Arguments to the initializing function
args: [],
// Mark chaincode as confidential
// confidential: true,
// Assign Alice's cert
metadata: userCert.encode(),
chaincodePath: 'github.com/zmanian/bracketStore',
certificatePath: '/certs/blockchain-cert.pem'
};
// Trigger the deploy transaction
var deployTx = user.deploy(deployRequest);
deployTx.on('complete', function (results) {
return cb(null, results);
});
deployTx.on('error', function (err) {
return cb(err);
});
});
}
function registrantPOST(opts, user) {
var args = new CreateRegistrantTX()
args.RegistrantName = opts.RegistrantName
args.RegistrantPubKey = new Buffer(opts.RegistrantPubkey, 'hex');
args.Signature = new Buffer(opts.Signature, 'hex');
args.Data = opts.Data;
return new Promise(function (resolve, reject) {
invoke(user, 'createRegistrant', args.encode().toString('hex'), function (err, data) {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
function registerThing(opts, user) {
var args = new RegisterThingTX()
args.Nonce = new Buffer(opts.Nonce, 'hex');
// console.log("Nonce: " + opts.Nonce)
args.Aliases = []
opts.Aliases.forEach(function(o) {
args.Aliases.push(o);
})
// console.log("\n\nidentities:\n" + args.Identities)
args.RegistrantPubkey = opts.RegistrantPubkey
args.Signature = new Buffer(opts.Signature, 'hex');
args.Data = opts.Data;
args.Spec = opts.Spec;
console.log("\n\n\nargs:\n" + JSON.stringify(args, null, 4) + "\n\n\n");
return new Promise(function (resolve, reject) {
invoke(user, 'registerThing', args.encode().toString('hex'), function (err, data) {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
function registerSpec(opts, user) {
var args = new RegisterSpecTX()
args.SpecName = opts.SpecName
args.RegistrantPubkey = opts.RegistrantPubkey
args.Signature = new Buffer(opts.Signature, 'hex');
args.Data = opts.Data;
return new Promise(function (resolve, reject) {
invoke(user, 'registerSpec', args.encode().toString('hex'), function (err, data) {
if (err) {
reject(err);
}
else {
resolve(data);
}
});
});
}
function registrantGET(RegistrantPubkey, user) {
return new Promise(function (resolve, reject) {
query(user, 'owner', RegistrantPubkey, function (err, data, txid) {
if (err) {
reject(err);
} else {
resolve({ data: data, txid: txid });
}
});
});
}
function thing(alias, user) {
return new Promise(function (resolve, reject) {
query(user, 'thing', alias, function (err, data, txid) {
if (err) {
reject(err);
} else {
resolve({ data: data, txid: txid });
}
});
});
}
function spec(specName, user) {
return new Promise(function (resolve, reject) {
query(user, 'spec', specName, function (err, data, txid) {
if (err) {
reject(err);
} else {
resolve({ data: data, txid: txid });
}
});
});
}
function notify(webhook, cb) {
return webHooks.add(webhookname, webhook);
}
function query(user, func, args, cb) {
if (!user)
throw new Error('missing user');
if (!func)
throw new Error('missing func');
user.getUserCert(null, function (err, userCert) {
if (err) {
return cb(err);
}
var queryRequest = {
// Name (hash) required for query
// Function to trigger
fcn: func,
// Parameters for the query function
args: [args],
chaincodeID: CHAINCODENAME,
confidential: false,
userCert: userCert
};
var queryTx = user.query(queryRequest);
queryTx.on('complete', function (results) {
return cb(null, results);
});
queryTx.on('error', function (err) {
return cb(err);
});
});
}
function invoke(user, func, args, cb) {
user.getUserCert(null, function (err, userCert) {
if (err)
return cb(err);
var invokeRequest = {
// Name (hash) required for invoke
chaincodeID: CHAINCODENAME,
// Function to trigger
fcn: func,
// Parameters for the invoke function
args: [args],
confidential: false,
userCert: userCert
};
var meta = {
txId: null
};
var invokeTx = user.invoke(invokeRequest);
invokeTx.on('submitted', function (id) {
console.log('invoke submitted');
meta.txId = id.uuid;
});
invokeTx.on('complete', function (results) {
console.log('invoke complete');
return cb(null, results, meta);
});
invokeTx.on('error', function (err) {
console.log('invoke error')
return cb(err);
});
});
}
module.exports = {
initialize: initialize,
registrantPOST: registrantPOST,
registerThing: registerThing,
registerSpec: registerSpec,
registrantGET: registrantGET,
thing: thing,
spec: spec,
};