-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
412 lines (324 loc) · 11.6 KB
/
client.js
File metadata and controls
412 lines (324 loc) · 11.6 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
var fs = require("fs");
var u = require("url");
var q = require("bluebird");
var argv = require("attrs.argv");
/** 初始化全局配置 */
var config = require("./lib/config.js");
var endpoint = require("./lib/endpoint.js");
var block = require("./lib/block.js");
var unls = require("./lib/unls.js");
var pki = require("./lib/pki.js");
var comment_regex = /(\/\*){1}.*(\*\/){1}/g;
var txn_template = `{
"version": 1,
"existent_entries": [ /** 本次交易必须存在的账户 */ ],
"nonexistent_entries": [ /** 本次交易必须不存在的账户 */ ],
"out_entries": [ /** 本次交易需要资源转出的账户 */ ],
"in_entries": [ /** 本次交易需要资源转入的账户 */ ],
"remark": "本次交易的交易文字描述",
"signs_fields": [
"version", "timestamp",
"existent_entries", "nonexistent_entries",
"out_entries", "in_entries", "script"
]
}
`;
var txn_js = `/**
javascript api:
# 1. 创建账户
createEntry(entryId, { //自定义字段
"字段名称": "字段类型"
});
系统会默认在账户上建立基础的系统字段如下:
* id: string 账户id
* name: string 账户人的名字
* cert: string base64格式的账户公钥证书
* balance: balance 默认的账户余额
函数会返回账户对象
# 2. 获取账户对象
entry(id);
获取的账户必须出现在out_entries或nonexistent_entries中。
# 3. 赋值
直接对象的字段赋值就可以。但是被赋值的账户必须出现在out_entries中。
# 4. 转账
transfer(src_id, target_id, "field_name", amount);
src_id 资金转出账户id
target_id 资金转入账户id
"field_name" 资金字段
amount 资金金额
# 5. 当前区块信息数据
block();
所有属性都是只读的
字段类型:
number 数值类型
string 字符串类型
balance 资金类型
array 数组类型
*/
`;
var def = function(func, message) {
func.message = message || "";
return func;
};
var getNode = function(nodeInd) {
if (nodeInd == null)
nodeInd = 0;
var index = Number(nodeInd);
if (!isNaN(index))
return unls.nodes()[index];
return unls.node(nodeInd);
};
var fetchNodeHealthy = function(node) {
return function() {
return endpoint.request(node, "/healthy")
.then(function(response) {
node.cert = new Buffer(response.data.cert, "base64");
return response;
}, function(err) {
console.error(err);
});
};
};
var actions = {
"help": def(() => {
console.log(`client for blockchain v0.1\n`);
console.log(`Actions:`);
var keyname = Buffer.alloc(12, ' ');
for(var key in actions)
{
keyname.fill(' ');
keyname.write(key, 0);
console.log(`\t--action ${keyname}\t${actions[key].message}`);
}
console.log(`\n© 2019 51ods.com.`);
}, "help message"),
"list-nodes": def(() => {
var nodes = unls.nodes();
console.log("index\tnode-uuid\thost\tport");
nodes.every(function(node, index) {
console.log(`${index}\t${node.uuid}\t${node.host}\t${node.port}`);
return true;
});
}, "print all nodes."),
"healthy": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
fetchNodeHealthy(node)()
.then(function(response) {
console.log(response.data);
});
}, `request healthy of specify node.
\t --node (index or uuid) the index or uuid of specify node.
`),
"nodes": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
q.resolve({ })
.then(fetchNodeHealthy(node))
.then(function() {
return endpoint.request(node, "/nodes")
.then(function(response) {
console.log(response.data);
}, function(err) {
console.error(err);
});
});
}, `request unls status of specify node.
\t --node (index or uuid) the index or uuid of specify node.
`),
"new-entry": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
var data = {
id: argv.id,
password: argv.password || "",
email: argv.email || ""
};
var output = argv.output || "./";
q.resolve({ })
.then(fetchNodeHealthy(node))
.then(function() {
return endpoint.request(node, u.format({
pathname: "/news",
query: data
}))
.then(function(response) {
if (response.data.status != 200)
{
console.error(response.data);
return;
}
var entryId = response.data.result.entryId;
var cert = Buffer.from(response.data.result.cert, "base64");
var key = Buffer.from(response.data.result.key, "base64");
console.log(`your entryId is ${entryId}.`);
console.log(`save your private key into ${output}/${entryId}.key.`);
fs.writeFileSync(`${output}/${entryId}.key`, key);
console.log(`save your public key into ${output}/${entryId}.crt.`);
fs.writeFileSync(`${output}/${entryId}.crt`, cert);
console.log(`your public key: \n${response.data.result.cert}`);
console.log("ok!");
}, function(err) {
console.error(err);
});
});
}, `request the init information of new entry.
\t --node (index or uuid) the index or uuid of specify node.
\t [ --id entryid ] the id of entry.
\t --password password the password of entry.
\t --email email the email of entry
\t --output outputdir destination of the init information of new entry.
`),
"new-txn": def(() => {
var output = argv.output || "./";
var txn = argv.txn || "txn";
txn = `${output}/${txn}`;
console.log(`create template of transaction into ${txn}.json`);
fs.writeFileSync(`${txn}.json`, txn_template, "utf-8");
console.log(`create script of transaction into ${txn}.js`);
fs.writeFileSync(`${txn}.js`, txn_js, "utf-8");
}, `init transaction file where specify path.
\t --txn name name of transaction
\t --output outputdir destination of the init transaction file.
`),
"build-txn": def(() => {
var txn = argv.txn || "./txn";
var json =
JSON.parse(fs.readFileSync(`${txn}.json`, "utf-8").replace(comment_regex, ""));
var script = fs.readFileSync(`${txn}.js`);
json = block.normalize(json);
json.timestamp = Date.now();
json.script = script.toString("base64");
json.hash =
Buffer.from(pki.digest(json, json.signs_fields).digest().getBytes()).toString("hex");
console.log(`create transaction data into ${txn}.dat with hash ${json.hash}`);
fs.writeFileSync(`${txn}.dat`, JSON.stringify(json, "", " "), "utf-8");
}, `build transaction
\t --txn path pathname of the transaction
`),
"sign-txn": def(() => {
var txn = argv.txn || "./txn";
var entryId = argv.id;
var keyname = argv.key;
var password = argv.password;
var json = JSON.parse(fs.readFileSync(`${txn}.dat`, "utf-8"));
var key = pki.loadPrivateKey(fs.readFileSync(`${keyname}`), password);
if (key == null)
{
console.error(`missing private key or incorrect password`);
return;
}
var digest = pki.digest(json, json.signs_fields);
if (json.hash != Buffer.from(digest.digest().getBytes()).toString("hex"))
{
console.error(`incorrect hash of transaction ${txn}.dat`);
return;
}
var entry_sign = json.entry_signs.find(function(entry_sign) {
return entry_sign.entry == entryId;
});
if (entry_sign == null)
{
json.entry_signs.push(
entry_sign = {
entry: entryId
}
);
}
entry_sign.sign = Buffer.from(pki.signIt(digest, key)).toString("hex");
console.log(`update transaction data into ${txn}.dat with hash ${json.hash}`);
fs.writeFileSync(`${txn}.dat`, JSON.stringify(json, "", " "), "utf-8");
}, `sign transaction
\t --txn path pathname of the transaction
\t --id entryId the id of entry
\t --key pathname pathname of the private key
\t --password password password of entry
`),
"txn-status": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
q.resolve({ })
.then(fetchNodeHealthy(node))
.then(function() {
return endpoint.request(node, u.format({
pathname: "/txnstatus",
query: {
hash: argv.hash
}
}))
.then(function(response) {
console.log(response.data);
})
});
}, `request status of a transaction
\t --node (index or uuid) the index or uuid of specify node.
\t --hash hash the hash of transaction
`),
"commit-txn": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
var txn = argv.txn || "./txn";
var json = JSON.parse(fs.readFileSync(`${txn}.dat`, "utf-8"));
q.resolve({ })
.then(fetchNodeHealthy(node))
.then(function() {
return endpoint.request(node, "/commit", "POST", json)
.then(function(response) {
console.log(response.data);
})
});
}, `commit transaction to node
\t --node (index or uuid) the index or uuid of specify node.
\t --txn path pathname of the transaction
`),
"reload": def(() => {
var node = getNode(argv.node);
if (node == null)
{
console.error(`node ${argv.node} not found`);
return;
}
q.resolve({ })
.then(fetchNodeHealthy(node))
.then(function() {
return endpoint.request(node, "/reload")
.then(function(response) {
console.log(response.data);
})
});
}, `notify node to reload config
\t --node (index or uuid) the index or uuid of specify node.
`)
};
q.resolve({ })
.then(function() {
return config.init("./config/client.json");
})
.then(function() {
return unls.init(false);
})
.done(function() {
var key = argv.action;
var action = actions[key] || actions["help"];
action();
});