JS: Remove ripple-lib and link to it via package.json instead.

This commit is contained in:
Stefan Thomas
2013-04-26 21:51:50 +02:00
parent 1605a74864
commit 0a2e7664b8
53 changed files with 50 additions and 7376 deletions

7
.gitmodules vendored
View File

@@ -1,10 +1,3 @@
[submodule "src/js/cryptojs"]
path = src/js/cryptojs
url = git://github.com/gwjjeff/cryptojs.git
[submodule "src/js/sjcl"]
path = src/js/sjcl
url = git://github.com/bitwiseshiftleft/sjcl.git
ignore = dirty
[submodule "src/cpp/leveldb"]
path = src/cpp/leveldb
url = https://code.google.com/p/leveldb/

View File

@@ -3,13 +3,10 @@ Ripple - P2P Payment Network
Some portions of this source code are currently closed source.
This is the repository for Ripple's:
* rippled - Reference P2P network server
* ripple.js - Reference JavaScript client libraries for node.js and browsers.
This is the repository for Ripple's `rippled`, reference P2P network server.
Build instructions:
* https://ripple.com/wiki/Rippled_build_instructions
* https://ripple.com/wiki/Ripple_JavaScript_library
Setup instructions:
* https://ripple.com/wiki/Rippled_setup_instructions

View File

@@ -23,7 +23,7 @@ var extend = require("extend");
var http = require("http");
var url = require("url");
var Remote = require("../src/js/remote.js").Remote;
var Remote = require("ripple-lib").Remote;
var program = process.argv[1];

View File

