-
Notifications
You must be signed in to change notification settings - Fork 36
/
BlocIO.js
162 lines (135 loc) · 3.69 KB
/
BlocIO.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
let API_URL = 'https://block.io/api/v2/';
let libPrefix = 'libblockio_'
function getCredential(options, prms){
let apiKey = ( prms.api_key ? prms.api_key :
Bot.getProperty( libPrefix + options.coin.toLowerCase() + 'apikey')
);
let pin = '';
if(options.method.indexOf('withdraw') + 1) {
pin = (prms.pin ? prms.pin :
Bot.getProperty( libPrefix + 'secretpin')
)
if(pin!=''){ pin = '&pin=' + pin }
}
return 'api_key=' + apiKey + pin;
}
function apiGet(options = {}, prms){
if(!verifyCallback(prms)){ return }
let callbacks = getCallbacks(prms, 'on_' + options.method);
let credential = getCredential(options, prms);
let url = API_URL + options.method + '?' + credential +
'&' + options.query;
HTTP.get( {
url: url,
success: callbacks.onSuccess,
error: callbacks.onError
} )
}
function onApiResponse(){
let json
try{
json = JSON.parse(content);
}catch(err){
throw 'Error with downloaded JSON content: ' + content
}
let arr = params.split(' ');
if(json.status=='success'){
let user_callback_cmd = arr[1];
Bot.runCommand(user_callback_cmd, json.data);
}else{
let user_callback_err_cmd = arr[arr.length-1];
Bot.runCommand(user_callback_err_cmd, json);
}
}
function onApiError(){
let json;
let user_callback_cmd = params.split(' ')[0];
if(content){
json = JSON.parse(content);
if(json.status=='fail'){
Bot.runCommand(
user_callback_cmd + ' ' + json.data.error_message
);
return
}
}else
Bot.runCommand(user_callback_cmd);
}
function verifyCallback(prms){
if(typeof(prms.onSuccess)!='string'){
throw 'Need handler callback command';
}
return true
}
function getCallbacks(prms, successKey){
return {
onSuccess: (libPrefix + 'callback ' + successKey +
' ' + prms.onSuccess + ' ' + prms.onError),
onError: (libPrefix + 'callback ' + 'on_api_error ' + prms.onError)
}
}
function snakeToCamel(string) {
return string.replace(/(_\w)/g, function(m){
return m[1].toUpperCase();
});
}
function doApiGetFor(coin, method, prms){
let query = '';
let i = 0;
let keys = Object.keys(prms);
for(let ind in prms){
query+= keys[i] + '=' + prms[ind] + '&'
i+=1;
}
apiGet( { method: method, coin: coin, query: query}, prms);
}
function getApiFunctions(coin, funcs_names){
let result = {};
let funcName;
for(let ind in funcs_names){
funcName = snakeToCamel(funcs_names[ind]);
result[funcName] = function(prms){ doApiGetFor(coin, funcs_names[ind], prms) }
}
return result;
}
function getMethodsForCoin(coin){
let result = getApiFunctions(coin, [
'get_new_address',
'get_balance',
'get_address_balance',
'get_my_addresses',
'get_address_by_label',
'is_valid_address',
'is_green_transaction',
'archive_addresses',
'unarchive_addresses',
'get_my_archived_addresses',
'get_transactions',
'get_raw_transaction',
'get_network_fee_estimate',
'get_current_price',
'withdraw',
'withdraw_from_addresses',
'withdraw_from_labels'
]);
result['setApiKey'] = function(apiKey){
Bot.setProperty( libPrefix + coin.toLowerCase() + 'apikey', apiKey, 'string');
return coin;
}
return result;
}
let setSecretPin = function(pin){
Bot.setProperty( libPrefix + 'secretpin', pin, 'string');
}
publish({
setSecretPin: setSecretPin,
testNet: {
Bitcoin: getMethodsForCoin('TestNetBitcoin'),
Litecoin: getMethodsForCoin('TestNetLitecoin'),
Dogecoin: getMethodsForCoin('TestNetDogecoin'),
},
Bitcoin: getMethodsForCoin('Bitcoin'),
Litecoin: getMethodsForCoin('Litecoin'),
Dogecoin: getMethodsForCoin('Dogecoin'),
})
on(libPrefix + 'callback', onApiResponse);