-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainer.js
More file actions
116 lines (116 loc) · 4.41 KB
/
container.js
File metadata and controls
116 lines (116 loc) · 4.41 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
// Generated by LiveScript 1.5.0
(function(){
var fs, p, Message, bitcore, superagent, wallet, generateKeys, guid, request, simple, status, methods, start, stop, info, getContainerList, create, update, method, sign, getContainer, out$ = typeof exports != 'undefined' && exports || this, toString$ = {}.toString;
fs = require('fs');
p = require('prelude-ls');
Message = require('bitcore-message');
bitcore = require('bitcore-lib');
superagent = require('superagent');
wallet = require('./wallet.js');
generateKeys = wallet.generateKeys;
guid = function(){
var s4;
s4 = function(){
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
};
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};
request = function(config, path, body, cb){
var mnemonic, name, node, parts, type, urlPart, ck, url, requestid, message, privateKey2, signature, req;
mnemonic = config.mnemonic, name = config.name, node = config.node;
if (mnemonic == null) {
return cb("Mnemonic is required");
}
if (node == null) {
return cb("Node is required");
}
parts = path.match(/^([A-Z]+) (\/.+)$/);
type = parts[1].toLowerCase();
urlPart = parts[2].replace(/:name/, name);
ck = generateKeys(mnemonic);
url = node + "" + urlPart;
requestid = guid();
message = p.join(';')(
p.map(JSON.stringify)(
[urlPart, body, requestid]));
privateKey2 = bitcore.PrivateKey.fromWIF(ck.privateKey);
signature = Message(message).sign(privateKey2);
req = superagent[type](url);
return req.send(body).set('requestid', requestid).set('address', ck.address).set('signature', signature).end(cb);
};
simple = curry$(function(path, config, cb){
return request(config, path, {}, function(err, data){
cb(err, data != null ? data.text : void 8);
});
});
status = simple("GET /container/status/:name");
methods = simple("GET /container/methods/:name");
start = simple("POST /container/start/:name");
stop = simple("POST /container/stop/:name");
info = simple("GET /container/:name");
out$.getContainerList = getContainerList = simple("GET /containers");
create = curry$(function(config, data, cb){
if (toString$.call(data).slice(8, -1) !== 'Object') {
return cb("Data Must be an Object");
}
if (toString$.call(data.files).slice(8, -1) !== 'Object') {
return cb("'files' is required field");
}
return request(config, "POST /container/create/:name", data, function(err, data){
cb(err, data != null ? data.text : void 8);
});
});
update = curry$(function(config, data, cb){
if (toString$.call(data).slice(8, -1) !== 'Object') {
return cb("Data Must be an Object");
}
if (toString$.call(data.affectedFiles).slice(8, -1) !== 'Object') {
return cb("'affected-files' is object: { filename: 'content', ... }");
}
if (toString$.call(data.deletesFiles).slice(8, -1) !== 'Array') {
return cb("'deletes-files' is array [\filename1, \filename2]");
}
return request(config, "POST /container/update/:name", data, function(err, data){
cb(err, data != null ? data.text : void 8);
});
});
method = curry$(function(config, method, data, cb){
if (toString$.call(data).slice(8, -1) !== 'Object') {
return cb("Data Must be Object");
}
return request(config, "POST /container/:name/" + method, data, function(err, data){
cb(err, data != null ? data.text : void 8);
});
});
out$.sign = sign = function(mnemonic, message){
var ck, privateKey2;
ck = generateKeys(mnemonic);
privateKey2 = bitcore.PrivateKey.fromWIF(ck.privateKey);
return Message(message).sign(privateKey2);
};
out$.getContainer = getContainer = function(config){
return {
status: status(config),
create: create(config),
update: update(config),
start: start(config),
stop: stop(config),
method: method(config),
methods: methods(config),
info: info(config)
};
};
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
};
return _curry();
}
}).call(this);