-
Notifications
You must be signed in to change notification settings - Fork 96
/
uri.js
296 lines (280 loc) · 10.4 KB
/
uri.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
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
/*jslint node: true */
"use strict";
var ValidationUtils = require("./validation_utils.js");
var constants = require("./constants.js");
var conf = require('./conf.js');
var Mnemonic = require('bitcore-mnemonic');
function parseUri(uri, callbacks){
var protocol = 'byteball';
if (process.env.testnet)
protocol += '-tn';
if (process.env.devnet)
protocol += '-dn';
var re = new RegExp('^'+protocol+':(.+)$', 'i');
var arrMatches = uri.match(re);
if (!arrMatches){ // try with obyte
var oprotocol = protocol.replace(/byteball/i, 'obyte');
re = new RegExp('^'+oprotocol+':(.+)$', 'i');
arrMatches = uri.match(re);
if (!arrMatches) {
// try the default proto
protocol = 'obyte';
re = new RegExp('^'+protocol+':(.+)$', 'i');
arrMatches = uri.match(re);
if (!arrMatches)
return callbacks.ifError("no "+protocol+" or "+oprotocol+" prefix");
}
}
var value = arrMatches[1];
var objRequest = {};
// pairing / start a chat
// var arrPairingMatches = value.match(/^([\w\/+]{44})@([\w.:\/-]+)(?:#|%23)([\w\/+]+)$/);
var arrPairingMatches = value.replace('%23', '#').match(/^([\w\/+]{44})@([\w.:\/-]+)#(.+)$/);
if (arrPairingMatches){
objRequest.type = "pairing";
objRequest.pubkey = arrPairingMatches[1];
objRequest.hub = arrPairingMatches[2];
objRequest.pairing_secret = arrPairingMatches[3];
//if (objRequest.pairing_secret.length > 12)
// return callbacks.ifError("pairing secret too long");
return callbacks.ifOk(objRequest);
}
// authentication/authorization
var arrAuthMatches = value.match(/^auth\?(.+)$/);
if (arrAuthMatches){
objRequest.type = "auth";
var query_string = arrAuthMatches[1];
var assocParams = parseQueryString(query_string);
if (assocParams.url){
if (!assocParams.url.match(/^https?:\/\//))
return callbacks.ifError("invalid url");
}
else if (assocParams.device){
if (!assocParams.pairing_secret)
return callbacks.ifError("no pairing secret in auth params");
if (!assocParams.app)
return callbacks.ifError("no app in auth params");
var arrParts = assocParams.device.split('@');
if (arrParts.length !== 2)
return callbacks.ifError("not 2 parts in full device address");
var pubkey = arrParts[0];
var hub = arrParts[1];
if (pubkey.length !== constants.PUBKEY_LENGTH)
return callbacks.ifError("pubkey length is not 44");
if (hub.match(/[^\w\.:-]/))
return callbacks.ifError("invalid hub address");
}
else
return callbacks.ifError("neither url nor device in auth params");
objRequest.params = assocParams;
return callbacks.ifOk(objRequest);
}
function handleMnemonic(mnemonic){
try {
if (Mnemonic.isValid(mnemonic)) {
objRequest.mnemonic = mnemonic;
return callbacks.ifOk(objRequest);
} else {
return callbacks.ifError("invalid mnemonic");
}
} catch(e) {
return callbacks.ifError("invalid mnemonic");
}
}
// claim textcoin using mnemonic
var arrMnemonicMatches = value.match(/^textcoin\?(.+)$/);
if (arrMnemonicMatches){
objRequest.type = "textcoin";
var mnemonic = arrMnemonicMatches[1].split('-').join(' ');
return handleMnemonic(mnemonic);
}
var arrWords = value.split('-');
if (arrWords.length === 12){
objRequest.type = "textcoin";
mnemonic = arrWords.join(' ');
return handleMnemonic(mnemonic);
}
// pay to address or send data
var arrParts = value.split('?');
if (arrParts.length > 2)
return callbacks.ifError("too many question marks");
var main_part = decodeURIComponent(arrParts[0]);
var query_string = arrParts[1];
if (main_part === 'data') {
if (!query_string)
return callbacks.ifError("data without query string");
var assocParams = parseQueryString(query_string);
objRequest = assocParams;
objRequest.type = 'data';
var app = assocParams.app;
if (!['data', 'temp_data', 'data_feed', 'attestation', 'profile', 'poll', 'vote', 'definition', 'text', 'system_vote', 'system_vote_count'].includes(app))
return callbacks.ifError("invalid app: " + app);
if (app === 'attestation' && !ValidationUtils.isValidAddress(assocParams.address))
return callbacks.ifError("invalid attested address: "+assocParams.address);
if (app === 'vote' && !ValidationUtils.isValidBase64(assocParams.unit, constants.HASH_LENGTH))
return callbacks.ifError("invalid poll unit: " + assocParams.unit);
if (app === 'definition') {
var definition = assocParams.definition;
if (!definition)
return callbacks.ifError("no definition");
if (definition.substr(0, 8) === 'https://') {
return fetchUrl(definition, function (err, response) {
if (err)
return callbacks.ifError(err);
assocParams.definition = response;
callbacks.ifOk(objRequest);
});
}
}
if (app === 'system_vote' || app === 'system_vote_count') {
const { subject, value } = assocParams;
if (!["op_list", "threshold_size", "base_tps_fee", "tps_interval", "tps_fee_multiplier"].includes(subject))
return callbacks.ifError("invalid subject: " + subject);
if (app === 'system_vote') {
if (!value)
return callbacks.ifError(`no value in system_vote`);
if (subject === 'op_list') {
const arrOPs = value.split('\n');
if (arrOPs.length !== constants.COUNT_WITNESSES)
return callbacks.ifError(`wrong number of OPs in ` + value);
if (!arrOPs.every(ValidationUtils.isValidAddress))
return callbacks.ifError(`some OP addresses are invalid in ` + value);
}
else if (!value.match(/^[\d.]+$/))
return callbacks.ifError(`${subject} must be a number, found ` + value);
}
}
return callbacks.ifOk(objRequest);
}
var address = main_part;
if (!ValidationUtils.isValidAddress(address) && !ValidationUtils.isValidEmail(address) && !address.match(/^(steem\/|reddit\/|github\/|bitcointalk\/|@).{3,}/i) && !address.match(/^\+\d{9,14}$/))
return callbacks.ifError("address "+address+" is invalid");
objRequest.type = "address";
objRequest.address = address;
if (query_string){
var assocParams = parseQueryString(query_string);
var strAmount = assocParams.amount;
if (typeof strAmount === 'string'){
var amount = parseInt(strAmount);
if (amount + '' !== strAmount)
return callbacks.ifError("invalid amount: "+strAmount);
if (!ValidationUtils.isPositiveInteger(amount))
return callbacks.ifError("nonpositive amount: "+strAmount);
objRequest.amount = amount;
}
var asset = assocParams.asset;
if (typeof asset === 'string'){
if (asset !== 'base' && !ValidationUtils.isValidBase64(asset, constants.HASH_LENGTH)) // invalid asset
return callbacks.ifError('invalid asset: '+asset);
objRequest.asset = asset;
}
if (!objRequest.asset && objRequest.amount) // when amount is set, asset must be also set
objRequest.asset = 'base';
if (objRequest.amount && objRequest.asset) {
var res = parseAdditionalAssets(objRequest.asset, assocParams);
if (res.error)
return callbacks.ifError(res.error);
if (res.additional_assets)
objRequest.additional_assets = res.additional_assets;
}
if (assocParams.device_address) {
objRequest.device_address = assocParams.device_address;
if (!ValidationUtils.isValidDeviceAddress(objRequest.device_address))
return callbacks.ifError('invalid device address: '+objRequest.device_address);
}
if (assocParams.base64data) {
objRequest.base64data = assocParams.base64data;
if (!ValidationUtils.isValidBase64(objRequest.base64data))
return callbacks.ifError('invalid base64 data: '+objRequest.base64data);
}
if (assocParams.from_address) {
objRequest.from_address = assocParams.from_address;
if (!ValidationUtils.isValidAddress(objRequest.from_address))
return callbacks.ifError('invalid from address: '+objRequest.from_address);
}
var single_address = assocParams.single_address;
if (single_address) {
single_address = single_address.replace(/^single/, ''); // backward compatibility
if (single_address && ValidationUtils.isValidAddress(single_address))
objRequest.from_address = single_address;
objRequest.single_address = 1;
}
}
callbacks.ifOk(objRequest);
}
function parseAdditionalAssets(main_asset, assocParams) {
var additional_assets = {};
var assets = [main_asset];
for (var i = 2; assocParams['amount' + i]; i++){
var additional_amount = parseInt(assocParams['amount' + i]);
if (additional_amount + '' !== assocParams['amount' + i])
return { error: "invalid additional amount: " + assocParams['amount' + i] };
if (!ValidationUtils.isPositiveInteger(additional_amount))
return { error: "nonpositive additional amount: " + additional_amount };
var additional_asset = assocParams['asset' + i] || 'base';
if (additional_asset !== 'base' && !ValidationUtils.isValidBase64(additional_asset, constants.HASH_LENGTH)) // invalid asset
return { error: 'invalid additional asset: ' + additional_asset };
if (assets.indexOf(additional_asset) >= 0)
return { error: 'asset ' + additional_asset + ' already used' };
assets.push(additional_asset);
additional_assets[additional_asset] = additional_amount;
}
return Object.keys(additional_assets).length > 0 ? { additional_assets: additional_assets } : {};
}
function parseQueryString(str, delimiter){
if (!delimiter)
delimiter = '&';
var arrPairs = str.split(delimiter);
var assocParams = {};
arrPairs.forEach(function(pair){
var arrNameValue = pair.split('=');
if (arrNameValue.length !== 2)
return;
var name = decodeURIComponent(arrNameValue[0]);
var value = decodeURIComponent(arrNameValue[1]);
assocParams[name] = value;
});
return assocParams;
}
function fetchUrl(url, cb) {
var https = require('https');
var bDone = false;
function returnError(err) {
console.log(err);
if (bDone)
return;
bDone = true;
cb(err);
}
try {
https.get(url, function (resp) {
if (resp.statusCode !== 200)
return returnError("non-200 response while trying to fetch " + url);
var data = '';
// A chunk of data has been recieved.
resp.on('data', function(chunk) {
data += chunk;
});
// aborted before the whole response has been received
resp.on('aborted', function () {
returnError("connection aborted while trying to fetch " + url);
});
// The whole response has been received
resp.on('end', function () {
if (bDone)
return;
bDone = true;
cb(null, data);
});
}).on("error", function(err) {
returnError("non-200 response while trying to fetch " + url + ": " + err.message);
});
}
catch(err) {
returnError(err.message);
}
}
exports.parseQueryString = parseQueryString;
exports.parseAdditionalAssets = parseAdditionalAssets;
exports.parseUri = parseUri;
exports.fetchUrl = fetchUrl;