mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Merge pull request #581 from clark800/binary-codec
Decouple UInt from non-serialization code
This commit is contained in:
@@ -4,21 +4,11 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const validator = require('is-my-json-valid');
|
const validator = require('is-my-json-valid');
|
||||||
const core = require('./utils').core;
|
|
||||||
const ValidationError = require('./errors').ValidationError;
|
const ValidationError = require('./errors').ValidationError;
|
||||||
|
const {isValidAddress} = require('ripple-address-codec');
|
||||||
|
|
||||||
let SCHEMAS = {};
|
let SCHEMAS = {};
|
||||||
|
|
||||||
function isValidAddress(address: string): boolean {
|
|
||||||
return typeof address === 'string' && address.length > 0 &&
|
|
||||||
address[0] === 'r' &&
|
|
||||||
core.UInt160.is_valid(address);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidLedgerHash(ledgerHash) {
|
|
||||||
return core.UInt256.is_valid(ledgerHash);
|
|
||||||
}
|
|
||||||
|
|
||||||
function loadSchemas() {
|
function loadSchemas() {
|
||||||
// listed explicitly for webpack (instead of scanning schemas directory)
|
// listed explicitly for webpack (instead of scanning schemas directory)
|
||||||
const schemas = [
|
const schemas = [
|
||||||
@@ -102,8 +92,7 @@ function formatSchemaErrors(errors) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function schemaValidate(schemaName: string, object: any): void {
|
function schemaValidate(schemaName: string, object: any): void {
|
||||||
const formats = {address: isValidAddress,
|
const formats = {address: isValidAddress};
|
||||||
ledgerHash: isValidLedgerHash};
|
|
||||||
const options = {schemas: SCHEMAS, formats: formats,
|
const options = {schemas: SCHEMAS, formats: formats,
|
||||||
verbose: true, greedy: true};
|
verbose: true, greedy: true};
|
||||||
const schema = SCHEMAS[schemaName];
|
const schema = SCHEMAS[schemaName];
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const Utils = require('./orderbookutils');
|
const Utils = require('./orderbookutils');
|
||||||
|
|
||||||
function assertValidNumber(number, message) {
|
function assertValidNumber(number, message) {
|
||||||
@@ -25,9 +24,7 @@ function AutobridgeCalculator(currencyGets, currencyPays,
|
|||||||
this._currencyGetsHex = currencyGets.to_hex();
|
this._currencyGetsHex = currencyGets.to_hex();
|
||||||
this._currencyPaysHex = currencyPays.to_hex();
|
this._currencyPaysHex = currencyPays.to_hex();
|
||||||
this._issuerGets = issuerGets;
|
this._issuerGets = issuerGets;
|
||||||
this._issuerGetsObj = UInt160.from_json(issuerGets);
|
|
||||||
this._issuerPays = issuerPays;
|
this._issuerPays = issuerPays;
|
||||||
this._issuerPaysObj = UInt160.from_json(issuerPays);
|
|
||||||
this.legOneOffers = _.cloneDeep(legOneOffers);
|
this.legOneOffers = _.cloneDeep(legOneOffers);
|
||||||
this.legTwoOffers = _.cloneDeep(legTwoOffers);
|
this.legTwoOffers = _.cloneDeep(legTwoOffers);
|
||||||
|
|
||||||
@@ -94,9 +91,9 @@ AutobridgeCalculator.prototype._calculateInternal = function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
|
|
||||||
if (legOneTakerGetsFunded.is_zero()) {
|
if (legOneTakerGetsFunded.is_zero()) {
|
||||||
legOnePointer++;
|
legOnePointer++;
|
||||||
@@ -157,19 +154,19 @@ AutobridgeCalculator.prototype._calculateInternal = function(
|
|||||||
AutobridgeCalculator.prototype.getAutobridgedOfferWithClampedLegOne =
|
AutobridgeCalculator.prototype.getAutobridgedOfferWithClampedLegOne =
|
||||||
function(legOneOffer, legTwoOffer) {
|
function(legOneOffer, legTwoOffer) {
|
||||||
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
|
|
||||||
const autobridgedTakerGets = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
const autobridgedTakerGets = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
const autobridgedTakerPays = legTwoTakerPaysFunded.multiply(legOneQuality);
|
const autobridgedTakerPays = legTwoTakerPaysFunded.multiply(legOneQuality);
|
||||||
|
|
||||||
if (legOneOffer.Account === legTwoOffer.Account) {
|
if (legOneOffer.Account === legTwoOffer.Account) {
|
||||||
const legOneTakerGets = Utils.getOfferTakerGets(legOneOffer,
|
const legOneTakerGets = Utils.getOfferTakerGets(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
const updatedTakerGets = legOneTakerGets.subtract(legTwoTakerPaysFunded);
|
const updatedTakerGets = legOneTakerGets.subtract(legTwoTakerPaysFunded);
|
||||||
|
|
||||||
this.setLegOneTakerGets(legOneOffer, updatedTakerGets);
|
this.setLegOneTakerGets(legOneOffer, updatedTakerGets);
|
||||||
@@ -202,19 +199,19 @@ function(legOneOffer, legTwoOffer) {
|
|||||||
AutobridgeCalculator.prototype.getAutobridgedOfferWithClampedLegTwo =
|
AutobridgeCalculator.prototype.getAutobridgedOfferWithClampedLegTwo =
|
||||||
function(legOneOffer, legTwoOffer) {
|
function(legOneOffer, legTwoOffer) {
|
||||||
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
const legOneTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
const legTwoTakerPaysFunded = Utils.getOfferTakerPaysFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
const legTwoQuality = Utils.getOfferQuality(legTwoOffer, this._currencyGets,
|
const legTwoQuality = Utils.getOfferQuality(legTwoOffer, this._currencyGets,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
|
|
||||||
const autobridgedTakerGets = legOneTakerGetsFunded.divide(legTwoQuality);
|
const autobridgedTakerGets = legOneTakerGetsFunded.divide(legTwoQuality);
|
||||||
const autobridgedTakerPays = Utils.getOfferTakerPaysFunded(legOneOffer,
|
const autobridgedTakerPays = Utils.getOfferTakerPaysFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
|
|
||||||
// Update funded amount since leg two offer was not completely consumed
|
// Update funded amount since leg two offer was not completely consumed
|
||||||
legTwoOffer.taker_gets_funded = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
legTwoOffer.taker_gets_funded = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj)
|
this._currencyGets, this._issuerGets)
|
||||||
.subtract(autobridgedTakerGets)
|
.subtract(autobridgedTakerGets)
|
||||||
.to_text();
|
.to_text();
|
||||||
legTwoOffer.taker_pays_funded = legTwoTakerPaysFunded
|
legTwoOffer.taker_pays_funded = legTwoTakerPaysFunded
|
||||||
@@ -239,9 +236,9 @@ function(legOneOffer, legTwoOffer) {
|
|||||||
AutobridgeCalculator.prototype.getAutobridgedOfferWithoutClamps =
|
AutobridgeCalculator.prototype.getAutobridgedOfferWithoutClamps =
|
||||||
function(legOneOffer, legTwoOffer) {
|
function(legOneOffer, legTwoOffer) {
|
||||||
const autobridgedTakerGets = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
const autobridgedTakerGets = Utils.getOfferTakerGetsFunded(legTwoOffer,
|
||||||
this._currencyGets, this._issuerGetsObj);
|
this._currencyGets, this._issuerGets);
|
||||||
const autobridgedTakerPays = Utils.getOfferTakerPaysFunded(legOneOffer,
|
const autobridgedTakerPays = Utils.getOfferTakerPaysFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
|
|
||||||
return this.formatAutobridgedOffer(
|
return this.formatAutobridgedOffer(
|
||||||
autobridgedTakerGets,
|
autobridgedTakerGets,
|
||||||
@@ -378,12 +375,12 @@ AutobridgeCalculator.prototype.unclampLegOneOwnerFunds = function(legOneOffer) {
|
|||||||
assertValidLegOneOffer(legOneOffer, 'Leg one offer is invalid');
|
assertValidLegOneOffer(legOneOffer, 'Leg one offer is invalid');
|
||||||
|
|
||||||
legOneOffer.initTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
legOneOffer.initTakerGetsFunded = Utils.getOfferTakerGetsFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
|
|
||||||
this.setLegOneTakerGetsFunded(
|
this.setLegOneTakerGetsFunded(
|
||||||
legOneOffer,
|
legOneOffer,
|
||||||
Utils.getOfferTakerGets(legOneOffer, this._currencyPays,
|
Utils.getOfferTakerGets(legOneOffer, this._currencyPays,
|
||||||
this._issuerPaysObj)
|
this._issuerPays)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -405,7 +402,7 @@ AutobridgeCalculator.prototype.clampLegOneOwnerFunds = function(legOneOffer) {
|
|||||||
assertValidLegOneOffer(legOneOffer, 'Leg one offer is invalid');
|
assertValidLegOneOffer(legOneOffer, 'Leg one offer is invalid');
|
||||||
|
|
||||||
const takerGets = Utils.getOfferTakerGets(legOneOffer, this._currencyPays,
|
const takerGets = Utils.getOfferTakerGets(legOneOffer, this._currencyPays,
|
||||||
this._issuerPaysObj);
|
this._issuerPays);
|
||||||
|
|
||||||
if (takerGets.compareTo(legOneOffer.initTakerGetsFunded) > 0) {
|
if (takerGets.compareTo(legOneOffer.initTakerGetsFunded) > 0) {
|
||||||
// After clamping, TakerGets is still greater than initial funded amount
|
// After clamping, TakerGets is still greater than initial funded amount
|
||||||
@@ -431,15 +428,15 @@ function(legOneOffer) {
|
|||||||
assert(!legOneOffer.is_fully_funded, 'Leg one offer cannot be fully funded');
|
assert(!legOneOffer.is_fully_funded, 'Leg one offer cannot be fully funded');
|
||||||
|
|
||||||
const fundedSum = Utils.getOfferTakerGetsFunded(legOneOffer,
|
const fundedSum = Utils.getOfferTakerGetsFunded(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj)
|
this._currencyPays, this._issuerPays)
|
||||||
.add(this.getLeftoverOwnerFunds(legOneOffer.Account));
|
.add(this.getLeftoverOwnerFunds(legOneOffer.Account));
|
||||||
|
|
||||||
if (fundedSum.compareTo(Utils.getOfferTakerGets(legOneOffer,
|
if (fundedSum.compareTo(Utils.getOfferTakerGets(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj)) >= 0
|
this._currencyPays, this._issuerPays)) >= 0
|
||||||
) {
|
) {
|
||||||
// There are enough extra funds to fully fund the offer
|
// There are enough extra funds to fully fund the offer
|
||||||
const legOneTakerGets = Utils.getOfferTakerGets(legOneOffer,
|
const legOneTakerGets = Utils.getOfferTakerGets(legOneOffer,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
const updatedLeftover = fundedSum.subtract(legOneTakerGets);
|
const updatedLeftover = fundedSum.subtract(legOneTakerGets);
|
||||||
|
|
||||||
this.setLegOneTakerGetsFunded(legOneOffer, legOneTakerGets);
|
this.setLegOneTakerGetsFunded(legOneOffer, legOneTakerGets);
|
||||||
@@ -467,7 +464,7 @@ function setLegOneTakerGetsFunded(legOneOffer, takerGetsFunded) {
|
|||||||
legOneOffer.taker_gets_funded = takerGetsFunded.to_text();
|
legOneOffer.taker_gets_funded = takerGetsFunded.to_text();
|
||||||
legOneOffer.taker_pays_funded = takerGetsFunded
|
legOneOffer.taker_pays_funded = takerGetsFunded
|
||||||
.multiply(Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
.multiply(Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
||||||
this._currencyPays, this._issuerPaysObj))
|
this._currencyPays, this._issuerPays))
|
||||||
.to_text();
|
.to_text();
|
||||||
|
|
||||||
if (legOneOffer.taker_gets_funded === legOneOffer.TakerGets.value) {
|
if (legOneOffer.taker_gets_funded === legOneOffer.TakerGets.value) {
|
||||||
@@ -489,7 +486,7 @@ function(legOneOffer, takerGets) {
|
|||||||
assert(takerGets instanceof Amount, 'Taker gets funded is invalid');
|
assert(takerGets instanceof Amount, 'Taker gets funded is invalid');
|
||||||
|
|
||||||
const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
const legOneQuality = Utils.getOfferQuality(legOneOffer, this._currencyGets,
|
||||||
this._currencyPays, this._issuerPaysObj);
|
this._currencyPays, this._issuerPays);
|
||||||
|
|
||||||
legOneOffer.TakerGets = takerGets.to_text();
|
legOneOffer.TakerGets = takerGets.to_text();
|
||||||
legOneOffer.TakerPays = takerGets.multiply(legOneQuality).to_json();
|
legOneOffer.TakerPays = takerGets.multiply(legOneQuality).to_json();
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
const assert = require('assert');
|
||||||
const extend = require('extend');
|
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const Float = require('./ieee754').Float;
|
const Float = require('./ieee754').Float;
|
||||||
|
const BN = require('bn.js');
|
||||||
|
|
||||||
//
|
//
|
||||||
// Currency support
|
// Currency support
|
||||||
//
|
//
|
||||||
|
|
||||||
const Currency = extend(function() {
|
function Currency() {
|
||||||
// Internal form: 0 = XRP. 3 letter-code.
|
// Internal form: 0 = XRP. 3 letter-code.
|
||||||
// XXX Internal should be 0 or hex with three letter annotation when valid.
|
// XXX Internal should be 0 or hex with three letter annotation when valid.
|
||||||
|
|
||||||
@@ -20,12 +19,12 @@ const Currency = extend(function() {
|
|||||||
|
|
||||||
this._value = NaN;
|
this._value = NaN;
|
||||||
this._update();
|
this._update();
|
||||||
}, UInt160);
|
}
|
||||||
|
|
||||||
Currency.prototype = Object.create(extend({}, UInt160.prototype));
|
|
||||||
Currency.prototype.constructor = Currency;
|
|
||||||
|
|
||||||
|
Currency.width = 20;
|
||||||
Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
|
Currency.HEX_CURRENCY_BAD = '0000000000000000000000005852500000000000';
|
||||||
|
Currency.HEX_ZERO = '0000000000000000000000000000000000000000';
|
||||||
|
Currency.HEX_ONE = '0000000000000000000000000000000000000001';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tries to correctly interpret a Currency as entered by a user.
|
* Tries to correctly interpret a Currency as entered by a user.
|
||||||
@@ -69,10 +68,54 @@ Currency.from_json = function(j, shouldInterpretXrpAsIou) {
|
|||||||
return (new Currency()).parse_json(j, shouldInterpretXrpAsIou);
|
return (new Currency()).parse_json(j, shouldInterpretXrpAsIou);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.from_hex = function(j) {
|
||||||
|
if (j instanceof this) {
|
||||||
|
return j.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new this()).parse_hex(j);
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.from_bytes = function(j) {
|
||||||
|
if (j instanceof this) {
|
||||||
|
return j.clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (new this()).parse_bytes(j);
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.to_hex = function() {
|
||||||
|
if (!this.is_valid()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.arrayToHex(this.to_bytes());
|
||||||
|
};
|
||||||
|
|
||||||
Currency.from_human = function(j, opts) {
|
Currency.from_human = function(j, opts) {
|
||||||
return (new Currency().parse_human(j, opts));
|
return (new Currency().parse_human(j, opts));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.json_rewrite = function(j, opts) {
|
||||||
|
return this.from_json(j).to_json(opts);
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.clone = function() {
|
||||||
|
return this.copyTo(new this.constructor());
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.equals = function(o) {
|
||||||
|
return this.is_valid() &&
|
||||||
|
o.is_valid() &&
|
||||||
|
// This throws but the expression will short circuit
|
||||||
|
this.cmp(o) === 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.cmp = function(o) {
|
||||||
|
assert(this.is_valid() && o.is_valid());
|
||||||
|
return this._value.cmp(o._value);
|
||||||
|
};
|
||||||
|
|
||||||
// this._value = NaN on error.
|
// this._value = NaN on error.
|
||||||
Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) {
|
Currency.prototype.parse_json = function(j, shouldInterpretXrpAsIou) {
|
||||||
this._value = NaN;
|
this._value = NaN;
|
||||||
@@ -197,6 +240,40 @@ Currency.prototype.parse_human = function(j) {
|
|||||||
return this.parse_json(j);
|
return this.parse_json(j);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.prototype.is_valid = function() {
|
||||||
|
return this._value instanceof BN;
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.parse_number = function(j) {
|
||||||
|
this._value = NaN;
|
||||||
|
|
||||||
|
if (typeof j === 'number' && isFinite(j) && j >= 0) {
|
||||||
|
this._value = new BN(j);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._update();
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.parse_hex = function(j) {
|
||||||
|
if (new RegExp(`^[0-9A-Fa-f]{${this.constructor.width * 2}}$`).test(j)) {
|
||||||
|
this._value = new BN(j, 16);
|
||||||
|
} else {
|
||||||
|
this._value = NaN;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._update();
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Currency.prototype.to_bytes = function() {
|
||||||
|
if (!this.is_valid()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this._value.toArray('be', this.constructor.width);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recalculate internal representation.
|
* Recalculate internal representation.
|
||||||
*
|
*
|
||||||
@@ -276,6 +353,16 @@ Currency.prototype.copyTo = function(d) {
|
|||||||
return d;
|
return d;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.prototype.parse_bytes = function(j) {
|
||||||
|
if (Array.isArray(j) && j.length === this.constructor.width) {
|
||||||
|
this._value = new BN(j);
|
||||||
|
} else {
|
||||||
|
this._value = NaN;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._update();
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
// XXX Probably not needed anymore?
|
// XXX Probably not needed anymore?
|
||||||
/*
|
/*
|
||||||
@@ -426,4 +513,8 @@ Currency.prototype.get_iso = function() {
|
|||||||
return this._iso_code;
|
return this._iso_code;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Currency.is_valid = function(j) {
|
||||||
|
return this.from_json(j).is_valid();
|
||||||
|
};
|
||||||
|
|
||||||
exports.Currency = Currency;
|
exports.Currency = Currency;
|
||||||
|
|||||||
@@ -6,9 +6,6 @@ exports.Account = require('./account').Account;
|
|||||||
exports.Transaction = require('./transaction').Transaction;
|
exports.Transaction = require('./transaction').Transaction;
|
||||||
exports.Currency = require('./currency').Currency;
|
exports.Currency = require('./currency').Currency;
|
||||||
exports.Base = require('./base').Base;
|
exports.Base = require('./base').Base;
|
||||||
exports.UInt128 = require('./uint128').UInt128;
|
|
||||||
exports.UInt160 = require('./uint160').UInt160;
|
|
||||||
exports.UInt256 = require('./uint256').UInt256;
|
|
||||||
exports.Meta = require('./meta').Meta;
|
exports.Meta = require('./meta').Meta;
|
||||||
exports.SerializedObject = require('./serializedobject').SerializedObject;
|
exports.SerializedObject = require('./serializedobject').SerializedObject;
|
||||||
exports.RippleError = require('./rippleerror').RippleError;
|
exports.RippleError = require('./rippleerror').RippleError;
|
||||||
@@ -24,7 +21,11 @@ exports._test = {
|
|||||||
PathFind: require('./pathfind').PathFind,
|
PathFind: require('./pathfind').PathFind,
|
||||||
TransactionManager: require('./transactionmanager').TransactionManager,
|
TransactionManager: require('./transactionmanager').TransactionManager,
|
||||||
RangeSet: require('./rangeset').RangeSet,
|
RangeSet: require('./rangeset').RangeSet,
|
||||||
HashPrefixes: require('./hashprefixes')
|
HashPrefixes: require('./hashprefixes'),
|
||||||
|
UInt128: require('./uint128').UInt128,
|
||||||
|
UInt160: require('./uint160').UInt160,
|
||||||
|
UInt256: require('./uint256').UInt256,
|
||||||
|
constants: require('./constants')
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.types = require('./serializedtypes');
|
exports.types = require('./serializedtypes');
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const extend = require('extend');
|
const extend = require('extend');
|
||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const ACCOUNT_ZERO = require('./constants').ACCOUNT_ZERO;
|
const ACCOUNT_ZERO = require('./constants').ACCOUNT_ZERO;
|
||||||
const {isValidAddress} = require('ripple-address-codec');
|
const {isValidAddress} = require('ripple-address-codec');
|
||||||
@@ -151,7 +150,7 @@ Meta.prototype.getAffectedAccounts = function() {
|
|||||||
for (const fieldName in fields) {
|
for (const fieldName in fields) {
|
||||||
const field = fields[fieldName];
|
const field = fields[fieldName];
|
||||||
|
|
||||||
if (this.isAccountField(fieldName) && UInt160.is_valid(field)) {
|
if (this.isAccountField(fieldName) && isValidAddress(field)) {
|
||||||
accounts.push(field);
|
accounts.push(field);
|
||||||
} else if (
|
} else if (
|
||||||
Meta.AMOUNT_FIELDS_AFFECTING_ISSUER.indexOf(fieldName) !== -1) {
|
Meta.AMOUNT_FIELDS_AFFECTING_ISSUER.indexOf(fieldName) !== -1) {
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ const extend = require('extend');
|
|||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
|
const {isValidAddress} = require('ripple-address-codec');
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const Currency = require('./currency').Currency;
|
const Currency = require('./currency').Currency;
|
||||||
const AutobridgeCalculator = require('./autobridgecalculator');
|
const AutobridgeCalculator = require('./autobridgecalculator');
|
||||||
const OrderBookUtils = require('./orderbookutils');
|
const OrderBookUtils = require('./orderbookutils');
|
||||||
@@ -795,7 +795,7 @@ OrderBook.prototype.parseAccountBalanceFromNode = function(node) {
|
|||||||
|
|
||||||
assert(!isNaN(result.balance), 'node has an invalid balance');
|
assert(!isNaN(result.balance), 'node has an invalid balance');
|
||||||
if (this._validAccounts[result.Account] === undefined) {
|
if (this._validAccounts[result.Account] === undefined) {
|
||||||
assert(UInt160.is_valid(result.account), 'node has an invalid account');
|
assert(isValidAddress(result.account), 'node has an invalid account');
|
||||||
this._validAccounts[result.Account] = true;
|
this._validAccounts[result.Account] = true;
|
||||||
this._validAccountsCount++;
|
this._validAccountsCount++;
|
||||||
}
|
}
|
||||||
@@ -920,8 +920,6 @@ OrderBook.prototype.updateFundedAmounts = function(transaction) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
OrderBook.prototype.updateOwnerOffersFundedAmount = function(account) {
|
||||||
// assert(UInt160.is_valid(account), 'Account is invalid');
|
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
|
|
||||||
if (!this.hasOwnerFunds(account)) {
|
if (!this.hasOwnerFunds(account)) {
|
||||||
@@ -1027,7 +1025,7 @@ OrderBook.prototype.notify = function(transaction) {
|
|||||||
switch (node.nodeType) {
|
switch (node.nodeType) {
|
||||||
case 'DeletedNode':
|
case 'DeletedNode':
|
||||||
if (self._validAccounts[node.fields.Account] === undefined) {
|
if (self._validAccounts[node.fields.Account] === undefined) {
|
||||||
assert(UInt160.is_valid(node.fields.Account),
|
assert(isValidAddress(node.fields.Account),
|
||||||
'node has an invalid account');
|
'node has an invalid account');
|
||||||
self._validAccounts[node.fields.Account] = true;
|
self._validAccounts[node.fields.Account] = true;
|
||||||
self._validAccountsCount++;
|
self._validAccountsCount++;
|
||||||
@@ -1043,7 +1041,7 @@ OrderBook.prototype.notify = function(transaction) {
|
|||||||
|
|
||||||
case 'ModifiedNode':
|
case 'ModifiedNode':
|
||||||
if (self._validAccounts[node.fields.Account] === undefined) {
|
if (self._validAccounts[node.fields.Account] === undefined) {
|
||||||
assert(UInt160.is_valid(node.fields.Account),
|
assert(isValidAddress(node.fields.Account),
|
||||||
'node has an invalid account');
|
'node has an invalid account');
|
||||||
self._validAccounts[node.fields.Account] = true;
|
self._validAccounts[node.fields.Account] = true;
|
||||||
self._validAccountsCount++;
|
self._validAccountsCount++;
|
||||||
@@ -1061,7 +1059,7 @@ OrderBook.prototype.notify = function(transaction) {
|
|||||||
|
|
||||||
case 'CreatedNode':
|
case 'CreatedNode':
|
||||||
if (self._validAccounts[node.fields.Account] === undefined) {
|
if (self._validAccounts[node.fields.Account] === undefined) {
|
||||||
assert(UInt160.is_valid(node.fields.Account),
|
assert(isValidAddress(node.fields.Account),
|
||||||
'node has an invalid account');
|
'node has an invalid account');
|
||||||
self._validAccounts[node.fields.Account] = true;
|
self._validAccounts[node.fields.Account] = true;
|
||||||
self._validAccountsCount++;
|
self._validAccountsCount++;
|
||||||
@@ -1249,7 +1247,7 @@ OrderBook.prototype.setOffers = function(offers) {
|
|||||||
offer = OrderBook.offerRewrite(offers[i]);
|
offer = OrderBook.offerRewrite(offers[i]);
|
||||||
|
|
||||||
if (this._validAccounts[offer.Account] === undefined) {
|
if (this._validAccounts[offer.Account] === undefined) {
|
||||||
assert(UInt160.is_valid(offer.Account), 'Account is invalid');
|
assert(isValidAddress(offer.Account), 'Account is invalid');
|
||||||
this._validAccounts[offer.Account] = true;
|
this._validAccounts[offer.Account] = true;
|
||||||
this._validAccountsCount++;
|
this._validAccountsCount++;
|
||||||
}
|
}
|
||||||
@@ -1353,9 +1351,9 @@ OrderBook.prototype.is_valid = function() {
|
|||||||
// XXX Should check for same currency (non-native) && same issuer
|
// XXX Should check for same currency (non-native) && same issuer
|
||||||
return (
|
return (
|
||||||
this._currencyPays && this._currencyPays.is_valid() &&
|
this._currencyPays && this._currencyPays.is_valid() &&
|
||||||
(this._currencyPays.is_native() || UInt160.is_valid(this._issuerPays)) &&
|
(this._currencyPays.is_native() || isValidAddress(this._issuerPays)) &&
|
||||||
this._currencyGets && this._currencyGets.is_valid() &&
|
this._currencyGets && this._currencyGets.is_valid() &&
|
||||||
(this._currencyGets.is_native() || UInt160.is_valid(this._issuerGets)) &&
|
(this._currencyGets.is_native() || isValidAddress(this._issuerGets)) &&
|
||||||
!(this._currencyPays.is_native() && this._currencyGets.is_native())
|
!(this._currencyPays.is_native() && this._currencyGets.is_native())
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const constants = require('./constants');
|
||||||
const SerializedObject = require('./serializedobject').SerializedObject;
|
const SerializedObject = require('./serializedobject').SerializedObject;
|
||||||
const Types = require('./serializedtypes');
|
const Types = require('./serializedtypes');
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const Currency = require('./currency').Currency;
|
const Currency = require('./currency').Currency;
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const {IOUValue} = require('ripple-lib-value');
|
const {IOUValue} = require('ripple-lib-value');
|
||||||
const OrderBookUtils = {};
|
const OrderBookUtils = {};
|
||||||
|
|
||||||
@@ -22,15 +22,13 @@ function assertValidNumber(number, message) {
|
|||||||
* @return JSON amount object
|
* @return JSON amount object
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function createAmount(value, currency_, counterparty_) {
|
function createAmount(value, currency_, counterparty) {
|
||||||
|
assert(_.isString(counterparty), 'counterparty must be a string');
|
||||||
|
|
||||||
const currency = currency_ instanceof Currency ?
|
const currency = currency_ instanceof Currency ?
|
||||||
currency_ :
|
currency_ :
|
||||||
Currency.from_json(currency_);
|
Currency.from_json(currency_);
|
||||||
|
|
||||||
const counterparty = counterparty_ instanceof UInt160 ?
|
|
||||||
counterparty_.to_json() : counterparty_;
|
|
||||||
|
|
||||||
return Amount.from_components_unsafe(new IOUValue(value),
|
return Amount.from_components_unsafe(new IOUValue(value),
|
||||||
currency, counterparty, false);
|
currency, counterparty, false);
|
||||||
}
|
}
|
||||||
@@ -172,7 +170,7 @@ OrderBookUtils.convertOfferQualityToHexFromText = function(quality) {
|
|||||||
|
|
||||||
OrderBookUtils.CURRENCY_ONE = Currency.from_json(1);
|
OrderBookUtils.CURRENCY_ONE = Currency.from_json(1);
|
||||||
|
|
||||||
OrderBookUtils.ISSUER_ONE = UInt160.from_json(1);
|
OrderBookUtils.ISSUER_ONE = constants.ACCOUNT_ONE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -18,13 +18,12 @@ const assert = require('assert');
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const LRU = require('lru-cache');
|
const LRU = require('lru-cache');
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
|
const constants = require('./constants');
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const Server = require('./server').Server;
|
const Server = require('./server').Server;
|
||||||
const Request = require('./request').Request;
|
const Request = require('./request').Request;
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const Currency = require('./currency').Currency;
|
const Currency = require('./currency').Currency;
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const UInt256 = require('./uint256').UInt256;
|
|
||||||
const Transaction = require('./transaction').Transaction;
|
const Transaction = require('./transaction').Transaction;
|
||||||
const Account = require('./account').Account;
|
const Account = require('./account').Account;
|
||||||
const Meta = require('./meta').Meta;
|
const Meta = require('./meta').Meta;
|
||||||
@@ -35,6 +34,7 @@ const RippleError = require('./rippleerror').RippleError;
|
|||||||
const utils = require('./utils');
|
const utils = require('./utils');
|
||||||
const hashprefixes = require('./hashprefixes');
|
const hashprefixes = require('./hashprefixes');
|
||||||
const log = require('./log').internal.sub('remote');
|
const log = require('./log').internal.sub('remote');
|
||||||
|
const {isValidAddress} = require('ripple-address-codec');
|
||||||
|
|
||||||
export type GetLedgerSequenceCallback = (err?: ?Error, index?: number) => void;
|
export type GetLedgerSequenceCallback = (err?: ?Error, index?: number) => void;
|
||||||
|
|
||||||
@@ -1188,9 +1188,13 @@ Remote.prototype.requestTransaction = function(options, callback) {
|
|||||||
* @throws {Error} if a marker is provided, but no ledger_index or ledger_hash
|
* @throws {Error} if a marker is provided, but no ledger_index or ledger_hash
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function isValidLedgerHash(hash) {
|
||||||
|
return /^[A-F0-9]{64}$/.test(hash);
|
||||||
|
}
|
||||||
|
|
||||||
Remote.prototype._accountRequest = function(command, options, callback) {
|
Remote.prototype._accountRequest = function(command, options, callback) {
|
||||||
if (options.marker) {
|
if (options.marker) {
|
||||||
if (!(Number(options.ledger) > 0) && !UInt256.is_valid(options.ledger)) {
|
if (!(Number(options.ledger) > 0) && !isValidLedgerHash(options.ledger)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'A ledger_index or ledger_hash must be provided when using a marker');
|
'A ledger_index or ledger_hash must be provided when using a marker');
|
||||||
}
|
}
|
||||||
@@ -1198,11 +1202,11 @@ Remote.prototype._accountRequest = function(command, options, callback) {
|
|||||||
|
|
||||||
const request = new Request(this, command);
|
const request = new Request(this, command);
|
||||||
|
|
||||||
request.message.account = UInt160.json_rewrite(options.account);
|
request.message.account = options.account;
|
||||||
request.selectLedger(options.ledger);
|
request.selectLedger(options.ledger);
|
||||||
|
|
||||||
if (UInt160.is_valid(options.peer)) {
|
if (isValidAddress(options.peer)) {
|
||||||
request.message.peer = UInt160.json_rewrite(options.peer);
|
request.message.peer = options.peer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isNaN(options.limit)) {
|
if (!isNaN(options.limit)) {
|
||||||
@@ -1530,7 +1534,7 @@ Remote.prototype.requestBookOffers = function(options, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!Currency.from_json(request.message.taker_gets.currency).is_native()) {
|
if (!Currency.from_json(request.message.taker_gets.currency).is_native()) {
|
||||||
request.message.taker_gets.issuer = UInt160.json_rewrite(taker_gets.issuer);
|
request.message.taker_gets.issuer = taker_gets.issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
request.message.taker_pays = {
|
request.message.taker_pays = {
|
||||||
@@ -1538,10 +1542,10 @@ Remote.prototype.requestBookOffers = function(options, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!Currency.from_json(request.message.taker_pays.currency).is_native()) {
|
if (!Currency.from_json(request.message.taker_pays.currency).is_native()) {
|
||||||
request.message.taker_pays.issuer = UInt160.json_rewrite(taker_pays.issuer);
|
request.message.taker_pays.issuer = taker_pays.issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
request.message.taker = taker ? taker : UInt160.ACCOUNT_ONE;
|
request.message.taker = taker ? taker : constants.ACCOUNT_ONE;
|
||||||
request.selectLedger(ledger);
|
request.selectLedger(ledger);
|
||||||
|
|
||||||
if (!isNaN(limit)) {
|
if (!isNaN(limit)) {
|
||||||
@@ -1775,7 +1779,7 @@ Remote.prototype.requestOwnerCount = function(options, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Remote.prototype.getAccount = function(accountID) {
|
Remote.prototype.getAccount = function(accountID) {
|
||||||
return this._accounts[UInt160.json_rewrite(accountID)];
|
return this._accounts[accountID];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1899,8 +1903,7 @@ Remote.prototype.book = Remote.prototype.createOrderBook = function(options) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Remote.prototype.accountSeq =
|
Remote.prototype.accountSeq =
|
||||||
Remote.prototype.getAccountSequence = function(account_, advance) {
|
Remote.prototype.getAccountSequence = function(account, advance) {
|
||||||
const account = UInt160.json_rewrite(account_);
|
|
||||||
const accountInfo = this.accounts[account];
|
const accountInfo = this.accounts[account];
|
||||||
|
|
||||||
if (!accountInfo) {
|
if (!accountInfo) {
|
||||||
@@ -1923,9 +1926,7 @@ Remote.prototype.getAccountSequence = function(account_, advance) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Remote.prototype.setAccountSequence =
|
Remote.prototype.setAccountSequence =
|
||||||
Remote.prototype.setAccountSeq = function(account_, sequence) {
|
Remote.prototype.setAccountSeq = function(account, sequence) {
|
||||||
const account = UInt160.json_rewrite(account_);
|
|
||||||
|
|
||||||
if (!this.accounts.hasOwnProperty(account)) {
|
if (!this.accounts.hasOwnProperty(account)) {
|
||||||
this.accounts[account] = { };
|
this.accounts[account] = { };
|
||||||
}
|
}
|
||||||
@@ -1991,8 +1992,7 @@ Remote.prototype.accountSeqCache = function(options, callback) {
|
|||||||
* @param {String} account
|
* @param {String} account
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Remote.prototype.dirtyAccountRoot = function(account_) {
|
Remote.prototype.dirtyAccountRoot = function(account) {
|
||||||
const account = UInt160.json_rewrite(account_);
|
|
||||||
delete this.ledgers.current.account_root[account];
|
delete this.ledgers.current.account_root[account];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2065,8 +2065,7 @@ Remote.prototype.requestRippleBalance = function(options, callback) {
|
|||||||
|
|
||||||
// accountHigh implies for account: balance is negated. highLimit is the
|
// accountHigh implies for account: balance is negated. highLimit is the
|
||||||
// limit set by account.
|
// limit set by account.
|
||||||
const accountHigh = UInt160.from_json(options.account)
|
const accountHigh = (options.account === highLimit.issuer());
|
||||||
.equals(highLimit.issuer());
|
|
||||||
|
|
||||||
request.emit('ripple_state', {
|
request.emit('ripple_state', {
|
||||||
account_balance: (accountHigh
|
account_balance: (accountHigh
|
||||||
@@ -2107,7 +2106,7 @@ Remote.prepareCurrencies = function(currency) {
|
|||||||
const newCurrency = { };
|
const newCurrency = { };
|
||||||
|
|
||||||
if (currency.hasOwnProperty('issuer')) {
|
if (currency.hasOwnProperty('issuer')) {
|
||||||
newCurrency.issuer = UInt160.json_rewrite(currency.issuer);
|
newCurrency.issuer = currency.issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currency.hasOwnProperty('currency')) {
|
if (currency.hasOwnProperty('currency')) {
|
||||||
@@ -2128,12 +2127,8 @@ Remote.prepareCurrencies = function(currency) {
|
|||||||
|
|
||||||
Remote.prototype.requestRipplePathFind = function(options, callback) {
|
Remote.prototype.requestRipplePathFind = function(options, callback) {
|
||||||
const request = new Request(this, 'ripple_path_find');
|
const request = new Request(this, 'ripple_path_find');
|
||||||
|
request.message.source_account = options.source_account;
|
||||||
request.message.source_account = UInt160.json_rewrite(options.source_account);
|
request.message.destination_account = options.destination_account;
|
||||||
|
|
||||||
request.message.destination_account =
|
|
||||||
UInt160.json_rewrite(options.destination_account);
|
|
||||||
|
|
||||||
request.message.destination_amount =
|
request.message.destination_amount =
|
||||||
Amount.json_rewrite(options.destination_amount);
|
Amount.json_rewrite(options.destination_amount);
|
||||||
|
|
||||||
@@ -2159,11 +2154,8 @@ Remote.prototype.requestPathFindCreate = function(options, callback) {
|
|||||||
const request = new Request(this, 'path_find');
|
const request = new Request(this, 'path_find');
|
||||||
request.message.subcommand = 'create';
|
request.message.subcommand = 'create';
|
||||||
|
|
||||||
request.message.source_account = UInt160.json_rewrite(options.source_account);
|
request.message.source_account = options.source_account;
|
||||||
|
request.message.destination_account = options.destination_account;
|
||||||
request.message.destination_account =
|
|
||||||
UInt160.json_rewrite(options.destination_account);
|
|
||||||
|
|
||||||
request.message.destination_amount =
|
request.message.destination_amount =
|
||||||
Amount.json_rewrite(options.destination_amount);
|
Amount.json_rewrite(options.destination_amount);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ const _ = require('lodash');
|
|||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
const async = require('async');
|
const async = require('async');
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const Currency = require('./currency').Currency;
|
const Currency = require('./currency').Currency;
|
||||||
const RippleError = require('./rippleerror').RippleError;
|
const RippleError = require('./rippleerror').RippleError;
|
||||||
|
|
||||||
@@ -370,7 +369,7 @@ Request.prototype.selectLedger = function(ledger, defaultValue) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Request.prototype.accountRoot = function(account) {
|
Request.prototype.accountRoot = function(account) {
|
||||||
this.message.account_root = UInt160.json_rewrite(account);
|
this.message.account_root = account;
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -384,7 +383,7 @@ Request.prototype.index = function(index) {
|
|||||||
// --> seq : sequence number of transaction creating offer (integer)
|
// --> seq : sequence number of transaction creating offer (integer)
|
||||||
Request.prototype.offerId = function(account, sequence) {
|
Request.prototype.offerId = function(account, sequence) {
|
||||||
this.message.offer = {
|
this.message.offer = {
|
||||||
account: UInt160.json_rewrite(account),
|
account: account,
|
||||||
seq: sequence
|
seq: sequence
|
||||||
};
|
};
|
||||||
return this;
|
return this;
|
||||||
@@ -422,8 +421,8 @@ Request.prototype.rippleState = function(account, issuer, currency) {
|
|||||||
this.message.ripple_state = {
|
this.message.ripple_state = {
|
||||||
currency: currency,
|
currency: currency,
|
||||||
accounts: [
|
accounts: [
|
||||||
UInt160.json_rewrite(account),
|
account,
|
||||||
UInt160.json_rewrite(issuer)
|
issuer
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
return this;
|
return this;
|
||||||
@@ -435,7 +434,7 @@ Request.prototype.accounts = function(accountsIn, proposed) {
|
|||||||
|
|
||||||
// Process accounts parameters
|
// Process accounts parameters
|
||||||
const processedAccounts = accounts.map(function(account) {
|
const processedAccounts = accounts.map(function(account) {
|
||||||
return UInt160.json_rewrite(account);
|
return account;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (proposed) {
|
if (proposed) {
|
||||||
@@ -453,7 +452,7 @@ Request.prototype.addAccount = function(account, proposed) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const processedAccount = UInt160.json_rewrite(account);
|
const processedAccount = account;
|
||||||
const prop = proposed === true ? 'accounts_proposed' : 'accounts';
|
const prop = proposed === true ? 'accounts_proposed' : 'accounts';
|
||||||
this.message[prop] = (this.message[prop] || []).concat(processedAccount);
|
this.message[prop] = (this.message[prop] || []).concat(processedAccount);
|
||||||
|
|
||||||
@@ -508,7 +507,7 @@ Request.prototype.addBook = function(book, snapshot) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!Currency.from_json(obj.currency).is_native()) {
|
if (!Currency.from_json(obj.currency).is_native()) {
|
||||||
obj.issuer = UInt160.json_rewrite(book[side].issuer);
|
obj.issuer = book[side].issuer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,11 @@ const utils = require('./utils');
|
|||||||
const sjclcodec = require('sjcl-codec');
|
const sjclcodec = require('sjcl-codec');
|
||||||
const Amount = require('./amount').Amount;
|
const Amount = require('./amount').Amount;
|
||||||
const Currency = require('./currency').Currency;
|
const Currency = require('./currency').Currency;
|
||||||
const UInt160 = require('./uint160').UInt160;
|
|
||||||
const SerializedObject = require('./serializedobject').SerializedObject;
|
const SerializedObject = require('./serializedobject').SerializedObject;
|
||||||
const RippleError = require('./rippleerror').RippleError;
|
const RippleError = require('./rippleerror').RippleError;
|
||||||
const hashprefixes = require('./hashprefixes');
|
const hashprefixes = require('./hashprefixes');
|
||||||
const log = require('./log').internal.sub('transaction');
|
const log = require('./log').internal.sub('transaction');
|
||||||
const {isValidAddress} = require('ripple-address-codec');
|
const {isValidAddress, decodeAddress} = require('ripple-address-codec');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor Transaction
|
* @constructor Transaction
|
||||||
@@ -470,7 +469,7 @@ Transaction.prototype.multiSigningData = function(account) {
|
|||||||
const so = new SerializedObject();
|
const so = new SerializedObject();
|
||||||
so.append(hashprefixes.HASH_TX_MULTISIGN_BYTES);
|
so.append(hashprefixes.HASH_TX_MULTISIGN_BYTES);
|
||||||
so.parse_json(this.tx_json);
|
so.parse_json(this.tx_json);
|
||||||
so.append(UInt160.from_json(account).to_bytes());
|
so.append(decodeAddress(account));
|
||||||
return so;
|
return so;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -692,14 +691,10 @@ Transaction.prototype.sourceTag = function(tag) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Transaction.prototype._setAccount = function(name, value) {
|
Transaction.prototype._setAccount = function(name, value) {
|
||||||
const uInt160 = UInt160.from_json(value);
|
if (!isValidAddress(value)) {
|
||||||
|
|
||||||
if (!uInt160.is_valid()) {
|
|
||||||
throw new Error(name + ' must be a valid account');
|
throw new Error(name + ' must be a valid account');
|
||||||
}
|
}
|
||||||
|
this.tx_json[name] = value;
|
||||||
this.tx_json[name] = uInt160.to_json();
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1176,11 +1171,11 @@ Transaction._rewritePath = function(path) {
|
|||||||
const newNode = { };
|
const newNode = { };
|
||||||
|
|
||||||
if (node.hasOwnProperty('account')) {
|
if (node.hasOwnProperty('account')) {
|
||||||
newNode.account = UInt160.json_rewrite(node.account);
|
newNode.account = node.account;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.hasOwnProperty('issuer')) {
|
if (node.hasOwnProperty('issuer')) {
|
||||||
newNode.issuer = UInt160.json_rewrite(node.issuer);
|
newNode.issuer = node.issuer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.hasOwnProperty('currency')) {
|
if (node.hasOwnProperty('currency')) {
|
||||||
@@ -1384,7 +1379,7 @@ Transaction.prototype.offerCancel = function(options_) {
|
|||||||
Transaction._prepareSignerEntry = function(signer) {
|
Transaction._prepareSignerEntry = function(signer) {
|
||||||
const {account, weight} = signer;
|
const {account, weight} = signer;
|
||||||
|
|
||||||
assert(UInt160.is_valid(account), 'Signer account invalid');
|
assert(isValidAddress(account), 'Signer account invalid');
|
||||||
assert(_.isNumber(weight), 'Signer weight missing');
|
assert(_.isNumber(weight), 'Signer weight missing');
|
||||||
assert(weight > 0 && weight <= 65535, 'Signer weight must be 1-65535');
|
assert(weight > 0 && weight <= 65535, 'Signer weight must be 1-65535');
|
||||||
|
|
||||||
@@ -1606,7 +1601,7 @@ Transaction.prototype.setSigners = function(signers) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Transaction.prototype.addMultiSigner = function(signer) {
|
Transaction.prototype.addMultiSigner = function(signer) {
|
||||||
assert(UInt160.is_valid(signer.Account), 'Signer must have a valid Account');
|
assert(isValidAddress(signer.Account), 'Signer must have a valid Account');
|
||||||
|
|
||||||
if (_.isUndefined(this.tx_json.Signers)) {
|
if (_.isUndefined(this.tx_json.Signers)) {
|
||||||
this.tx_json.Signers = [];
|
this.tx_json.Signers = [];
|
||||||
@@ -1615,8 +1610,8 @@ Transaction.prototype.addMultiSigner = function(signer) {
|
|||||||
this.tx_json.Signers.push({Signer: signer});
|
this.tx_json.Signers.push({Signer: signer});
|
||||||
|
|
||||||
this.tx_json.Signers.sort((a, b) => {
|
this.tx_json.Signers.sort((a, b) => {
|
||||||
return UInt160.from_json(a.Signer.Account)
|
return (new Buffer(decodeAddress(a.Signer.Account))).compare(
|
||||||
.cmp(UInt160.from_json(b.Signer.Account));
|
new Buffer(decodeAddress(b.Signer.Account)));
|
||||||
});
|
});
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const Amount = require('ripple-lib').Amount;
|
const Amount = require('ripple-lib').Amount;
|
||||||
const UInt160 = require('ripple-lib').UInt160;
|
|
||||||
|
|
||||||
|
|
||||||
describe('Amount', function() {
|
describe('Amount', function() {
|
||||||
describe('Negatives', function() {
|
describe('Negatives', function() {
|
||||||
@@ -292,26 +290,6 @@ describe('Amount', function() {
|
|||||||
assert.strictEqual('1', Amount.json_rewrite(1));
|
assert.strictEqual('1', Amount.json_rewrite(1));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('UInt160', function() {
|
|
||||||
it('Parse 0 export', function() {
|
|
||||||
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_generic('0').set_version(0).to_json());
|
|
||||||
});
|
|
||||||
it('Parse 1', function() {
|
|
||||||
assert.deepEqual(UInt160.ACCOUNT_ONE, UInt160.from_generic('1').set_version(0).to_json());
|
|
||||||
});
|
|
||||||
it('Parse rrrrrrrrrrrrrrrrrrrrrhoLvTp export', function() {
|
|
||||||
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrrhoLvTp').to_json());
|
|
||||||
});
|
|
||||||
it('Parse rrrrrrrrrrrrrrrrrrrrBZbvji export', function() {
|
|
||||||
assert.strictEqual(UInt160.ACCOUNT_ONE, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrBZbvji').to_json());
|
|
||||||
});
|
|
||||||
it('is_valid rrrrrrrrrrrrrrrrrrrrrhoLvTp', function() {
|
|
||||||
assert(UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp'));
|
|
||||||
});
|
|
||||||
it('!is_valid rrrrrrrrrrrrrrrrrrrrrhoLvT', function() {
|
|
||||||
assert(!UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvT'));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Amount validity', function() {
|
describe('Amount validity', function() {
|
||||||
it('is_valid 1', function() {
|
it('is_valid 1', function() {
|
||||||
assert(Amount.is_valid(1));
|
assert(Amount.is_valid(1));
|
||||||
|
|||||||
2
test/fixtures/schemas/ledgerhash.json
vendored
2
test/fixtures/schemas/ledgerhash.json
vendored
@@ -3,5 +3,5 @@
|
|||||||
"title": "ledgerhash",
|
"title": "ledgerhash",
|
||||||
"description": "A ledger hash",
|
"description": "A ledger hash",
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"format": "ledgerHash"
|
"pattern": "^[A-F0-9]{64}$"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,14 @@
|
|||||||
|
|
||||||
const assert = require('assert-diff');
|
const assert = require('assert-diff');
|
||||||
const lodash = require('lodash');
|
const lodash = require('lodash');
|
||||||
const ripple = require('ripple-lib');
|
|
||||||
const Remote = require('ripple-lib').Remote;
|
const Remote = require('ripple-lib').Remote;
|
||||||
const Server = require('ripple-lib').Server;
|
const Server = require('ripple-lib').Server;
|
||||||
const Transaction = require('ripple-lib').Transaction;
|
const Transaction = require('ripple-lib').Transaction;
|
||||||
const UInt160 = require('ripple-lib').UInt160;
|
|
||||||
const Currency = require('ripple-lib').Currency;
|
const Currency = require('ripple-lib').Currency;
|
||||||
const Amount = require('ripple-lib').Amount;
|
const Amount = require('ripple-lib').Amount;
|
||||||
const PathFind = require('ripple-lib')._test.PathFind;
|
const PathFind = require('ripple-lib')._test.PathFind;
|
||||||
const Log = require('ripple-lib')._test.Log;
|
const Log = require('ripple-lib')._test.Log;
|
||||||
|
const ACCOUNT_ONE = require('ripple-lib')._test.constants.ACCOUNT_ONE;
|
||||||
|
|
||||||
let options;
|
let options;
|
||||||
let remote;
|
let remote;
|
||||||
@@ -30,7 +29,7 @@ const TX_JSON = {
|
|||||||
Flags: 0,
|
Flags: 0,
|
||||||
TransactionType: 'Payment',
|
TransactionType: 'Payment',
|
||||||
Account: ADDRESS,
|
Account: ADDRESS,
|
||||||
Destination: ripple.UInt160.ACCOUNT_ONE,
|
Destination: ACCOUNT_ONE,
|
||||||
Amount: {
|
Amount: {
|
||||||
value: '1',
|
value: '1',
|
||||||
currency: 'USD',
|
currency: 'USD',
|
||||||
@@ -1492,7 +1491,7 @@ describe('Remote', function() {
|
|||||||
|
|
||||||
it('Construct account_tx request', function() {
|
it('Construct account_tx request', function() {
|
||||||
let request = remote.requestAccountTransactions({
|
let request = remote.requestAccountTransactions({
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
ledger_index_min: -1,
|
ledger_index_min: -1,
|
||||||
ledger_index_max: -1,
|
ledger_index_max: -1,
|
||||||
limit: 5,
|
limit: 5,
|
||||||
@@ -1503,7 +1502,7 @@ describe('Remote', function() {
|
|||||||
assert.deepEqual(request.message, {
|
assert.deepEqual(request.message, {
|
||||||
command: 'account_tx',
|
command: 'account_tx',
|
||||||
id: undefined,
|
id: undefined,
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
ledger_index_min: -1,
|
ledger_index_min: -1,
|
||||||
ledger_index_max: -1,
|
ledger_index_max: -1,
|
||||||
binary: true,
|
binary: true,
|
||||||
@@ -1513,14 +1512,14 @@ describe('Remote', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
request = remote.requestAccountTransactions({
|
request = remote.requestAccountTransactions({
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
min_ledger: -1,
|
min_ledger: -1,
|
||||||
max_ledger: -1
|
max_ledger: -1
|
||||||
});
|
});
|
||||||
assert.deepEqual(request.message, {
|
assert.deepEqual(request.message, {
|
||||||
command: 'account_tx',
|
command: 'account_tx',
|
||||||
id: undefined,
|
id: undefined,
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
binary: true,
|
binary: true,
|
||||||
ledger_index_min: -1,
|
ledger_index_min: -1,
|
||||||
ledger_index_max: -1
|
ledger_index_max: -1
|
||||||
@@ -1528,7 +1527,7 @@ describe('Remote', function() {
|
|||||||
});
|
});
|
||||||
it('Construct account_tx request -- no binary', function() {
|
it('Construct account_tx request -- no binary', function() {
|
||||||
const request = remote.requestAccountTransactions({
|
const request = remote.requestAccountTransactions({
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
ledger_index_min: -1,
|
ledger_index_min: -1,
|
||||||
ledger_index_max: -1,
|
ledger_index_max: -1,
|
||||||
limit: 5,
|
limit: 5,
|
||||||
@@ -1540,7 +1539,7 @@ describe('Remote', function() {
|
|||||||
assert.deepEqual(request.message, {
|
assert.deepEqual(request.message, {
|
||||||
command: 'account_tx',
|
command: 'account_tx',
|
||||||
id: undefined,
|
id: undefined,
|
||||||
account: UInt160.ACCOUNT_ONE,
|
account: ACCOUNT_ONE,
|
||||||
ledger_index_min: -1,
|
ledger_index_min: -1,
|
||||||
ledger_index_max: -1,
|
ledger_index_max: -1,
|
||||||
binary: false,
|
binary: false,
|
||||||
@@ -1618,7 +1617,7 @@ describe('Remote', function() {
|
|||||||
taker_pays: {
|
taker_pays: {
|
||||||
currency: Currency.from_human('XRP').to_hex()
|
currency: Currency.from_human('XRP').to_hex()
|
||||||
},
|
},
|
||||||
taker: UInt160.ACCOUNT_ONE
|
taker: ACCOUNT_ONE
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1645,7 +1644,7 @@ describe('Remote', function() {
|
|||||||
taker_pays: {
|
taker_pays: {
|
||||||
currency: Currency.from_human('XRP').to_hex()
|
currency: Currency.from_human('XRP').to_hex()
|
||||||
},
|
},
|
||||||
taker: UInt160.ACCOUNT_ONE,
|
taker: ACCOUNT_ONE,
|
||||||
ledger_hash: LEDGER_HASH,
|
ledger_hash: LEDGER_HASH,
|
||||||
limit: 10
|
limit: 10
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const Transaction = require('ripple-lib').Transaction;
|
|||||||
const TransactionQueue = require('ripple-lib').TransactionQueue;
|
const TransactionQueue = require('ripple-lib').TransactionQueue;
|
||||||
const Remote = require('ripple-lib').Remote;
|
const Remote = require('ripple-lib').Remote;
|
||||||
const Server = require('ripple-lib').Server;
|
const Server = require('ripple-lib').Server;
|
||||||
|
const {decodeAddress} = require('ripple-address-codec');
|
||||||
|
|
||||||
const transactionResult = {
|
const transactionResult = {
|
||||||
engine_result: 'tesSUCCESS',
|
engine_result: 'tesSUCCESS',
|
||||||
@@ -2257,7 +2258,7 @@ describe('Transaction', function() {
|
|||||||
|
|
||||||
const tbytes = ripple.SerializedObject.from_json(
|
const tbytes = ripple.SerializedObject.from_json(
|
||||||
lodash.merge(transaction.tx_json, {SigningPubKey: ''})).buffer;
|
lodash.merge(transaction.tx_json, {SigningPubKey: ''})).buffer;
|
||||||
const abytes = ripple.UInt160.from_json(a1).to_bytes();
|
const abytes = decodeAddress(a1);
|
||||||
const prefix = require('ripple-lib')._test.HashPrefixes.HASH_TX_MULTISIGN_BYTES;
|
const prefix = require('ripple-lib')._test.HashPrefixes.HASH_TX_MULTISIGN_BYTES;
|
||||||
|
|
||||||
assert.deepEqual(d1.buffer, prefix.concat(tbytes, abytes));
|
assert.deepEqual(d1.buffer, prefix.concat(tbytes, abytes));
|
||||||
|
|||||||
@@ -4,8 +4,9 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const assert = require('assert-diff');
|
const assert = require('assert-diff');
|
||||||
const lodash = require('lodash');
|
const lodash = require('lodash');
|
||||||
const ripple = require('ripple-lib');
|
const ripple = require('ripple-lib')._test;
|
||||||
const fixtures = require('./fixtures/uint');
|
const fixtures = require('./fixtures/uint');
|
||||||
|
const UInt160 = ripple.UInt160;
|
||||||
|
|
||||||
function resultError(test, result) {
|
function resultError(test, result) {
|
||||||
function type(e) {
|
function type(e) {
|
||||||
@@ -101,4 +102,25 @@ function makeTests(uIntType) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe('UInt160', function() {
|
||||||
|
it('Parse 0 export', function() {
|
||||||
|
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_generic('0').set_version(0).to_json());
|
||||||
|
});
|
||||||
|
it('Parse 1', function() {
|
||||||
|
assert.deepEqual(UInt160.ACCOUNT_ONE, UInt160.from_generic('1').set_version(0).to_json());
|
||||||
|
});
|
||||||
|
it('Parse rrrrrrrrrrrrrrrrrrrrrhoLvTp export', function() {
|
||||||
|
assert.strictEqual(UInt160.ACCOUNT_ZERO, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrrhoLvTp').to_json());
|
||||||
|
});
|
||||||
|
it('Parse rrrrrrrrrrrrrrrrrrrrBZbvji export', function() {
|
||||||
|
assert.strictEqual(UInt160.ACCOUNT_ONE, UInt160.from_json('rrrrrrrrrrrrrrrrrrrrBZbvji').to_json());
|
||||||
|
});
|
||||||
|
it('is_valid rrrrrrrrrrrrrrrrrrrrrhoLvTp', function() {
|
||||||
|
assert(UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp'));
|
||||||
|
});
|
||||||
|
it('!is_valid rrrrrrrrrrrrrrrrrrrrrhoLvT', function() {
|
||||||
|
assert(!UInt160.is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvT'));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
['UInt128', 'UInt160', 'UInt256'].forEach(makeTests);
|
['UInt128', 'UInt160', 'UInt256'].forEach(makeTests);
|
||||||
|
|||||||
Reference in New Issue
Block a user