@@ -1,4 +1,4 @@
var ripple = require('../src/js');
var ripple = require('ripple-lib');
var v = {
seed: "snoPBrXtMeMyMHUVTgbuqAfg1SUTb",

View File

@@ -1,10 +1,10 @@
#!/usr/bin/node
var async = require('async');
var Remote = require('../src/js/remote').Remote;
var Transaction = require('../src/js/transaction').Transaction;
var UInt160 = require('../src/js/uint160').UInt160;
var Amount = require('../src/js/amount').Amount;
var Remote = require('ripple-lib').Remote;
var Transaction = require('ripple-lib').Transaction;
var UInt160 = require('ripple-lib').UInt160;
var Amount = require('ripple-lib').Amount;
var book_key = function (book) {
return book.taker_pays.currency

View File

@@ -1,6 +1,6 @@
#!/usr/bin/node
var Transaction = require('../src/js/transaction').Transaction;
var Transaction = require('ripple-lib').Transaction;
var cursor = 2;
var verbose;

View File

@@ -1,19 +1,16 @@
{
"name": "ripple-lib",
"version": "0.7.10",
"description": "Ripple JavaScript client library",
"name": "rippled",
"version": "0.0.1",
"description": "Rippled Server",
"private": true,
"files": [
"src/js/*.js",
"build/sjcl.js",
"bin/rsign.js"
],
"main": "src/js",
"directories": {
"test": "test"
},
"dependencies": {
"ripple-lib": "0.7.10",
"async": "~0.1.22",
"ws": "~0.4.22",
"extend": "~1.1.1",

View File

@@ -1,3 +0,0 @@
# Ripple JavaScript library
This library lets you connect to a ripple server via websockets.

View File

@@ -1,148 +0,0 @@
// Routines for working with an account.
//
// You should not instantiate this class yourself, instead use Remote#account.
//
// Events:
// wallet_clean : True, iff the wallet has been updated.
// wallet_dirty : True, iff the wallet needs to be updated.
// balance : The current stamp balance.
// balance_proposed
//
// var network = require("./network.js");
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Amount = require('./amount').Amount;
var UInt160 = require('./uint160').UInt160;
var extend = require('extend');
var Account = function (remote, account) {
EventEmitter.call(this);
var self = this;
this._remote = remote;
this._account = UInt160.from_json(account);
this._account_id = this._account.to_json();
this._subs = 0;
// Ledger entry object
// Important: This must never be overwritten, only extend()-ed
this._entry = {};
this.on('newListener', function (type, listener) {
if (Account.subscribe_events.indexOf(type) !== -1) {
if (!self._subs && 'open' === self._remote._online_state) {
self._remote.request_subscribe()
.accounts(self._account_id)
.request();
}
self._subs += 1;
}
});
this.on('removeListener', function (type, listener) {
if (Account.subscribe_events.indexOf(type) !== -1) {
self._subs -= 1;
if (!self._subs && 'open' === self._remote._online_state) {
self._remote.request_unsubscribe()
.accounts(self._account_id)
.request();
}
}
});
this._remote.on('prepare_subscribe', function (request) {
if (self._subs) request.accounts(self._account_id);
});
this.on('transaction', function (msg) {
var changed = false;
msg.mmeta.each(function (an) {
if (an.entryType === 'AccountRoot' &&
an.fields.Account === self._account_id) {
extend(self._entry, an.fieldsNew, an.fieldsFinal);
changed = true;
}
});
if (changed) {
self.emit('entry', self._entry);
}
});
return this;
};
util.inherits(Account, EventEmitter);
/**
* List of events that require a remote subscription to the account.
*/
Account.subscribe_events = ['transaction', 'entry'];
Account.prototype.to_json = function ()
{
return this._account.to_json();
};
/**
* Whether the AccountId is valid.
*
* Note: This does not tell you whether the account exists in the ledger.
*/
Account.prototype.is_valid = function ()
{
return this._account.is_valid();
};
/**
* Retrieve the current AccountRoot entry.
*
* To keep up-to-date with changes to the AccountRoot entry, subscribe to the
* "entry" event.
*
* @param {function (err, entry)} callback Called with the result
*/
Account.prototype.entry = function (callback)
{
var self = this;
self._remote.request_account_info(this._account_id)
.on('success', function (e) {
extend(self._entry, e.account_data);
self.emit('entry', self._entry);
if ("function" === typeof callback) {
callback(null, e);
}
})
.on('error', function (e) {
callback(e);
})
.request();
return this;
};
/**
* Notify object of a relevant transaction.
*
* This is only meant to be called by the Remote class. You should never have to
* call this yourself.
*/
Account.prototype.notifyTx = function (message)
{
// Only trigger the event if the account object is actually
// subscribed - this prevents some weird phantom events from
// occurring.
if (this._subs) {
this.emit('transaction', message);
}
};
exports.Account = Account;
// vim:sw=2:sts=2:ts=8:et

File diff suppressed because it is too large Load Diff

View File

@@ -1,140 +0,0 @@
var sjcl = require('../../build/sjcl');
var utils = require('./utils');
var jsbn = require('./jsbn');
var extend = require('extend');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var Base = {};
var alphabets = Base.alphabets = {
'ripple' : "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
'tipple' : "RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz",
'bitcoin' : "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
};
extend(Base, {
'VER_NONE' : 1,
'VER_NODE_PUBLIC' : 28,
'VER_NODE_PRIVATE' : 32,
'VER_ACCOUNT_ID' : 0,
'VER_ACCOUNT_PUBLIC' : 35,
'VER_ACCOUNT_PRIVATE' : 34,
'VER_FAMILY_GENERATOR' : 41,
'VER_FAMILY_SEED' : 33
});
var sha256 = function (bytes) {
return sjcl.codec.bytes.fromBits(sjcl.hash.sha256.hash(sjcl.codec.bytes.toBits(bytes)));
};
var sha256hash = function (bytes) {
return sha256(sha256(bytes));
};
// --> input: big-endian array of bytes.
// <-- string at least as long as input.
Base.encode = function (input, alpha) {
var alphabet = alphabets[alpha || 'ripple'];
var bi_base = new BigInteger(String(alphabet.length));
var bi_q = nbi();
var bi_r = nbi();
var bi_value = new BigInteger(input);
var buffer = [];
while (bi_value.compareTo(BigInteger.ZERO) > 0)
{
bi_value.divRemTo(bi_base, bi_q, bi_r);
bi_q.copyTo(bi_value);
buffer.push(alphabet[bi_r.intValue()]);
}
var i;
for (i = 0; i != input.length && !input[i]; i += 1) {
buffer.push(alphabet[0]);
}
return buffer.reverse().join("");
};
// --> input: String
// <-- array of bytes or undefined.
Base.decode = function (input, alpha) {
var alphabet = alphabets[alpha || 'ripple'];
var bi_base = new BigInteger(String(alphabet.length));
var bi_value = nbi();
var i;
for (i = 0; i != input.length && input[i] === alphabet[0]; i += 1)
;
for (; i != input.length; i += 1) {
var v = alphabet.indexOf(input[i]);
if (v < 0)
return undefined;
var r = nbi();
r.fromInt(v);
bi_value = bi_value.multiply(bi_base).add(r);
}
// toByteArray:
// - Returns leading zeros!
// - Returns signed bytes!
var bytes = bi_value.toByteArray().map(function (b) { return b ? b < 0 ? 256+b : b : 0; });
var extra = 0;
while (extra != bytes.length && !bytes[extra])
extra += 1;
if (extra)
bytes = bytes.slice(extra);
var zeros = 0;
while (zeros !== input.length && input[zeros] === alphabet[0])
zeros += 1;
return [].concat(utils.arraySet(zeros, 0), bytes);
};
// --> input: Array
// <-- String
Base.encode_check = function (version, input, alphabet) {
var buffer = [].concat(version, input);
var check = sha256(sha256(buffer)).slice(0, 4);
return Base.encode([].concat(buffer, check), alphabet);
}
// --> input : String
// <-- NaN || BigInteger
Base.decode_check = function (version, input, alphabet) {
var buffer = Base.decode(input, alphabet);
if (!buffer || buffer[0] !== version || buffer.length < 5)
return NaN;
var computed = sha256hash(buffer.slice(0, -4)).slice(0, 4);
var checksum = buffer.slice(-4);
var i;
for (i = 0; i != 4; i += 1)
if (computed[i] !== checksum[i])
return NaN;
// We'll use the version byte to add a leading zero, this ensures JSBN doesn't
// intrepret the value as a negative number
buffer[0] = 0;
return new BigInteger(buffer.slice(0, -4), 256);
}
exports.Base = Base;

View File

@@ -1,90 +0,0 @@
var ST = require("./serializedtypes");
var REQUIRED = exports.REQUIRED = 0,
OPTIONAL = exports.OPTIONAL = 1,
DEFAULT = exports.DEFAULT = 2;
ST.Int16.id = 1;
ST.Int32.id = 2;
ST.Int64.id = 3;
ST.Hash128.id = 4;
ST.Hash256.id = 5;
ST.Amount.id = 6;
ST.VariableLength.id = 7;
ST.Account.id = 8;
ST.Object.id = 14;
ST.Array.id = 15;
ST.Int8.id = 16;
ST.Hash160.id = 17;
ST.PathSet.id = 18;
ST.Vector256.id = 19;
var base = [
[ 'TransactionType' , REQUIRED, 2, ST.Int16 ],
[ 'Flags' , OPTIONAL, 2, ST.Int32 ],
[ 'SourceTag' , OPTIONAL, 3, ST.Int32 ],
[ 'Account' , REQUIRED, 1, ST.Account ],
[ 'Sequence' , REQUIRED, 4, ST.Int32 ],
[ 'Fee' , REQUIRED, 8, ST.Amount ],
[ 'OperationLimit' , OPTIONAL, 29, ST.Int32 ],
[ 'SigningPubKey' , REQUIRED, 3, ST.VariableLength ],
[ 'TxnSignature' , OPTIONAL, 4, ST.VariableLength ]
];
exports.tx = {
AccountSet: [3].concat(base, [
[ 'EmailHash' , OPTIONAL, 1, ST.Hash128 ],
[ 'WalletLocator' , OPTIONAL, 7, ST.Hash256 ],
[ 'WalletSize' , OPTIONAL, 12, ST.Int32 ],
[ 'MessageKey' , OPTIONAL, 2, ST.VariableLength ],
[ 'Domain' , OPTIONAL, 7, ST.VariableLength ],
[ 'TransferRate' , OPTIONAL, 11, ST.Int32 ]
]),
TrustSet: [20].concat(base, [
[ 'LimitAmount' , OPTIONAL, 3, ST.Amount ],
[ 'QualityIn' , OPTIONAL, 20, ST.Int32 ],
[ 'QualityOut' , OPTIONAL, 21, ST.Int32 ]
]),
OfferCreate: [7].concat(base, [
[ 'TakerPays' , REQUIRED, 4, ST.Amount ],
[ 'TakerGets' , REQUIRED, 5, ST.Amount ],
[ 'Expiration' , OPTIONAL, 10, ST.Int32 ]
]),
OfferCancel: [8].concat(base, [
[ 'OfferSequence' , REQUIRED, 25, ST.Int32 ]
]),
SetRegularKey: [5].concat(base, [
[ 'RegularKey' , REQUIRED, 8, ST.Account ]
]),
Payment: [0].concat(base, [
[ 'Destination' , REQUIRED, 3, ST.Account ],
[ 'Amount' , REQUIRED, 1, ST.Amount ],
[ 'SendMax' , OPTIONAL, 9, ST.Amount ],
[ 'Paths' , DEFAULT , 1, ST.PathSet ],
[ 'InvoiceID' , OPTIONAL, 17, ST.Hash256 ],
[ 'DestinationTag' , OPTIONAL, 14, ST.Int32 ]
]),
Contract: [9].concat(base, [
[ 'Expiration' , REQUIRED, 10, ST.Int32 ],
[ 'BondAmount' , REQUIRED, 23, ST.Int32 ],
[ 'StampEscrow' , REQUIRED, 22, ST.Int32 ],
[ 'RippleEscrow' , REQUIRED, 17, ST.Amount ],
[ 'CreateCode' , OPTIONAL, 11, ST.VariableLength ],
[ 'FundCode' , OPTIONAL, 8, ST.VariableLength ],
[ 'RemoveCode' , OPTIONAL, 9, ST.VariableLength ],
[ 'ExpireCode' , OPTIONAL, 10, ST.VariableLength ]
]),
RemoveContract: [10].concat(base, [
[ 'Target' , REQUIRED, 7, ST.Account ]
]),
EnableFeature: [100].concat(base, [
[ 'Feature' , REQUIRED, 19, ST.Hash256 ]
]),
SetFee: [101].concat(base, [
[ 'Features' , REQUIRED, 9, ST.Array ],
[ 'BaseFee' , REQUIRED, 5, ST.Int64 ],
[ 'ReferenceFeeUnits' , REQUIRED, 30, ST.Int32 ],
[ 'ReserveBase' , REQUIRED, 31, ST.Int32 ],
[ 'ReserveIncrement' , REQUIRED, 32, ST.Int32 ]
])
};

View File

@@ -1,10 +0,0 @@
// This object serves as a singleton to store config options
var extend = require("extend");
var config = module.exports = {
load: function (newOpts) {
extend(config, newOpts);
return config;
}
};

Submodule src/js/cryptojs deleted from c3c843c513

View File

@@ -1,83 +0,0 @@
//
// Currency support
//
// XXX Internal form should be UInt160.
var Currency = function () {
// Internal form: 0 = XRP. 3 letter-code.
// XXX Internal should be 0 or hex with three letter annotation when valid.
// Json form:
// '', 'XRP', '0': 0
// 3-letter code: ...
// XXX Should support hex, C++ doesn't currently allow it.
this._value = NaN;
}
// Given "USD" return the json.
Currency.json_rewrite = function (j) {
return Currency.from_json(j).to_json();
};
Currency.from_json = function (j) {
if (j instanceof Currency) return j.clone();
else if ('string' === typeof j) return (new Currency()).parse_json(j);
else return new Currency(); // NaN
};
Currency.is_valid = function (j) {
return Currency.from_json(j).is_valid();
};
Currency.prototype.clone = function() {
return this.copyTo(new Currency());
};
// Returns copy.
Currency.prototype.copyTo = function (d) {
d._value = this._value;
return d;
};
Currency.prototype.equals = function (d) {
return ('string' !== typeof this._value && isNaN(this._value))
|| ('string' !== typeof d._value && isNaN(d._value)) ? false : this._value === d._value;
};
// this._value = NaN on error.
Currency.prototype.parse_json = function (j) {
if ("" === j || "0" === j || "XRP" === j) {
this._value = 0;
}
else if ('string' != typeof j || 3 !== j.length) {
this._value = NaN;
}
else {
this._value = j;
}
return this;
};
Currency.prototype.is_native = function () {
return !isNaN(this._value) && !this._value;
};
Currency.prototype.is_valid = function () {
return 'string' === typeof this._value || !isNaN(this._value);
};
Currency.prototype.to_json = function () {
return this._value ? this._value : "XRP";
};
Currency.prototype.to_human = function () {
return this._value ? this._value : "XRP";
};
exports.Currency = Currency;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,22 +0,0 @@
exports.Remote = require('./remote').Remote;
exports.Amount = require('./amount').Amount;
exports.Currency = require('./currency').Currency;
exports.UInt160 = require('./amount').UInt160;
exports.Seed = require('./amount').Seed;
exports.Transaction = require('./transaction').Transaction;
exports.Meta = require('./meta').Meta;
exports.utils = require('./utils');
// Important: We do not guarantee any specific version of SJCL or for any
// specific features to be included. The version and configuration may change at
// any time without warning.
//
// However, for programs that are tied to a specific version of ripple.js like
// the official client, it makes sense to expose the SJCL instance so we don't
// have to include it twice.
exports.sjcl = require('../../build/sjcl');
exports.config = require('./config');
// vim:sw=2:sts=2:ts=8:et

File diff suppressed because it is too large Load Diff

View File

@@ -1,67 +0,0 @@
var sjcl = require('../../build/sjcl');
var UInt256 = require('./uint256').UInt256;
var KeyPair = function ()
{
this._curve = sjcl.ecc.curves['c256'];
this._secret = null;
this._pubkey = null;
};
KeyPair.from_bn_secret = function (j)
{
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_bn_secret(j);
}
};
KeyPair.prototype.parse_bn_secret = function (j)
{
this._secret = new sjcl.ecc.ecdsa.secretKey(sjcl.ecc.curves['c256'], j);
return this;
};
/**
* Returns public key as sjcl public key.
*
* @private
*/
KeyPair.prototype._pub = function ()
{
var curve = this._curve;
if (!this._pubkey && this._secret) {
var exponent = this._secret._exponent;
this._pubkey = new sjcl.ecc.ecdsa.publicKey(curve, curve.G.mult(exponent));
}
return this._pubkey;
};
/**
* Returns public key as hex.
*
* Key will be returned as a compressed pubkey - 33 bytes converted to hex.
*/
KeyPair.prototype.to_hex_pub = function ()
{
var pub = this._pub();
if (!pub) return null;
var point = pub._point, y_even = point.y.mod(2).equals(0);
return sjcl.codec.hex.fromBits(sjcl.bitArray.concat(
[sjcl.bitArray.partial(8, y_even ? 0x02 : 0x03)],
point.x.toBits(this._curve.r.bitLength())
)).toUpperCase();
};
KeyPair.prototype.sign = function (hash)
{
hash = UInt256.from_json(hash);
return this._secret.signDER(hash.to_bits(), 0);
};
exports.KeyPair = KeyPair;

View File

@@ -1,137 +0,0 @@
var extend = require('extend');
var utils = require('./utils');
var UInt160 = require('./uint160').UInt160;
var Amount = require('./amount').Amount;
/**
* Meta data processing facility.
*/
var Meta = function (raw_data)
{
this.nodes = [];
for (var i = 0, l = raw_data.AffectedNodes.length; i < l; i++) {
var an = raw_data.AffectedNodes[i],
result = {};
["CreatedNode", "ModifiedNode", "DeletedNode"].forEach(function (x) {
if (an[x]) result.diffType = x;
});
if (!result.diffType) return null;
an = an[result.diffType];
result.entryType = an.LedgerEntryType;
result.ledgerIndex = an.LedgerIndex;
result.fields = extend({}, an.PreviousFields, an.NewFields, an.FinalFields);
result.fieldsPrev = an.PreviousFields || {};
result.fieldsNew = an.NewFields || {};
result.fieldsFinal = an.FinalFields || {};
this.nodes.push(result);
}
};
/**
* Execute a function on each affected node.
*
* The callback is passed two parameters. The first is a node object which looks
* like this:
*
* {
* // Type of diff, e.g. CreatedNode, ModifiedNode
* diffType: 'CreatedNode'
*
* // Type of node affected, e.g. RippleState, AccountRoot
* entryType: 'RippleState',
*
* // Index of the ledger this change occurred in
* ledgerIndex: '01AB01AB...',
*
* // Contains all fields with later versions taking precedence
* //
* // This is a shorthand for doing things like checking which account
* // this affected without having to check the diffType.
* fields: {...},
*
* // Old fields (before the change)
* fieldsPrev: {...},
*
* // New fields (that have been added)
* fieldsNew: {...},
*
* // Changed fields
* fieldsFinal: {...}
* }
*
* The second parameter to the callback is the index of the node in the metadata
* (first entry is index 0).
*/
Meta.prototype.each = function (fn)
{
for (var i = 0, l = this.nodes.length; i < l; i++) {
fn(this.nodes[i], i);
}
};
var amountFieldsAffectingIssuer = [
"LowLimit", "HighLimit", "TakerPays", "TakerGets"
];
Meta.prototype.getAffectedAccounts = function ()
{
var accounts = [];
// This code should match the behavior of the C++ method:
// TransactionMetaSet::getAffectedAccounts
this.each(function (an) {
var fields = (an.diffType === "CreatedNode") ? an.fieldsNew : an.fieldsFinal;
for (var i in fields) {
var field = fields[i];
if ("string" === typeof field && UInt160.is_valid(field)) {
accounts.push(field);
} else if (amountFieldsAffectingIssuer.indexOf(i) !== -1) {
var amount = Amount.from_json(field);
var issuer = amount.issuer();
if (issuer.is_valid() && !issuer.is_zero()) {
accounts.push(issuer.to_json());
}
}
}
});
accounts = utils.arrayUnique(accounts);
return accounts;
};
Meta.prototype.getAffectedBooks = function ()
{
var books = [];
this.each(function (an) {
if (an.entryType !== 'Offer') return;
var gets = Amount.from_json(an.fields.TakerGets);
var pays = Amount.from_json(an.fields.TakerPays);
var getsKey = gets.currency().to_json();
if (getsKey !== 'XRP') getsKey += '/' + gets.issuer().to_json();
var paysKey = pays.currency().to_json();
if (paysKey !== 'XRP') paysKey += '/' + pays.issuer().to_json();
var key = getsKey + ":" + paysKey;
books.push(key);
});
books = utils.arrayUnique(books);
return books;
};
exports.Meta = Meta;

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

@@ -1,250 +0,0 @@
// Routines for working with an orderbook.
//
// One OrderBook object represents one half of an order book. (i.e. bids OR
// asks) Which one depends on the ordering of the parameters.
//
// Events:
// - transaction A transaction that affects the order book.
// var network = require("./network.js");
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Amount = require('./amount').Amount;
var UInt160 = require('./uint160').UInt160;
var Currency = require('./currency').Currency;
var extend = require('extend');
var OrderBook = function (remote,
currency_gets, issuer_gets,
currency_pays, issuer_pays) {
EventEmitter.call(this);
var self = this;
this._remote = remote;
this._currency_gets = currency_gets;
this._issuer_gets = issuer_gets;
this._currency_pays = currency_pays;
this._issuer_pays = issuer_pays;
this._subs = 0;
// We consider ourselves synchronized if we have a current copy of the offers,
// we are online and subscribed to updates.
this._sync = false;
// Offers
this._offers = [];
this.on('newListener', function (type, listener) {
if (OrderBook.subscribe_events.indexOf(type) !== -1) {
if (!self._subs && 'open' === self._remote._online_state) {
self._subscribe();
}
self._subs += 1;
}
});
this.on('removeListener', function (type, listener) {
if (OrderBook.subscribe_events.indexOf(type) !== -1) {
self._subs -= 1;
if (!self._subs && 'open' === self._remote._online_state) {
self._sync = false;
self._remote.request_unsubscribe()
.books([self.to_json()])
.request();
}
}
});
this._remote.on('connect', function () {
if (self._subs) {
self._subscribe();
}
});
this._remote.on('disconnect', function () {
self._sync = false;
});
return this;
};
util.inherits(OrderBook, EventEmitter);
/**
* List of events that require a remote subscription to the orderbook.
*/
OrderBook.subscribe_events = ['transaction', 'model', 'trade'];
/**
* Subscribes to orderbook.
*
* @private
*/
OrderBook.prototype._subscribe = function ()
{
var self = this;
self._remote.request_subscribe()
.books([self.to_json()], true)
.on('error', function () {
// XXX What now?
})
.on('success', function (res) {
self._sync = true;
self._offers = res.offers;
self.emit('model', self._offers);
})
.request();
};
OrderBook.prototype.to_json = function ()
{
var json = {
"taker_gets": {
"currency": this._currency_gets
},
"taker_pays": {
"currency": this._currency_pays
}
};
if (this._currency_gets !== "XRP") json["taker_gets"]["issuer"] = this._issuer_gets;
if (this._currency_pays !== "XRP") json["taker_pays"]["issuer"] = this._issuer_pays;
return json;
};
/**
* Whether the OrderBook is valid.
*
* Note: This only checks whether the parameters (currencies and issuer) are
* syntactically valid. It does not check anything against the ledger.
*/
OrderBook.prototype.is_valid = function ()
{
// XXX Should check for same currency (non-native) && same issuer
return (
Currency.is_valid(this._currency_pays) &&
(this._currency_pays === "XRP" || UInt160.is_valid(this._issuer_pays)) &&
Currency.is_valid(this._currency_gets) &&
(this._currency_gets === "XRP" || UInt160.is_valid(this._issuer_gets)) &&
!(this._currency_pays === "XRP" && this._currency_gets === "XRP")
);
};
/**
* Notify object of a relevant transaction.
*
* This is only meant to be called by the Remote class. You should never have to
* call this yourself.
*/
OrderBook.prototype.notifyTx = function (message)
{
var self = this;
var changed = false;
var trade_gets = Amount.from_json("0" + ((this._currency_gets === 'XRP') ? "" :
"/" + this._currency_gets +
"/" + this._issuer_gets));
var trade_pays = Amount.from_json("0" + ((this._currency_pays === 'XRP') ? "" :
"/" + this._currency_pays +
"/" + this._issuer_pays));
message.mmeta.each(function (an) {
if (an.entryType !== 'Offer') return;
var i, l, offer;
if (an.diffType === 'DeletedNode' ||
an.diffType === 'ModifiedNode') {
for (i = 0, l = self._offers.length; i < l; i++) {
offer = self._offers[i];
if (offer.index === an.ledgerIndex) {
if (an.diffType === 'DeletedNode') {
self._offers.splice(i, 1);
}
else extend(offer, an.fieldsFinal);
changed = true;
break;
}
}
// We don't want to count a OfferCancel as a trade
if (message.transaction.TransactionType === "OfferCancel") return;
trade_gets = trade_gets.add(an.fieldsPrev.TakerGets);
trade_pays = trade_pays.add(an.fieldsPrev.TakerPays);
if (an.diffType === 'ModifiedNode') {
trade_gets = trade_gets.subtract(an.fieldsFinal.TakerGets);
trade_pays = trade_pays.subtract(an.fieldsFinal.TakerPays);
}
} else if (an.diffType === 'CreatedNode') {
var price = Amount.from_json(an.fields.TakerPays).ratio_human(an.fields.TakerGets);
for (i = 0, l = self._offers.length; i < l; i++) {
offer = self._offers[i];
var priceItem = Amount.from_json(offer.TakerPays).ratio_human(offer.TakerGets);
if (price.compareTo(priceItem) <= 0) {
var obj = an.fields;
obj.index = an.ledgerIndex;
self._offers.splice(i, 0, an.fields);
changed = true;
break;
}
}
}
});
// Only trigger the event if the account object is actually
// subscribed - this prevents some weird phantom events from
// occurring.
if (this._subs) {
this.emit('transaction', message);
if (changed) this.emit('model', this._offers);
if (!trade_gets.is_zero()) this.emit('trade', trade_pays, trade_gets);
}
};
/**
* Get offers model asynchronously.
*
* This function takes a callback and calls it with an array containing the
* current set of offers in this order book.
*
* If the data is available immediately, the callback may be called synchronously.
*/
OrderBook.prototype.offers = function (callback)
{
var self = this;
if ("function" === typeof callback) {
if (this._sync) {
callback(this._offers);
} else {
this.once('model', function (offers) {
callback(offers);
});
}
}
return this;
};
/**
* Return latest known offers.
*
* Usually, this will just be an empty array if the order book hasn't been
* loaded yet. But this accessor may be convenient in some circumstances.
*/
OrderBook.prototype.offersSync = function ()
{
return this._offers;
};
exports.OrderBook = OrderBook;
// vim:sw=2:sts=2:ts=8:et

File diff suppressed because it is too large Load Diff

View File

@@ -1,116 +0,0 @@
//
// Seed support
//
var sjcl = require('../../build/sjcl');
var utils = require('./utils');
var jsbn = require('./jsbn');
var extend = require('extend');
var BigInteger = jsbn.BigInteger;
var Base = require('./base').Base,
UInt = require('./uint').UInt,
UInt256 = require('./uint256').UInt256,
KeyPair = require('./keypair').KeyPair;
var Seed = extend(function () {
// Internal form: NaN or BigInteger
this._curve = sjcl.ecc.curves['c256'];
this._value = NaN;
}, UInt);
Seed.width = 16;
Seed.prototype = extend({}, UInt.prototype);
Seed.prototype.constructor = Seed;
// value = NaN on error.
// One day this will support rfc1751 too.
Seed.prototype.parse_json = function (j) {
if ('string' === typeof j) {
if (!j.length) {
this._value = NaN;
// XXX Should actually always try and continue if it failed.
} else if (j[0] === "s") {
this._value = Base.decode_check(Base.VER_FAMILY_SEED, j);
} else if (j.length === 32) {
this._value = this.parse_hex(j);
// XXX Should also try 1751
} else {
this.parse_passphrase(j);
}
} else {
this._value = NaN;
}
return this;
};
Seed.prototype.parse_passphrase = function (j) {
if ("string" !== typeof j) {
throw new Error("Passphrase must be a string");
}
var hash = sjcl.hash.sha512.hash(sjcl.codec.utf8String.toBits(j));
var bits = sjcl.bitArray.bitSlice(hash, 0, 128);
this.parse_bits(bits);
return this;
};
Seed.prototype.to_json = function () {
if (!(this._value instanceof BigInteger))
return NaN;
var output = Base.encode_check(Base.VER_FAMILY_SEED, this.to_bytes());
return output;
};
function append_int(a, i) {
return [].concat(a, i >> 24, (i >> 16) & 0xff, (i >> 8) & 0xff, i & 0xff);
}
function firstHalfOfSHA512(bytes) {
return sjcl.bitArray.bitSlice(
sjcl.hash.sha512.hash(sjcl.codec.bytes.toBits(bytes)),
0, 256
);
}
function SHA256_RIPEMD160(bits) {
return sjcl.hash.ripemd160.hash(sjcl.hash.sha256.hash(bits));
}
Seed.prototype.get_key = function (account_id) {
if (!this.is_valid()) {
throw new Error("Cannot generate keys from invalid seed!");
}
// XXX Should loop over keys until we find the right one
var curve = this._curve;
var seq = 0;
var private_gen, public_gen, i = 0;
do {
private_gen = sjcl.bn.fromBits(firstHalfOfSHA512(append_int(this.to_bytes(), i)));
i++;
} while (!curve.r.greaterEquals(private_gen));
public_gen = curve.G.mult(private_gen);
var sec;
i = 0;
do {
sec = sjcl.bn.fromBits(firstHalfOfSHA512(append_int(append_int(public_gen.toBytesCompressed(), seq), i)));
i++;
} while (!curve.r.greaterEquals(sec));
sec = sec.add(private_gen).mod(curve.r);
return KeyPair.from_bn_secret(sec);
};
exports.Seed = Seed;

View File

@@ -1,144 +0,0 @@
var binformat = require('./binformat'),
sjcl = require('../../build/sjcl'),
extend = require('extend'),
stypes = require('./serializedtypes');
var UInt256 = require('./uint256').UInt256;
var SerializedObject = function () {
this.buffer = [];
this.pointer = 0;
};
SerializedObject.from_json = function (obj) {
var typedef;
var so = new SerializedObject();
// Create a copy of the object so we don't modify it
obj = extend({}, obj);
if ("number" === typeof obj.TransactionType) {
obj.TransactionType = SerializedObject.lookup_type_tx(obj.TransactionType);
if (!obj.TransactionType) {
throw new Error("Transaction type ID is invalid.");
}
}
if ("string" === typeof obj.TransactionType) {
typedef = binformat.tx[obj.TransactionType].slice();
obj.TransactionType = typedef.shift();
} else if ("undefined" !== typeof obj.LedgerEntryType) {
// XXX: TODO
throw new Error("Ledger entry binary format not yet implemented.");
} else throw new Error("Object to be serialized must contain either " +
"TransactionType or LedgerEntryType.");
so.serialize(typedef, obj);
return so;
};
SerializedObject.prototype.append = function (bytes) {
this.buffer = this.buffer.concat(bytes);
this.pointer += bytes.length;
};
SerializedObject.prototype.to_bits = function ()
{
return sjcl.codec.bytes.toBits(this.buffer);
};
SerializedObject.prototype.to_hex = function () {
return sjcl.codec.hex.fromBits(this.to_bits()).toUpperCase();
};
SerializedObject.prototype.serialize = function (typedef, obj)
{
// Ensure canonical order
typedef = SerializedObject._sort_typedef(typedef.slice());
// Serialize fields
for (var i = 0, l = typedef.length; i < l; i++) {
var spec = typedef[i];
this.serialize_field(spec, obj);
}
};
SerializedObject.prototype.signing_hash = function (prefix)
{
var sign_buffer = new SerializedObject();
stypes.Int32.serialize(sign_buffer, prefix);
sign_buffer.append(this.buffer);
return sign_buffer.hash_sha512_half();
};
SerializedObject.prototype.hash_sha512_half = function ()
{
var bits = sjcl.codec.bytes.toBits(this.buffer),
hash = sjcl.bitArray.bitSlice(sjcl.hash.sha512.hash(bits), 0, 256);
return UInt256.from_hex(sjcl.codec.hex.fromBits(hash));
};
SerializedObject.prototype.serialize_field = function (spec, obj)
{
spec = spec.slice();
var name = spec.shift(),
presence = spec.shift(),
field_id = spec.shift(),
Type = spec.shift();
if ("undefined" !== typeof obj[name]) {
//console.log(name, Type.id, field_id);
this.append(SerializedObject.get_field_header(Type.id, field_id));
try {
Type.serialize(this, obj[name]);
} catch (e) {
// Add field name to message and rethrow
e.message = "Error serializing '"+name+"': "+e.message;
throw e;
}
} else if (presence === binformat.REQUIRED) {
throw new Error('Missing required field '+name);
}
};
SerializedObject.get_field_header = function (type_id, field_id)
{
var buffer = [0];
if (type_id > 0xf) buffer.push(type_id & 0xff);
else buffer[0] += (type_id & 0xf) << 4;
if (field_id > 0xf) buffer.push(field_id & 0xff);
else buffer[0] += field_id & 0xf;
return buffer;
};
function sort_field_compare(a, b) {
// Sort by type id first, then by field id
return a[3].id !== b[3].id ?
a[3].id - b[3].id :
a[2] - b[2];
};
SerializedObject._sort_typedef = function (typedef) {
return typedef.sort(sort_field_compare);
};
SerializedObject.lookup_type_tx = function (id) {
for (var i in binformat.tx) {
if (!binformat.tx.hasOwnProperty(i)) continue;
if (binformat.tx[i][0] === id) {
return i;
}
}
return null;
};
exports.SerializedObject = SerializedObject;

View File

@@ -1,319 +0,0 @@
/**
* Type definitions for binary format.
*
* This file should not be included directly. Instead, find the format you're
* trying to parse or serialize in binformat.js and pass that to
* SerializedObject.parse() or SerializedObject.serialize().
*/
var extend = require('extend'),
utils = require('./utils'),
sjcl = require('../../build/sjcl');
var amount = require('./amount'),
UInt160 = amount.UInt160,
Amount = amount.Amount,
Currency= amount.Currency;
// Shortcuts
var hex = sjcl.codec.hex,
bytes = sjcl.codec.bytes;
var SerializedType = function (methods) {
extend(this, methods);
};
SerializedType.prototype.serialize_hex = function (so, hexData) {
var byteData = bytes.fromBits(hex.toBits(hexData));
this.serialize_varint(so, byteData.length);
so.append(byteData);
};
SerializedType.prototype.serialize_varint = function (so, val) {
if (val < 0) {
throw new Error("Variable integers are unsigned.");
}
if (val <= 192) {
so.append([val]);
} else if (val <= 12,480) {
val -= 193;
so.append([193 + (val >>> 8), val & 0xff]);
} else if (val <= 918744) {
val -= 12481;
so.append([
241 + (val >>> 16),
val >>> 8 & 0xff,
val & 0xff
]);
} else throw new Error("Variable integer overflow.");
};
var STInt8 = exports.Int8 = new SerializedType({
serialize: function (so, val) {
so.append([val & 0xff]);
},
parse: function (so) {
return so.read(1)[0];
}
});
var STInt16 = exports.Int16 = new SerializedType({
serialize: function (so, val) {
so.append([
val >>> 8 & 0xff,
val & 0xff
]);
},
parse: function (so) {
// XXX
throw new Error("Parsing Int16 not implemented");
}
});
var STInt32 = exports.Int32 = new SerializedType({
serialize: function (so, val) {
so.append([
val >>> 24 & 0xff,
val >>> 16 & 0xff,
val >>> 8 & 0xff,
val & 0xff
]);
},
parse: function (so) {
// XXX
throw new Error("Parsing Int32 not implemented");
}
});
var STInt64 = exports.Int64 = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Int64 not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Int64 not implemented");
}
});
var STHash128 = exports.Hash128 = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Hash128 not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Hash128 not implemented");
}
});
var STHash256 = exports.Hash256 = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Hash256 not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Hash256 not implemented");
}
});
var STHash160 = exports.Hash160 = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Hash160 not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Hash160 not implemented");
}
});
// Internal
var STCurrency = new SerializedType({
serialize: function (so, val) {
var currency = val.to_json();
if ("string" === typeof currency && currency.length === 3) {
var currencyCode = currency.toUpperCase(),
currencyData = utils.arraySet(20, 0);
if (!/^[A-Z]{3}$/.test(currencyCode)) {
throw new Error('Invalid currency code');
}
currencyData[12] = currencyCode.charCodeAt(0) & 0xff;
currencyData[13] = currencyCode.charCodeAt(1) & 0xff;
currencyData[14] = currencyCode.charCodeAt(2) & 0xff;
so.append(currencyData);
} else {
throw new Error('Tried to serialize invalid/unimplemented currency type.');
}
},
parse: function (so) {
// XXX
throw new Error("Parsing Currency not implemented");
}
});
var STAmount = exports.Amount = new SerializedType({
serialize: function (so, val) {
var amount = Amount.from_json(val);
if (!amount.is_valid()) {
throw new Error("Not a valid Amount object.");
}
// Amount (64-bit integer)
var valueBytes = utils.arraySet(8, 0);
if (amount.is_native()) {
var valueHex = amount._value.toString(16);
// Enforce correct length (64 bits)
if (valueHex.length > 16) {
throw new Error('Value out of bounds');
}
while (valueHex.length < 16) {
valueHex = "0" + valueHex;
}
valueBytes = bytes.fromBits(hex.toBits(valueHex));
// Clear most significant two bits - these bits should already be 0 if
// Amount enforces the range correctly, but we'll clear them anyway just
// so this code can make certain guarantees about the encoded value.
valueBytes[0] &= 0x3f;
if (!amount.is_negative()) valueBytes[0] |= 0x40;
} else {
var hi = 0, lo = 0;
// First bit: non-native
hi |= 1 << 31;
if (!amount.is_zero()) {
// Second bit: non-negative?
if (!amount.is_negative()) hi |= 1 << 30;
// Next eight bits: offset/exponent
hi |= ((97 + amount._offset) & 0xff) << 22;
// Remaining 52 bits: mantissa
hi |= amount._value.shiftRight(32).intValue() & 0x3fffff;
lo = amount._value.intValue() & 0xffffffff;
}
valueBytes = sjcl.codec.bytes.fromBits([hi, lo]);
}
so.append(valueBytes);
if (!amount.is_native()) {
// Currency (160-bit hash)
var currency = amount.currency();
STCurrency.serialize(so, currency);
// Issuer (160-bit hash)
so.append(amount.issuer().to_bytes());
}
},
parse: function (so) {
// XXX
throw new Error("Parsing Amount not implemented");
}
});
var STVL = exports.VariableLength = new SerializedType({
serialize: function (so, val) {
if ("string" === typeof val) this.serialize_hex(so, val);
else throw new Error("Unknown datatype.");
},
parse: function (so) {
// XXX
throw new Error("Parsing VL not implemented");
}
});
var STAccount = exports.Account = new SerializedType({
serialize: function (so, val) {
var account = UInt160.from_json(val);
this.serialize_hex(so, account.to_hex());
},
parse: function (so) {
// XXX
throw new Error("Parsing Account not implemented");
}
});
var STPathSet = exports.PathSet = new SerializedType({
typeBoundary: 0xff,
typeEnd: 0x00,
typeAccount: 0x01,
typeCurrency: 0x10,
typeIssuer: 0x20,
serialize: function (so, val) {
// XXX
for (var i = 0, l = val.length; i < l; i++) {
// Boundary
if (i) STInt8.serialize(so, this.typeBoundary);
for (var j = 0, l2 = val[i].length; j < l2; j++) {
var entry = val[i][j];
var type = 0;
if (entry.account) type |= this.typeAccount;
if (entry.currency) type |= this.typeCurrency;
if (entry.issuer) type |= this.typeIssuer;
STInt8.serialize(so, type);
if (entry.account) {
so.append(UInt160.from_json(entry.account).to_bytes());
}
if (entry.currency) {
var currency = Currency.from_json(entry.currency);
STCurrency.serialize(so, currency);
}
if (entry.issuer) {
so.append(UInt160.from_json(entry.issuer).to_bytes());
}
}
}
STInt8.serialize(so, this.typeEnd);
},
parse: function (so) {
// XXX
throw new Error("Parsing PathSet not implemented");
}
});
var STVector256 = exports.Vector256 = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Vector256 not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Vector256 not implemented");
}
});
var STObject = exports.Object = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Object not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Object not implemented");
}
});
var STArray = exports.Array = new SerializedType({
serialize: function (so, val) {
// XXX
throw new Error("Serializing Array not implemented");
},
parse: function (so) {
// XXX
throw new Error("Parsing Array not implemented");
}
});

