Merge branch 'develop' of https://github.com/ripple/ripple-lib into develop

Conflicts:
	src/js/ripple/serializedtypes.js
	test/serializedtypes-test.js

JSONify complex structures before comparing them in tests.
This commit is contained in:
jatchili
2013-09-10 16:46:53 -07:00
13 changed files with 152 additions and 269 deletions

View File

@@ -101,7 +101,11 @@ remote.connect(function() {
var transaction = remote.transaction();
transaction.payment(MY_ADDRESS, RECIPIENT, AMOUNT);
transaction.payment({
from: MY_ADDRESS,
to: RECIPIENT,
amount: AMOUNT
});
transaction.submit(function(err, res) {
/* handle submission errors / success */
@@ -136,10 +140,8 @@ var Amount = require('ripple-lib').Amount;
var MY_ADDRESS = 'rrrMyAddress';
var MY_SECRET = 'secret';
// TAKER_PAYS is the amount that the other party will pay you
// TAKER_GETS is the amount you are offering to them
var TAKER_PAYS = Amount.from_human('100XRP');
var TAKER_GETS = Amount.from_human('1USD');
var BUY_AMOUNT = Amount.from_human('100XRP');
var SELL_AMOUNT = Amount.from_human('1USD');
// EXPIRATION must be a Date object, leave undefined to submit offer that won't expire
var now = new Date();
@@ -153,7 +155,12 @@ remote.connect(function() {
var transaction = remote.transaction();
transaction.offer_create(MY_ADDRESS, TAKER_PAYS, TAKER_GETS, EXPIRATION);
transaction.offer_create({
from: MY_ADDRESS,
buy: BUY_AMOUNT,
sell: SELL_AMOUNT,
expiration: EXPIRATION
});
transaction.submit(function(err, res) {
/* handle submission errors / success */

View File

@@ -27,8 +27,8 @@
"mocha": "~1.12.1"
},
"scripts": {
"test": "node_modules/.bin/mocha test/*-test.js",
"build": "node_modules/.bin/grunt"
"test": "mocha --reporter spec test/*-test.js",
"build": "grunt"
},
"repository": {
"type": "git",

View File

@@ -1,38 +1,38 @@
// Represent Ripple amounts and currencies.
// - Numbers in hex are big-endian.
var sjcl = require('../../../build/sjcl');
var bn = sjcl.bn;
var utils = require('./utils');
var jsbn = require('./jsbn');
var sjcl = require('../../../build/sjcl');
var bn = sjcl.bn;
var utils = require('./utils');
var jsbn = require('./jsbn');
var BigInteger = jsbn.BigInteger;
var UInt160 = require('./uint160').UInt160,
Seed = require('./seed').Seed,
Currency = require('./currency').Currency;
var UInt160 = require('./uint160').UInt160;
var Seed = require('./seed').Seed;
var Currency = require('./currency').Currency;
var consts = exports.consts = {
'currency_xns' : 0,
'currency_one' : 1,
'xns_precision' : 6,
currency_xns: 0,
currency_one: 1,
xns_precision: 6,
// BigInteger values prefixed with bi_.
'bi_5' : new BigInteger('5'),
'bi_7' : new BigInteger('7'),
'bi_10' : new BigInteger('10'),
'bi_1e14' : new BigInteger(String(1e14)),
'bi_1e16' : new BigInteger(String(1e16)),
'bi_1e17' : new BigInteger(String(1e17)),
'bi_1e32' : new BigInteger('100000000000000000000000000000000'),
'bi_man_max_value' : new BigInteger('9999999999999999'),
'bi_man_min_value' : new BigInteger('1000000000000000'),
'bi_xns_max' : new BigInteger("9000000000000000000"), // Json wire limit.
'bi_xns_min' : new BigInteger("-9000000000000000000"), // Json wire limit.
'bi_xns_unit' : new BigInteger('1000000'),
bi_5: new BigInteger('5'),
bi_7: new BigInteger('7'),
bi_10: new BigInteger('10'),
bi_1e14: new BigInteger(String(1e14)),
bi_1e16: new BigInteger(String(1e16)),
bi_1e17: new BigInteger(String(1e17)),
bi_1e32: new BigInteger('100000000000000000000000000000000'),
bi_man_max_value: new BigInteger('9999999999999999'),
bi_man_min_value: new BigInteger('1000000000000000'),
bi_xns_max: new BigInteger("9000000000000000000"), // Json wire limit.
bi_xns_min: new BigInteger("-9000000000000000000"),// Json wire limit.
bi_xns_unit: new BigInteger('1000000'),
'cMinOffset' : -96,
'cMaxOffset' : 80,
cMinOffset: -96,
cMaxOffset: 80,
};
@@ -41,24 +41,23 @@ var consts = exports.consts = {
// http://docs.oracle.com/javase/1.3/docs/api/java/math/BigInteger.html
//
var Amount = function () {
function Amount() {
// Json format:
// integer : XRP
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
this._value = new BigInteger(); // NaN for bad value. Always positive.
this._offset = 0; // Always 0 for XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._value = new BigInteger(); // NaN for bad value. Always positive.
this._offset = 0; // Always 0 for XRP.
this._is_native = true; // Default to XRP. Only valid if value is not NaN.
this._is_negative = false;
this._currency = new Currency();
this._issuer = new UInt160();
this._issuer = new UInt160();
};
// Given "100/USD/mtgox" return the a string with mtgox remapped.
Amount.text_full_rewrite = function (j) {
return Amount.from_json(j).to_text_full();
}
};
// Given "100/USD/mtgox" return the json.
Amount.json_rewrite = function (j) {
@@ -91,9 +90,7 @@ Amount.is_valid_full = function (j) {
Amount.NaN = function () {
var result = new Amount();
result._value = NaN;
return result;
};
@@ -110,17 +107,14 @@ Amount.prototype.add = function (v) {
if (!this.is_comparable(v)) {
result = Amount.NaN();
}
else if (v.is_zero()) {
result = this;
}
else if (this.is_zero()) {
} else if (v.is_zero()) {
result = this;
} else if (this.is_zero()) {
result = v.clone();
result._is_native = this._is_native;
result._currency = this._currency;
result._issuer = this._issuer;
}
else if (this._is_native) {
} else if (this._is_native) {
result = new Amount();
var v1 = this._is_negative ? this._value.negate() : this._value;
@@ -131,9 +125,7 @@ Amount.prototype.add = function (v) {
result._value = result._is_negative ? s.negate() : s;
result._currency = this._currency;
result._issuer = this._issuer;
}
else
{
} else {
var v1 = this._is_negative ? this._value.negate() : this._value;
var o1 = this._offset;
var v2 = v._is_negative ? v._value.negate() : v._value;
@@ -169,19 +161,15 @@ Amount.prototype.add = function (v) {
};
Amount.prototype.canonicalize = function () {
if (!(this._value instanceof BigInteger))
{
if (!(this._value instanceof BigInteger)) {
// NaN.
// nothing
}
else if (this._is_native) {
} else if (this._is_native) {
// Native.
if (this._value.equals(BigInteger.ZERO)) {
this._offset = 0;
this._is_negative = false;
}
else {
} else {
// Normalize _offset to 0.
while (this._offset < 0) {
@@ -196,13 +184,10 @@ Amount.prototype.canonicalize = function () {
}
// XXX Make sure not bigger than supported. Throw if so.
}
else if (this.is_zero()) {
} else if (this.is_zero()) {
this._offset = -100;
this._is_negative = false;
}
else
{
} else {
// Normalize mantissa to valid range.
while (this._value.compareTo(consts.bi_man_min_value) < 0) {
@@ -228,32 +213,26 @@ Amount.prototype.compareTo = function (v) {
if (!this.is_comparable(v)) {
result = Amount.NaN();
}
else if (this._is_negative !== v._is_negative) {
} else if (this._is_negative !== v._is_negative) {
// Different sign.
result = this._is_negative ? -1 : 1;
}
else if (this._value.equals(BigInteger.ZERO)) {
} else if (this._value.equals(BigInteger.ZERO)) {
// Same sign: positive.
result = v._value.equals(BigInteger.ZERO) ? 0 : -1;
}
else if (v._value.equals(BigInteger.ZERO)) {
} else if (v._value.equals(BigInteger.ZERO)) {
// Same sign: positive.
result = 1;
}
else if (!this._is_native && this._offset > v._offset) {
} else if (!this._is_native && this._offset > v._offset) {
result = this._is_negative ? -1 : 1;
}
else if (!this._is_native && this._offset < v._offset) {
} else if (!this._is_native && this._offset < v._offset) {
result = this._is_negative ? 1 : -1;
}
else {
} else {
result = this._value.compareTo(v._value);
if (result > 0)
if (result > 0) {
result = this._is_negative ? -1 : 1;
else if (result < 0)
} else if (result < 0) {
result = this._is_negative ? 1 : -1;
}
}
return result;
@@ -262,26 +241,25 @@ Amount.prototype.compareTo = function (v) {
// Make d a copy of this. Returns d.
// Modification of objects internally refered to is not allowed.
Amount.prototype.copyTo = function (d, negate) {
if ('object' === typeof this._value)
{
if (typeof this._value === 'object') {
this._value.copyTo(d._value);
}
else
{
} else {
d._value = this._value;
}
d._offset = this._offset;
d._is_native = this._is_native;
d._offset = this._offset;
d._is_native = this._is_native;
d._is_negative = negate
? !this._is_negative // Negating.
: this._is_negative; // Just copying.
? !this._is_negative // Negating.
: this._is_negative; // Just copying.
d._currency = this._currency;
d._issuer = this._issuer;
// Prevent negative zero
if (d.is_zero()) d._is_negative = false;
if (d.is_zero()) {
d._is_negative = false;
}
return d;
};
@@ -291,28 +269,19 @@ Amount.prototype.currency = function () {
};
Amount.prototype.equals = function (d, ignore_issuer) {
if ("string" === typeof d) {
if (typeof d === 'string') {
return this.equals(Amount.from_json(d));
}
if (this === d) return true;
var result = true;
if (d instanceof Amount) {
if (!this.is_valid() || !d.is_valid()) return false;
if (this._is_native !== d._is_native) return false;
result = !((!this.is_valid() || !d.is_valid())
|| (this._is_native !== d._is_native)
|| (!this._value.equals(d._value) || this._offset !== d._offset)
|| (this._is_negative !== d._is_negative)
|| (!this._is_native && (!this._currency.equals(d._currency) || !ignore_issuer && !this._issuer.equals(d._issuer))))
if (!this._value.equals(d._value) || this._offset !== d._offset) {
return false;
}
if (this._is_negative !== d._is_negative) return false;
if (!this._is_native) {
if (!this._currency.equals(d._currency)) return false;
if (!ignore_issuer && !this._issuer.equals(d._issuer)) return false;
}
return true;
} else return false;
return result;
};
// Result in terms of this' currency and issuer.
@@ -386,7 +355,7 @@ Amount.prototype.divide = function (d) {
* @return {Amount} The resulting ratio. Unit will be the same as numerator.
*/
Amount.prototype.ratio_human = function (denominator) {
if ("number" === typeof denominator && parseInt(denominator) === denominator) {
if (typeof denominator === 'number' && parseInt(denominator, 10) === denominator) {
// Special handling of integer arguments
denominator = Amount.from_json("" + denominator + ".0");
} else {
@@ -439,7 +408,7 @@ Amount.prototype.ratio_human = function (denominator) {
* @return {Amount} The product. Unit will be the same as the first factor.
*/
Amount.prototype.product_human = function (factor) {
if ("number" === typeof factor && parseInt(factor) === factor) {
if (typeof factor === 'number' && parseInt(factor, 10) === factor) {
// Special handling of integer arguments
factor = Amount.from_json("" + factor + ".0");
} else {

View File

@@ -1,58 +0,0 @@
//
// Access to the Ripple network via multiple untrusted servers or a single trusted server.
//
// Overview:
// Network configuration.
// Can leverage local storage to remember network configuration
// Aquires the network
// events:
// online
// offline
//
var remote = require("./remote.js");
var opts_default = {
DEFAULT_VALIDATORS_SITE : "redstem.com",
ips = {
}
};
//
// opts : {
// cache : undefined || {
// get : function () { return cached_value; },
// set : function (value) { cached_value = value; },
// },
//
// // Where to get validators.txt if needed.
// DEFAULT_VALIDATORS_SITE : _domain_,
//
// // Validator.txt to use.
// validators : _txt_,
// }
//
var Network = function (opts) {
};
// Set the network configuration.
Network.protocol.configure = function () {
};
// Target state: connectted
Network.protocol.start = function () {
};
// Target state: disconnect
Network.protocol.stop = function () {
};
exports.Network = Network;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -153,7 +153,6 @@ SerializedObject.prototype.to_json = function() {
function jsonify_structure(thing, field_name) {
var output;
console.log("JSONIFYING:", thing, field_name);
switch (typeof thing) {
case 'number':
switch (field_name) {
@@ -167,9 +166,6 @@ function jsonify_structure(thing, field_name) {
output = TRANSACTION_TYPES[thing] || thing;
break;
default:
if (typeof thing.to_json === 'function') {
console.log("WE COULD HAVE DONE:", thing.to_json());
}
output = thing;
}
break;
@@ -190,10 +186,11 @@ function jsonify_structure(thing, field_name) {
default:
output = thing;
}
console.log("AND THE RESULT WAS:", output);
return output;
};
SerializedObject.jsonify_structure = jsonify_structure; //So that we can access it from elsewhere.
SerializedObject.prototype.serialize = function (typedef, obj) {
// Ensure canonical order
typedef = SerializedObject._sort_typedef(typedef.slice());

View File

@@ -412,8 +412,8 @@ Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expi
var options = src;
cancel_sequence = options.cancel_sequence;
expiration = options.expiration;
taker_gets = options.taker_gets;
taker_pays = options.taker_pays;
taker_gets = options.taker_gets || options.sell;
taker_pays = options.taker_pays || options.buy;
src = options.source || options.from;
}
@@ -468,7 +468,7 @@ Transaction.prototype.password_set = function (src, authorized_key, generator, p
public_key = options.public_key;
generator = options.generator;
authorized_key = options.authorized_key;
src = options.src || options.from;
src = options.source || options.from;
}
if (!UInt160.is_valid(src)) {
@@ -517,6 +517,10 @@ Transaction.prototype.payment = function (src, dst, amount) {
throw new Error('Payment destination address invalid');
}
if (typeof amount === 'string' && !Number(amount)) {
amount = Amount.from_human(amount);
}
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'Payment';
this.tx_json.Account = UInt160.json_rewrite(src);
@@ -532,7 +536,7 @@ Transaction.prototype.ripple_line_set = function (src, limit, quality_in, qualit
quality_out = options.quality_out;
quality_in = options.quality_in;
limit = options.limit;
src = options.src || options.from;
src = options.source || options.from;
}
if (!UInt160.is_valid(src)) {
@@ -568,7 +572,7 @@ Transaction.prototype.wallet_add = function (src, amount, authorized_key, public
public_key = options.public_key;
authorized_key = options.authorized_key;
amount = options.amount;
src = options.src || options.from;
src = options.source || options.from;
}
if (!UInt160.is_valid(src)) {

View File

@@ -1,12 +1,10 @@
var assert = require('assert');
var jsbn = require('../src/js/ripple/jsbn');
var utils = require('./testutils');
var jsbn = utils.load_module('jsbn');
var BigInteger = jsbn.BigInteger;
var Amount = require('../src/js/ripple/amount').Amount;
var UInt160 = require('../src/js/ripple/uint160').UInt160;
var config = require('./testutils').get_config();
var Amount = utils.load_module('amount').Amount;
var UInt160 = utils.load_module('uint160').UInt160;
var config = utils.get_config();
describe('Amount', function() {
describe('Negatives', function() {

View File

@@ -1,7 +1,6 @@
var assert = require('assert');
var Seed = require('../src/js/ripple/seed').Seed;
var utils = require('./testutils');
var Seed = utils.load_module('seed').Seed;
var config = require('./testutils').get_config();
describe('Base58', function() {

View File

@@ -1,5 +1,6 @@
var utils = require('./testutils');
var assert = require('assert');
var SerializedObject = require('../src/js/ripple/serializedobject').SerializedObject;
var SerializedObject = utils.load_module('serializedobject').SerializedObject;
describe('Serialied object', function() {
describe('Serialized object', function() {

View File

@@ -1,12 +1,16 @@
var assert = require('assert');
var SerializedObject = require('../src/js/ripple/serializedobject').SerializedObject;
var types = require('../src/js/ripple/serializedtypes');
var jsbn = require('../src/js/ripple/jsbn');
var BigInteger = jsbn.BigInteger;
var utils = require('./testutils');
var assert = require('assert');
var SerializedObject = utils.load_module('serializedobject').SerializedObject;
var types = utils.load_module('serializedtypes');
var jsbn = utils.load_module('jsbn');
var BigInteger = jsbn.BigInteger;
var config = require('./testutils').get_config();
describe('Serialized types', function() {
describe('Int8', function() {
it('Serialize 0', function () {
@@ -466,38 +470,18 @@ describe('Serialized types', function() {
});
it('Parse [[e],[e,e]]', function () {
var so = new SerializedObject('31000000000000000000000000000000000000007B00000000000000000000000055534400000000000000000000000000000000000000000000000315FF31000000000000000000000000000000000000007B000000000000000000000000425443000000000000000000000000000000000000000000000003153100000000000000000000000000000000000003DB0000000000000000000000004555520000000000000000000000000000000000000000000000014100');
//console.log("NEW SO:", so); //001201
var parsed_path = types.PathSet.parse(so);
var internal_jsonification = internally_jsonify(parsed_path);
//parsed_path = [];
//console.log("AND FINALLY", JSON.stringify(parsed_path));
//console.log("WHAT WE GOT?", JSON.stringify(so.to_json()));
console.log("PARSED THING:", JSON.stringify(parsed_path));
assert.deepEqual(parsed_path,
[[{"account":{"_value":123},
"currency":{"_value":"USD"},
"issuer":{"_value":789}}],
[{"account":{"_value":123},
"currency":{"_value":"BTC"},
"issuer":{"_value":789}},
{"account":{"_value":987},
"currency":{"_value":"EUR"},
"issuer":{"_value":321}}]]
);
/*assert.deepEqual(parsed_path, [[{
account : {_value: 123 },
currency: {_value: 'USD'},
issuer: {_value: 789}}],
[{
account : {_value: 123},
currency: {_value: 'BTC'},
issuer: {_value: 789}
},
{
account : {_value: 987},
currency: {_value: 'EUR'},
issuer: {_value: 321}
}]]);*/
var parsed_path = types.PathSet.parse(so);
var comp =[ [ { account: 'rrrrrrrrrrrrrrrrrrrrNxV3Xza',
currency: 'USD',
issuer: 'rrrrrrrrrrrrrrrrrrrpYnYCNYf' } ],
[ { account: 'rrrrrrrrrrrrrrrrrrrrNxV3Xza',
currency: 'BTC',
issuer: 'rrrrrrrrrrrrrrrrrrrpYnYCNYf' },
{ account: 'rrrrrrrrrrrrrrrrrrrpvQsW3V3',
currency: 'EUR',
issuer: 'rrrrrrrrrrrrrrrrrrrdHRtqg2' } ] ];
assert.deepEqual(SerializedObject.jsonify_structure(parsed_path, ""), comp);
});
});
@@ -522,41 +506,22 @@ describe('Serialized types', function() {
assert.strictEqual(so.to_hex(), '64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1');
//TODO: Check independently.
});
/*it('Parse same object', function () {
it('Parse same object', function () {
var so = new SerializedObject('64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1');
var parsed_object=types.Object.parse(so);
assert.deepEqual(parsed_object, {
TakerPays: {
_value: { '0': 56357454, '1': 32653779, t: 2, s: 0 },
_offset: -8,
_is_native: false,
_is_negative: false,
_currency: { _value: 'EUR' },
_issuer: { _value: -422657445385694440895149034202122766475892017176 } },
TakerGets: {
_value: { '0': 213, '1': 0, '2': 0, t: 1, s: 0 },
_offset: 0,
_is_native: true,
_is_negative: false,
_currency: { _value: NaN },
_issuer: { _value: NaN }
},
Fee: {
_value: { '0': 789, '1': 0, '2': 0, t: 1, s: 0 },
_offset: 0,
_is_native: true,
_is_negative: false,
_currency: { _value: NaN },
_issuer: { _value: NaN }
}
});
var comp = { TakerPays:
{ value: '87654321.12345678',
currency: 'EUR',
issuer: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh' },
TakerGets: '213',
Fee: '789' };
assert.deepEqual(SerializedObject.jsonify_structure(parsed_object, ""), comp);
//TODO: Check independently.
});*/
});
it('Serialize simple object {DestinationTag:123, QualityIn:456, QualityOut:789}', function () {
var so = new SerializedObject();
types.Object.serialize(so, {DestinationTag:123, QualityIn:456, QualityOut:789});
//console.log('DOES THE JSON METHOD WORK?', so.to_json());
assert.strictEqual(so.to_hex(), '2E0000007B2014000001C8201500000315E1');
//TODO: Check independently.
});
@@ -585,31 +550,25 @@ describe('Serialized types', function() {
//TODO: Check this manually
assert.strictEqual(so.to_hex(), '64400000000000007B6540000000000001C8684000000000000315F1');
});
/*it('Parse the same array', function () {
it('Parse the same array', function () {
var so = new SerializedObject('64400000000000007B6540000000000001C8684000000000000315F1');
var parsed_object=types.Array.parse(so);
//console.log('WE GOT:', parsed_object[0].TakerPays._value, parsed_object[1].TakerGets._value, parsed_object[2].Fee._value);
assert.deepEqual([123,456,789],[
parsed_object[0].TakerPays._value,
parsed_object[1].TakerGets._value,
parsed_object[2].Fee._value]);
});*/
var comp = [ { TakerPays: '123' }, { TakerGets: '456' }, { Fee: '789' } ];
assert.deepEqual(SerializedObject.jsonify_structure(parsed_object, ""), comp);
});
it('Serialize 3-length array [{DestinationTag:123}); {QualityIn:456}, {Fee:789}]', function () {
var so = new SerializedObject();
types.Array.serialize(so, [{DestinationTag:123}, {QualityIn:456}, {Fee:789}]);
//TODO: Check this manually
//console.log('DOES THE JSON METHOD WORK2?', so.to_json());
assert.strictEqual(so.to_hex(), '2E0000007B2014000001C8684000000000000315F1');
});
/*it('Parse the same array 2', function () {
it('Parse the same array 2', function () {
var so = new SerializedObject('2E0000007B2014000001C8684000000000000315F1');
var parsed_object=types.Array.parse(so);
var parsed_object = types.Array.parse(so);
var comp = [ { DestinationTag: 123 }, { QualityIn: 456 }, { Fee: '789' } ];
//TODO: Is this correct? Return some things as integers, and others as objects?
assert.deepEqual([123,456,789],[
parsed_object[0].DestinationTag,
parsed_object[1].QualityIn,
parsed_object[2].Fee._value]);
});*/
assert.deepEqual( SerializedObject.jsonify_structure(parsed_object, ""), comp);
});
});
});

View File

@@ -1,5 +1,6 @@
var assert = require('assert');
var Seed = require('../src/js/ripple/seed').Seed;
var utils = require('./testutils');
var Seed = utils.load_module('seed').Seed;
describe('Signing', function() {
describe('Keys', function() {

View File

@@ -16,3 +16,9 @@ exports.load_config = load_config;
function load_config(config) {
return( require('../src/js/ripple/config')).load(config);
}
exports.load_module = load_module;
function load_module(name) {
return require((process.env.RIPPLE_LIB_COV ? '../lib-cov/' : '../src/js/ripple/') + name);
}

View File

@@ -1,6 +1,6 @@
var fs = require('fs');
var assert = require('assert');
var utils = require('../src/js/ripple/utils.js');
var utils = require('./testutils').load_module('utils');
describe('Utils', function() {
describe('hexToString and stringToHex', function() {