-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpoeRouter.js
261 lines (233 loc) · 7.21 KB
/
poeRouter.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
/*
Copyright 2017 BlocLedger
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
///////////////////////////////////////////////////
//
// POE application REST endpoints
//
///////////////////////////////////////////////////
// Add support for the Applicaion specific REST calls
// addDoc, listDoc, transferDoc, verifyDoc, and delDoc
/* jshint node: true */
'use strict';
var express = require('express');
var router = express.Router();
var retryCount = require('./configuration.js').retryCount; // SDK retry count
var sdkInterface = require('./sdkInterface.js');
var init = require('./initialize.js');
var debug = require('debug')('poe');
var GlobalAppUser = init.GlobalAppUser;
var poeChaincode = init.poeChaincode;
var confidentialSetting = false;
// setup the sdk interface
var retrySdkInvoke = sdkInterface.retrySdkInvoke;
var retrySdkQuery = sdkInterface.retrySdkQuery;
// addDoc Add a new document to the block chain
router.post('/addDoc', function(req, res) {
debug('/addDoc request body %j', req.body);
var hashValid = true;
if (!req.body.hash) {
hashValid = false;
console.log('no hash provided');
} else if (req.body.hash == 'file hash') {
hashValid = false;
} else if (req.body.hash.length != 64) {
hashValid = false;
}
if (hashValid === false) {
console.log('The hash is invalid.');
return res.status(500).send('Error: invalid hash');
}
debug('hash equals %s', req.body.hash);
var params = req.body;
var hash = req.body.hash;
var appUser = GlobalAppUser[req.session.appUser];
debug(appUser);
params.owner = appUser.userName;
var invokeRequest = {
chaincodeID: poeChaincode.id,
fcn: 'addDoc',
args: [hash, JSON.stringify(params)],
confidential: confidentialSetting
};
debug('The invoke args = ', invokeRequest.args);
retrySdkInvoke(appUser.hfcUser, invokeRequest, retryCount)
.then(function(results) {
console.log('The completion results for /addDoc %j', results);
if (results instanceof Error) {
res.status(500).send(results.msg);
}
res.json(results);
}, function(err) {
// Invoke transaction submission failed
var msg = 'unknown error';
if (!err) {
msg = 'error undefined';
} else if (err.msg) {
msg = err.msg;
} else if (err.context) {
debug(err.context);
msg = err.context.msg;
} else if (err.message) {
msg = err.message;
}
console.log('Invoke failed for addDoc: ' + msg);
debug(err);
res.status(500).send(msg);
});
});
router.get('/verifyDoc/:hash', function(req, res) {
debug('received /verifyDoc with hash = %s', req.params.hash);
var appUser = GlobalAppUser[req.session.appUser];
var queryRequest = {
chaincodeID: poeChaincode.id,
fcn: 'readDoc',
args: [req.params.hash]
};
retrySdkQuery(appUser.hfcUser, queryRequest, retryCount)
.then(function(results) {
debug('successfully queried an existing document: ');
debug(typeof(results));
debug(results);
results.forEach(function(result) {
debug(result.toString());
});
if (results.length !== 0) {
var params = JSON.parse(results);
debug(params);
res.json(params);
} else {
res.status(500).send('Document not found');
}
}, function(err) {
var msg = 'unknown error';
if (!err) {
msg = 'error undefined';
} else if (err.msg) {
msg = err.msg;
} else if (err.context) {
debug(err.context);
msg = err.context.msg;
} else if (err.message) {
msg = err.message;
}
console.log('/verifyDoc query failed: ', msg);
res.status(500).send(msg);
});
});
// list document
// returns a list of all of the documents
router.get('/listDoc', function(req, res) {
debug('received /listDoc');
var appUser = GlobalAppUser[req.session.appUser];
var queryRequest = {
chaincodeID: poeChaincode.id,
fcn: 'listDoc',
args: []
};
retrySdkQuery(appUser.hfcUser, queryRequest, retryCount)
.then(function(results) {
debug('successfully queried for the document list.');
debug(results);
if (results.length !== 0) {
var list = JSON.parse(results);
debug(list);
res.json(list);
} else {
res.status(500).send('Document list invalid');
}
}, function(err) {
var msg = 'unknown error';
if (!err) {
msg = 'error undefined';
} else if (err.msg) {
msg = err.msg;
} else if (err.context) {
debug(err.context);
msg = err.context.msg;
} else if (err.message) {
msg = err.message;
}
console.log('/listDoc query failed: ', msg);
res.status(500).send(msg);
});
});
// delDoc Delete a document from the block chain
router.post('/delDoc', function(req, res) {
debug('/delDoc request body %j', req.body);
var appUser = GlobalAppUser[req.session.appUser];
debug('hash equals %s', req.body.hash);
var hash = req.body.hash;
var invokeRequest = {
chaincodeID: poeChaincode.id,
fcn: 'delDoc',
args: [hash],
confidential: confidentialSetting
};
debug('The invoke args = ', invokeRequest.args);
retrySdkInvoke(appUser.hfcUser, invokeRequest, retryCount)
.then(function(results) {
console.log('The completion results for /delDoc %j', results);
res.json(results);
}, function(err) {
// Invoke transaction submission failed
var msg = 'unknown error';
if (!err) {
msg = 'error undefined';
} else if (err.msg) {
msg = err.msg;
} else if (err.context) {
debug(err.context);
msg = err.context.msg;
} else if (err.message) {
msg = err.message;
}
console.log('Invoke failed for delDoc: ' + msg);
res.status(500).send(msg);
});
});
// editDoc changes the owner of the doc but may be enhanced to include other parameters
router.post('/editDoc', function(req, res) {
debug('/editDoc request body %j', req.body);
var appUser = GlobalAppUser[req.session.appUser];
var hash = req.body.hash;
var invokeRequest = {
chaincodeID: poeChaincode.id,
fcn: 'transferDoc',
args: [req.body.hash, req.body.owner],
confidential: confidentialSetting
};
debug('The invoke args = ', invokeRequest.args);
retrySdkInvoke(appUser.hfcUser, invokeRequest, retryCount)
.then(function(results) {
console.log('The completion results for /transferDoc %j', results);
res.json(results);
}, function(err) {
// Invoke transaction submission failed
debug('POE transferDoc invoke failed.');
debug(err);
var msg = 'unknown error';
if (!err) {
msg = 'error undefined';
} else if (err.msg) {
msg = err.msg;
} else if (err.context) {
debug(err.context);
msg = err.context.msg;
} else if (err.message) {
msg = err.message;
}
console.log('Invoke failed for transferDoc: ' + msg);
res.status(500).send(msg);
});
});
module.exports = router;