View File

@@ -1,255 +0,0 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var utils = require('./utils');
var Server = function (remote, cfg)
{
EventEmitter.call(this);
if ("object" !== typeof cfg || "string" !== typeof cfg.url) {
throw new Error("Invalid server configuration.");
}
this._remote = remote;
this._cfg = cfg;
this._ws = null;
this._connected = false;
this._should_connect = false;
this._state = null;
this._id = 0;
this._retry = 0;
this._requests = {};
this.on('message', this._handle_message.bind(this));
this.on('response_subscribe', this._handle_response_subscribe.bind(this));
};
util.inherits(Server, EventEmitter);
/**
* Server states that we will treat as the server being online.
*
* Our requirements are that the server can process transactions and notify
* us of changes.
*/
Server.online_states = [
'proposing',
'validating',
'full'
];
Server.prototype.connect = function ()
{
var self = this;
// We don't connect if we believe we're already connected. This means we have
// recently received a message from the server and the WebSocket has not
// reported any issues either. If we do fail to ping or the connection drops,
// we will automatically reconnect.
if (this._connected === true) return;
if (this._remote.trace) console.log("server: connect: %s", this._cfg.url);
// Ensure any existing socket is given the command to close first.
if (this._ws) this._ws.close();
var WebSocket = require('ws');
var ws = this._ws = new WebSocket(this._cfg.url);
this._should_connect = true;
self.emit('connecting');
ws.onopen = function () {
// If we are no longer the active socket, simply ignore any event
if (ws !== self._ws) return;
self.emit('socket_open');
// Subscribe to events
var request = self._remote._server_prepare_subscribe();
self.request(request);
};
ws.onerror = function (e) {
// If we are no longer the active socket, simply ignore any event
if (ws !== self._ws) return;
if (self._remote.trace) console.log("server: onerror: %s", e.data || e);
// Most connection errors for WebSockets are conveyed as "close" events with
// code 1006. This is done for security purposes and therefore unlikely to
// ever change.
// This means that this handler is hardly ever called in practice. If it is,
// it probably means the server's WebSocket implementation is corrupt, or
// the connection is somehow producing corrupt data.
// Most WebSocket applications simply log and ignore this error. Once we
// support for multiple servers, we may consider doing something like
// lowering this server's quality score.
// However, in Node.js this event may be triggered instead of the close
// event, so we need to handle it.
handleConnectionClose();
};
// Failure to open.
ws.onclose = function () {
// If we are no longer the active socket, simply ignore any event
if (ws !== self._ws) return;
if (self._remote.trace) console.log("server: onclose: %s", ws.readyState);
handleConnectionClose();
};
function handleConnectionClose()
{
self.emit('socket_close');
self._set_state('offline');
// Prevent additional events from this socket
ws.onopen = ws.onerror = ws.onclose = ws.onmessage = function () {};
// Should we be connected?
if (!self._should_connect) return;
// Delay and retry.
self._retry += 1;
self._retry_timer = setTimeout(function () {
if (self._remote.trace) console.log("server: retry");
if (!self._should_connect) return;
self.connect();
}, self._retry < 40
? 1000/20 // First, for 2 seconds: 20 times per second
: self._retry < 40+60
? 1000 // Then, for 1 minute: once per second
: self._retry < 40+60+60
? 10*1000 // Then, for 10 minutes: once every 10 seconds
: 30*1000); // Then: once every 30 seconds
}
ws.onmessage = function (msg) {
self.emit('message', msg.data);
};
};
Server.prototype.disconnect = function ()
{
this._should_connect = false;
this._set_state('offline');
if (this.ws) {
this.ws.close();
}
};
/**
* Submit a Request object to this server.
*/
Server.prototype.request = function (request)
{
var self = this;
// Only bother if we are still connected.
if (self._ws) {
request.message.id = self._id;
self._requests[request.message.id] = request;
// Advance message ID
self._id++;
if (self._state === "online" ||
(request.message.command === "subscribe" && self._ws.readyState === 1)) {
if (self._remote.trace) {
utils.logObject("server: request: %s", request.message);
}
self._ws.send(JSON.stringify(request.message));
} else {
// XXX There are many ways to make self smarter.
self.once('connect', function () {
if (self._remote.trace) {
utils.logObject("server: request: %s", request.message);
}
self._ws.send(JSON.stringify(request.message));
});
}
} else {
if (self._remote.trace) {
utils.logObject("server: request: DROPPING: %s", request.message);
}
}
};
Server.prototype._set_state = function (state) {
if (state !== this._state) {
this._state = state;
this.emit('state', state);
if (state === 'online') {
this.emit('connect');
} else if (state === 'offline') {
this.emit('disconnect');
}
}
};
Server.prototype._handle_message = function (json) {
var self = this;
var message = JSON.parse(json);
if (message.type === 'response') {
// A response to a request.
var request = self._requests[message.id];
delete self._requests[message.id];
if (!request) {
if (self._remote.trace) utils.logObject("server: UNEXPECTED: %s", message);
} else if ('success' === message.status) {
if (self._remote.trace) utils.logObject("server: response: %s", message);
request.emit('success', message.result);
self.emit('response_'+request.message.command, message.result, request, message);
self._remote.emit('response_'+request.message.command, message.result, request, message);
} else if (message.error) {
if (self._remote.trace) utils.logObject("server: error: %s", message);
request.emit('error', {
'error' : 'remoteError',
'error_message' : 'Remote reported an error.',
'remote' : message
});
}
} else if (message.type === 'serverStatus') {
// This message is only received when online. As we are connected, it is the definative final state.
self._set_state(
Server.online_states.indexOf(message.server_status) !== -1
? 'online'
: 'offline');
}
};
Server.prototype._handle_response_subscribe = function (message)
{
var self = this;
self._server_status = message.server_status;
if (Server.online_states.indexOf(message.server_status) !== -1) {
self._set_state('online');
}
};
exports.Server = Server;
// vim:sw=2:sts=2:ts=8:et

