Skip to content

Commit b5ed8f5

Browse files
committed
Add Amount.strict_mode for toggling range validation
1 parent 52526f9 commit b5ed8f5

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

src/js/ripple/amount.js

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function Amount() {
3030
this._issuer = new UInt160();
3131
}
3232

33+
/**
34+
* Set strict_mode = false to disable amount range checking
35+
*/
36+
37+
Amount.strict_mode = true;
38+
3339
var consts = {
3440
currency_xns: 0,
3541
currency_one: 1,
@@ -338,6 +344,9 @@ Amount.prototype.canonicalize = function(roundingMode) {
338344
};
339345

340346
Amount.prototype._check_limits = function() {
347+
if (!Amount.strict_mode) {
348+
return this;
349+
}
341350
if (this._value.isNaN() || this._value.isZero()) {
342351
return this;
343352
}

src/js/ripple/serializedtypes.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
var assert = require('assert');
1010
var extend = require('extend');
11+
var GlobalBigNumber = require('bignumber.js');
1112
var binformat = require('./binformat');
1213
var utils = require('./utils');
14+
var Amount = require('./amount');
1315
var sjcl = utils.sjcl;
14-
var GlobalBigNumber = require('bignumber.js');
1516

1617
var UInt128 = require('./uint128').UInt128;
1718
var UInt160 = require('./uint160').UInt160;
@@ -288,6 +289,7 @@ var STCurrency = new SerializedType({
288289
var STAmount = exports.Amount = new SerializedType({
289290
serialize: function (so, val) {
290291
var amount = Amount.from_json(val);
292+
291293
if (!amount.is_valid()) {
292294
throw new Error('Not a valid Amount object.');
293295
}
@@ -301,12 +303,12 @@ var STAmount = exports.Amount = new SerializedType({
301303
if (amount.is_native()) {
302304
var valueHex = value.abs().toString(16);
303305

304-
if (value.abs().greaterThan(Amount.bi_xns_max)) {
306+
if (Amount.strict_mode && value.abs().greaterThan(Amount.bi_xns_max)) {
305307
throw new Error('Value out of bounds');
306308
}
307309

308310
// Enforce correct length (64 bits)
309-
if (valueHex.length > 16) {
311+
if (Amount.strict_mode && valueHex.length > 16) {
310312
throw new Error('Value out of bounds');
311313
}
312314

test/serializedobject-test.js

+53-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
var assert = require('assert');
1+
var assert = require('assert');
22
var SerializedObject = require('ripple-lib').SerializedObject;
3-
var sjcl = require('ripple-lib').sjcl;
3+
var Amount = require('ripple-lib').Amount;
4+
var sjcl = require('ripple-lib').sjcl;
45

56
// Shortcuts
67
var hex = sjcl.codec.hex;
@@ -54,6 +55,56 @@ describe('Serialized object', function() {
5455
});
5556
});
5657

58+
describe('#from_json(v).to_json() == v -- invalid amount', function(){
59+
it('outputs same as passed to from_json', function() {
60+
var input_json = {
61+
Account: 'rUR9gTCcrUY9fMkz9rwcM9urPREh3LKXoW',
62+
Fee: '10',
63+
Flags: 0,
64+
Sequence: 65,
65+
SigningPubKey: '033D0B1FB932E0408C119107483190B61561DCE8529E29CB5D1C69128DA54DA715',
66+
TakerGets: '2188313981504612096',
67+
TakerPays: {
68+
currency: 'USD',
69+
issuer: 'r9rp9MUFRJVCVLRm3MTmUvSPNBSL3BuEFx',
70+
value: '99999999999'
71+
},
72+
TransactionType: 'OfferCreate',
73+
TxnSignature: '304602210085C6AE945643150E6D450CF796E45D74FB24B4E03E964A29CC6AFFEB346C77C80221009BE1B6678CF6C2E61F8F2696144C75AFAF66DF4FC0733DF9118EDEFEEFE33243'
74+
};
75+
76+
assert.throws(function() {
77+
SerializedObject.from_json(input_json).to_json();
78+
});
79+
});
80+
});
81+
82+
describe('#from_json(v).to_json() == v -- invalid amount, strict_mode = false', function(){
83+
it('outputs same as passed to from_json', function() {
84+
var input_json = {
85+
Account: 'rUR9gTCcrUY9fMkz9rwcM9urPREh3LKXoW',
86+
Fee: '10',
87+
Flags: 0,
88+
Sequence: 65,
89+
SigningPubKey: '033D0B1FB932E0408C119107483190B61561DCE8529E29CB5D1C69128DA54DA715',
90+
TakerGets: '2188313981504612096',
91+
TakerPays: {
92+
currency: 'USD',
93+
issuer: 'r9rp9MUFRJVCVLRm3MTmUvSPNBSL3BuEFx',
94+
value: '99999999999'
95+
},
96+
TransactionType: 'OfferCreate',
97+
TxnSignature: 'FFFFFF210085C6AE945643150E6D450CF796E45D74FB24B4E03E964A29CC6AFFEB346C77C80221009BE1B6678CF6C2E61F8F2696144C75AFAF66DF4FC0733DF9118EDEFEEFE33243'
98+
};
99+
100+
var strictMode = Amount.strict_mode;
101+
Amount.strict_mode = false;
102+
var output_json = SerializedObject.from_json(input_json).to_json();
103+
assert.deepEqual(input_json, output_json);
104+
Amount.strict_mode = strictMode;
105+
});
106+
});
107+
57108
describe('#from_json', function() {
58109
it('understands TransactionType as a Number', function() {
59110
var input_json = {

0 commit comments

Comments
 (0)