-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
83 lines (83 loc) · 2.82 KB
/
api.js
File metadata and controls
83 lines (83 loc) · 2.82 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
// Generated by LiveScript 1.6.0
(function(){
var post, guid, jsonStringify, jsonParse, bitcore, Message, createSignature, genericCall, kycBuilder, convertBuilder, createClientid, getAddress, createApi, out$ = typeof exports != 'undefined' && exports || this;
post = require('superagent').post;
guid = require('./guid.js');
jsonStringify = require('./json-stringify.js');
jsonParse = require('./json-parse.js');
bitcore = require('bitcore-lib');
Message = require('bitcore-message');
createSignature = function(privateKeyHex, request, cb){
var privateKey;
privateKey = bitcore.PrivateKey.fromWIF(privateKeyHex);
return jsonStringify(request, function(err, str){
var signature;
if (err != null) {
return cb(err);
}
signature = Message(str).sign(privateKey);
if (err != null) {
return cb(err);
}
return cb(null, signature);
});
};
genericCall = function(fields, name){
return function(config){
return function(request, cb){
var apiUrl, privateKey, i$, ref$, len$, item;
apiUrl = config.apiUrl, privateKey = config.privateKey;
for (i$ = 0, len$ = (ref$ = fields).length; i$ < len$; ++i$) {
item = ref$[i$];
if (request[item] == null) {
return cb("`" + item + "` is required");
}
}
return createSignature(privateKey, request, function(err, signature){
if (err != null) {
return cb(err);
}
return post(apiUrl + "/" + name, request).set('signature', signature).end(function(err, data){
if (err != null) {
return cb(err);
}
return jsonParse(data.text, function(err, result){
if (err != null) {
return cb(err);
}
return cb(null, result);
});
});
});
};
};
};
kycBuilder = genericCall(['firstname', 'lastname', 'bill', 'photo', 'clientid'], 'kyc');
convertBuilder = genericCall(['to', 'from', 'value', 'address', 'clientid'], 'exchange');
out$.createClientid = createClientid = guid;
out$.getAddress = getAddress = function(key){
var privateKey;
privateKey = bitcore.PrivateKey.fromWIF(key);
return privateKey.toAddress(bitcore.Networks.livenet).toString();
};
out$.createApi = createApi = function(arg$, cb){
var apiUrl, privateKey, config, convert, kyc;
apiUrl = arg$.apiUrl, privateKey = arg$.privateKey;
if (apiUrl == null) {
return cb("apiUrl is required");
}
if (privateKey == null) {
return cb("private-key is required");
}
config = {
apiUrl: apiUrl,
privateKey: privateKey
};
convert = convertBuilder(config);
kyc = kycBuilder(config);
return cb(null, {
convert: convert,
kyc: kyc
});
};
}).call(this);