Submodule src/js/sjcl deleted from dbdef434e7

View File

@@ -1,34 +0,0 @@
sjcl.ecc.ecdsa.secretKey.prototype.signDER = function(hash, paranoia) {
return this.encodeDER(this.sign(hash, paranoia));
};
sjcl.ecc.ecdsa.secretKey.prototype.encodeDER = function(rs) {
var w = sjcl.bitArray,
R = this._curve.r,
l = R.bitLength();
var rb = sjcl.codec.bytes.fromBits(w.bitSlice(rs,0,l)),
sb = sjcl.codec.bytes.fromBits(w.bitSlice(rs,l,2*l));
// Drop empty leading bytes
while (!rb[0] && rb.length) rb.shift();
while (!sb[0] && sb.length) sb.shift();
// If high bit is set, prepend an extra zero byte (DER signed integer)
if (rb[0] & 0x80) rb.unshift(0);
if (sb[0] & 0x80) sb.unshift(0);
var buffer = [].concat(
0x30,
4 + rb.length + sb.length,
0x02,
rb.length,
rb,
0x02,
sb.length,
sb
);
return sjcl.codec.bytes.toBits(buffer);
};

View File

@@ -1,61 +0,0 @@
sjcl.bn.ZERO = new sjcl.bn(0);
/** [ this / that , this % that ] */
sjcl.bn.prototype.divRem = function (that) {
if (typeof(that) !== "object") { that = new this._class(that); }
var thisa = this.abs(), thata = that.abs(), quot = new this._class(0),
ci = 0;
if (!thisa.greaterEquals(thata)) {
this.initWith(0);
return this;
} else if (thisa.equals(thata)) {
this.initWith(1);
return this;
}
for (; thisa.greaterEquals(thata); ci++) {
thata.doubleM();
}
for (; ci > 0; ci--) {
quot.doubleM();
thata.halveM();
if (thisa.greaterEquals(thata)) {
quot.addM(1);
thisa.subM(that).normalize();
}
}
return [quot, thisa];
};
/** this /= that (rounded to nearest int) */
sjcl.bn.prototype.divRound = function (that) {
var dr = this.divRem(that), quot = dr[0], rem = dr[1];
if (rem.doubleM().greaterEquals(that)) {
quot.addM(1);
}
return quot;
};
/** this /= that (rounded down) */
sjcl.bn.prototype.div = function (that) {
var dr = this.divRem(that);
return dr[0];
};
sjcl.bn.prototype.sign = function () {
return this.greaterEquals(sjcl.bn.ZERO) ? 1 : -1;
};
/** -this */
sjcl.bn.prototype.neg = function () {
return sjcl.bn.ZERO.sub(this);
};
/** |this| */
sjcl.bn.prototype.abs = function () {
if (this.sign() === -1) {
return this.neg();
} else return this;
};

View File

