This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
settings.js
370 lines (316 loc) · 10.7 KB
/
settings.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
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
var assert = require('assert');
var async = require('async');
var ripple = require('ripple-lib');
var remote = require('./../lib/remote.js');
var respond = require('./../lib/response-handler.js');
var errors = require('./../lib/errors.js');
const InvalidRequestError = errors.InvalidRequestError;
const AccountRootFlags = {
PasswordSpent: { name: 'password_spent', value: 0x00010000 },
RequireDestTag: { name: 'require_destination_tag', value: 0x00020000 },
RequireAuth: { name: 'require_authorization', value: 0x00040000 },
DisallowXRP: { name: 'disallow_xrp', value: 0x00080000 },
DisableMaster: { name: 'disable_master', value: 0x00100000 },
NoFreeze: { name: 'no_freeze', value: 0x00200000 },
GlobalFreeze: { name: 'global_freeze', value: 0x00400000 }
};
const AccountRootFields = {
Sequence: { name: 'transaction_sequence' },
EmailHash: { name: 'email_hash', encoding: 'hex', length: 32 },
WalletLocator: { name: 'wallet_locator', encoding: 'hex', length: 64 },
WalletSize: { name: 'wallet_size' },
MessageKey: { name: 'message_key' },
Domain: { name: 'domain', encoding: 'hex' },
TransferRate: { name: 'transfer_rate' },
Signers: { name: 'signers' }
};
// Emptry string passed to setting will clear it
const CLEAR_SETTING = '';
/**
* There are different ways to clear account root fields. Find the clearing
* value for a given field name
*
* @param {String} fieldName
*/
function getClearValue(fieldName) {
assert.strictEqual(typeof fieldName, 'string');
switch (fieldName) {
case 'Emailhash':
case 'WalletLocator':
return '0';
break;
case 'TransferRate':
case 'WalletSize':
return 0;
break;
default:
return '';
}
};
/**
* Pad the value of a fixed-length field
*
* @param {String} value
* @param {Number} length
* @return {String}
*/
function padValue(value, length) {
assert.strictEqual(typeof value, 'string');
assert.strictEqual(typeof length, 'number');
var result = value;
while (result.length < length) {
result = '0' + result;
}
return result;
};
/**
* Retrieves account settings for a given account
*
* @url
* @param {String} request.params.account
*
* @param {Express.js Response} response
* @param {Express.js Next} next
*/
function getSettings(request, response, next) {
remote.requestAccountInfo({account: request.params.account}, function(error, info) {
if (error) {
return next(error);
}
var data = info.account_data;
var settings = {
account: data.Account,
transfer_rate: '0'
};
// Attach account flags
for (var flagName in AccountRootFlags) {
var flag = AccountRootFlags[flagName];
settings[flag.name] = Boolean(data.Flags & flag.value);
}
// Attach account fields
for (var fieldName in AccountRootFields) {
var field = AccountRootFields[fieldName];
var value = data[fieldName] || '';
if (field.encoding === 'hex' && !field.length) {
value = new Buffer(value, 'hex').toString('ascii');
}
settings[field.name] = value;
}
settings.transaction_sequence = String(settings.transaction_sequence);
respond.success(response, { settings: settings });
});
};
/**
* Change account settings
*
* @body
* @param {Settings} request.body.settings
* @param {String} request.body.secret
*
* @query
* @param {String "true"|"false"} request.query.validated Used to force request to wait until rippled has finished validating the submitted transaction
*
* @param {Express.js Response} response
* @param {Express.js Next} next
*/
function changeSettings(request, response, next) {
var options = request.params;
Object.keys(request.body).forEach(function(param) {
options[param] = request.body[param];
});
options.validated = request.query.validated === 'true';
function validateOptions(callback) {
if (typeof options.settings !== 'object') {
return callback(new InvalidRequestError('Parameter missing: settings'));
}
if (!ripple.UInt160.is_valid(options.account)) {
return callback(new InvalidRequestError(
'Parameter is not a valid Ripple address: account'));
}
if (!options.secret) {
return callback(new InvalidRequestError('Parameter missing: secret'));
}
if (!/(undefined|string)/.test(typeof options.settings.domain)) {
return callback(new InvalidRequestError(
'Parameter must be a string: domain'));
}
if (!/(undefined|string)/.test(typeof options.settings.wallet_locator)) {
return callback(new InvalidRequestError(
'Parameter must be a string: wallet_locator'));
}
if (!/(undefined|string)/.test(typeof options.settings.email_hash)) {
return callback(new InvalidRequestError(
'Parameter must be a string: email_hash'));
}
if (!/(undefined|string)/.test(typeof options.settings.message_key)) {
return callback(new InvalidRequestError(
'Parameter must be a string: message_key'));
}
if (!/(undefined|number)/.test(typeof options.settings.transfer_rate)) {
if (options.settings.transfer_rate !== '') {
return callback(new InvalidRequestError(
'Parameter must be a number: transfer_rate'));
}
}
if (!/(undefined|number)/.test(typeof options.settings.wallet_size)) {
if (options.settings.wallet_size !== '') {
return callback(new InvalidRequestError(
'Parameter must be a number: wallet_size'));
}
}
if (!/(undefined|boolean)/.test(typeof options.settings.no_freeze)) {
return callback(new InvalidRequestError(
'Parameter must be a boolean: no_freeze'));
}
if (!/(undefined|boolean)/.test(typeof options.settings.global_freeze)) {
return callback(new InvalidRequestError(
'Parameter must be a boolean: global_freeze'));
}
if (!/(undefined|boolean)/.test(typeof options.settings.password_spent)) {
return callback(new InvalidRequestError(
'Parameter must be a boolean: password_spent'));
}
if (!/(undefined|boolean)/.test(typeof options.settings.disable_master)) {
return callback(new InvalidRequestError(
'Parameter must be a boolean: disable_master'));
}
callback();
};
function changeAccountSettings(callback) {
// Decimal flags
const SetClearFlags = ripple.Transaction.set_clear_flags.AccountSet;
// Bit flags
const FlagSet = {
require_destination_tag: {
unset: 'OptionalDestTag',
set: 'RequireDestTag'
},
require_authorization: {
unset: 'OptionalAuth',
set: 'RequireAuth'
},
disallow_xrp: {
unset: 'AllowXRP',
set: 'DisallowXRP'
}
};
var settings = { };
var transaction = remote.transaction();
function transactionSent(m) {
var summary = transaction.summary();
var result = { success: true };
if (summary.result) {
settings.hash = summary.result.transaction_hash;
settings.ledger = String(summary.submitIndex)
}
result.settings = settings;
result.settings.state = m.validated === true ? 'validated' : 'pending';
callback(null, result);
};
transaction.once('error', callback);
transaction.once('proposed', function(message) {
if (options.validated === false) {
transactionSent(message);
}
});
transaction.once('final', function(message) {
if (/^tes/.test(message.engine_result) && options.validated === true) {
transactionSent(message);
}
});
try {
transaction.accountSet(options.account);
transaction.secret(options.secret);
} catch (exception) {
return callback(exception);
}
for (var flagName in FlagSet) {
// Set transaction flags
if (!(flagName in options.settings)) {
continue;
}
var flag = FlagSet[flagName];
var value = options.settings[flagName];
if (value === CLEAR_SETTING) {
value = false;
}
if (typeof value !== 'boolean') {
return callback(new InvalidRequestError(
'Parameter must be a boolean: ' + flagName));
}
settings[flagName] = value;
transaction.setFlags(value ? flag.set : flag.unset);
}
if (options.settings.hasOwnProperty('no_freeze')) {
// Set/clear NoFreeze
settings.no_freeze = options.settings.no_freeze;
if (options.settings.no_freeze) {
transaction.tx_json.SetFlag = SetClearFlags.asfNoFreeze;
} else {
transaction.tx_json.ClearFlag = SetClearFlags.asfNoFreeze;
}
}
if (options.settings.hasOwnProperty('global_freeze')) {
// Set/clear GlobalFreeze
settings.global_freeze = options.settings.global_freeze;
if (options.settings.global_freeze) {
transaction.tx_json.SetFlag = SetClearFlags.asfGlobalFreeze;
} else {
transaction.tx_json.ClearFlag = SetClearFlags.asfGlobalFreeze;
}
}
var setCollision = (typeof settings.no_freeze === 'boolean')
&& (typeof settings.global_freeze === 'boolean')
&& settings.no_freeze === settings.global_freeze;
if (setCollision) {
return callback(new InvalidRequestError(
'Unable to set/clear no_freeze and global_freeze'));
}
for (var fieldName in AccountRootFields) {
// Set transaction fields
var field = AccountRootFields[fieldName];
var value = options.settings[field.name];
if (typeof value === 'undefined') {
continue;
}
if (value === CLEAR_SETTING) {
// The value required to clear an account root field varies
value = getClearValue(fieldName);
}
if (field.encoding === 'hex') {
if (field.length) {
// Field is fixed length
if (value.length > field.length) {
return callback(new InvalidRequestError(
'Parameter length exceeded: ' + fieldName));
} else if (value.length < field.length) {
value = padValue(value, field.length);
}
} else {
// Field is variable length. Expecting an ascii string as input.
// This is currently only used for Domain field
value = new Buffer(value, 'ascii').toString('hex');
}
value = value.toUpperCase();
}
settings[field.name] = options.settings[field.name];
transaction.tx_json[fieldName] = value;
}
transaction.submit();
};
var steps = [
validateOptions,
changeAccountSettings
];
async.waterfall(steps, function(error, settings) {
if (error) {
next(error);
} else {
respond.success(response, settings);
}
});
};
module.exports = {
get: getSettings,
change: changeSettings
};