Skip to content

Commit 7cb113f

Browse files
committed
[TASK] set binary as default for commands that accept the flag
1 parent 3498dea commit 7cb113f

9 files changed

+2263
-152
lines changed

src/js/ripple/remote.js

+139-20
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,52 @@ Remote.prototype.requestLedgerCurrent = function(callback) {
10371037
return new Request(this, 'ledger_current').callback(callback);
10381038
};
10391039

1040+
/**
1041+
* Request ledger_data
1042+
*
1043+
* Get the contents of a specified ledger
1044+
*
1045+
* @param {Object} options
1046+
* @property {Boolean} [options.binary] - Flag which determines if rippled returns binary or parsed JSON
1047+
* @property {String|Number} [options.ledger] - Hash or sequence of a ledger to get contents for
1048+
* @property {Number} [options.limit] - Number of contents to retrieve from the ledger
1049+
* @property {Function} callback
1050+
*
1051+
* @callback
1052+
* @param {Error} error
1053+
* @param {LedgerData} ledgerData
1054+
*
1055+
* @return {Request} request
1056+
*/
1057+
1058+
Remote.prototype.requestLedgerData = function(options, callback) {
1059+
var request = new Request(this, 'ledger_data');
1060+
1061+
request.message.binary = options.binary !== false;
1062+
request.selectLedger(options.ledger);
1063+
request.message.limit = options.limit;
1064+
1065+
request.once('success', function(res) {
1066+
if (options.binary === false) {
1067+
request.emit('state', res);
1068+
return;
1069+
}
1070+
1071+
async.mapSeries(res.state, function(ledgerData, next) {
1072+
async.setImmediate(function() {
1073+
next(null, Remote.parseBinaryLedgerData(ledgerData));
1074+
});
1075+
}, function(err, state) {
1076+
res.state = state;
1077+
request.emit('state', res);
1078+
});
1079+
});
1080+
1081+
request.callback(callback, 'state');
1082+
1083+
return request;
1084+
};
1085+
10401086
/**
10411087
* Request ledger_entry
10421088
*
@@ -1201,17 +1247,40 @@ Remote.prototype.requestTransactionEntry = function(hash, ledgerHash, callback)
12011247
/**
12021248
* Request tx
12031249
*
1204-
* @param {String} transaction hash
1250+
* @param {Object|String} hash
1251+
* @property {String} hash.hash - Transaction hash
1252+
* @property {Boolean} [hash.binary=true] - Flag which determines if rippled returns binary or parsed JSON
12051253
* @param [Function] callback
12061254
* @return {Request} request
12071255
*/
12081256