@@ -1,207 +0,0 @@
/** @fileOverview Javascript RIPEMD-160 implementation.
*
* @author Artem S Vybornov <vybornov@gmail.com>
*/
(function() {
/**
* Context for a RIPEMD-160 operation in progress.
* @constructor
* @class RIPEMD, 160 bits.
*/
sjcl.hash.ripemd160 = function (hash) {
if (hash) {
this._h = hash._h.slice(0);
this._buffer = hash._buffer.slice(0);
this._length = hash._length;
} else {
this.reset();
}
};
/**
* Hash a string or an array of words.
* @static
* @param {bitArray|String} data the data to hash.
* @return {bitArray} The hash value, an array of 5 big-endian words.
*/
sjcl.hash.ripemd160.hash = function (data) {
return (new sjcl.hash.ripemd160()).update(data).finalize();
};
sjcl.hash.ripemd160.prototype = {
/**
* Reset the hash state.
* @return this
*/
reset: function () {
this._h = _h0.slice(0);
this._buffer = [];
this._length = 0;
return this;
},
/**
* Reset the hash state.
* @param {bitArray|String} data the data to hash.
* @return this
*/
update: function (data) {
if ( typeof data === "string" )
data = sjcl.codec.utf8String.toBits(data);
var i, b = this._buffer = sjcl.bitArray.concat(this._buffer, data),
ol = this._length,
nl = this._length = ol + sjcl.bitArray.bitLength(data);
for (i = 512+ol & -512; i <= nl; i+= 512) {
var words = b.splice(0,16);
for ( var w = 0; w < 16; ++w )
words[w] = _cvt(words[w]);
_block.call( this, words );
}
return this;
},
/**
* Complete hashing and output the hash value.
* @return {bitArray} The hash value, an array of 5 big-endian words.
*/
finalize: function () {
var b = sjcl.bitArray.concat( this._buffer, [ sjcl.bitArray.partial(1,1) ] ),
l = ( this._length + 1 ) % 512,
z = ( l > 448 ? 512 : 448 ) - l % 448,
zp = z % 32;
if ( zp > 0 )
b = sjcl.bitArray.concat( b, [ sjcl.bitArray.partial(zp,0) ] )
for ( ; z >= 32; z -= 32 )
b.push(0);
b.push( _cvt( this._length | 0 ) );
b.push( _cvt( Math.floor(this._length / 0x100000000) ) );
while ( b.length ) {
var words = b.splice(0,16);
for ( var w = 0; w < 16; ++w )
words[w] = _cvt(words[w]);
_block.call( this, words );
}
var h = this._h;
this.reset();
for ( var w = 0; w < 5; ++w )
h[w] = _cvt(h[w]);
return h;
}
};
var _h0 = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ];
var _k1 = [ 0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e ];
var _k2 = [ 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000 ];
for ( var i = 4; i >= 0; --i ) {
for ( var j = 1; j < 16; ++j ) {
_k1.splice(i,0,_k1[i]);
_k2.splice(i,0,_k2[i]);
}
}
var _r1 = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 ];
var _r2 = [ 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 ];
var _s1 = [ 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 ];
var _s2 = [ 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ];
function _f0(x,y,z) {
return x ^ y ^ z;
};
function _f1(x,y,z) {
return (x & y) | (~x & z);
};
function _f2(x,y,z) {
return (x | ~y) ^ z;
};
function _f3(x,y,z) {
return (x & z) | (y & ~z);
};
function _f4(x,y,z) {
return x ^ (y | ~z);
};
function _rol(n,l) {
return (n << l) | (n >>> (32-l));
}
function _cvt(n) {
return ( (n & 0xff << 0) << 24 )
| ( (n & 0xff << 8) << 8 )
| ( (n & 0xff << 16) >>> 8 )
| ( (n & 0xff << 24) >>> 24 );
}
function _block(X) {
var A1 = this._h[0], B1 = this._h[1], C1 = this._h[2], D1 = this._h[3], E1 = this._h[4],
A2 = this._h[0], B2 = this._h[1], C2 = this._h[2], D2 = this._h[3], E2 = this._h[4];
var j = 0, T;
for ( ; j < 16; ++j ) {
T = _rol( A1 + _f0(B1,C1,D1) + X[_r1[j]] + _k1[j], _s1[j] ) + E1;
A1 = E1; E1 = D1; D1 = _rol(C1,10); C1 = B1; B1 = T;
T = _rol( A2 + _f4(B2,C2,D2) + X[_r2[j]] + _k2[j], _s2[j] ) + E2;
A2 = E2; E2 = D2; D2 = _rol(C2,10); C2 = B2; B2 = T; }
for ( ; j < 32; ++j ) {
T = _rol( A1 + _f1(B1,C1,D1) + X[_r1[j]] + _k1[j], _s1[j] ) + E1;
A1 = E1; E1 = D1; D1 = _rol(C1,10); C1 = B1; B1 = T;
T = _rol( A2 + _f3(B2,C2,D2) + X[_r2[j]] + _k2[j], _s2[j] ) + E2;
A2 = E2; E2 = D2; D2 = _rol(C2,10); C2 = B2; B2 = T; }
for ( ; j < 48; ++j ) {
T = _rol( A1 + _f2(B1,C1,D1) + X[_r1[j]] + _k1[j], _s1[j] ) + E1;
A1 = E1; E1 = D1; D1 = _rol(C1,10); C1 = B1; B1 = T;
T = _rol( A2 + _f2(B2,C2,D2) + X[_r2[j]] + _k2[j], _s2[j] ) + E2;
A2 = E2; E2 = D2; D2 = _rol(C2,10); C2 = B2; B2 = T; }
for ( ; j < 64; ++j ) {
T = _rol( A1 + _f3(B1,C1,D1) + X[_r1[j]] + _k1[j], _s1[j] ) + E1;
A1 = E1; E1 = D1; D1 = _rol(C1,10); C1 = B1; B1 = T;
T = _rol( A2 + _f1(B2,C2,D2) + X[_r2[j]] + _k2[j], _s2[j] ) + E2;
A2 = E2; E2 = D2; D2 = _rol(C2,10); C2 = B2; B2 = T; }
for ( ; j < 80; ++j ) {
T = _rol( A1 + _f4(B1,C1,D1) + X[_r1[j]] + _k1[j], _s1[j] ) + E1;
A1 = E1; E1 = D1; D1 = _rol(C1,10); C1 = B1; B1 = T;
T = _rol( A2 + _f0(B2,C2,D2) + X[_r2[j]] + _k2[j], _s2[j] ) + E2;
A2 = E2; E2 = D2; D2 = _rol(C2,10); C2 = B2; B2 = T; }
T = this._h[1] + C1 + D2;
this._h[1] = this._h[2] + D1 + E2;
this._h[2] = this._h[3] + E1 + A2;
this._h[3] = this._h[4] + A1 + B2;
this._h[4] = this._h[0] + B1 + C2;
this._h[0] = T;
}
})();

View File

@@ -1,72 +0,0 @@
// ----- for secp256k1 ------
// Overwrite NIST-P256 with secp256k1 so we're on even footing
sjcl.ecc.curves.c256 = new sjcl.ecc.curve(
sjcl.bn.pseudoMersennePrime(256, [[0,-1],[4,-1],[6,-1],[7,-1],[8,-1],[9,-1],[32,-1]]),
"0x14551231950b75fc4402da1722fc9baee",
0,
7,
"0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
"0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"
);
// Replace point addition and doubling algorithms
// NIST-P256 is a=-3, we need algorithms for a=0
sjcl.ecc.pointJac.prototype.add = function(T) {
var S = this;
if (S.curve !== T.curve) {
throw("sjcl.ecc.add(): Points must be on the same curve to add them!");
}
if (S.isIdentity) {
return T.toJac();
} else if (T.isIdentity) {
return S;
}
var z1z1 = S.z.square();
var h = T.x.mul(z1z1).subM(S.x);
var s2 = T.y.mul(S.z).mul(z1z1);
if (h.equals(0)) {
if (S.y.equals(T.y.mul(z1z1.mul(S.z)))) {
// same point
return S.doubl();
} else {
// inverses
return new sjcl.ecc.pointJac(S.curve);
}
}
var hh = h.square();
var i = hh.copy().doubleM().doubleM();
var j = h.mul(i);
var r = s2.sub(S.y).doubleM();
var v = S.x.mul(i);
var x = r.square().subM(j).subM(v.copy().doubleM());
var y = r.mul(v.sub(x)).subM(S.y.mul(j).doubleM());
var z = S.z.add(h).square().subM(z1z1).subM(hh);
return new sjcl.ecc.pointJac(this.curve,x,y,z);
};
sjcl.ecc.pointJac.prototype.doubl = function () {
if (this.isIdentity) { return this; }
var a = this.x.square();
var b = this.y.square();
var c = b.square();
var d = this.x.add(b).square().subM(a).subM(c).doubleM();
var e = a.mul(3);
var f = e.square();
var x = f.sub(d.copy().doubleM());
var y = e.mul(d.sub(x)).subM(c.doubleM().doubleM().doubleM());
var z = this.y.mul(this.z).doubleM();
return new sjcl.ecc.pointJac(this.curve, x, y, z);
};
sjcl.ecc.point.prototype.toBytesCompressed = function () {
var header = this.y.mod(2).toString() == "0x0" ? 0x02 : 0x03;
return [header].concat(sjcl.codec.bytes.fromBits(this.x.toBits()))
};

View File

@@ -1,30 +0,0 @@
sjcl.ecc.ecdsa.secretKey.prototype = {
sign: function(hash, paranoia) {
var R = this._curve.r,
l = R.bitLength(),
k = sjcl.bn.random(R.sub(1), paranoia).add(1),
r = this._curve.G.mult(k).x.mod(R),
s = sjcl.bn.fromBits(hash).add(r.mul(this._exponent)).mul(k.inverseMod(R)).mod(R);
return sjcl.bitArray.concat(r.toBits(l), s.toBits(l));
}
};
sjcl.ecc.ecdsa.publicKey.prototype = {
verify: function(hash, rs) {
var w = sjcl.bitArray,
R = this._curve.r,
l = R.bitLength(),
r = sjcl.bn.fromBits(w.bitSlice(rs,0,l)),
s = sjcl.bn.fromBits(w.bitSlice(rs,l,2*l)),
sInv = s.modInverse(R),
hG = sjcl.bn.fromBits(hash).mul(sInv).mod(R),
hA = r.mul(sInv).mod(R),
r2 = this._curve.G.mult2(hG, hA, this._point).x;
if (r.equals(0) || s.equals(0) || r.greaterEquals(R) || s.greaterEquals(R) || !r2.equals(r)) {
throw (new sjcl.exception.corrupt("signature didn't check out"));
}
return true;
}
};

View File

