forked from bmos299/Fabric1GettingStartred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello-world-ui.js
More file actions
227 lines (216 loc) · 8.34 KB
/
hello-world-ui.js
File metadata and controls
227 lines (216 loc) · 8.34 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
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
// An end-to-end sample program on Fabric V1.0
'use strict';
process.env.GOPATH = __dirname;
var express = require('express');
var app = express();
var http = require('http');
var path = require('path');
var hfc = require('.');
var util = require('util');
var fs = require('fs');
var cors = require('cors');
// Enable CORS preflight across the board.
app.options('*', cors());
app.use(cors());
app.use(express.static('public'));
var bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.json());
var config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
var server = http.createServer(app).listen('3000', function() {});
server.timeout = 240000;
console.log('####################### Server Up - localhost:3000 #######################');
var chain = hfc.newChain(config.chainName);
var webUser;
var chaincodeID = "hello-wrold";
chain.setKeyValueStore(hfc.newKeyValueStore({
path: path.join(__dirname, "/" + config.KeyValueStore)
}));
chain.setMemberServicesUrl(config.ca.ca_url);
chain.setOrderer(config.orderer.orderer_url);
var targets = hfc.getPeer(config.peers[0].peer_url, config.peers[1].peer_url);
app.post('/login', function(req, res) {
// TODO: Get rid off Hardcoding the secret
chain.enroll(req.body.user, config.users[0].secret)
.then(
function(admin) {
webUser = admin;
console.log('Successfully enrolled user \'admin\'');
res.send('SUCCESS')
},
function(err) {
console.log('Failed to enroll user \'admin\'. ' + err);
process.exit();
}
).catch(
function(err) {
console.log('Failed to Register admin , hence login faled');
process.exit();
}
);
});
app.post('/deploy', function(req, res) {
chain.enroll(config.users[0].username, config.users[0].secret)
.then(
function(admin) {
webUser = admin;
var args = [];//getArgs(config.deployRequest.args);
args.push(req.body.user1);
args.push(req.body.val1);
args.push(req.body.user2);
args.push(req.body.val2);
// send proposal to endorser
var request = {
targets: targets,
chaincodePath: config.deployRequest.chaincodePath,
fcn: req.body.functionName,
args: args
};
console.log('###################### Deploy started');
return admin.sendDeploymentProposal(request);
}).then(
function(results) {
var proposalResponses = results[0];
var proposal = results[1];
if (proposalResponses && proposalResponses[0].response && proposalResponses[0].response.status === 200) {
console.log(util.format('Successfully sent Proposal and received ProposalResponse: Status - %s, message - "%s", metadata - "%s", endorsement signature: %s', proposalResponses[0].response.status, proposalResponse[0].response.message, proposalResponse[0].response.payload, proposalResponse[0].endorsement.signature));
res.send(chaincodeID);
return webUser.sendTransaction(proposalResponse, proposal);
} else {
console.log('Failed to send Proposal or receive valid response. Response null or status is not 200. exiting...');
process.exit(0);
}
},
function(err) {
console.log('Failed to send deployment proposal due to error: ' + err.stack ? err.stack : err);
process.exit(0);
}
).then(
function(response) {
if (response.Status === 'SUCCESS') {
console.log('Successfully ordered deployment endorsement.');
console.log('<==== wait for '+config.deployWaitTime+' (ms) the committer to catch up');
return sleep(parseInt(config.deployWaitTime));
} else {
console.log('Failed to order the deployment endorsement. Error code: ' + response.status);
process.exit();
}
},
function(err) {
console.log('Failed to send deployment e due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).catch(
function(err) {
console.log('Failed to send deployment proposal due to erro');
console.log( err.stack ? err.stack : err);
process.exit();
}
);
});
app.post('/invoke', function(req, res) {
chain.enroll(config.users[0].username, config.users[0].secret)
.then(
function(admin) {
webUser = admin;
var args = [];//getArgs(config.deployRequest.args);
args.push(req.body.method);
args.push(req.body.user1);
args.push(req.body.user2);
args.push(req.body.val);
// send proposal to endorser
var request = {
targets: targets,
chaincodeId: chaincodeID,
fcn: req.body.functionName,
args: args
};
console.log('###################### Invoke: sending TransactionProposal');
return admin.sendTransactionProposal(request);
}).then(
function(results) {
var proposalResponses = results[0];
var proposal = results[1];
if (proposalResponses[0].response.status === 200) {
console.log('Successfully obtained transaction endorsement.' + JSON.stringify(proposalResponses));
res.send('Successfully obtained transaction endorsement. Looks like your transaction is successful !!!');
return webUser.sendTransaction(proposalResponses, proposal);
} else {
console.log('Failed to obtain transaction endorsement. Error code: ' + status);
process.exit();
}
},
function(err) {
console.log('Failed to send transaction proposal due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).then(
function(response) {
if (response.Status === 'SUCCESS') {
console.log('Successfully ordered endorsement transaction.');
console.log('<==== wait for '+config.transactionWaitTime+' (ms) the committer to catch up');
return sleep(parseInt(config.transactionWaitTime));
} else {
console.log('Failed to order the endorsement of the transaction. Error code: ' + response.status);
process.exit();
}
},
function(err) {
console.log('Failed to send transaction proposal due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).catch(
function(err) {
console.log('Failed to send deployment proposal due to erro');
console.log( err.stack ? err.stack : err);
process.exit();
}
);
});
app.post('/query', function(req, res) {
chain.enroll(config.users[0].username, config.users[0].secret)
.then(
function(admin) {
webUser = admin;
var args = [];//getArgs(config.deployRequest.args);
args.push(req.body.method);
args.push(req.body.user);
// send proposal to endorser
var request = {
targets: targets,
chaincodeId: chaincodeID,
fcn: req.body.functionName,
args: args
};
console.log('###################### Query: queryByChaincode');
return admin.queryByChaincode(request);
}).then(
function(response_payload) {
console.log('User b now has ', response_payload.toString('utf8'));
res.send(response_payload.toString('utf8'));
//process.exit();
},
function(err) {
console.log('Failed to send query due to error: ' + err.stack ? err.stack : err);
process.exit();
}
).catch(
function(err) {
console.log('Failed to send deployment proposal due to erro');
console.log( err.stack ? err.stack : err);
process.exit();
}
);
});
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getArgs(requestArgs) {
var args = [];
for (var i = 0; i < requestArgs.length; i++) {
args.push(requestArgs[i]);
}
return args;
}