mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Moved cpp code to src/cpp and js code to src/js.
This commit is contained in:
626
src/js/amount.js
Normal file
626
src/js/amount.js
Normal file
@@ -0,0 +1,626 @@
|
||||
// Represent Ripple amounts and currencies.
|
||||
// - Numbers in hex are big-endian.
|
||||
|
||||
var sjcl = require('./sjcl/core.js');
|
||||
var bn = require('./sjcl/core.js').bn;
|
||||
var utils = require('./utils.js');
|
||||
var jsbn = require('./jsbn.js');
|
||||
|
||||
// Don't include in browser context.
|
||||
var config = require('../test/config.js');
|
||||
|
||||
var BigInteger = jsbn.BigInteger;
|
||||
var nbi = jsbn.nbi;
|
||||
|
||||
var alphabets = {
|
||||
'ripple' : "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz",
|
||||
'bitcoin' : "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
||||
};
|
||||
|
||||
// --> input: big-endian array of bytes.
|
||||
// <-- string at least as long as input.
|
||||
var encode_base = function (input, alphabet) {
|
||||
var alphabet = alphabets[alphabet || '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.
|
||||
var decode_base = function (input, alphabet) {
|
||||
var alphabet = alphabets[alphabet || '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);
|
||||
};
|
||||
|
||||
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: Array
|
||||
// <-- String
|
||||
var encode_base_check = function (version, input, alphabet) {
|
||||
var buffer = [].concat(version, input);
|
||||
var check = sha256(sha256(buffer)).slice(0, 4);
|
||||
|
||||
return encode_base([].concat(buffer, check), alphabet);
|
||||
}
|
||||
|
||||
// --> input : String
|
||||
// <-- NaN || BigInteger
|
||||
var decode_base_check = function (version, input, alphabet) {
|
||||
var buffer = decode_base(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;
|
||||
|
||||
return new BigInteger(buffer.slice(1, -4));
|
||||
}
|
||||
|
||||
var UInt160 = function () {
|
||||
// Internal form: NaN or BigInteger
|
||||
this._value = NaN;
|
||||
};
|
||||
|
||||
UInt160.json_rewrite = function (j) {
|
||||
return UInt160.from_json(j).to_json();
|
||||
};
|
||||
|
||||
// Return a new UInt160 from j.
|
||||
UInt160.from_json = function (j) {
|
||||
return 'string' === typeof j
|
||||
? (new UInt160()).parse_json(j)
|
||||
: j.clone();
|
||||
};
|
||||
|
||||
UInt160.prototype.clone = function() {
|
||||
return this.copyTo(new UInt160());
|
||||
};
|
||||
|
||||
// Returns copy.
|
||||
UInt160.prototype.copyTo = function(d) {
|
||||
d._value = this._value;
|
||||
|
||||
return d;
|
||||
};
|
||||
|
||||
UInt160.prototype.equals = function(d) {
|
||||
return isNaN(this._value) || isNaN(d._value) ? false : this._value.equals(d._value);
|
||||
};
|
||||
|
||||
// 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;
|
||||
|
||||
switch (j) {
|
||||
case undefined:
|
||||
case "0":
|
||||
case exports.consts.address_xns:
|
||||
case exports.consts.uint160_xns:
|
||||
case exports.consts.hex_xns:
|
||||
this._value = nbi();
|
||||
break;
|
||||
|
||||
case "1":
|
||||
case exports.consts.address_one:
|
||||
case exports.consts.uint160_one:
|
||||
case exports.consts.hex_one:
|
||||
this._value = new BigInteger([1]);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if ('string' !== typeof j) {
|
||||
this._value = NaN;
|
||||
}
|
||||
else if (20 === j.length) {
|
||||
this._value = new BigInteger(utils.stringToArray(j), 256);
|
||||
}
|
||||
else if (40 === j.length) {
|
||||
// XXX Check char set!
|
||||
this._value = new BigInteger(j, 16);
|
||||
}
|
||||
else if (j[0] === "r") {
|
||||
this._value = decode_base_check(0, j);
|
||||
}
|
||||
else {
|
||||
this._value = NaN;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// Convert from internal form.
|
||||
// XXX Json form should allow 0 and 1, C++ doesn't currently allow it.
|
||||
UInt160.prototype.to_json = function () {
|
||||
if (isNaN(this._value))
|
||||
return NaN;
|
||||
|
||||
var bytes = this._value.toByteArray().map(function (b) { return b ? b < 0 ? 256+b : b : 0});
|
||||
var target = 20;
|
||||
|
||||
// XXX Make sure only trim off leading zeros.
|
||||
var array = bytes.length < target
|
||||
? bytes.length
|
||||
? [].concat(utils.arraySet(target - bytes.length, 0), bytes)
|
||||
: utils.arraySet(target, 0)
|
||||
: bytes.slice(target - bytes.length);
|
||||
var output = encode_base_check(0, array);
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
var Currency = function () {
|
||||
// Internal form: 0 = XNS. 3 letter-code.
|
||||
// XXX Internal should be 0 or hex with three letter annotation when valid.
|
||||
|
||||
// Json form:
|
||||
// '', 'XNS', '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) {
|
||||
return 'string' === typeof j
|
||||
? (new Currency()).parse_json(j)
|
||||
: j.clone();
|
||||
};
|
||||
|
||||
Currency.prototype.clone = function() {
|
||||
return this.copyTo(new Currency());
|
||||
};
|
||||
|
||||
// Returns copy.
|
||||
Currency.prototype.copyTo = function(d) {
|
||||
d._value = this._value;
|
||||
|
||||
return d;
|
||||
};
|
||||
|
||||
// this._value = NaN on error.
|
||||
Currency.prototype.parse_json = function(j) {
|
||||
if ("" === j || "0" === j || "XNS" === j) {
|
||||
this._value = 0;
|
||||
}
|
||||
else if ('string' != typeof j || 3 !== j.length) {
|
||||
this._value = NaN;
|
||||
}
|
||||
else {
|
||||
this._value = j;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Currency.prototype.to_json = function () {
|
||||
return this._value ? this._value : "XNS";
|
||||
};
|
||||
|
||||
Currency.prototype.to_human = function() {
|
||||
return this._value ? this._value : "XNS";
|
||||
};
|
||||
|
||||
var Amount = function () {
|
||||
// Json format:
|
||||
// integer : XNS
|
||||
// { 'value' : ..., 'currency' : ..., 'issuer' : ...}
|
||||
|
||||
this._value = new BigInteger(); // NaN for bad value. Always positive for non-XNS.
|
||||
this._offset = undefined; // For non-XNS.
|
||||
this._is_native = true; // Default to XNS. Only valid if value is not NaN.
|
||||
this._is_negative = undefined; // Undefined for XNS.
|
||||
|
||||
this.currency = new Currency();
|
||||
this.issuer = new UInt160();
|
||||
};
|
||||
|
||||
// Given "100/USD/mtgox" return the a string with mtgox remapped.
|
||||
Amount.text_full_rewrite = function (j) {
|
||||
return Amount.from_json(j).to_text_full();
|
||||
}
|
||||
|
||||
// Given "100/USD/mtgox" return the json.
|
||||
Amount.json_rewrite = function(j) {
|
||||
return Amount.from_json(j).to_json();
|
||||
};
|
||||
|
||||
Amount.from_json = function(j) {
|
||||
return (new Amount()).parse_json(j);
|
||||
};
|
||||
|
||||
Amount.prototype.clone = function(negate) {
|
||||
return this.copyTo(new Amount(), negate);
|
||||
};
|
||||
|
||||
// Returns copy.
|
||||
Amount.prototype.copyTo = function(d, negate) {
|
||||
if ('object' === typeof this._value)
|
||||
{
|
||||
if (this._is_native && negate)
|
||||
this._value.negate().copyTo(d._value);
|
||||
else
|
||||
this._value.copyTo(d._value);
|
||||
}
|
||||
else
|
||||
{
|
||||
d._value = this._value;
|
||||
}
|
||||
|
||||
d._offset = this._offset;
|
||||
d._is_native = this._is_native;
|
||||
d._is_negative = this._is_native
|
||||
? undefined // Native sign in BigInteger.
|
||||
: negate
|
||||
? !this._is_negative // Negating.
|
||||
: this._is_negative; // Just copying.
|
||||
|
||||
this.currency.copyTo(d.currency);
|
||||
this.issuer.copyTo(d.issuer);
|
||||
|
||||
return d;
|
||||
};
|
||||
|
||||
// YYY Might also provide is_valid_json.
|
||||
Amount.prototype.is_valid = function() {
|
||||
return !isNaN(this._value);
|
||||
};
|
||||
|
||||
// Convert only value to JSON wire format.
|
||||
Amount.prototype.to_text = function(allow_nan) {
|
||||
if (isNaN(this._value)) {
|
||||
// Never should happen.
|
||||
return allow_nan ? NaN : "0";
|
||||
}
|
||||
else if (this._is_native) {
|
||||
if (this._value.compareTo(exports.consts.bi_xns_max) > 0 || this._value.compareTo(exports.consts.bi_xns_min) < 0)
|
||||
{
|
||||
// Never should happen.
|
||||
return allow_nan ? NaN : "0";
|
||||
}
|
||||
else
|
||||
{
|
||||
return this._value.toString();
|
||||
}
|
||||
}
|
||||
else if (this._value.equals(BigInteger.ZERO))
|
||||
{
|
||||
return "0";
|
||||
}
|
||||
else if (this._offset < -25 || this._offset > -5)
|
||||
{
|
||||
// Use e notation.
|
||||
// XXX Clamp output.
|
||||
|
||||
return (this._is_negative ? "-" : "") + this._value.toString() + "e" + this._offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
var val = "000000000000000000000000000" + this._value.toString() + "00000000000000000000000";
|
||||
var pre = val.substring(0, this._offset + 43);
|
||||
var post = val.substring(this._offset + 43);
|
||||
var s_pre = pre.match(/[1-9].*$/); // Everything but leading zeros.
|
||||
var s_post = post.match(/[1-9]0*$/); // Last non-zero plus trailing zeros.
|
||||
|
||||
return (this._is_negative ? "-" : "")
|
||||
+ (s_pre ? s_pre[0] : "0")
|
||||
+ (s_post ? "." + post.substring(0, 1+post.length-s_post[0].length) : "");
|
||||
}
|
||||
};
|
||||
|
||||
Amount.prototype.canonicalize = function() {
|
||||
if (isNaN(this._value) || !this.currency) {
|
||||
// nothing
|
||||
}
|
||||
else if (this._value.equals(BigInteger.ZERO)) {
|
||||
this._offset = -100;
|
||||
this._is_negative = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (this._value.compareTo(exports.consts.bi_man_min_value) < 0) {
|
||||
this._value = this._value.multiply(exports.consts.bi_10);
|
||||
this._offset -= 1;
|
||||
}
|
||||
|
||||
while (this._value.compareTo(exports.consts.bi_man_max_value) > 0) {
|
||||
this._value = this._value.divide(exports.consts.bi_10);
|
||||
this._offset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Amount.prototype.is_native = function () {
|
||||
return this._is_native;
|
||||
};
|
||||
|
||||
// Return a new value.
|
||||
Amount.prototype.negate = function () {
|
||||
return this.clone('NEGATE');
|
||||
};
|
||||
|
||||
Amount.prototype.to_json = function() {
|
||||
if (this._is_native) {
|
||||
return this.to_text();
|
||||
}
|
||||
else
|
||||
{
|
||||
return {
|
||||
'value' : this.to_text(),
|
||||
'currency' : this.currency.to_json(),
|
||||
'issuer' : this.issuer.to_json(),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Amount.prototype.to_text_full = function() {
|
||||
return isNaN(this._value)
|
||||
? NaN
|
||||
: this._is_native
|
||||
? this.to_text() + "/XNS"
|
||||
: this.to_text() + "/" + this.currency.to_json() + "/" + this.issuer.to_json();
|
||||
};
|
||||
|
||||
// Parse a XNS value from untrusted input.
|
||||
// - integer = raw units
|
||||
// - float = with precision 6
|
||||
// XXX Improvements: disallow leading zeros.
|
||||
Amount.prototype.parse_native = function(j) {
|
||||
var m;
|
||||
|
||||
if ('string' === typeof j)
|
||||
m = j.match(/^(-?)(\d+)(\.\d{0,6})?$/);
|
||||
|
||||
if (m) {
|
||||
if (undefined === m[3]) {
|
||||
// Integer notation
|
||||
|
||||
this._value = new BigInteger(m[2]);
|
||||
}
|
||||
else {
|
||||
// Float notation
|
||||
|
||||
var int_part = (new BigInteger(m[2])).multiply(exports.consts.bi_xns_unit);
|
||||
var fraction_part = (new BigInteger(m[3])).multiply(new BigInteger(String(Math.pow(10, 1+exports.consts.xns_precision-m[3].length))));
|
||||
|
||||
this._value = int_part.add(fraction_part);
|
||||
}
|
||||
|
||||
if (m[1])
|
||||
this._value = this._value.negate();
|
||||
|
||||
this._is_native = true;
|
||||
this._offset = undefined;
|
||||
this._is_negative = undefined;
|
||||
|
||||
if (this._value.compareTo(exports.consts.bi_xns_max) > 0 || this._value.compareTo(exports.consts.bi_xns_min) < 0)
|
||||
{
|
||||
this._value = NaN;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this._value = NaN;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// Parse a non-native value.
|
||||
Amount.prototype.parse_value = function(j) {
|
||||
this._is_native = false;
|
||||
|
||||
if ('number' === typeof j) {
|
||||
this._is_negative = j < 0;
|
||||
if (this._is_negative) j = -j;
|
||||
this._value = new BigInteger(j);
|
||||
this._offset = 0;
|
||||
|
||||
this.canonicalize();
|
||||
}
|
||||
else if ('string' === typeof j) {
|
||||
var i = j.match(/^(-?)(\d+)$/);
|
||||
var d = !i && j.match(/^(-?)(\d+)\.(\d*)$/);
|
||||
var e = !e && j.match(/^(-?)(\d+)e(\d+)$/);
|
||||
|
||||
if (e) {
|
||||
// e notation
|
||||
|
||||
this._value = new BigInteger(e[2]);
|
||||
this._offset = parseInt(e[3]);
|
||||
this._is_negative = !!e[1];
|
||||
|
||||
this.canonicalize();
|
||||
}
|
||||
else if (d) {
|
||||
// float notation
|
||||
|
||||
var integer = new BigInteger(d[2]);
|
||||
var fraction = new BigInteger(d[3]);
|
||||
var precision = d[3].length;
|
||||
|
||||
this._value = integer.multiply(exports.consts.bi_10.clone().pow(precision)).add(fraction);
|
||||
this._offset = -precision;
|
||||
this._is_negative = !!d[1];
|
||||
|
||||
this.canonicalize();
|
||||
}
|
||||
else if (i) {
|
||||
// integer notation
|
||||
|
||||
this._value = new BigInteger(i[2]);
|
||||
this._offset = 0;
|
||||
this._is_negative = !!i[1];
|
||||
|
||||
this.canonicalize();
|
||||
}
|
||||
else {
|
||||
this._value = NaN;
|
||||
}
|
||||
}
|
||||
else if (j.constructor == BigInteger) {
|
||||
this._value = j.clone();
|
||||
}
|
||||
else {
|
||||
this._value = NaN;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// <-> j
|
||||
Amount.prototype.parse_json = function(j) {
|
||||
if ('string' === typeof j) {
|
||||
// .../.../... notation is not a wire format. But allowed for easier testing.
|
||||
var m = j.match(/^(.+)\/(...)\/(.+)$/);
|
||||
|
||||
if (m) {
|
||||
this.parse_value(m[1]);
|
||||
this.currency = Currency.from_json(m[2]);
|
||||
this.issuer = UInt160.from_json(m[3]);
|
||||
}
|
||||
else {
|
||||
this.parse_native(j);
|
||||
this.currency = new Currency();
|
||||
this.issuer = new UInt160();
|
||||
}
|
||||
}
|
||||
else if ('object' === typeof j && j.constructor == Amount) {
|
||||
j.copyTo(this);
|
||||
}
|
||||
else if ('object' === typeof j && 'value' in j) {
|
||||
// Parse the passed value to sanitize and copy it.
|
||||
|
||||
this.parse_value(j.value);
|
||||
this.currency.parse_json(j.currency); // Never XNS.
|
||||
this.issuer.parse_json(j.issuer);
|
||||
}
|
||||
else {
|
||||
this._value = NaN;
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
Amount.prototype.parse_issuer = function (issuer) {
|
||||
this.issuer.parse_json(issuer);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// Check BigInteger NaN
|
||||
Amount.prototype.equals = function (d) {
|
||||
return 'string' === typeof (d)
|
||||
? this.equals(Amount.from_json(d))
|
||||
: this === d
|
||||
|| (d.constructor === Amount
|
||||
&& this._is_native === d._is_native
|
||||
&& (this._is_native
|
||||
? this._value.equals(d._value)
|
||||
: this._is_negative === d._is_negative
|
||||
? this._value.equals(d._value)
|
||||
: this._value.equals(BigInteger.ZERO) && d._value.equals(BigInteger.ZERO)));
|
||||
};
|
||||
|
||||
exports.Amount = Amount;
|
||||
exports.Currency = Currency;
|
||||
exports.UInt160 = UInt160;
|
||||
|
||||
exports.consts = {
|
||||
'address_xns' : "rrrrrrrrrrrrrrrrrrrrrhoLvTp",
|
||||
'address_one' : "rrrrrrrrrrrrrrrrrrrrBZbvji",
|
||||
'currency_xns' : 0,
|
||||
'currency_one' : 1,
|
||||
'uint160_xns' : utils.hexToString("0000000000000000000000000000000000000000"),
|
||||
'uint160_one' : utils.hexToString("0000000000000000000000000000000000000001"),
|
||||
'hex_xns' : "0000000000000000000000000000000000000000",
|
||||
'hex_one' : "0000000000000000000000000000000000000001",
|
||||
'xns_precision' : 6,
|
||||
|
||||
// BigInteger values prefixed with bi_.
|
||||
'bi_10' : new BigInteger('10'),
|
||||
'bi_man_max_value' : new BigInteger('9999999999999999'),
|
||||
'bi_man_min_value' : new BigInteger('1000000000000000'),
|
||||
'bi_xns_max' : new BigInteger("9000000000000000000"), // Json wire limit.
|
||||
'bi_xns_min' : new BigInteger("-9000000000000000000"), // Json wire limit.
|
||||
'bi_xns_unit' : new BigInteger('1000000'),
|
||||
};
|
||||
|
||||
// vim:sw=2:sts=2:ts=8
|
||||
1
src/js/cryptojs
Submodule
1
src/js/cryptojs
Submodule
Submodule src/js/cryptojs added at c3c843c513
291
src/js/events.js
Normal file
291
src/js/events.js
Normal file
@@ -0,0 +1,291 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
var isArray = Array.isArray;
|
||||
var domain;
|
||||
|
||||
function EventEmitter() {
|
||||
if (exports.usingDomains) {
|
||||
// if there is an active domain, then attach to it.
|
||||
domain = domain || require('domain');
|
||||
if (domain.active && !(this instanceof domain.Domain)) {
|
||||
this.domain = domain.active;
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.EventEmitter = EventEmitter;
|
||||
|
||||
// By default EventEmitters will print a warning if more than
|
||||
// 10 listeners are added to it. This is a useful default which
|
||||
// helps finding memory leaks.
|
||||
//
|
||||
// Obviously not all Emitters should be limited to 10. This function allows
|
||||
// that to be increased. Set to zero for unlimited.
|
||||
var defaultMaxListeners = 10;
|
||||
EventEmitter.prototype.setMaxListeners = function(n) {
|
||||
if (!this._events) this._events = {};
|
||||
this._maxListeners = n;
|
||||
};
|
||||
|
||||
// non-global reference, for speed.
|
||||
var PROCESS;
|
||||
|
||||
EventEmitter.prototype.emit = function() {
|
||||
var type = arguments[0];
|
||||
// If there is no 'error' event listener then throw.
|
||||
if (type === 'error') {
|
||||
if (!this._events || !this._events.error ||
|
||||
(isArray(this._events.error) && !this._events.error.length))
|
||||
{
|
||||
if (this.domain) {
|
||||
var er = arguments[1];
|
||||
er.domain_emitter = this;
|
||||
er.domain = this.domain;
|
||||
er.domain_thrown = false;
|
||||
this.domain.emit('error', er);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (arguments[1] instanceof Error) {
|
||||
throw arguments[1]; // Unhandled 'error' event
|
||||
} else {
|
||||
throw new Error("Uncaught, unspecified 'error' event.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this._events) return false;
|
||||
var handler = this._events[type];
|
||||
if (!handler) return false;
|
||||
|
||||
if (typeof handler == 'function') {
|
||||
if (this.domain) {
|
||||
PROCESS = PROCESS || process;
|
||||
if (this !== PROCESS) {
|
||||
this.domain.enter();
|
||||
}
|
||||
}
|
||||
switch (arguments.length) {
|
||||
// fast cases
|
||||
case 1:
|
||||
handler.call(this);
|
||||
break;
|
||||
case 2:
|
||||
handler.call(this, arguments[1]);
|
||||
break;
|
||||
case 3:
|
||||
handler.call(this, arguments[1], arguments[2]);
|
||||
break;
|
||||
// slower
|
||||
default:
|
||||
var l = arguments.length;
|
||||
var args = new Array(l - 1);
|
||||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
||||
handler.apply(this, args);
|
||||
}
|
||||
if (this.domain && this !== PROCESS) {
|
||||
this.domain.exit();
|
||||
}
|
||||
return true;
|
||||
|
||||
} else if (isArray(handler)) {
|
||||
if (this.domain) {
|
||||
PROCESS = PROCESS || process;
|
||||
if (this !== PROCESS) {
|
||||
this.domain.enter();
|
||||
}
|
||||
}
|
||||
var l = arguments.length;
|
||||
var args = new Array(l - 1);
|
||||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
||||
|
||||
var listeners = handler.slice();
|
||||
for (var i = 0, l = listeners.length; i < l; i++) {
|
||||
listeners[i].apply(this, args);
|
||||
}
|
||||
if (this.domain && this !== PROCESS) {
|
||||
this.domain.exit();
|
||||
}
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
EventEmitter.prototype.addListener = function(type, listener) {
|
||||
if ('function' !== typeof listener) {
|
||||
throw new Error('addListener only takes instances of Function');
|
||||
}
|
||||
|
||||
if (!this._events) this._events = {};
|
||||
|
||||
// To avoid recursion in the case that type == "newListeners"! Before
|
||||
// adding it to the listeners, first emit "newListeners".
|
||||
if (this._events.newListener) {
|
||||
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
||||
listener.listener : listener);
|
||||
}
|
||||
|
||||
if (!this._events[type]) {
|
||||
// Optimize the case of one listener. Don't need the extra array object.
|
||||
this._events[type] = listener;
|
||||
} else if (isArray(this._events[type])) {
|
||||
|
||||
// If we've already got an array, just append.
|
||||
this._events[type].push(listener);
|
||||
|
||||
} else {
|
||||
// Adding the second element, need to change to array.
|
||||
this._events[type] = [this._events[type], listener];
|
||||
|
||||
}
|
||||
|
||||
// Check for listener leak
|
||||
if (isArray(this._events[type]) && !this._events[type].warned) {
|
||||
var m;
|
||||
if (this._maxListeners !== undefined) {
|
||||
m = this._maxListeners;
|
||||
} else {
|
||||
m = defaultMaxListeners;
|
||||
}
|
||||
|
||||
if (m && m > 0 && this._events[type].length > m) {
|
||||
this._events[type].warned = true;
|
||||
console.error('(node) warning: possible EventEmitter memory ' +
|
||||
'leak detected. %d listeners added. ' +
|
||||
'Use emitter.setMaxListeners() to increase limit.',
|
||||
this._events[type].length);
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
||||
|
||||
EventEmitter.prototype.once = function(type, listener) {
|
||||
if ('function' !== typeof listener) {
|
||||
throw new Error('.once only takes instances of Function');
|
||||
}
|
||||
|
||||
var self = this;
|
||||
function g() {
|
||||
self.removeListener(type, g);
|
||||
listener.apply(this, arguments);
|
||||
};
|
||||
|
||||
g.listener = listener;
|
||||
self.on(type, g);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
// emits a 'removeListener' event iff the listener was removed
|
||||
EventEmitter.prototype.removeListener = function(type, listener) {
|
||||
if ('function' !== typeof listener) {
|
||||
throw new Error('removeListener only takes instances of Function');
|
||||
}
|
||||
|
||||
// does not use listeners(), so no side effect of creating _events[type]
|
||||
if (!this._events || !this._events[type]) return this;
|
||||
|
||||
var list = this._events[type];
|
||||
|
||||
if (isArray(list)) {
|
||||
var position = -1;
|
||||
for (var i = 0, length = list.length; i < length; i++) {
|
||||
if (list[i] === listener ||
|
||||
(list[i].listener && list[i].listener === listener))
|
||||
{
|
||||
position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (position < 0) return this;
|
||||
list.splice(position, 1);
|
||||
if (list.length == 0)
|
||||
delete this._events[type];
|
||||
|
||||
if (this._events.removeListener) {
|
||||
this.emit('removeListener', type, listener);
|
||||
}
|
||||
} else if (list === listener ||
|
||||
(list.listener && list.listener === listener))
|
||||
{
|
||||
delete this._events[type];
|
||||
|
||||
if (this._events.removeListener) {
|
||||
this.emit('removeListener', type, listener);
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.removeAllListeners = function(type) {
|
||||
if (!this._events) return this;
|
||||
|
||||
// fast path
|
||||
if (!this._events.removeListener) {
|
||||
if (arguments.length === 0) {
|
||||
this._events = {};
|
||||
} else if (type && this._events && this._events[type]) {
|
||||
this._events[type] = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
// slow(ish) path, emit 'removeListener' events for all removals
|
||||
if (arguments.length === 0) {
|
||||
for (var key in this._events) {
|
||||
if (key === 'removeListener') continue;
|
||||
this.removeAllListeners(key);
|
||||
}
|
||||
this.removeAllListeners('removeListener');
|
||||
this._events = {};
|
||||
return this;
|
||||
}
|
||||
|
||||
var listeners = this._events[type];
|
||||
if (isArray(listeners)) {
|
||||
while (listeners.length) {
|
||||
// LIFO order
|
||||
this.removeListener(type, listeners[listeners.length - 1]);
|
||||
}
|
||||
} else if (listeners) {
|
||||
this.removeListener(type, listeners);
|
||||
}
|
||||
this._events[type] = null;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.listeners = function(type) {
|
||||
if (!this._events || !this._events[type]) return [];
|
||||
if (!isArray(this._events[type])) {
|
||||
return [this._events[type]];
|
||||
}
|
||||
return this._events[type].slice(0);
|
||||
};
|
||||
1219
src/js/jsbn.js
Normal file
1219
src/js/jsbn.js
Normal file
File diff suppressed because it is too large
Load Diff
91
src/js/nodeutils.js
Normal file
91
src/js/nodeutils.js
Normal file
@@ -0,0 +1,91 @@
|
||||
var async = require("async");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
|
||||
var utils = require("./utils.js");
|
||||
|
||||
// Empty a directory.
|
||||
// done(err) : err = true if an error occured.
|
||||
var emptyPath = function(dirPath, done) {
|
||||
fs.readdir(dirPath, function(err, files) {
|
||||
if (err) {
|
||||
done(err);
|
||||
}
|
||||
else {
|
||||
async.some(files.map(function(f) { return path.join(dirPath, f); }), rmPath, done);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Make a directory and sub-directories.
|
||||
var mkPath = function(dirPath, mode, done) {
|
||||
fs.mkdir(dirPath, typeof mode === "string" ? parseInt(mode, 8) : mode, function(e) {
|
||||
if (!e || e.code === "EEXIST") {
|
||||
// Created or already exists, done.
|
||||
done();
|
||||
}
|
||||
else if (e.code === "ENOENT") {
|
||||
// Missing sub dir.
|
||||
|
||||
mkPath(path.dirname(dirPath), mode, function(e) {
|
||||
if (e) {
|
||||
throw e;
|
||||
}
|
||||
else {
|
||||
mkPath(dirPath, mode, done);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Create directory if needed and empty if needed.
|
||||
var resetPath = function(dirPath, mode, done) {
|
||||
mkPath(dirPath, mode, function(e) {
|
||||
if (e) {
|
||||
done(e);
|
||||
}
|
||||
else {
|
||||
emptyPath(dirPath, done);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Remove path recursively.
|
||||
// done(err)
|
||||
var rmPath = function(dirPath, done) {
|
||||
// console.log("rmPath: %s", dirPath);
|
||||
|
||||
fs.lstat(dirPath, function(err, stats) {
|
||||
if (err && err.code == "ENOENT") {
|
||||
done();
|
||||
}
|
||||
if (err) {
|
||||
done(err);
|
||||
}
|
||||
else if (stats.isDirectory()) {
|
||||
emptyPath(dirPath, function(e) {
|
||||
if (e) {
|
||||
done(e);
|
||||
}
|
||||
else {
|
||||
// console.log("rmdir: %s", dirPath); done();
|
||||
fs.rmdir(dirPath, done);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
// console.log("unlink: %s", dirPath); done();
|
||||
fs.unlink(dirPath, done);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.mkPath = mkPath;
|
||||
exports.resetPath = resetPath;
|
||||
exports.rmPath = rmPath;
|
||||
|
||||
// vim:sw=2:sts=2:ts=8
|
||||
1273
src/js/remote.js
Normal file
1273
src/js/remote.js
Normal file
File diff suppressed because it is too large
Load Diff
44
src/js/serializer.js
Normal file
44
src/js/serializer.js
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
|
||||
var serializer = {};
|
||||
|
||||
serializer.addUInt16 = function(value) {
|
||||
switch (typeof value) {
|
||||
case 'string':
|
||||
addUInt16(value.charCodeAt(0));
|
||||
break;
|
||||
|
||||
case 'integer':
|
||||
for (i = 16/8; i; i -=1) {
|
||||
raw.push(value & 255);
|
||||
value >>= 8;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw 'UNEXPECTED_TYPE';
|
||||
}
|
||||
};
|
||||
|
||||
serializer.addUInt160 = function(value) {
|
||||
switch (typeof value) {
|
||||
case 'array':
|
||||
raw.concat(value);
|
||||
break;
|
||||
|
||||
case 'integer':
|
||||
for (i = 160/8; i; i -=1) {
|
||||
raw.push(value & 255);
|
||||
value >>= 8;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw 'UNEXPECTED_TYPE';
|
||||
}
|
||||
};
|
||||
|
||||
serializer.getSHA512Half = function() {
|
||||
};
|
||||
|
||||
// vim:ts=4
|
||||
1
src/js/sjcl
Submodule
1
src/js/sjcl
Submodule
Submodule src/js/sjcl added at d04d0bdccd
79
src/js/utils.js
Normal file
79
src/js/utils.js
Normal file
@@ -0,0 +1,79 @@
|
||||
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 a = new Array(count);
|
||||
var i;
|
||||
|
||||
for (i = 0; i != count; i += 1)
|
||||
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;
|
||||
};
|
||||
|
||||
exports.trace = trace;
|
||||
exports.arraySet = arraySet;
|
||||
exports.hexToString = hexToString;
|
||||
exports.stringToArray = stringToArray;
|
||||
exports.stringToHex = stringToHex;
|
||||
|
||||
// vim:sw=2:sts=2:ts=8
|
||||
Reference in New Issue
Block a user