@@ -1,680 +0,0 @@
//
// Transactions
//
// Construction:
// remote.transaction() // Build a transaction object.
// .offer_create(...) // Set major parameters.
// .set_flags() // Set optional parameters.
// .on() // Register for events.
// .submit(); // Send to network.
//
// Events:
// 'success' : Transaction submitted without error.
// 'error' : Error submitting transaction.
// 'proposed' : Advisory proposed status transaction.
// - A client should expect 0 to multiple results.
// - Might not get back. The remote might just forward the transaction.
// - A success could be reverted in final.
// - local error: other remotes might like it.
// - malformed error: local server thought it was malformed.
// - The client should only trust this when talking to a trusted server.
// 'final' : Final status of transaction.
// - Only expect a final from dishonest servers after a tesSUCCESS or ter*.
// 'lost' : Gave up looking for on ledger_closed.
// 'pending' : Transaction was not found on ledger_closed.
// 'state' : Follow the state of a transaction.
// 'client_submitted' - Sent to remote
// |- 'remoteError' - Remote rejected transaction.
// \- 'client_proposed' - Remote provisionally accepted transaction.
// |- 'client_missing' - Transaction has not appeared in ledger as expected.
// | |\- 'client_lost' - No longer monitoring missing transaction.
// |/
// |- 'tesSUCCESS' - Transaction in ledger as expected.
// |- 'ter...' - Transaction failed.
// \- 'tec...' - Transaction claimed fee only.
//
// Notes:
// - All transactions including those with local and malformed errors may be
// forwarded anyway.
// - A malicous server can:
// - give any proposed result.
// - it may declare something correct as incorrect or something correct as incorrect.
// - it may not communicate with the rest of the network.
// - may or may not forward.
//
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var sjcl = require('../../build/sjcl');
var Amount = require('./amount').Amount;
var Currency = require('./amount').Currency;
var UInt160 = require('./amount').UInt160;
var Seed = require('./seed').Seed;
var SerializedObject = require('./serializedobject').SerializedObject;
var config = require('./config');
var SUBMIT_MISSING = 4; // Report missing.
var SUBMIT_LOST = 8; // Give up tracking.
// A class to implement transactions.
// - Collects parameters
// - Allow event listeners to be attached to determine the outcome.
var Transaction = function (remote) {
EventEmitter.call(this);
// YYY Make private as many variables as possible.
var self = this;
this.callback = undefined;
this.remote = remote;
this._secret = undefined;
this._build_path = false;
this.tx_json = { // Transaction data.
'Flags' : 0, // XXX Would be nice if server did not require this.
};
this.hash = undefined;
this.submit_index = undefined; // ledger_current_index was this when transaction was submited.
this.state = undefined; // Under construction.
this.finalized = false;
this.on('success', function (message) {
if (message.engine_result) {
self.hash = message.tx_json.hash;
self.set_state('client_proposed');
self.emit('proposed', {
'tx_json' : message.tx_json,
'result' : message.engine_result,
'result_code' : message.engine_result_code,
'result_message' : message.engine_result_message,
'rejected' : self.isRejected(message.engine_result_code), // If server is honest, don't expect a final if rejected.
});
}
});
this.on('error', function (message) {
// Might want to give more detailed information.
self.set_state('remoteError');
});
};
util.inherits(Transaction, EventEmitter);
// XXX This needs to be determined from the network.
Transaction.fees = {
'default' : Amount.from_json("10"),
'nickname_create' : Amount.from_json("1000"),
'offer' : Amount.from_json("10"),
};
Transaction.flags = {
'OfferCreate' : {
'Passive' : 0x00010000,
'ImmediateOrCancel' : 0x00020000,
'FillOrKill' : 0x00040000,
'Sell' : 0x00080000,
},
'Payment' : {
'NoRippleDirect' : 0x00010000,
'PartialPayment' : 0x00020000,
'LimitQuality' : 0x00040000,
},
};
Transaction.formats = require('./binformat').tx;
Transaction.HASH_SIGN = 0x53545800;
Transaction.HASH_SIGN_TESTNET = 0x73747800;
Transaction.prototype.consts = {
'telLOCAL_ERROR' : -399,
'temMALFORMED' : -299,
'tefFAILURE' : -199,
'terRETRY' : -99,
'tesSUCCESS' : 0,
'tecCLAIMED' : 100,
};
Transaction.prototype.isTelLocal = function (ter) {
return ter >= this.consts.telLOCAL_ERROR && ter < this.consts.temMALFORMED;
};
Transaction.prototype.isTemMalformed = function (ter) {
return ter >= this.consts.temMALFORMED && ter < this.consts.tefFAILURE;
};
Transaction.prototype.isTefFailure = function (ter) {
return ter >= this.consts.tefFAILURE && ter < this.consts.terRETRY;
};
Transaction.prototype.isTerRetry = function (ter) {
return ter >= this.consts.terRETRY && ter < this.consts.tesSUCCESS;
};
Transaction.prototype.isTepSuccess = function (ter) {
return ter >= this.consts.tesSUCCESS;
};
Transaction.prototype.isTecClaimed = function (ter) {
return ter >= this.consts.tecCLAIMED;
};
Transaction.prototype.isRejected = function (ter) {
return this.isTelLocal(ter) || this.isTemMalformed(ter) || this.isTefFailure(ter);
};
Transaction.prototype.set_state = function (state) {
if (this.state !== state) {
this.state = state;
this.emit('state', state);
}
};
/**
* Attempts to complete the transaction for submission.
*
* This function seeks to fill out certain fields, such as Fee and
* SigningPubKey, which can be determined by the library based on network
* information and other fields.
*/
Transaction.prototype.complete = function () {
var tx_json = this.tx_json;
if (undefined === tx_json.Fee && this.remote.local_fee) {
tx_json.Fee = Transaction.fees['default'].to_json();
}
if (undefined === tx_json.SigningPubKey && (!this.remote || this.remote.local_signing)) {
var seed = Seed.from_json(this._secret);
var key = seed.get_key(this.tx_json.Account);
tx_json.SigningPubKey = key.to_hex_pub();
}
};
Transaction.prototype.serialize = function () {
return SerializedObject.from_json(this.tx_json);
};
Transaction.prototype.signing_hash = function () {
var prefix = config.testnet
? Transaction.HASH_SIGN_TESTNET
: Transaction.HASH_SIGN;
return SerializedObject.from_json(this.tx_json).signing_hash(prefix);
};
Transaction.prototype.sign = function () {
var seed = Seed.from_json(this._secret),
hash = this.signing_hash();
var key = seed.get_key(this.tx_json.Account),
sig = key.sign(hash, 0),
hex = sjcl.codec.hex.fromBits(sig).toUpperCase();
this.tx_json.TxnSignature = hex;
};
// Submit a transaction to the network.
// XXX Don't allow a submit without knowing ledger_index.
// XXX Have a network canSubmit(), post events for following.
// XXX Also give broader status for tracking through network disconnects.
// callback = function (status, info) {
// // status is final status. Only works under a ledger_accepting conditions.
// switch status:
// case 'tesSUCCESS': all is well.
// case 'tejSecretUnknown': unable to sign transaction - secret unknown
// case 'tejServerUntrusted': sending secret to untrusted server.
// case 'tejInvalidAccount': locally detected error.
// case 'tejLost': locally gave up looking
// default: some other TER
// }
Transaction.prototype.submit = function (callback) {
var self = this;
var tx_json = this.tx_json;
this.callback = callback;
if ('string' !== typeof tx_json.Account)
{
(this.callback || this.emit)('error', {
'error' : 'tejInvalidAccount',
'error_message' : 'Bad account.'
});
return this;
}
// YYY Might check paths for invalid accounts.
this.complete();
if (this.callback || this.listeners('final').length || this.listeners('lost').length || this.listeners('pending').length) {
// There are listeners for callback, 'final', 'lost', or 'pending' arrange to emit them.
this.submit_index = this.remote._ledger_current_index;
// When a ledger closes, look for the result.
var on_ledger_closed = function (message) {
var ledger_hash = message.ledger_hash;
var ledger_index = message.ledger_index;
var stop = false;
// XXX make sure self.hash is available.
self.remote.request_transaction_entry(self.hash)
.ledger_hash(ledger_hash)
.on('success', function (message) {
if (self.finalized) return;
self.set_state(message.metadata.TransactionResult);
self.remote.removeListener('ledger_closed', on_ledger_closed);
self.emit('final', message);
self.finalized = true;
if (self.callback)
self.callback(message.metadata.TransactionResult, message);
})
.on('error', function (message) {
if (self.finalized) return;
if ('remoteError' === message.error
&& 'transactionNotFound' === message.remote.error) {
if (self.submit_index + SUBMIT_LOST < ledger_index) {
self.set_state('client_lost'); // Gave up.
self.emit('lost');
if (self.callback)
self.callback('tejLost', message);
self.remote.removeListener('ledger_closed', on_ledger_closed);
self.emit('final', message);
self.finalized = true;
}
else if (self.submit_index + SUBMIT_MISSING < ledger_index) {
self.set_state('client_missing'); // We don't know what happened to transaction, still might find.
self.emit('pending');
}
else {
self.emit('pending');
}
}
// XXX Could log other unexpectedness.
})
.request();
};
this.remote.on('ledger_closed', on_ledger_closed);
if (this.callback) {
this.on('error', function (message) {
self.callback(message.error, message);
});
}
}
this.set_state('client_submitted');
if (self.remote.local_sequence && !self.tx_json.Sequence) {
self.tx_json.Sequence = this.remote.account_seq(self.tx_json.Account, 'ADVANCE');
// console.log("Sequence: %s", self.tx_json.Sequence);
if (!self.tx_json.Sequence) {
// Look in the last closed ledger.
this.remote.account_seq_cache(self.tx_json.Account, false)
.on('success_account_seq_cache', function () {
// Try again.
self.submit();
})
.on('error_account_seq_cache', function (message) {
// XXX Maybe be smarter about this. Don't want to trust an untrusted server for this seq number.
// Look in the current ledger.
self.remote.account_seq_cache(self.tx_json.Account, 'CURRENT')
.on('success_account_seq_cache', function () {
// Try again.
self.submit();
})
.on('error_account_seq_cache', function (message) {
// Forward errors.
self.emit('error', message);
})
.request();
})
.request();
return this;
}
// If the transaction fails we want to either undo incrementing the sequence
// or submit a noop transaction to consume the sequence remotely.
this.on('success', function (res) {
if (!res || "string" !== typeof res.engine_result) return;
switch (res.engine_result.slice(0, 3)) {
// Synchronous local error
case 'tej':
self.remote.account_seq(self.tx_json.Account, 'REWIND');
break;
// XXX: What do we do in case of ter?
case 'tel':
case 'tem':
case 'tef':
// XXX Once we have a transaction submission manager class, we can
// check if there are any other transactions pending. If there are,
// we should submit a dummy transaction to ensure those
// transactions are still valid.
//var noop = self.remote.transaction().account_set(self.tx_json.Account);
//noop.submit();
// XXX Hotfix. This only works if no other transactions are pending.
self.remote.account_seq(self.tx_json.Account, 'REWIND');
break;
}
});
}
// Prepare request
var request = this.remote.request_submit();
// Forward successes and errors.
request.on('success', function (message) {
self.emit('success', message);
});
request.on('error', function (message) {
self.emit('error', message);
});
if (!this._secret && !this.tx_json.Signature) {
this.emit('error', {
'result' : 'tejSecretUnknown',
'result_message' : "Could not sign transactions because we."
});
return this;
} else if (this.remote.local_signing) {
this.sign();
request.tx_blob(this.serialize().to_hex());
} else {
if (!this.remote.trusted) {
this.emit('error', {
'result' : 'tejServerUntrusted',
'result_message' : "Attempt to give a secret to an untrusted server."
});
return this;
}
request.secret(this._secret);
request.build_path(this._build_path);
request.tx_json(this.tx_json);
}
request.request();
return this;
}
//
// Set options for Transactions
//
// --> build: true, to have server blindly construct a path.
//
// "blindly" because the sender has no idea of the actual cost except that is must be less than send max.
Transaction.prototype.build_path = function (build) {
this._build_path = build;
return this;
}
// tag should be undefined or a 32 bit integer.
// YYY Add range checking for tag.
Transaction.prototype.destination_tag = function (tag) {
if (undefined !== tag)
this.tx_json.DestinationTag = tag;
return this;
}
Transaction._path_rewrite = function (path) {
var path_new = [];
for (var i = 0, l = path.length; i < l; i++) {
var node = path[i];
var node_new = {};
if ('account' in node)
node_new.account = UInt160.json_rewrite(node.account);
if ('issuer' in node)
node_new.issuer = UInt160.json_rewrite(node.issuer);
if ('currency' in node)
node_new.currency = Currency.json_rewrite(node.currency);
path_new.push(node_new);
}
return path_new;
}
Transaction.prototype.path_add = function (path) {
this.tx_json.Paths = this.tx_json.Paths || [];
this.tx_json.Paths.push(Transaction._path_rewrite(path));
return this;
}
// --> paths: undefined or array of path
// A path is an array of objects containing some combination of: account, currency, issuer
Transaction.prototype.paths = function (paths) {
for (var i = 0, l = paths.length; i < l; i++) {
this.path_add(paths[i]);
}
return this;
}
// If the secret is in the config object, it does not need to be provided.
Transaction.prototype.secret = function (secret) {
this._secret = secret;
}
Transaction.prototype.send_max = function (send_max) {
if (send_max)
this.tx_json.SendMax = Amount.json_rewrite(send_max);
return this;
}
// tag should be undefined or a 32 bit integer.
// YYY Add range checking for tag.
Transaction.prototype.source_tag = function (tag) {
if (undefined !== tag)
this.tx_json.SourceTag = tag;
return this;
}
// --> rate: In billionths.
Transaction.prototype.transfer_rate = function (rate) {
this.tx_json.TransferRate = Number(rate);
if (this.tx_json.TransferRate < 1e9)
throw 'invalidTransferRate';
return this;
}
// Add flags to a transaction.
// --> flags: undefined, _flag_, or [ _flags_ ]
Transaction.prototype.set_flags = function (flags) {
if (flags) {
var transaction_flags = Transaction.flags[this.tx_json.TransactionType];
if (undefined == this.tx_json.Flags) // We plan to not define this field on new Transaction.
this.tx_json.Flags = 0;
var flag_set = 'object' === typeof flags ? flags : [ flags ];
for (var index in flag_set) {
if (!flag_set.hasOwnProperty(index)) continue;
var flag = flag_set[index];
if (flag in transaction_flags) {
this.tx_json.Flags += transaction_flags[flag];
} else {
// XXX Immediately report an error or mark it.
}
}
}
return this;
}
//
// Transactions
//
Transaction.prototype._account_secret = function (account) {
// Fill in secret from remote, if available.
return this.remote.secrets[account];
};
// Options:
// .domain() NYI
// .message_key() NYI
// .transfer_rate()
// .wallet_locator() NYI
// .wallet_size() NYI
Transaction.prototype.account_set = function (src) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'AccountSet';
this.tx_json.Account = UInt160.json_rewrite(src);
return this;
};
Transaction.prototype.claim = function (src, generator, public_key, signature) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'Claim';
this.tx_json.Generator = generator;
this.tx_json.PublicKey = public_key;
this.tx_json.Signature = signature;
return this;
};
Transaction.prototype.offer_cancel = function (src, sequence) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'OfferCancel';
this.tx_json.Account = UInt160.json_rewrite(src);
this.tx_json.OfferSequence = Number(sequence);
return this;
};
// Options:
// .set_flags()
// --> expiration : Date or Number
Transaction.prototype.offer_create = function (src, taker_pays, taker_gets, expiration) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'OfferCreate';
this.tx_json.Account = UInt160.json_rewrite(src);
this.tx_json.TakerPays = Amount.json_rewrite(taker_pays);
this.tx_json.TakerGets = Amount.json_rewrite(taker_gets);
if (this.remote.local_fee) {
this.tx_json.Fee = Transaction.fees.offer.to_json();
}
if (expiration)
this.tx_json.Expiration = Date === expiration.constructor
? expiration.getTime()
: Number(expiration);
return this;
};
Transaction.prototype.password_fund = function (src, dst) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'PasswordFund';
this.tx_json.Destination = UInt160.json_rewrite(dst);
return this;
}
Transaction.prototype.password_set = function (src, authorized_key, generator, public_key, signature) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'PasswordSet';
this.tx_json.RegularKey = authorized_key;
this.tx_json.Generator = generator;
this.tx_json.PublicKey = public_key;
this.tx_json.Signature = signature;
return this;
}
// Construct a 'payment' transaction.
//
// When a transaction is submitted:
// - If the connection is reliable and the server is not merely forwarding and is not malicious,
// --> src : UInt160 or String
// --> dst : UInt160 or String
// --> deliver_amount : Amount or String.
//
// Options:
// .paths()
// .build_path()
// .destination_tag()
// .path_add()
// .secret()
// .send_max()
// .set_flags()
// .source_tag()
Transaction.prototype.payment = function (src, dst, deliver_amount) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'Payment';
this.tx_json.Account = UInt160.json_rewrite(src);
this.tx_json.Amount = Amount.json_rewrite(deliver_amount);
this.tx_json.Destination = UInt160.json_rewrite(dst);
return this;
}
Transaction.prototype.ripple_line_set = function (src, limit, quality_in, quality_out) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'TrustSet';
this.tx_json.Account = UInt160.json_rewrite(src);
// Allow limit of 0 through.
if (undefined !== limit)
this.tx_json.LimitAmount = Amount.json_rewrite(limit);
if (quality_in)
this.tx_json.QualityIn = quality_in;
if (quality_out)
this.tx_json.QualityOut = quality_out;
// XXX Throw an error if nothing is set.
return this;
};
Transaction.prototype.wallet_add = function (src, amount, authorized_key, public_key, signature) {
this._secret = this._account_secret(src);
this.tx_json.TransactionType = 'WalletAdd';
this.tx_json.Amount = Amount.json_rewrite(amount);
this.tx_json.RegularKey = authorized_key;
this.tx_json.PublicKey = public_key;
this.tx_json.Signature = signature;
return this;
};
exports.Transaction = Transaction;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,227 +0,0 @@
var sjcl = require('../../build/sjcl');
var utils = require('./utils');
var config = require('./config');
var jsbn = require('./jsbn');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var Base = require('./base').Base;
//
// Abstract UInt class
//
// Base class for UInt??? classes
//
var UInt = function () {
// Internal form: NaN or BigInteger
this._value = NaN;
};
UInt.json_rewrite = function (j, opts) {
return this.from_json(j).to_json(opts);
};
// Return a new UInt from j.
UInt.from_generic = function (j) {
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_generic(j);
}
};
// Return a new UInt from j.
UInt.from_hex = function (j) {
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_hex(j);
}
};
// Return a new UInt from j.
UInt.from_json = function (j) {
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_json(j);
}
};
// Return a new UInt from j.
UInt.from_bits = function (j) {
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_bits(j);
}
};
// Return a new UInt from j.
UInt.from_bn = function (j) {
if (j instanceof this) {
return j.clone();
} else {
return (new this()).parse_bn(j);
}
};
UInt.is_valid = function (j) {
return this.from_json(j).is_valid();
};
UInt.prototype.clone = function () {
return this.copyTo(new this.constructor());
};
// Returns copy.
UInt.prototype.copyTo = function (d) {
d._value = this._value;
return d;
};
UInt.prototype.equals = function (d) {
return this._value instanceof BigInteger && d._value instanceof BigInteger && this._value.equals(d._value);
};
UInt.prototype.is_valid = function () {
return this._value instanceof BigInteger;
};
UInt.prototype.is_zero = function () {
return this._value.equals(BigInteger.ZERO);
};
// value = NaN on error.
UInt.prototype.parse_generic = function (j) {
// Canonicalize and validate
if (config.accounts && j in config.accounts)
j = config.accounts[j].account;
switch (j) {
case undefined:
case "0":
case this.constructor.STR_ZERO:
case this.constructor.ACCOUNT_ZERO:
case this.constructor.HEX_ZERO:
this._value = nbi();
break;
case "1":
case this.constructor.STR_ONE:
case this.constructor.ACCOUNT_ONE:
case this.constructor.HEX_ONE:
this._value = new BigInteger([1]);
break;
default:
if ('string' !== typeof j) {
this._value = NaN;
}
else if (j[0] === "r") {
this._value = Base.decode_check(Base.VER_ACCOUNT_ID, j);
}
else if (this.constructor.width === j.length) {
this._value = new BigInteger(utils.stringToArray(j), 256);
}
else if ((this.constructor.width*2) === j.length) {
// XXX Check char set!
this._value = new BigInteger(j, 16);
}
else {
this._value = NaN;
}
}
return this;
};
UInt.prototype.parse_hex = function (j) {
if ('string' === typeof j &&
j.length === (this.constructor.width * 2)) {
this._value = new BigInteger(j, 16);
} else {
this._value = NaN;
}
return this;
};
UInt.prototype.parse_bits = function (j) {
if (sjcl.bitArray.bitLength(j) !== this.constructor.width * 8) {
this._value = NaN;
} else {
var bytes = sjcl.codec.bytes.fromBits(j);
this._value = new BigInteger(bytes, 256);
}
return this;
};
UInt.prototype.parse_json = UInt.prototype.parse_hex;
UInt.prototype.parse_bn = function (j) {
if (j instanceof sjcl.bn &&
j.bitLength() <= this.constructor.width * 8) {
var bytes = sjcl.codec.bytes.fromBits(j.toBits());
this._value = new BigInteger(bytes, 256);
} else {
this._value = NaN;
}
return this;
};
// Convert from internal form.
UInt.prototype.to_bytes = function () {
if (!(this._value instanceof BigInteger))
return null;
var bytes = this._value.toByteArray();
bytes = bytes.map(function (b) { return (b+256) % 256; });
var target = this.constructor.width;
// XXX Make sure only trim off leading zeros.
bytes = bytes.slice(-target);
while (bytes.length < target) bytes.unshift(0);
return bytes;
};
UInt.prototype.to_hex = function () {
if (!(this._value instanceof BigInteger))
return null;
var bytes = this.to_bytes();
return sjcl.codec.hex.fromBits(sjcl.codec.bytes.toBits(bytes)).toUpperCase();
};
UInt.prototype.to_json = UInt.prototype.to_hex;
UInt.prototype.to_bits = function () {
if (!(this._value instanceof BigInteger))
return null;
var bytes = this.to_bytes();
return sjcl.codec.bytes.toBits(bytes);
};
UInt.prototype.to_bn = function () {
if (!(this._value instanceof BigInteger))
return null;
var bits = this.to_bits();
return sjcl.bn.fromBits(bits);
};
exports.UInt = UInt;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,70 +0,0 @@
var sjcl = require('../../build/sjcl');
var utils = require('./utils');
var config = require('./config');
var jsbn = require('./jsbn');
var extend = require('extend');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var UInt = require('./uint').UInt,
Base = require('./base').Base;
//
// UInt160 support
//
var UInt160 = extend(function () {
// Internal form: NaN or BigInteger
this._value = NaN;
}, UInt);
UInt160.width = 20;
UInt160.prototype = extend({}, UInt.prototype);
UInt160.prototype.constructor = UInt160;
var ACCOUNT_ZERO = UInt160.ACCOUNT_ZERO = "rrrrrrrrrrrrrrrrrrrrrhoLvTp";
var ACCOUNT_ONE = UInt160.ACCOUNT_ONE = "rrrrrrrrrrrrrrrrrrrrBZbvji";
var HEX_ZERO = UInt160.HEX_ZERO = "0000000000000000000000000000000000000000";
var HEX_ONE = UInt160.HEX_ONE = "0000000000000000000000000000000000000001";
var STR_ZERO = UInt160.STR_ZERO = utils.hexToString(HEX_ZERO);
var STR_ONE = UInt160.STR_ONE = utils.hexToString(HEX_ONE);
// value = NaN on error.
UInt160.prototype.parse_json = function (j) {
// Canonicalize and validate
if (config.accounts && j in config.accounts)
j = config.accounts[j].account;
if ('string' !== typeof j) {
this._value = NaN;
}
else if (j[0] === "r") {
this._value = Base.decode_check(Base.VER_ACCOUNT_ID, j);
}
else {
this._value = NaN;
}
return this;
};
// XXX Json form should allow 0 and 1, C++ doesn't currently allow it.
UInt160.prototype.to_json = function (opts) {
opts = opts || {};
if (!(this._value instanceof BigInteger))
return NaN;
var output = Base.encode_check(Base.VER_ACCOUNT_ID, this.to_bytes());
if (opts.gateways && output in opts.gateways)
output = opts.gateways[output];
return output;
};
exports.UInt160 = UInt160;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,34 +0,0 @@
var sjcl = require('../../build/sjcl');
var utils = require('./utils');
var config = require('./config');
var jsbn = require('./jsbn');
var extend = require('extend');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var UInt = require('./uint').UInt,
Base = require('./base').Base;
//
// UInt256 support
//
var UInt256 = extend(function () {
// Internal form: NaN or BigInteger
this._value = NaN;
}, UInt);
UInt256.width = 32;
UInt256.prototype = extend({}, UInt.prototype);
UInt256.prototype.constructor = UInt256;
var HEX_ZERO = UInt256.HEX_ZERO = "00000000000000000000000000000000" +
"00000000000000000000000000000000";
var HEX_ONE = UInt256.HEX_ONE = "00000000000000000000000000000000" +
"00000000000000000000000000000001";
var STR_ZERO = UInt256.STR_ZERO = utils.hexToString(HEX_ZERO);
var STR_ONE = UInt256.STR_ONE = utils.hexToString(HEX_ONE);
exports.UInt256 = UInt256;

