mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 12:15:51 +00:00
Fix eslint issues.
This commit is contained in:
46
src/base.js
46
src/base.js
@@ -1,13 +1,13 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var _ = require('lodash');
|
const _ = require('lodash');
|
||||||
var sjcl = require('./utils').sjcl;
|
const sjcl = require('./utils').sjcl;
|
||||||
var utils = require('./utils');
|
const utils = require('./utils');
|
||||||
var extend = require('extend');
|
const extend = require('extend');
|
||||||
var convertBase = require('./baseconverter');
|
const convertBase = require('./baseconverter');
|
||||||
|
|
||||||
var Base = {};
|
const Base = {};
|
||||||
|
|
||||||
var alphabets = Base.alphabets = {
|
const alphabets = Base.alphabets = {
|
||||||
ripple: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz',
|
ripple: 'rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz',
|
||||||
tipple: 'RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz',
|
tipple: 'RPShNAF39wBUDnEGHJKLM4pQrsT7VWXYZ2bcdeCg65jkm8ofqi1tuvaxyz',
|
||||||
bitcoin: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
bitcoin: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
||||||
@@ -34,16 +34,16 @@ function encodeString(alphabet, input) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
var leadingZeros = _.takeWhile(input, function(d) {
|
const leadingZeros = _.takeWhile(input, function(d) {
|
||||||
return d === 0;
|
return d === 0;
|
||||||
});
|
});
|
||||||
var out = convertBase(input, 256, 58).map(function(digit) {
|
const out = convertBase(input, 256, 58).map(function(digit) {
|
||||||
if (digit < 0 || digit >= alphabet.length) {
|
if (digit < 0 || digit >= alphabet.length) {
|
||||||
throw new Error('Value ' + digit + ' is out of bounds for encoding');
|
throw new Error('Value ' + digit + ' is out of bounds for encoding');
|
||||||
}
|
}
|
||||||
return alphabet[digit];
|
return alphabet[digit];
|
||||||
});
|
});
|
||||||
var prefix = leadingZeros.map(function() {
|
const prefix = leadingZeros.map(function() {
|
||||||
return alphabet[0];
|
return alphabet[0];
|
||||||
});
|
});
|
||||||
return prefix.concat(out).join('');
|
return prefix.concat(out).join('');
|
||||||
@@ -54,23 +54,23 @@ function decodeString(indexes, input) {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
var input58 = input.split('').map(function(c) {
|
const input58 = input.split('').map(function(c) {
|
||||||
var charCode = c.charCodeAt(0);
|
const charCode = c.charCodeAt(0);
|
||||||
if (charCode >= indexes.length || indexes[charCode] === -1) {
|
if (charCode >= indexes.length || indexes[charCode] === -1) {
|
||||||
throw new Error('Character ' + c + ' is not valid for encoding');
|
throw new Error('Character ' + c + ' is not valid for encoding');
|
||||||
}
|
}
|
||||||
return indexes[charCode];
|
return indexes[charCode];
|
||||||
});
|
});
|
||||||
var leadingZeros = _.takeWhile(input58, function(d) {
|
const leadingZeros = _.takeWhile(input58, function(d) {
|
||||||
return d === 0;
|
return d === 0;
|
||||||
});
|
});
|
||||||
var out = convertBase(input58, 58, 256);
|
const out = convertBase(input58, 58, 256);
|
||||||
return leadingZeros.concat(out);
|
return leadingZeros.concat(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
function Base58(alphabet) {
|
function Base58(alphabet) {
|
||||||
var indexes = utils.arraySet(128, -1);
|
const indexes = utils.arraySet(128, -1);
|
||||||
for (var i = 0; i < alphabet.length; i++) {
|
for (let i = 0; i < alphabet.length; i++) {
|
||||||
indexes[alphabet.charCodeAt(i)] = i;
|
indexes[alphabet.charCodeAt(i)] = i;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -104,16 +104,16 @@ Base.decode = function(input, alpha) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Base.verify_checksum = function(bytes) {
|
Base.verify_checksum = function(bytes) {
|
||||||
var computed = sha256(sha256(bytes.slice(0, -4))).slice(0, 4);
|
const computed = sha256(sha256(bytes.slice(0, -4))).slice(0, 4);
|
||||||
var checksum = bytes.slice(-4);
|
const checksum = bytes.slice(-4);
|
||||||
return _.isEqual(computed, checksum);
|
return _.isEqual(computed, checksum);
|
||||||
};
|
};
|
||||||
|
|
||||||
// --> input: Array
|
// --> input: Array
|
||||||
// <-- String
|
// <-- String
|
||||||
Base.encode_check = function(version, input, alphabet) {
|
Base.encode_check = function(version, input, alphabet) {
|
||||||
var buffer = [].concat(version, input);
|
const buffer = [].concat(version, input);
|
||||||
var check = sha256(sha256(buffer)).slice(0, 4);
|
const check = sha256(sha256(buffer)).slice(0, 4);
|
||||||
|
|
||||||
return Base.encode([].concat(buffer, check), alphabet);
|
return Base.encode([].concat(buffer, check), alphabet);
|
||||||
};
|
};
|
||||||
@@ -121,7 +121,7 @@ Base.encode_check = function(version, input, alphabet) {
|
|||||||
// --> input : String
|
// --> input : String
|
||||||
// <-- NaN || sjcl.bn
|
// <-- NaN || sjcl.bn
|
||||||
Base.decode_check = function(version, input, alphabet) {
|
Base.decode_check = function(version, input, alphabet) {
|
||||||
var buffer = Base.decode(input, alphabet);
|
const buffer = Base.decode(input, alphabet);
|
||||||
|
|
||||||
if (!buffer || buffer.length < 5) {
|
if (!buffer || buffer.length < 5) {
|
||||||
return NaN;
|
return NaN;
|
||||||
@@ -134,8 +134,8 @@ Base.decode_check = function(version, input, alphabet) {
|
|||||||
|
|
||||||
// Multiple allowed versions
|
// Multiple allowed versions
|
||||||
if (Array.isArray(version) && _.every(version, function(v) {
|
if (Array.isArray(version) && _.every(version, function(v) {
|
||||||
return v !== buffer[0];
|
return v !== buffer[0];
|
||||||
})) {
|
})) {
|
||||||
return NaN;
|
return NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,16 +40,16 @@ exports.types = require('./serializedtypes');
|
|||||||
|
|
||||||
// camelCase to under_scored API conversion
|
// camelCase to under_scored API conversion
|
||||||
function attachUnderscored(name) {
|
function attachUnderscored(name) {
|
||||||
var o = exports[name];
|
const o = exports[name];
|
||||||
|
|
||||||
Object.keys(o.prototype).forEach(function(key) {
|
Object.keys(o.prototype).forEach(function(key) {
|
||||||
var UPPERCASE = /([A-Z]{1})[a-z]+/g;
|
const UPPERCASE = /([A-Z]{1})[a-z]+/g;
|
||||||
|
|
||||||
if (!UPPERCASE.test(key)) {
|
if (!UPPERCASE.test(key)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var underscored = key.replace(UPPERCASE, function(c) {
|
const underscored = key.replace(UPPERCASE, function(c) {
|
||||||
return '_' + c.toLowerCase();
|
return '_' + c.toLowerCase();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
/* eslint-disable valid-jsdoc */
|
/* eslint-disable valid-jsdoc */
|
||||||
'use strict';
|
'use strict';
|
||||||
var async = require('async');
|
const async = require('async');
|
||||||
var sjcl = require('./utils').sjcl;
|
const sjcl = require('./utils').sjcl;
|
||||||
var Remote = require('./remote').Remote;
|
const Remote = require('./remote').Remote;
|
||||||
var Seed = require('./seed').Seed;
|
const Seed = require('./seed').Seed;
|
||||||
var KeyPair = require('./keypair').KeyPair;
|
const KeyPair = require('./keypair').KeyPair;
|
||||||
var Account = require('./account').Account;
|
const Account = require('./account').Account;
|
||||||
var UInt160 = require('./uint160').UInt160;
|
const UInt160 = require('./uint160').UInt160;
|
||||||
|
|
||||||
// Message class (static)
|
// Message class (static)
|
||||||
var Message = {};
|
const Message = {};
|
||||||
|
|
||||||
Message.hashFunction = sjcl.hash.sha512.hash;
|
Message.hashFunction = sjcl.hash.sha512.hash;
|
||||||
Message.MAGIC_BYTES = 'Ripple Signed Message:\n';
|
Message.MAGIC_BYTES = 'Ripple Signed Message:\n';
|
||||||
|
|
||||||
var REGEX_HEX = /^[0-9a-fA-F]+$/;
|
const REGEX_HEX = /^[0-9a-fA-F]+$/;
|
||||||
var REGEX_BASE64 =
|
const REGEX_BASE64 =
|
||||||
/^([A-Za-z0-9\+]{4})*([A-Za-z0-9\+]{2}==)|([A-Za-z0-9\+]{3}=)?$/;
|
/^([A-Za-z0-9\+]{4})*([A-Za-z0-9\+]{2}==)|([A-Za-z0-9\+]{3}=)?$/;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,25 +58,24 @@ Message.signMessage = function(message, secret_key, account) {
|
|||||||
* the secret generator will be used.
|
* the secret generator will be used.
|
||||||
* @returns {Base64-encoded String} signature
|
* @returns {Base64-encoded String} signature
|
||||||
*/
|
*/
|
||||||
Message.signHash = function(hash, secret_key, account) {
|
Message.signHash = function(_hash, secret_key_, account) {
|
||||||
|
|
||||||
if (typeof hash === 'string' && /^[0-9a-fA-F]+$/.test(hash)) {
|
const hash = typeof _hash === 'string' && /^[0-9a-fA-F]+$/.test(_hash) ?
|
||||||
hash = sjcl.codec.hex.toBits(hash);
|
sjcl.codec.hex.toBits(_hash) : _hash;
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof hash !== 'object' || hash.length <= 0
|
if (typeof hash !== 'object' ||
|
||||||
|| typeof hash[0] !== 'number') {
|
typeof hash[0] !== 'number' ||
|
||||||
|
hash.length <= 0) {
|
||||||
throw new Error('Hash must be a bitArray or hex-encoded string');
|
throw new Error('Hash must be a bitArray or hex-encoded string');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(secret_key instanceof sjcl.ecc.ecdsa.secretKey)) {
|
const secret_key = !(secret_key_ instanceof sjcl.ecc.ecdsa.secretKey) ?
|
||||||
secret_key = Seed.from_json(secret_key).get_key(account)._secret;
|
Seed.from_json(secret_key_).get_key(account)._secret : secret_key_;
|
||||||
}
|
|
||||||
|
|
||||||
var PARANOIA_256_BITS = 6; // sjcl constant for ensuring 256 bits of entropy
|
const PARANOIA_256_BITS = 6; // sjcl constant for ensuring 256 bits of entropy
|
||||||
var signature_bits = secret_key.signWithRecoverablePublicKey(hash,
|
const signature_bits = secret_key.signWithRecoverablePublicKey(hash,
|
||||||
PARANOIA_256_BITS);
|
PARANOIA_256_BITS);
|
||||||
var signature_base64 = sjcl.codec.base64.fromBits(signature_bits);
|
const signature_base64 = sjcl.codec.base64.fromBits(signature_bits);
|
||||||
|
|
||||||
return signature_base64;
|
return signature_base64;
|
||||||
|
|
||||||
@@ -139,34 +138,30 @@ Message.verifyMessageSignature = function(data, remote, callback) {
|
|||||||
*/
|
*/
|
||||||
Message.verifyHashSignature = function(data, remote, callback) {
|
Message.verifyHashSignature = function(data, remote, callback) {
|
||||||
|
|
||||||
var hash,
|
|
||||||
account,
|
|
||||||
signature;
|
|
||||||
|
|
||||||
if (typeof callback !== 'function') {
|
if (typeof callback !== 'function') {
|
||||||
throw new Error('Must supply callback function');
|
throw new Error('Must supply callback function');
|
||||||
}
|
}
|
||||||
|
|
||||||
hash = data.hash;
|
const hash = REGEX_HEX.test(data.hash) ?
|
||||||
if (hash && typeof hash === 'string' && REGEX_HEX.test(hash)) {
|
sjcl.codec.hex.toBits(data.hash) :
|
||||||
hash = sjcl.codec.hex.toBits(hash);
|
data.hash;
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof hash !== 'object' || hash.length <= 0
|
if (typeof hash !== 'object' || hash.length <= 0
|
||||||
|| typeof hash[0] !== 'number') {
|
|| typeof hash[0] !== 'number') {
|
||||||
return callback(new Error('Hash must be a bitArray or hex-encoded string'));
|
return callback(new Error('Hash must be a bitArray or hex-encoded string'));
|
||||||
}
|
}
|
||||||
|
|
||||||
account = data.account || data.address;
|
const account = data.account || data.address;
|
||||||
if (!account || !UInt160.from_json(account).is_valid()) {
|
if (!account || !UInt160.from_json(account).is_valid()) {
|
||||||
return callback(new Error('Account must be a valid ripple address'));
|
return callback(new Error('Account must be a valid ripple address'));
|
||||||
}
|
}
|
||||||
|
|
||||||
signature = data.signature;
|
const signature = data.signature;
|
||||||
if (typeof signature !== 'string' || !REGEX_BASE64.test(signature)) {
|
if (typeof signature !== 'string' || !REGEX_BASE64.test(signature)) {
|
||||||
return callback(new Error('Signature must be a Base64-encoded string'));
|
return callback(new Error('Signature must be a Base64-encoded string'));
|
||||||
}
|
}
|
||||||
signature = sjcl.codec.base64.toBits(signature);
|
|
||||||
|
const signature_bits = sjcl.codec.base64.toBits(signature);
|
||||||
|
|
||||||
if (!(remote instanceof Remote) || remote.state !== 'online') {
|
if (!(remote instanceof Remote) || remote.state !== 'online') {
|
||||||
return callback(new Error(
|
return callback(new Error(
|
||||||
@@ -174,11 +169,10 @@ Message.verifyHashSignature = function(data, remote, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function recoverPublicKey(async_callback) {
|
function recoverPublicKey(async_callback) {
|
||||||
|
let public_key;
|
||||||
var public_key;
|
|
||||||
try {
|
try {
|
||||||
public_key =
|
public_key =
|
||||||
sjcl.ecc.ecdsa.publicKey.recoverFromSignature(hash, signature);
|
sjcl.ecc.ecdsa.publicKey.recoverFromSignature(hash, signature_bits);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return async_callback(err);
|
return async_callback(err);
|
||||||
}
|
}
|
||||||
@@ -194,16 +188,16 @@ Message.verifyHashSignature = function(data, remote, callback) {
|
|||||||
function checkPublicKeyIsValid(public_key, async_callback) {
|
function checkPublicKeyIsValid(public_key, async_callback) {
|
||||||
|
|
||||||
// Get hex-encoded public key
|
// Get hex-encoded public key
|
||||||
var key_pair = new KeyPair();
|
const key_pair = new KeyPair();
|
||||||
key_pair._pubkey = public_key;
|
key_pair._pubkey = public_key;
|
||||||
var public_key_hex = key_pair.to_hex_pub();
|
const public_key_hex = key_pair.to_hex_pub();
|
||||||
|
|
||||||
var account_class_instance = new Account(remote, account);
|
const account_class_instance = new Account(remote, account);
|
||||||
account_class_instance.publicKeyIsActive(public_key_hex, async_callback);
|
account_class_instance.publicKeyIsActive(public_key_hex, async_callback);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var steps = [
|
const steps = [
|
||||||
recoverPublicKey,
|
recoverPublicKey,
|
||||||
checkPublicKeyIsValid
|
checkPublicKeyIsValid
|
||||||
];
|
];
|
||||||
|
|||||||
36
src/seed.js
36
src/seed.js
@@ -4,16 +4,16 @@
|
|||||||
// Seed support
|
// Seed support
|
||||||
//
|
//
|
||||||
|
|
||||||
var extend = require('extend');
|
const extend = require('extend');
|
||||||
var utils = require('./utils');
|
const utils = require('./utils');
|
||||||
var sjcl = utils.sjcl;
|
const sjcl = utils.sjcl;
|
||||||
|
|
||||||
var Base = require('./base').Base;
|
const Base = require('./base').Base;
|
||||||
var UInt = require('./uint').UInt;
|
const UInt = require('./uint').UInt;
|
||||||
var UInt160 = require('./uint160').UInt160;
|
const UInt160 = require('./uint160').UInt160;
|
||||||
var KeyPair = require('./keypair').KeyPair;
|
const KeyPair = require('./keypair').KeyPair;
|
||||||
|
|
||||||
var Seed = extend(function() {
|
const Seed = extend(function() {
|
||||||
this._curve = sjcl.ecc.curves.k256;
|
this._curve = sjcl.ecc.curves.k256;
|
||||||
this._value = NaN;
|
this._value = NaN;
|
||||||
}, UInt);
|
}, UInt);
|
||||||
@@ -49,8 +49,8 @@ Seed.prototype.parse_passphrase = function(j) {
|
|||||||
throw new Error('Passphrase must be a string');
|
throw new Error('Passphrase must be a string');
|
||||||
}
|
}
|
||||||
|
|
||||||
var hash = sjcl.hash.sha512.hash(sjcl.codec.utf8String.toBits(j));
|
const hash = sjcl.hash.sha512.hash(sjcl.codec.utf8String.toBits(j));
|
||||||
var bits = sjcl.bitArray.bitSlice(hash, 0, 128);
|
const bits = sjcl.bitArray.bitSlice(hash, 0, 128);
|
||||||
|
|
||||||
this.parse_bits(bits);
|
this.parse_bits(bits);
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ Seed.prototype.to_json = function() {
|
|||||||
return NaN;
|
return NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
var output = Base.encode_check(Base.VER_FAMILY_SEED, this.to_bytes());
|
const output = Base.encode_check(Base.VER_FAMILY_SEED, this.to_bytes());
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
@@ -96,8 +96,8 @@ function firstHalfOfSHA512(bytes) {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
Seed.prototype.get_key = function(account, maxLoops) {
|
Seed.prototype.get_key = function(account, maxLoops) {
|
||||||
var account_number = 0, address;
|
let account_number = 0, address;
|
||||||
var max_loops = maxLoops || 1;
|
let max_loops = maxLoops || 1;
|
||||||
|
|
||||||
if (!this.is_valid()) {
|
if (!this.is_valid()) {
|
||||||
throw new Error('Cannot generate keys from invalid seed!');
|
throw new Error('Cannot generate keys from invalid seed!');
|
||||||
@@ -111,9 +111,9 @@ Seed.prototype.get_key = function(account, maxLoops) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var private_gen, public_gen;
|
let private_gen, public_gen;
|
||||||
var curve = this._curve;
|
const curve = this._curve;
|
||||||
var i = 0;
|
let i = 0;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
private_gen = sjcl.bn.fromBits(
|
private_gen = sjcl.bn.fromBits(
|
||||||
@@ -123,8 +123,8 @@ Seed.prototype.get_key = function(account, maxLoops) {
|
|||||||
|
|
||||||
public_gen = curve.G.mult(private_gen);
|
public_gen = curve.G.mult(private_gen);
|
||||||
|
|
||||||
var sec;
|
let sec;
|
||||||
var key_pair;
|
let key_pair;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
|
|||||||
42
src/utils.js
42
src/utils.js
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function getMantissaDecimalString(bignum) {
|
function getMantissaDecimalString(bignum) {
|
||||||
var mantissa = bignum.toPrecision(16)
|
let mantissa = bignum.toPrecision(16)
|
||||||
.replace(/\./, '') // remove decimal point
|
.replace(/\./, '') // remove decimal point
|
||||||
.replace(/e.*/, '') // remove scientific notation
|
.replace(/e.*/, '') // remove scientific notation
|
||||||
.replace(/^0*/, ''); // remove leading zeroes
|
.replace(/^0*/, ''); // remove leading zeroes
|
||||||
@@ -19,9 +19,9 @@ function trace(comment, func) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function arraySet(count, value) {
|
function arraySet(count, value) {
|
||||||
var a = new Array(count);
|
const a = new Array(count);
|
||||||
|
|
||||||
for (var i = 0; i < count; i++) {
|
for (let i = 0; i < count; i++) {
|
||||||
a[i] = value;
|
a[i] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,8 +29,8 @@ function arraySet(count, value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function hexToString(h) {
|
function hexToString(h) {
|
||||||
var a = [];
|
const a = [];
|
||||||
var i = 0;
|
let i = 0;
|
||||||
|
|
||||||
if (h.length % 2) {
|
if (h.length % 2) {
|
||||||
a.push(String.fromCharCode(parseInt(h.substring(0, 1), 16)));
|
a.push(String.fromCharCode(parseInt(h.substring(0, 1), 16)));
|
||||||
@@ -45,18 +45,18 @@ function hexToString(h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function stringToHex(s) {
|
function stringToHex(s) {
|
||||||
var result = '';
|
let result = '';
|
||||||
for (var i = 0; i < s.length; i++) {
|
for (let i = 0; i < s.length; i++) {
|
||||||
var b = s.charCodeAt(i);
|
const b = s.charCodeAt(i);
|
||||||
result += b < 16 ? '0' + b.toString(16) : b.toString(16);
|
result += b < 16 ? '0' + b.toString(16) : b.toString(16);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringToArray(s) {
|
function stringToArray(s) {
|
||||||
var a = new Array(s.length);
|
const a = new Array(s.length);
|
||||||
|
|
||||||
for (var i = 0; i < a.length; i += 1) {
|
for (let i = 0; i < a.length; i += 1) {
|
||||||
a[i] = s.charCodeAt(i);
|
a[i] = s.charCodeAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,14 +69,15 @@ function hexToArray(h) {
|
|||||||
|
|
||||||
function arrayToHex(a) {
|
function arrayToHex(a) {
|
||||||
return a.map(function(byteValue) {
|
return a.map(function(byteValue) {
|
||||||
var hex = byteValue.toString(16);
|
const hex = byteValue.toString(16);
|
||||||
return hex.length > 1 ? hex : '0' + hex;
|
return hex.length > 1 ? hex : '0' + hex;
|
||||||
}).join('');
|
}).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
function chunkString(str, n, leftAlign) {
|
function chunkString(str, n, leftAlign) {
|
||||||
var ret = [];
|
const ret = [];
|
||||||
var i = 0, len = str.length;
|
let i = 0;
|
||||||
|
const len = str.length;
|
||||||
|
|
||||||
if (leftAlign) {
|
if (leftAlign) {
|
||||||
i = str.length % n;
|
i = str.length % n;
|
||||||
@@ -103,10 +104,10 @@ function assert(assertion, msg) {
|
|||||||
* @return {Array} unique values (for string representation of value) in `arr`
|
* @return {Array} unique values (for string representation of value) in `arr`
|
||||||
*/
|
*/
|
||||||
function arrayUnique(arr) {
|
function arrayUnique(arr) {
|
||||||
var u = {}, a = [];
|
const u = {}, a = [];
|
||||||
|
|
||||||
for (var i = 0, l = arr.length; i < l; i++) {
|
for (let i = 0, l = arr.length; i < l; i++) {
|
||||||
var k = arr[i];
|
const k = arr[i];
|
||||||
if (u[k]) {
|
if (u[k]) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -131,11 +132,10 @@ function toTimestamp(rpepoch) {
|
|||||||
* @return {Number} seconds since ripple epoch ( 1/1/2000 GMT)
|
* @return {Number} seconds since ripple epoch ( 1/1/2000 GMT)
|
||||||
*/
|
*/
|
||||||
function fromTimestamp(timestamp) {
|
function fromTimestamp(timestamp) {
|
||||||
if (timestamp instanceof Date) {
|
const timestamp_ = timestamp instanceof Date ?
|
||||||
timestamp = timestamp.getTime();
|
timestamp.getTime() :
|
||||||
}
|
timestamp;
|
||||||
|
return Math.round(timestamp_ / 1000) - 0x386D4380;
|
||||||
return Math.round(timestamp / 1000) - 0x386D4380;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.time = {
|
exports.time = {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var Base = require('ripple-lib').Base;
|
const Base = require('ripple-lib').Base;
|
||||||
var fixtures = require('./fixtures/base58.json');
|
const fixtures = require('./fixtures/base58.json');
|
||||||
|
|
||||||
function digitArray(str) {
|
function digitArray(str) {
|
||||||
return str.split('').map(function(d) {
|
return str.split('').map(function(d) {
|
||||||
@@ -10,8 +10,8 @@ function digitArray(str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function hexToByteArray(hex) {
|
function hexToByteArray(hex) {
|
||||||
var byteArray = [];
|
const byteArray = [];
|
||||||
for (var i = 0; i < hex.length / 2; i++) {
|
for (let i = 0; i < hex.length / 2; i++) {
|
||||||
byteArray.push(parseInt(hex.slice(2 * i, 2 * i + 2), 16));
|
byteArray.push(parseInt(hex.slice(2 * i, 2 * i + 2), 16));
|
||||||
}
|
}
|
||||||
return byteArray;
|
return byteArray;
|
||||||
@@ -19,46 +19,46 @@ function hexToByteArray(hex) {
|
|||||||
|
|
||||||
describe('Base', function() {
|
describe('Base', function() {
|
||||||
describe('encode_check', function() {
|
describe('encode_check', function() {
|
||||||
it('0', function () {
|
it('0', function() {
|
||||||
var encoded = Base.encode_check(0, digitArray('00000000000000000000'));
|
const encoded = Base.encode_check(0, digitArray('00000000000000000000'));
|
||||||
assert.strictEqual(encoded, 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
|
assert.strictEqual(encoded, 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
|
||||||
});
|
});
|
||||||
it('1', function () {
|
it('1', function() {
|
||||||
var encoded = Base.encode_check(0, digitArray('00000000000000000001'));
|
const encoded = Base.encode_check(0, digitArray('00000000000000000001'));
|
||||||
assert.strictEqual(encoded, 'rrrrrrrrrrrrrrrrrrrrBZbvji');
|
assert.strictEqual(encoded, 'rrrrrrrrrrrrrrrrrrrrBZbvji');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('decode_check', function() {
|
describe('decode_check', function() {
|
||||||
it('rrrrrrrrrrrrrrrrrrrrrhoLvTp', function() {
|
it('rrrrrrrrrrrrrrrrrrrrrhoLvTp', function() {
|
||||||
var decoded = Base.decode_check(0, 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
|
const decoded = Base.decode_check(0, 'rrrrrrrrrrrrrrrrrrrrrhoLvTp');
|
||||||
assert(decoded.equals(0));
|
assert(decoded.equals(0));
|
||||||
});
|
});
|
||||||
it('rrrrrrrrrrrrrrrrrrrrBZbvji', function() {
|
it('rrrrrrrrrrrrrrrrrrrrBZbvji', function() {
|
||||||
var decoded = Base.decode_check(0, 'rrrrrrrrrrrrrrrrrrrrBZbvji');
|
const decoded = Base.decode_check(0, 'rrrrrrrrrrrrrrrrrrrrBZbvji');
|
||||||
assert(decoded.equals(1));
|
assert(decoded.equals(1));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('decode-encode identity', function() {
|
describe('decode-encode identity', function() {
|
||||||
it('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function() {
|
it('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function() {
|
||||||
var decoded = Base.decode('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
const decoded = Base.decode('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
var encoded = Base.encode(decoded);
|
const encoded = Base.encode(decoded);
|
||||||
assert.strictEqual(encoded, 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
assert.strictEqual(encoded, 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('encode', function() {
|
describe('encode', function() {
|
||||||
it('fixtures', function() {
|
it('fixtures', function() {
|
||||||
for (var i = 0; i < fixtures.ripple.length; i++) {
|
for (let i = 0; i < fixtures.ripple.length; i++) {
|
||||||
var testCase = fixtures.ripple[i];
|
const testCase = fixtures.ripple[i];
|
||||||
var encoded = Base.encode(hexToByteArray(testCase.hex));
|
const encoded = Base.encode(hexToByteArray(testCase.hex));
|
||||||
assert.strictEqual(encoded, testCase.string);
|
assert.strictEqual(encoded, testCase.string);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('decode', function() {
|
describe('decode', function() {
|
||||||
it('fixtures', function() {
|
it('fixtures', function() {
|
||||||
for (var i = 0; i < fixtures.ripple.length; i++) {
|
for (let i = 0; i < fixtures.ripple.length; i++) {
|
||||||
var testCase = fixtures.ripple[i];
|
const testCase = fixtures.ripple[i];
|
||||||
var decoded = Base.decode(testCase.string);
|
const decoded = Base.decode(testCase.string);
|
||||||
assert.deepEqual(decoded, hexToByteArray(testCase.hex));
|
assert.deepEqual(decoded, hexToByteArray(testCase.hex));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
/* eslint-disable max-len */
|
/* eslint-disable max-len */
|
||||||
'use strict';
|
'use strict';
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var sjcl = require('ripple-lib').sjcl;
|
const sjcl = require('ripple-lib').sjcl;
|
||||||
var Message = require('ripple-lib').Message;
|
const Message = require('ripple-lib').Message;
|
||||||
var Seed = require('ripple-lib').Seed;
|
const Seed = require('ripple-lib').Seed;
|
||||||
var Remote = require('ripple-lib').Remote;
|
const Remote = require('ripple-lib').Remote;
|
||||||
|
|
||||||
describe('Message', function() {
|
describe('Message', function() {
|
||||||
|
|
||||||
@@ -12,11 +12,11 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should prepend the MAGIC_BYTES, call the hashFunction, and then call signHash', function() {
|
it('should prepend the MAGIC_BYTES, call the hashFunction, and then call signHash', function() {
|
||||||
|
|
||||||
var normal_signHash = Message.signHash;
|
const normal_signHash = Message.signHash;
|
||||||
|
|
||||||
var message_text = 'Hello World!';
|
const message_text = 'Hello World!';
|
||||||
|
|
||||||
var signHash_called = false;
|
let signHash_called = false;
|
||||||
Message.signHash = function(hash) {
|
Message.signHash = function(hash) {
|
||||||
signHash_called = true;
|
signHash_called = true;
|
||||||
assert.deepEqual(hash, Message.hashFunction(Message.MAGIC_BYTES + message_text));
|
assert.deepEqual(hash, Message.hashFunction(Message.MAGIC_BYTES + message_text));
|
||||||
@@ -35,22 +35,22 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should accept the hash as either a hex string or a bitArray', function() {
|
it('should accept the hash as either a hex string or a bitArray', function() {
|
||||||
|
|
||||||
var normal_random = sjcl.random.randomWords;
|
const normal_random = sjcl.random.randomWords;
|
||||||
|
|
||||||
sjcl.random.randomWords = function(num_words) {
|
sjcl.random.randomWords = function(num_words) {
|
||||||
var words = [];
|
const words = [];
|
||||||
for (var w = 0; w < num_words; w++) {
|
for (let w = 0; w < num_words; w++) {
|
||||||
words.push(sjcl.codec.hex.toBits('00000000'));
|
words.push(sjcl.codec.hex.toBits('00000000'));
|
||||||
}
|
}
|
||||||
return words;
|
return words;
|
||||||
};
|
};
|
||||||
|
|
||||||
var secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
const secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
||||||
// var address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
// const address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
||||||
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
const hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
||||||
|
|
||||||
var signature1 = Message.signHash(hash, secret_string);
|
const signature1 = Message.signHash(hash, secret_string);
|
||||||
var signature2 = Message.signHash(sjcl.codec.hex.toBits(hash), secret_string);
|
const signature2 = Message.signHash(sjcl.codec.hex.toBits(hash), secret_string);
|
||||||
|
|
||||||
assert.strictEqual(signature1, signature2);
|
assert.strictEqual(signature1, signature2);
|
||||||
|
|
||||||
@@ -60,22 +60,22 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should accept the secret as a string or scjl.ecc.ecdsa.secretKey object', function() {
|
it('should accept the secret as a string or scjl.ecc.ecdsa.secretKey object', function() {
|
||||||
|
|
||||||
var normal_random = sjcl.random.randomWords;
|
const normal_random = sjcl.random.randomWords;
|
||||||
|
|
||||||
sjcl.random.randomWords = function(num_words) {
|
sjcl.random.randomWords = function(num_words) {
|
||||||
var words = [];
|
const words = [];
|
||||||
for (var w = 0; w < num_words; w++) {
|
for (let w = 0; w < num_words; w++) {
|
||||||
words.push(sjcl.codec.hex.toBits('00000000'));
|
words.push(sjcl.codec.hex.toBits('00000000'));
|
||||||
}
|
}
|
||||||
return words;
|
return words;
|
||||||
};
|
};
|
||||||
|
|
||||||
var secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
const secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
||||||
// var address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
// const address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
||||||
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
const hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
||||||
|
|
||||||
var signature1 = Message.signHash(hash, secret_string);
|
const signature1 = Message.signHash(hash, secret_string);
|
||||||
var signature2 = Message.signHash(hash, Seed.from_json(secret_string).get_key()._secret);
|
const signature2 = Message.signHash(hash, Seed.from_json(secret_string).get_key()._secret);
|
||||||
|
|
||||||
assert.strictEqual(signature1, signature2);
|
assert.strictEqual(signature1, signature2);
|
||||||
|
|
||||||
@@ -94,8 +94,8 @@ describe('Message', function() {
|
|||||||
// The rest will be assumed to be a passphrase.
|
// The rest will be assumed to be a passphrase.
|
||||||
|
|
||||||
// This is a bad b58 seed
|
// This is a bad b58 seed
|
||||||
var secret_string = 'sbadsafRpB5euNL52PZPTSqrE9gvuFwTC';
|
const secret_string = 'sbadsafRpB5euNL52PZPTSqrE9gvuFwTC';
|
||||||
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
const hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
Message.signHash(hash, secret_string);
|
Message.signHash(hash, secret_string);
|
||||||
@@ -105,8 +105,8 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should throw an error if the parameters are reversed', function() {
|
it('should throw an error if the parameters are reversed', function() {
|
||||||
|
|
||||||
var secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
const secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
||||||
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
const hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
||||||
|
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
Message.signHash(secret_string, hash);
|
Message.signHash(secret_string, hash);
|
||||||
@@ -127,23 +127,23 @@ describe('Message', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should produce a base64-encoded signature', function() {
|
it('should produce a base64-encoded signature', function() {
|
||||||
var REGEX_BASE64 = /^([A-Za-z0-9\+]{4})*([A-Za-z0-9\+]{2}==)|([A-Za-z0-9\+]{3}=)?$/;
|
const REGEX_BASE64 = /^([A-Za-z0-9\+]{4})*([A-Za-z0-9\+]{2}==)|([A-Za-z0-9\+]{3}=)?$/;
|
||||||
|
|
||||||
var normal_random = sjcl.random.randomWords;
|
const normal_random = sjcl.random.randomWords;
|
||||||
|
|
||||||
sjcl.random.randomWords = function(num_words) {
|
sjcl.random.randomWords = function(num_words) {
|
||||||
var words = [];
|
const words = [];
|
||||||
for (var w = 0; w < num_words; w++) {
|
for (let w = 0; w < num_words; w++) {
|
||||||
words.push(sjcl.codec.hex.toBits('00000000'));
|
words.push(sjcl.codec.hex.toBits('00000000'));
|
||||||
}
|
}
|
||||||
return words;
|
return words;
|
||||||
};
|
};
|
||||||
|
|
||||||
var secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
const secret_string = 'safRpB5euNL52PZPTSqrE9gvuFwTC';
|
||||||
// var address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
// const address = 'rLLzaq61D633b5hhbNXKM9CkrYHboobVv3';
|
||||||
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
const hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
|
||||||
|
|
||||||
var signature = Message.signHash(hash, secret_string);
|
const signature = Message.signHash(hash, secret_string);
|
||||||
|
|
||||||
assert(REGEX_BASE64.test(signature));
|
assert(REGEX_BASE64.test(signature));
|
||||||
|
|
||||||
@@ -156,14 +156,14 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should prepend the MAGIC_BYTES, call the hashFunction, and then call verifyHashSignature', function() {
|
it('should prepend the MAGIC_BYTES, call the hashFunction, and then call verifyHashSignature', function() {
|
||||||
|
|
||||||
var normal_verifyHashSignature = Message.verifyHashSignature;
|
const normal_verifyHashSignature = Message.verifyHashSignature;
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
signature: 'AAAAGzFa1pYjhssCpDFZgFSnYQ8qCnMkLaZrg0mXZyNQ2NxgMQ8z9U3ngYerxSZCEt3Q4raMIpt03db7jDNGbfmHy8I='
|
signature: 'AAAAGzFa1pYjhssCpDFZgFSnYQ8qCnMkLaZrg0mXZyNQ2NxgMQ8z9U3ngYerxSZCEt3Q4raMIpt03db7jDNGbfmHy8I='
|
||||||
};
|
};
|
||||||
|
|
||||||
var verifyHashSignature_called = false;
|
let verifyHashSignature_called = false;
|
||||||
Message.verifyHashSignature = function(vhs_data, remote, callback) {
|
Message.verifyHashSignature = function(vhs_data, remote, callback) {
|
||||||
verifyHashSignature_called = true;
|
verifyHashSignature_called = true;
|
||||||
|
|
||||||
@@ -188,7 +188,7 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should throw an error if a callback function is not supplied', function() {
|
it('should throw an error if a callback function is not supplied', function() {
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
||||||
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk=',
|
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk=',
|
||||||
@@ -202,13 +202,13 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should respond with an error if the hash is missing or invalid', function(done) {
|
it('should respond with an error if the hash is missing or invalid', function(done) {
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk=',
|
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk=',
|
||||||
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
|
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
|
||||||
};
|
};
|
||||||
|
|
||||||
var test_remote = new Remote();
|
const test_remote = new Remote();
|
||||||
test_remote.state = 'online';
|
test_remote.state = 'online';
|
||||||
|
|
||||||
Message.verifyHashSignature(data, test_remote, function(err) {
|
Message.verifyHashSignature(data, test_remote, function(err) {
|
||||||
@@ -220,13 +220,13 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should respond with an error if the account is missing or invalid', function(done) {
|
it('should respond with an error if the account is missing or invalid', function(done) {
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
||||||
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk='
|
signature: 'AAAAHOUJQzG/7BO82fGNt1TNE+GGVXKuQQ0N2nTO+iJETE69PiHnaAkkOzovM177OosxbKjpt3KvwuJflgUB2YGvgjk='
|
||||||
};
|
};
|
||||||
|
|
||||||
var test_remote = new Remote();
|
const test_remote = new Remote();
|
||||||
test_remote.state = 'online';
|
test_remote.state = 'online';
|
||||||
|
|
||||||
Message.verifyHashSignature(data, test_remote, function(err) {
|
Message.verifyHashSignature(data, test_remote, function(err) {
|
||||||
@@ -238,13 +238,13 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should respond with an error if the signature is missing or invalid', function(done) {
|
it('should respond with an error if the signature is missing or invalid', function(done) {
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
hash: '861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8',
|
||||||
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
|
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz'
|
||||||
};
|
};
|
||||||
|
|
||||||
var test_remote = new Remote();
|
const test_remote = new Remote();
|
||||||
test_remote.state = 'online';
|
test_remote.state = 'online';
|
||||||
|
|
||||||
Message.verifyHashSignature(data, test_remote, function(err) {
|
Message.verifyHashSignature(data, test_remote, function(err) {
|
||||||
@@ -256,17 +256,17 @@ describe('Message', function() {
|
|||||||
|
|
||||||
it('should respond true if the signature is valid and corresponds to an active public key for the account', function(done) {
|
it('should respond true if the signature is valid and corresponds to an active public key for the account', function(done) {
|
||||||
|
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
hash: 'e9a82ea40514787918959b1100481500a5d384030f8770575c6a587675025fe212e6623e25643f251666a7b8b23af476c2850a8ea92153de5724db432892c752',
|
hash: 'e9a82ea40514787918959b1100481500a5d384030f8770575c6a587675025fe212e6623e25643f251666a7b8b23af476c2850a8ea92153de5724db432892c752',
|
||||||
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz',
|
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz',
|
||||||
signature: 'AAAAHMIPCQGLgdnpX1Ccv1wHb56H4NggxIM6U08Qkb9mUjN2Vn9pZ3CHvq1yWLBi6NqpW+7kedLnmfu4VG2+y43p4Xs='
|
signature: 'AAAAHMIPCQGLgdnpX1Ccv1wHb56H4NggxIM6U08Qkb9mUjN2Vn9pZ3CHvq1yWLBi6NqpW+7kedLnmfu4VG2+y43p4Xs='
|
||||||
};
|
};
|
||||||
|
|
||||||
var test_remote = new Remote();
|
const test_remote = new Remote();
|
||||||
test_remote.state = 'online';
|
test_remote.state = 'online';
|
||||||
test_remote.requestAccountInfo = function(options, callback) {
|
test_remote.requestAccountInfo = function(options, callback) {
|
||||||
var account = options.account;
|
const account = options.account;
|
||||||
if (account === data.account) {
|
if (account === data.account) {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
'account_data': {
|
'account_data': {
|
||||||
@@ -292,17 +292,17 @@ describe('Message', function() {
|
|||||||
it('should respond false if a key can be recovered from the signature but it does not correspond to an active public key', function(done) {
|
it('should respond false if a key can be recovered from the signature but it does not correspond to an active public key', function(done) {
|
||||||
|
|
||||||
// Signature created by disabled master key
|
// Signature created by disabled master key
|
||||||
var data = {
|
const data = {
|
||||||
message: 'Hello world!',
|
message: 'Hello world!',
|
||||||
hash: 'e9a82ea40514787918959b1100481500a5d384030f8770575c6a587675025fe212e6623e25643f251666a7b8b23af476c2850a8ea92153de5724db432892c752',
|
hash: 'e9a82ea40514787918959b1100481500a5d384030f8770575c6a587675025fe212e6623e25643f251666a7b8b23af476c2850a8ea92153de5724db432892c752',
|
||||||
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz',
|
account: 'rKXCummUHnenhYudNb9UoJ4mGBR75vFcgz',
|
||||||
signature: 'AAAAG+dB/rAjZ5m8eQ/opcqQOJsFbKxOu9jq9KrOAlNO4OdcBDXyCBlkZqS9Xr8oZI2uh0boVsgYOS3pOLJz+Dh3Otk='
|
signature: 'AAAAG+dB/rAjZ5m8eQ/opcqQOJsFbKxOu9jq9KrOAlNO4OdcBDXyCBlkZqS9Xr8oZI2uh0boVsgYOS3pOLJz+Dh3Otk='
|
||||||
};
|
};
|
||||||
|
|
||||||
var test_remote = new Remote();
|
const test_remote = new Remote();
|
||||||
test_remote.state = 'online';
|
test_remote.state = 'online';
|
||||||
test_remote.requestAccountInfo = function(options, callback) {
|
test_remote.requestAccountInfo = function(options, callback) {
|
||||||
var account = options.account;
|
const account = options.account;
|
||||||
if (account === data.account) {
|
if (account === data.account) {
|
||||||
callback(null, {
|
callback(null, {
|
||||||
'account_data': {
|
'account_data': {
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
/* eslint max-len: 0 */
|
/* eslint max-len: 0 */
|
||||||
'use strict';
|
'use strict';
|
||||||
var assert = require('assert');
|
const assert = require('assert');
|
||||||
var Seed = require('ripple-lib').Seed;
|
const Seed = require('ripple-lib').Seed;
|
||||||
|
|
||||||
function assert_helper(seed_json, address_or_nth, expected) {
|
function assert_helper(seed_json, address_or_nth, expected) {
|
||||||
var seed = Seed.from_json(seed_json);
|
const seed = Seed.from_json(seed_json);
|
||||||
var keypair = seed.get_key(address_or_nth, 500);
|
const keypair = seed.get_key(address_or_nth, 500);
|
||||||
assert.strictEqual(keypair.to_hex_pub(), expected);
|
assert.strictEqual(keypair.to_hex_pub(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('Seed', function() {
|
describe('Seed', function() {
|
||||||
it('saESc82Vun7Ta5EJRzGJbrXb5HNYk', function () {
|
it('saESc82Vun7Ta5EJRzGJbrXb5HNYk', function() {
|
||||||
var seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk');
|
const seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk');
|
||||||
assert.strictEqual(seed.to_hex(), 'FF1CF838D02B2CF7B45BAC27F5F24F4F');
|
assert.strictEqual(seed.to_hex(), 'FF1CF838D02B2CF7B45BAC27F5F24F4F');
|
||||||
});
|
});
|
||||||
it('sp6iDHnmiPN7tQFHm5sCW59ax3hfE', function () {
|
it('sp6iDHnmiPN7tQFHm5sCW59ax3hfE', function() {
|
||||||
var seed = Seed.from_json('sp6iDHnmiPN7tQFHm5sCW59ax3hfE');
|
const seed = Seed.from_json('sp6iDHnmiPN7tQFHm5sCW59ax3hfE');
|
||||||
assert.strictEqual(seed.to_hex(), '00AD8DA764C3C8AF5F9B8D51C94B9E49');
|
assert.strictEqual(seed.to_hex(), '00AD8DA764C3C8AF5F9B8D51C94B9E49');
|
||||||
});
|
});
|
||||||
it('can generate many addresses', function () {
|
it('can generate many addresses', function() {
|
||||||
|
|
||||||
var test_data = [
|
const test_data = [
|
||||||
// Format:
|
// Format:
|
||||||
// [passphrase, address, nth-for-seed, expected-public-key]
|
// [passphrase, address, nth-for-seed, expected-public-key]
|
||||||
['masterpassphrase', 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', 0,
|
['masterpassphrase', 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', 0,
|
||||||
@@ -39,11 +39,11 @@ describe('Seed', function() {
|
|||||||
'023A2876EA130CBE7BBA0573C2DB4C4CEB9A7547666915BD40366CDC6150CF54DC']
|
'023A2876EA130CBE7BBA0573C2DB4C4CEB9A7547666915BD40366CDC6150CF54DC']
|
||||||
];
|
];
|
||||||
|
|
||||||
for (var nth = 0; nth < test_data.length; nth++) {
|
for (let nth = 0; nth < test_data.length; nth++) {
|
||||||
var seed_json = test_data[nth][0];
|
const seed_json = test_data[nth][0];
|
||||||
var address = test_data[nth][1];
|
const address = test_data[nth][1];
|
||||||
var nth_for_seed = test_data[nth][2];
|
const nth_for_seed = test_data[nth][2];
|
||||||
var expected = test_data[nth][3];
|
const expected = test_data[nth][3];
|
||||||
|
|
||||||
// `seed.get_key($ripple_address)` is arguably an ill concieved feature
|
// `seed.get_key($ripple_address)` is arguably an ill concieved feature
|
||||||
// as it needs to generate `nth` many keypairs and generate hashed public
|
// as it needs to generate `nth` many keypairs and generate hashed public
|
||||||
@@ -58,17 +58,17 @@ describe('Seed', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should return the key_pair for a valid account and secret pair', function() {
|
it('should return the key_pair for a valid account and secret pair', function() {
|
||||||
var address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
|
const address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
|
||||||
var seed = Seed.from_json('shsWGZcmZz6YsWWmcnpfr6fLTdtFV');
|
const seed = Seed.from_json('shsWGZcmZz6YsWWmcnpfr6fLTdtFV');
|
||||||
var keyPair = seed.get_key(address);
|
const keyPair = seed.get_key(address);
|
||||||
assert.strictEqual(keyPair.get_address().to_json(), address);
|
assert.strictEqual(keyPair.get_address().to_json(), address);
|
||||||
assert.strictEqual(keyPair.to_hex_pub(), '02F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D8');
|
assert.strictEqual(keyPair.to_hex_pub(), '02F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D8');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not find a KeyPair for a secret that does not belong to the given account', function() {
|
it('should not find a KeyPair for a secret that does not belong to the given account', function() {
|
||||||
var address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
|
const address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
|
||||||
var secret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';
|
const secret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';
|
||||||
var seed = Seed.from_json('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
|
const seed = Seed.from_json('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
|
||||||
try {
|
try {
|
||||||
seed.get_key(address);
|
seed.get_key(address);
|
||||||
assert(false, 'should throw an error');
|
assert(false, 'should throw an error');
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
var assert = require('assert');
|
'use strict';
|
||||||
var Seed = require('ripple-lib').Seed;
|
|
||||||
|
const assert = require('assert');
|
||||||
|
const Seed = require('ripple-lib').Seed;
|
||||||
|
|
||||||
function _isNaN(n) {
|
function _isNaN(n) {
|
||||||
return typeof n === 'number' && isNaN(n);
|
return typeof n === 'number' && isNaN(n);
|
||||||
@@ -7,17 +9,21 @@ function _isNaN(n) {
|
|||||||
|
|
||||||
describe('Signing', function() {
|
describe('Signing', function() {
|
||||||
describe('Keys', function() {
|
describe('Keys', function() {
|
||||||
it('SigningPubKey 1 (ripple-client issue #245)', function () {
|
it('SigningPubKey 1 (ripple-client issue #245)', function() {
|
||||||
var seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk');
|
const seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk');
|
||||||
var key = seed.get_key('rBZ4j6MsoctipM6GEyHSjQKzXG3yambDnZ');
|
const key = seed.get_key('rBZ4j6MsoctipM6GEyHSjQKzXG3yambDnZ');
|
||||||
var pub = key.to_hex_pub();
|
const pub = key.to_hex_pub();
|
||||||
assert.strictEqual(pub, '0396941B22791A448E5877A44CE98434DB217D6FB97D63F0DAD23BE49ED45173C9');
|
assert.strictEqual(
|
||||||
|
pub,
|
||||||
|
'0396941B22791A448E5877A44CE98434DB217D6FB97D63F0DAD23BE49ED45173C9');
|
||||||
});
|
});
|
||||||
it('SigningPubKey 2 (master seed)', function () {
|
it('SigningPubKey 2 (master seed)', function() {
|
||||||
var seed = Seed.from_json('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
|
const seed = Seed.from_json('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
|
||||||
var key = seed.get_key('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
const key = seed.get_key('rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh');
|
||||||
var pub = key.to_hex_pub();
|
const pub = key.to_hex_pub();
|
||||||
assert.strictEqual(pub, '0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020');
|
assert.strictEqual(
|
||||||
|
pub,
|
||||||
|
'0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('parse_json', function() {
|
describe('parse_json', function() {
|
||||||
@@ -26,12 +32,14 @@ describe('Signing', function() {
|
|||||||
});
|
});
|
||||||
it('hex string', function() {
|
it('hex string', function() {
|
||||||
// 32 0s is a valid hex repr of seed bytes
|
// 32 0s is a valid hex repr of seed bytes
|
||||||
var str = new Array(33).join('0');
|
const str = new Array(33).join('0');
|
||||||
assert.strictEqual((new Seed().parse_json(str).to_json()), 'sp6JS7f14BuwFY8Mw6bTtLKWauoUs');
|
assert.strictEqual((new Seed().parse_json(str).to_json()),
|
||||||
|
'sp6JS7f14BuwFY8Mw6bTtLKWauoUs');
|
||||||
});
|
});
|
||||||
it('passphrase', function() {
|
it('passphrase', function() {
|
||||||
var str = new Array(60).join('0');
|
const str = new Array(60).join('0');
|
||||||
assert.strictEqual('snFRPnVL3secohdpwSie8ANXdFQvG', new Seed().parse_json(str).to_json());
|
assert.strictEqual('snFRPnVL3secohdpwSie8ANXdFQvG',
|
||||||
|
new Seed().parse_json(str).to_json());
|
||||||
});
|
});
|
||||||
it('null', function() {
|
it('null', function() {
|
||||||
assert(_isNaN(new Seed().parse_json(null).to_json()));
|
assert(_isNaN(new Seed().parse_json(null).to_json()));
|
||||||
@@ -52,5 +60,3 @@ describe('Signing', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// vim:sw=2:sts=2:ts=8:et
|
|
||||||
|
|||||||
Reference in New Issue
Block a user