From ca83a142f8c1943c5c970d2e9bd811f8f580c5cb Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Fri, 6 Mar 2015 15:51:01 -0800 Subject: [PATCH] [TASK] Fix issues in base.js and add more tests --- src/js/ripple/base.js | 32 ++------- test/base-test.js | 27 ++++++++ test/base58-test.js | 17 ----- test/fixtures/base58.json | 141 ++++++++++++++++++++++++++++++++++++++ test/seed-test.js | 8 +++ 5 files changed, 183 insertions(+), 42 deletions(-) delete mode 100644 test/base58-test.js create mode 100644 test/fixtures/base58.json diff --git a/src/js/ripple/base.js b/src/js/ripple/base.js index bcbe6d37..c4da264d 100644 --- a/src/js/ripple/base.js +++ b/src/js/ripple/base.js @@ -31,7 +31,7 @@ function sha256(bytes) { function encodeString(alphabet, input) { if (input.length === 0) { - return []; + return ''; } var leadingZeros = _.takeWhile(input, function(d) { @@ -65,10 +65,7 @@ function decodeString(indexes, input) { return d === 0; }); var out = convertBase(input58, 58, 256); - var prefix = leadingZeros.map(function() { - return 0; - }); - return prefix.concat(out); + return leadingZeros.concat(out); } function Base58(alphabet) { @@ -109,16 +106,7 @@ 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); - var result = true; - - for (var i = 0; i < 4; i++) { - if (computed[i] !== checksum[i]) { - result = false; - break; - } - } - - return result; + return _.isEqual(computed, checksum); }; // --> input: Array @@ -145,16 +133,10 @@ Base.decode_check = function(version, input, alphabet) { } // Multiple allowed versions - if (Array.isArray(version)) { - var match = false; - - for (var i = 0, l = version.length; i < l; i++) { - match |= version[i] === buffer[0]; - } - - if (!match) { - return NaN; - } + if (Array.isArray(version) && _.every(version, function(v) { + return v !== buffer[0]; + })) { + return NaN; } if (!Base.verify_checksum(buffer)) { diff --git a/test/base-test.js b/test/base-test.js index 9b2e5df7..c9a52c28 100644 --- a/test/base-test.js +++ b/test/base-test.js @@ -1,6 +1,7 @@ 'use strict'; var assert = require('assert'); var Base = require('ripple-lib').Base; +var fixtures = require('./fixtures/base58.json'); function digitArray(str) { return str.split('').map(function(d) { @@ -8,6 +9,14 @@ function digitArray(str) { }); } +function hexToByteArray(hex) { + var byteArray = []; + for (var i = 0; i < hex.length / 2; i++) { + byteArray.push(parseInt(hex.slice(2 * i, 2 * i + 2), 16)); + } + return byteArray; +} + describe('Base', function() { describe('encode_check', function() { it('0', function () { @@ -36,4 +45,22 @@ describe('Base', function() { 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)); + 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); + assert.deepEqual(decoded, hexToByteArray(testCase.hex)); + } + }); + }); }); diff --git a/test/base58-test.js b/test/base58-test.js deleted file mode 100644 index 856e5619..00000000 --- a/test/base58-test.js +++ /dev/null @@ -1,17 +0,0 @@ -var assert = require('assert'); -var Seed = require('ripple-lib').Seed; - -describe('Base58', function() { - describe('Seed', function() { - it('saESc82Vun7Ta5EJRzGJbrXb5HNYk', function () { - var seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk'); - assert.strictEqual(seed.to_hex(), 'FF1CF838D02B2CF7B45BAC27F5F24F4F'); - }); - it('sp6iDHnmiPN7tQFHm5sCW59ax3hfE', function () { - var seed = Seed.from_json('sp6iDHnmiPN7tQFHm5sCW59ax3hfE'); - assert.strictEqual(seed.to_hex(), '00AD8DA764C3C8AF5F9B8D51C94B9E49'); - }); - }); -}); - -// vim:sw=2:sts=2:ts=8:et diff --git a/test/fixtures/base58.json b/test/fixtures/base58.json new file mode 100644 index 00000000..896121f8 --- /dev/null +++ b/test/fixtures/base58.json @@ -0,0 +1,141 @@ +{ + "ripple": [ + { + "hex": "", + "string": "" + }, + { + "hex": "61", + "string": "pg" + }, + { + "hex": "626262", + "string": "2sgV" + }, + { + "hex": "636363", + "string": "2PNi" + }, + { + "hex": "73696d706c792061206c6f6e6720737472696e67", + "string": "pcEuFj68N1S8n9qHX1tmKpCCFLvp" + }, + { + "hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647", + "string": "r4Srf52g9jJgTHDrVXjvLUN8ZuQsiJDN9L" + }, + { + "hex": "516b6fcd0f", + "string": "wB8LTmg" + }, + { + "hex": "bf4f89001e670274dd", + "string": "sSNosLWLoP8tU" + }, + { + "hex": "572e4794", + "string": "sNE7fm" + }, + { + "hex": "ecac89cad93923c02321", + "string": "NJDM3diCXwauyw" + }, + { + "hex": "10c8511e", + "string": "Rtnzm" + }, + { + "hex": "00000000000000000000", + "string": "rrrrrrrrrr" + }, + { + "hex": "801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e", + "string": "nHxrnHEGyeFpUCPx1JKepCXJ1UV8nDN5yoeGGEaJZjGbTR8qC5D" + }, + { + "hex": "003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187", + "string": "ra7jcY4BG9GTKhuqpCfyYNbu5CqUzoLMGS" + } + ], + "bitcoin": [ + { + "hex": "", + "string": "" + }, + { + "hex": "61", + "string": "2g" + }, + { + "hex": "626262", + "string": "a3gV" + }, + { + "hex": "636363", + "string": "aPEr" + }, + { + "hex": "73696d706c792061206c6f6e6720737472696e67", + "string": "2cFupjhnEsSn59qHXstmK2ffpLv2" + }, + { + "hex": "00eb15231dfceb60925886b67d065299925915aeb172c06647", + "string": "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L" + }, + { + "hex": "516b6fcd0f", + "string": "ABnLTmg" + }, + { + "hex": "bf4f89001e670274dd", + "string": "3SEo3LWLoPntC" + }, + { + "hex": "572e4794", + "string": "3EFU7m" + }, + { + "hex": "ecac89cad93923c02321", + "string": "EJDM8drfXA6uyA" + }, + { + "hex": "10c8511e", + "string": "Rt5zm" + }, + { + "hex": "00000000000000000000", + "string": "1111111111" + }, + { + "hex": "801184cd2cdd640ca42cfc3a091c51d549b2f016d454b2774019c2b2d2e08529fd206ec97e", + "string": "5Hx15HFGyep2CfPxsJKe2fXJsCVn5DEiyoeGGF6JZjGbTRnqfiD" + }, + { + "hex": "003c176e659bea0f29a3e9bf7880c112b1b31b4dc826268187", + "string": "16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS" + } + ], + "invalid": [ + { + "description": "non-base58 string", + "string": "invalid" + }, + { + "description": "non-base58 alphabet", + "string": "c2F0b3NoaQo=" + }, + { + "description": "leading whitespace", + "string": " 1111111111" + }, + { + "description": "trailing whitespace", + "string": "1111111111 " + }, + { + "description": "unexpected character after whitespace", + "string": " \t\n\u000b\f\r skip \r\f\u000b\n\t a" + } + ] + +} diff --git a/test/seed-test.js b/test/seed-test.js index 7a9d5aa6..da2d114b 100644 --- a/test/seed-test.js +++ b/test/seed-test.js @@ -2,6 +2,14 @@ var assert = require('assert'); var Seed = require('ripple-lib').Seed; describe('Seed', function() { + it('saESc82Vun7Ta5EJRzGJbrXb5HNYk', function () { + var seed = Seed.from_json('saESc82Vun7Ta5EJRzGJbrXb5HNYk'); + assert.strictEqual(seed.to_hex(), 'FF1CF838D02B2CF7B45BAC27F5F24F4F'); + }); + it('sp6iDHnmiPN7tQFHm5sCW59ax3hfE', function () { + var seed = Seed.from_json('sp6iDHnmiPN7tQFHm5sCW59ax3hfE'); + assert.strictEqual(seed.to_hex(), '00AD8DA764C3C8AF5F9B8D51C94B9E49'); + }); it('can generate many addresses', function () { var test_data = [