View File

@@ -1,136 +0,0 @@
Function.prototype.method = function(name,func) {
this.prototype[name] = func;
return this;
};
var filterErr = function(code, done) {
return function(e) {
done(e.code !== code ? e : undefined);
};
};
var throwErr = function(done) {
return function(e) {
if (e)
throw e;
done();
};
};
var trace = function(comment, func) {
return function() {
console.log("%s: %s", trace, arguments.toString);
func(arguments);
};
};
var arraySet = function (count, value) {
var i, a = new Array(count);
for (i = 0; i < count; i++) {
a[i] = value;
}
return a;
};
var hexToString = function (h) {
var a = [];
var i = 0;
if (h.length % 2) {
a.push(String.fromCharCode(parseInt(h.substring(0, 1), 16)));
i = 1;
}
for (; i != h.length; i += 2) {
a.push(String.fromCharCode(parseInt(h.substring(i, i+2), 16)));
}
return a.join("");
};
var stringToHex = function (s) {
return Array.prototype.map.call(s, function (c) {
var b = c.charCodeAt(0);
return b < 16 ? "0" + b.toString(16) : b.toString(16);
}).join("");
};
var stringToArray = function (s) {
var a = new Array(s.length);
var i;
for (i = 0; i != a.length; i += 1)
a[i] = s.charCodeAt(i);
return a;
};
var hexToArray = function (h) {
return stringToArray(hexToString(h));
}
var chunkString = function (str, n, leftAlign) {
var ret = [];
var i=0, len=str.length;
if (leftAlign) {
i = str.length % n;
if (i) ret.push(str.slice(0, i));
}
for(; i < len; i += n) {
ret.push(str.slice(i, n+i));
}
return ret;
};
var logObject = function (msg, obj) {
console.log(msg, JSON.stringify(obj, undefined, 2));
};
var assert = function (assertion, msg) {
if (!assertion) {
throw new Error("Assertion failed" + (msg ? ": "+msg : "."));
}
};
/**
* Return unique values in array.
*/
var arrayUnique = function (arr) {
var u = {}, a = [];
for (var i = 0, l = arr.length; i < l; ++i){
if (u.hasOwnProperty(arr[i])) {
continue;
}
a.push(arr[i]);
u[arr[i]] = 1;
}
return a;
};
/**
* Convert a ripple epoch to a JavaScript timestamp.
*
* JavaScript timestamps are unix epoch in milliseconds.
*/
var toTimestamp = function (rpepoch) {
return (rpepoch + 0x386D4380) * 1000;
};
exports.trace = trace;
exports.arraySet = arraySet;
exports.hexToString = hexToString;
exports.hexToArray = hexToArray;
exports.stringToArray = stringToArray;
exports.stringToHex = stringToHex;
exports.chunkString = chunkString;
exports.logObject = logObject;
exports.assert = assert;
exports.arrayUnique = arrayUnique;
exports.toTimestamp = toTimestamp;
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,11 +0,0 @@
var exports = module.exports = require('./utils.js');
// We override this function for browsers, because they print objects nicer
// natively than JSON.stringify can.
exports.logObject = function (msg, obj) {
if (/MSIE/.test(navigator.userAgent)) {
console.log(msg, JSON.stringify(obj));
} else {
console.log(msg, "", obj);
}
};

View File

@@ -1,14 +1,14 @@
var async = require("async");
var buster = require("buster");
var Amount = require("../src/js/amount").Amount;
var Remote = require("../src/js/remote").Remote;
var Transaction = require("../src/js/transaction").Transaction;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Transaction = require("ripple-lib").Transaction;
var Server = require("./server").Server;
var testutils = require("./testutils");
require('../src/js/config').load(require('./config'));
require('ripple-lib').config.load(require('./config'));
buster.testRunner.timeout = 350000; //This is a very long test!

View File