12091257
Remote.prototype.requestTransaction =
12101258
Remote.prototype.requestTx = function(hash, callback) {
1259+
var options;
1260+
1261+
if (typeof hash === 'string') {
1262+
options = {
1263+
hash: hash
1264+
}
1265+
} else {
1266+
options = hash;
1267+
}
1268+
12111269
var request = new Request(this, 'tx');
12121270

1213-
request.message.transaction = hash;
1214-
request.callback(callback);
1271+
request.message.binary = options.binary !== false;
1272+
request.message.transaction = options.hash;
1273+
1274+
request.once('success', function(res) {
1275+
if (options.binary === false) {
1276+
request.emit('transaction', res);
1277+
return;
1278+
}
1279+
1280+
request.emit('transaction', Remote.parseBinaryTransaction(res));
1281+
});
1282+
1283+
request.callback(callback, 'transaction');
12151284

12161285
return request;
12171286
};
@@ -1387,16 +1456,15 @@ Remote.prototype.requestAccountOffers = function(options, callback) {
13871456
* Request account_tx
13881457
*
13891458
* @param {Object} options
1390-
*
1391-
* @param {String} account
1392-
* @param [Number] ledger_index_min defaults to -1 if ledger_index_max is specified.
1393-
* @param [Number] ledger_index_max defaults to -1 if ledger_index_min is specified.
1394-
* @param [Boolean] binary, defaults to false
1395-
* @param [Boolean] parseBinary, defaults to true
1396-
* @param [Boolean] count, defaults to false
1397-
* @param [Boolean] descending, defaults to false
1398-
* @param [Number] offset, defaults to 0
1399-
* @param [Number] limit
1459+
* @property {String} options.account
1460+
* @property {Number} options.ledger_index_min - Defaults to -1 if ledger_index_max is specified.
1461+
* @property {Number} options.ledger_index_max - Defaults to -1 if ledger_index_min is specified.
1462+
* @property {Boolean} options.binary - Defaults to true
1463+
* @property {Boolean} options.parseBinary - Defaults to true
1464+
* @property {Boolean} options.count - Defaults to false
1465+
* @property {Boolean} options.descending - Defaults to false
1466+
* @property {Number} options.offset - Defaults to 0
1467+
* @property {Number} options.limit
14001468
*
14011469
* @param [Function] callback
14021470
* @return {Request}
@@ -1409,6 +1477,8 @@ Remote.prototype.requestAccountTx = function(options, callback) {
14091477

14101478
var request = new Request(this, 'account_tx');
14111479

1480+
options.binary = options.binary !== false;
1481+
14121482
if (options.min_ledger !== void(0)) {
14131483
options.ledger_index_min = options.min_ledger;
14141484
}
@@ -1448,7 +1518,7 @@ Remote.prototype.requestAccountTx = function(options, callback) {
14481518

14491519
async.mapSeries(res.transactions, function(transaction, next) {
14501520
async.setImmediate(function() {
1451-
next(null, Remote.parseBinaryTransaction(transaction));
1521+
next(null, Remote.parseBinaryAccountTransaction(transaction));
14521522
});
14531523
}, function(err, transactions) {
14541524
res.transactions = transactions;
@@ -1466,22 +1536,71 @@ Remote.prototype.requestAccountTx = function(options, callback) {
14661536
* @return {Transaction}
14671537
*/
14681538

1469-
Remote.parseBinaryTransaction = function(transaction) {
1539+
Remote.parseBinaryAccountTransaction = function(transaction) {
14701540
var tx_obj = new SerializedObject(transaction.tx_blob);
1471-
var meta = new SerializedObject(transaction.meta);
1541+
var tx_obj_json = tx_obj.to_json();
1542+
var meta = new SerializedObject(transaction.meta).to_json();
14721543

14731544
var tx_result = {
1474-
validated: transaction.validated,
1475-
ledger_index: transaction.ledger_index
1545+
validated: transaction.validated
14761546
};
14771547

1478-
tx_result.meta = meta.to_json();
1479-
tx_result.tx = tx_obj.to_json();
1548+
tx_result.meta = meta;
1549+
tx_result.tx = tx_obj_json;
14801550
tx_result.tx.hash = tx_obj.hash(hashprefixes.HASH_TX_ID).to_hex();
1551+
tx_result.tx.ledger_index = transaction.ledger_index;
1552+
tx_result.tx.inLedger = transaction.ledger_index;
1553+
1554+
if (typeof meta.DeliveredAmount === 'object') {
1555+
tx_result.meta.delivered_amount = meta.DeliveredAmount;
1556+
} else if (typeof tx_obj_json.Amount === 'string' || typeof tx_obj_json.Amount === 'object') {
1557+
tx_result.meta.delivered_amount = tx_obj_json.Amount;
1558+
}
1559+
1560+
return tx_result;
1561+
};
1562+
1563+
Remote.parseBinaryTransaction = function(transaction) {
1564+
var tx_obj = new SerializedObject(transaction.tx).to_json();
1565+
var meta = new SerializedObject(transaction.meta).to_json();
1566+
1567+
var tx_result = tx_obj;
1568+
1569+
tx_result.date = transaction.date;
1570+
tx_result.hash = transaction.hash;
1571+
tx_result.inLedger = transaction.inLedger;
1572+
tx_result.ledger_index = transaction.ledger_index;
1573+
tx_result.meta = meta;
1574+
tx_result.validated = transaction.validated;
1575+
1576+
if (typeof meta.DeliveredAmount === 'object') {
1577+
tx_result.meta.delivered_amount = meta.DeliveredAmount;
1578+
} else if (typeof tx_obj.Amount === 'string' || typeof tx_obj.Amount === 'object') {
1579+
tx_result.meta.delivered_amount = tx_obj.Amount;
1580+
}
14811581

14821582
return tx_result;
14831583
};
14841584

1585+
/**
1586+
* Parse binary ledger state data
1587+
*
1588+
* @param {Object} ledgerData
1589+
* @property {String} ledgerData.data
1590+
* @property {String} ledgerData.index
1591+
*
1592+
* @return {State}
1593+
*/
1594+
1595+
Remote.parseBinaryLedgerData = function(ledgerData) {
1596+
var data = new SerializedObject(ledgerData.data);
1597+
1598+
var state = data.to_json();
1599+
state.index = ledgerData.index;
1600+
1601+
return state;
1602+
};
1603+
14851604
/**
14861605
* Request the overall transaction history.
14871606
*

src/js/ripple/serializedobject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ SerializedObject.jsonify_structure = function(structure, field_name) {
210210
if (typeof structure.to_json === 'function') {
211211
output = structure.to_json();
212212
} else if (structure instanceof BigInteger) {
213-
output = structure.toString(16).toUpperCase();
213+
output = ('0000000000000000' + structure.toString(16).toUpperCase()).slice(-16);
214214
} else {
215215
//new Array or Object
216216
output = new structure.constructor();

src/js/ripple/serializedtypes.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ var STInt64 = exports.Int64 = new SerializedType({
234234
// pessimistic numeric fraek. What doth lief?
235235
var result = new BigInteger([0].concat(bytes), 256);
236236
assert(result instanceof BigInteger);
237+
237238
return result;
238239
}
239240
});
@@ -541,30 +542,38 @@ var STPathSet = exports.PathSet = new SerializedType({
541542
//It's an entry-begin tag.
542543
//console.log('It's an entry-begin tag.');
543544
var entry = {};
545+
var type = 0;
544546

545547
if (tag_byte & this.typeAccount) {
546548
//console.log('entry.account');
547549
/*var bta = so.read(20);
548550
console.log('BTA:', bta);*/
549551
entry.account = STHash160.parse(so);
550552
entry.account.set_version(Base.VER_ACCOUNT_ID);
553+
type = type | this.typeAccount;
551554
}
552555
if (tag_byte & this.typeCurrency) {
553556
//console.log('entry.currency');
554557
entry.currency = STCurrency.parse(so);
555558
if (entry.currency.to_json() === 'XRP' && !entry.currency.is_native()) {
556559
entry.non_native = true;
557560
}
561+
type = type | this.typeCurrency;
558562
}
559563
if (tag_byte & this.typeIssuer) {
560564
//console.log('entry.issuer');
561565
entry.issuer = STHash160.parse(so);
562566
// Enable and set correct type of base-58 encoding
563567
entry.issuer.set_version(Base.VER_ACCOUNT_ID);
564568
//console.log('DONE WITH ISSUER!');
569+
570+
type = type | this.typeIssuer;
565571
}
566572

567573
if (entry.account || entry.currency || entry.issuer) {
574+
entry.type = type;
575+
entry.type_hex = ("000000000000000" + type.toString(16)).slice(-16);
576+
568577
current_path.push(entry);
569578
} else {
570579
throw new Error('Invalid path entry'); //It must have at least something in it.
@@ -674,7 +683,11 @@ var STMemo = exports.STMemo = new SerializedType({
674683
}
675684

676685
if (output['MemoType'] !== void(0)) {
677-
output['parsed_memo_type'] = convertHexToString(output['MemoType']);
686+
var parsedType = convertHexToString(output['MemoType']);
687+
688+
if (parsedType !== 'unformatted_memo') {
689+
output['parsed_memo_type'] = convertHexToString(output['MemoType']);
690+
}
678691
}
679692

680693
if (output['MemoFormat'] !== void(0)) {

0 commit comments

Comments
 (0)