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