@@ -1,355 +0,0 @@
var buster = require("buster");
var jsbn = require('../src/js/jsbn');
var BigInteger = jsbn.BigInteger;
var nbi = jsbn.nbi;
var Amount = require("../src/js/amount").Amount;
var UInt160 = require("../src/js/uint160").UInt160;
var config = require('../src/js/config').load(require('./config'));
// XXX Add test cases that push XRP vs non-XRP behavior.
buster.testCase("Amount", {
"UInt160" : {
"Parse 0" : function () {
buster.assert.equals(nbi(), UInt160.from_generic("0")._value);
},
"Parse 0 export" : function () {
buster.assert.equals(UInt160.ACCOUNT_ZERO, UInt160.from_generic("0").to_json());
},
"Parse 1" : function () {
buster.assert.equals(new BigInteger([1]), UInt160.from_generic("1")._value);
},
"Parse rrrrrrrrrrrrrrrrrrrrrhoLvTp export" : function () {
buster.assert.equals(UInt160.ACCOUNT_ZERO, UInt160.from_json("rrrrrrrrrrrrrrrrrrrrrhoLvTp").to_json());
},
"Parse rrrrrrrrrrrrrrrrrrrrBZbvji export" : function () {
buster.assert.equals(UInt160.ACCOUNT_ONE, UInt160.from_json("rrrrrrrrrrrrrrrrrrrrBZbvji").to_json());
},
"Parse mtgox export" : function () {
buster.assert.equals(config.accounts["mtgox"].account, UInt160.from_json("mtgox").to_json());
},
"is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvTp')" : function () {
buster.assert(UInt160.is_valid("rrrrrrrrrrrrrrrrrrrrrhoLvTp"));
},
"!is_valid('rrrrrrrrrrrrrrrrrrrrrhoLvT')" : function () {
buster.refute(UInt160.is_valid("rrrrrrrrrrrrrrrrrrrrrhoLvT"));
},
},
"Amount parsing" : {
"Parse 800/USD/mtgox" : function () {
buster.assert.equals("800/USD/"+config.accounts["mtgox"].account, Amount.from_json("800/USD/mtgox").to_text_full());
},
"Parse native 0" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0").to_text_full());
},
"Parse native 0.0" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0.0").to_text_full());
},
"Parse native -0" : function () {
buster.assert.equals("0/XRP", Amount.from_json("-0").to_text_full());
},
"Parse native -0.0" : function () {
buster.assert.equals("0/XRP", Amount.from_json("-0.0").to_text_full());
},
"Parse native 1000" : function () {
buster.assert.equals("1000/XRP", Amount.from_json("1000").to_text_full());
},
"Parse native 12.3" : function () {
buster.assert.equals("12300000/XRP", Amount.from_json("12.3").to_text_full());
},
"Parse native -12.3" : function () {
buster.assert.equals("-12300000/XRP", Amount.from_json("-12.3").to_text_full());
},
"Parse 123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123./USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("12300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("12.3/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse 1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("1.23/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("1.2300/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse -0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
"Parse -0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-0.0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").to_text_full());
},
},
"Amount operations" : {
"Negate native 123" : function () {
buster.assert.equals("-123/XRP", Amount.from_json("123").negate().to_text_full());
},
"Negate native -123" : function () {
buster.assert.equals("123/XRP", Amount.from_json("-123").negate().to_text_full());
},
"Negate non-native 123" : function () {
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
},
"Negate non-native -123" : function () {
buster.assert.equals("123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").negate().to_text_full());
},
"Clone non-native -123" : function () {
buster.assert.equals("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-123/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").clone().to_text_full());
},
"Add XRP to XRP" : function () {
buster.assert.equals("200/XRP", Amount.from_json("150").add(Amount.from_json("50")).to_text_full());
},
"Add USD to USD" : function () {
buster.assert.equals("200.52/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("150.02/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").add(Amount.from_json("50.5/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply 0 XRP with 0 XRP" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0").multiply(Amount.from_json("0")).to_text_full());
},
"Multiply 0 USD with 0 XRP" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("0")).to_text_full());
},
"Multiply 0 XRP with 0 USD" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0").multiply(Amount.from_json("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply 1 XRP with 0 XRP" : function () {
buster.assert.equals("0/XRP", Amount.from_json("1").multiply(Amount.from_json("0")).to_text_full());
},
"Multiply 1 USD with 0 XRP" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("0")).to_text_full());
},
"Multiply 1 XRP with 0 USD" : function () {
buster.assert.equals("0/XRP", Amount.from_json("1").multiply(Amount.from_json("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply 0 XRP with 1 XRP" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0").multiply(Amount.from_json("1")).to_text_full());
},
"Multiply 0 USD with 1 XRP" : function () {
buster.assert.equals("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("0/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("1")).to_text_full());
},
"Multiply 0 XRP with 1 USD" : function () {
buster.assert.equals("0/XRP", Amount.from_json("0").multiply(Amount.from_json("1/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply XRP with USD" : function () {
buster.assert.equals("2000/XRP", Amount.from_json("200").multiply(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply XRP with USD" : function () {
buster.assert.equals("200000/XRP", Amount.from_json("20000").multiply(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply XRP with USD" : function () {
buster.assert.equals("20000000/XRP", Amount.from_json("2000000").multiply(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply XRP with USD, neg" : function () {
buster.assert.equals("-2000/XRP", Amount.from_json("200").multiply(Amount.from_json("-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply XRP with USD, neg, frac" : function () {
buster.assert.equals("-222000/XRP", Amount.from_json("-6000").multiply(Amount.from_json("37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply USD with USD" : function () {
buster.assert.equals("20000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply USD with USD" : function () {
buster.assert.equals("200000000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply EUR with USD, result < 1" : function () {
buster.assert.equals("100000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply EUR with USD, neg" : function () {
buster.assert.equals("-48000000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply EUR with USD, neg, <1" : function () {
buster.assert.equals("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Multiply EUR with XRP, factor < 1" : function () {
buster.assert.equals("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("2000")).to_text_full());
},
"Multiply EUR with XRP, neg" : function () {
buster.assert.equals("-500/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("5")).to_text_full());
},
"Multiply EUR with XRP, neg, <1" : function () {
buster.assert.equals("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").multiply(Amount.from_json("2000")).to_text_full());
},
"Multiply XRP with XRP" : function () {
buster.assert.equals("100/XRP", Amount.from_json("10").multiply(Amount.from_json("10")).to_text_full());
},
"Divide XRP by USD" : function () {
buster.assert.equals("20/XRP", Amount.from_json("200").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide XRP by USD" : function () {
buster.assert.equals("2000/XRP", Amount.from_json("20000").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide XRP by USD" : function () {
buster.assert.equals("200000/XRP", Amount.from_json("2000000").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide XRP by USD, neg" : function () {
buster.assert.equals("-20/XRP", Amount.from_json("200").divide(Amount.from_json("-10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide XRP by USD, neg, frac" : function () {
buster.assert.equals("-162/XRP", Amount.from_json("-6000").divide(Amount.from_json("37/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide USD by USD" : function () {
buster.assert.equals("200/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("10/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide USD by USD, fractional" : function () {
buster.assert.equals("57142.85714285714/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("35/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide USD by USD" : function () {
buster.assert.equals("20/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("2000000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("100000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide EUR by USD, factor < 1" : function () {
buster.assert.equals("0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide EUR by USD, neg" : function () {
buster.assert.equals("-12/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-24000/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide EUR by USD, neg, <1" : function () {
buster.assert.equals("-0.1/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("-1000/USD/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh")).to_text_full());
},
"Divide EUR by XRP, result < 1" : function () {
buster.assert.equals("0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000")).to_text_full());
},
"Divide EUR by XRP, neg" : function () {
buster.assert.equals("-20/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("5")).to_text_full());
},
"Divide EUR by XRP, neg, <1" : function () {
buster.assert.equals("-0.05/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh", Amount.from_json("-100/EUR/rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh").divide(Amount.from_json("2000")).to_text_full());
}
},
"Amount comparisons" : {
"0 USD == 0 USD" : function () {
var a = Amount.from_json("0/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("0/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"0 USD == -0 USD" : function () {
var a = Amount.from_json("0/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("-0/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"0 XRP == 0 XRP" : function () {
var a = Amount.from_json("0");
var b = Amount.from_json("0.0");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"0 XRP == -0 XRP" : function () {
var a = Amount.from_json("0");
var b = Amount.from_json("-0");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"10 USD == 10 USD" : function () {
var a = Amount.from_json("10/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("10/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"123.4567 USD == 123.4567 USD" : function () {
var a = Amount.from_json("123.4567/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("123.4567/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"10 XRP == 10 XRP" : function () {
var a = Amount.from_json("10");
var b = Amount.from_json("10");
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"1.1 XRP == 1.1 XRP" : function () {
var a = Amount.from_json("1.1");
var b = Amount.from_json("11.0").ratio_human(10);
buster.assert(a.equals(b));
buster.refute(a.not_equals_why(b));
},
"0 USD == 0 USD (ignore issuer)" : function () {
var a = Amount.from_json("0/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("0/USD/rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP");
buster.assert(a.equals(b, true));
buster.refute(a.not_equals_why(b, true));
},
"1.1 USD == 1.10 USD (ignore issuer)" : function () {
var a = Amount.from_json("1.1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("1.10/USD/rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP");
buster.assert(a.equals(b, true));
buster.refute(a.not_equals_why(b, true));
},
// Exponent mismatch
"10 USD != 100 USD" : function () {
var a = Amount.from_json("10/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("100/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP value differs.");
},
"10 XRP != 100 XRP" : function () {
var a = Amount.from_json("10");
var b = Amount.from_json("100");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "XRP value differs.");
},
// Mantissa mismatch
"1 USD != 2 USD" : function () {
var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("2/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP value differs.");
},
"1 XRP != 2 XRP" : function () {
var a = Amount.from_json("1");
var b = Amount.from_json("2");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "XRP value differs.");
},
"0.1 USD != 0.2 USD" : function () {
var a = Amount.from_json("0.1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("0.2/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP value differs.");
},
// Sign mismatch
"1 USD != -1 USD" : function () {
var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("-1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP sign differs.");
},
"1 XRP != -1 XRP" : function () {
var a = Amount.from_json("1");
var b = Amount.from_json("-1");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "XRP sign differs.");
},
"1 USD != 1 USD (issuer mismatch)" : function () {
var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("1/USD/rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP issuer differs: rH5aWQJ4R7v4Mpyf4kDBUvDFT5cbpFq3XP/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
},
"1 USD != 1 EUR" : function () {
var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("1/EUR/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Non-XRP currency differs.");
},
"1 USD != 1 XRP" : function () {
var a = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
var b = Amount.from_json("1");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Native mismatch.");
},
"1 XRP != 1 USD" : function () {
var a = Amount.from_json("1");
var b = Amount.from_json("1/USD/rNDKeo9RrCiRdfsMG8AdoZvNZxHASGzbZL");
buster.refute(a.equals(b));
buster.assert.equals(a.not_equals_why(b), "Native mismatch.");
}
}
});
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,20 +0,0 @@
var buster = require("buster");
var Seed = require("../src/js/seed").Seed;
var config = require('../src/js/config').load(require('./config'));
buster.testCase("Base58", {
"Seed" : {
"saESc82Vun7Ta5EJRzGJbrXb5HNYk" : function () {
var seed = Seed.from_json("saESc82Vun7Ta5EJRzGJbrXb5HNYk");
buster.assert.equals(seed.to_hex(), "FF1CF838D02B2CF7B45BAC27F5F24F4F");
},
"sp6iDHnmiPN7tQFHm5sCW59ax3hfE" : function () {
var seed = Seed.from_json("sp6iDHnmiPN7tQFHm5sCW59ax3hfE");
buster.assert.equals(seed.to_hex(), "00AD8DA764C3C8AF5F9B8D51C94B9E49");
}
}
});
// vim:sw=2:sts=2:ts=8:et

View File

@@ -4,13 +4,13 @@ var http = require("http");
var jsonrpc = require("simple-jsonrpc");
var EventEmitter = require('events').EventEmitter;
var Amount = require("../src/js/amount").Amount;
var Remote = require("../src/js/remote").Remote;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Server = require("./server").Server;
var testutils = require("./testutils");
var config = require('../src/js/config').load(require('./config'));
var config = require('ripple-lib').config.load(require('./config'));
// How long to wait for server to start.
var serverDelay = 1500;

View File

@@ -1,13 +1,13 @@
var async = require("async");
var buster = require("buster");
var Amount = require("../src/js/amount").Amount;
var Remote = require("../src/js/remote").Remote;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Server = require("./server").Server;
var testutils = require("./testutils");
require('../src/js/config').load(require('./config'));
require('ripple-lib').config.load(require('./config'));
buster.testRunner.timeout = 5000;

View File

@@ -2,7 +2,7 @@ var async = require("async");
var fs = require("fs");
var path = require("path");
var utils = require("./utils.js");
var utils = require("ripple-lib").utils;
// Empty a directory.
// done(err) : err = true if an error occured.

View File

@@ -2,14 +2,14 @@
var async = require("async");
var buster = require("buster");
var Amount = require("../src/js/amount").Amount;
var Remote = require("../src/js/remote").Remote;
var Transaction = require("../src/js/transaction").Transaction;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Transaction = require("ripple-lib").Transaction;
var Server = require("./server").Server;
var testutils = require("./testutils");
require('../src/js/config').load(require('./config'));
require('ripple-lib').config.load(require('./config'));
buster.testRunner.timeout = 5000;

View File

@@ -1,14 +1,14 @@
var async = require("async");
var buster = require("buster");
var Amount = require("../src/js/amount.js").Amount;
var Remote = require("../src/js/remote.js").Remote;
var Transaction = require("../src/js/transaction.js").Transaction;
var Server = require("./server.js").Server;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Transaction = require("ripple-lib").Transaction;
var Server = require("./server").Server;
var testutils = require("./testutils.js");
var testutils = require("./testutils");
require('../src/js/config').load(require('./config'));
require('ripple-lib').config.load(require('./config'));
buster.testRunner.timeout = 5000;

View File

@@ -1,12 +1,12 @@
var buster = require("buster");
var Amount = require("../src/js/amount.js").Amount;
var Remote = require("../src/js/remote.js").Remote;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Server = require("./server.js").Server;
var testutils = require("./testutils.js");
var config = require('../src/js/config').load(require('./config'));
var config = require('ripple-lib').config.load(require('./config'));
// How long to wait for server to start.
var serverDelay = 1500; // XXX Not implemented.

View File

@@ -1,13 +1,13 @@
var async = require("async");
var buster = require("buster");
var Amount = require("../src/js/amount.js").Amount;
var Remote = require("../src/js/remote.js").Remote;
var Server = require("./server.js").Server;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Server = require("./server").Server;
var testutils = require("./testutils.js");
var testutils = require("./testutils");
var config = require('../src/js/config').load(require('./config'));
var config = require('ripple-lib').config.load(require('./config'));
// How long to wait for server to start.
var serverDelay = 1500;

View File

@@ -22,8 +22,8 @@ var path = require("path");
var util = require("util");
var EventEmitter = require('events').EventEmitter;
var config = require("./config.js");
var nodeutils = require("../src/js/nodeutils.js");
var config = require("./config");
var nodeutils = require("./nodeutils");
// Create a server object
var Server = function (name, config, verbose) {

View File

@@ -1,24 +0,0 @@
var buster = require("buster");
var Seed = require("../src/js/seed").Seed;
var config = require('../src/js/config').load(require('./config'));
buster.testCase("Signing", {
"Keys" : {
"SigningPubKey 1 (ripple-client issue #245)" : function () {
var seed = Seed.from_json("saESc82Vun7Ta5EJRzGJbrXb5HNYk");
var key = seed.get_key("rBZ4j6MsoctipM6GEyHSjQKzXG3yambDnZ");
var pub = key.to_hex_pub();
buster.assert.equals(pub, "0396941B22791A448E5877A44CE98434DB217D6FB97D63F0DAD23BE49ED45173C9");
},
"SigningPubKey 2 (master seed)" : function () {
var seed = Seed.from_json("snoPBrXtMeMyMHUVTgbuqAfg1SUTb");
var key = seed.get_key("rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh");
var pub = key.to_hex_pub();
buster.assert.equals(pub, "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020");
}
}
});
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,10 +1,10 @@
var async = require("async");
var Amount = require("../src/js/amount").Amount;
var Remote = require("../src/js/remote").Remote;
var Amount = require("ripple-lib").Amount;
var Remote = require("ripple-lib").Remote;
var Server = require("./server").Server;
var config = require('../src/js/config').load(require('./config'));
var config = require('ripple-lib').config.load(require('./config'));
var account_dump = function (remote, account, callback) {
var self = this;

View File

@@ -1,26 +0,0 @@
var fs = require("fs");
var buster = require("buster");
var utils = require("../src/js/utils.js");
buster.testCase("Utils", {
"hexToString and stringToHex" : {
"Even: 123456" : function () {
buster.assert.equals("123456", utils.stringToHex(utils.hexToString("123456")));
},
"Odd: 12345" : function () {
buster.assert.equals("012345", utils.stringToHex(utils.hexToString("12345")));
},
"Under 10: 0" : function () {
buster.assert.equals("00", utils.stringToHex(utils.hexToString("0")));
},
"Under 10: 1" : function () {
buster.assert.equals("01", utils.stringToHex(utils.hexToString("1")));
},
"Empty" : function () {
buster.assert.equals("", utils.stringToHex(utils.hexToString("")));
}
}
});
// vim:sw=2:sts=2:ts=8:et

View File

@@ -1,9 +1,9 @@
var buster = require("buster");
var Server = require("./server").Server;
var Remote = require("../src/js/remote").Remote;
var Remote = require("ripple-lib").Remote;
var config = require('../src/js/config').load(require('./config'));
var config = require('ripple-lib').config.load(require('./config'));
buster.testRunner.timeout = 5000;