Fix #160, Seed.parse_json of hex

This commit is contained in:
Nicholas Dudfield
2014-09-06 10:21:55 +07:00
parent 03386a61e9
commit b4564a86b4
3 changed files with 14 additions and 4 deletions

View File

@@ -33,8 +33,8 @@ Seed.prototype.parse_json = function (j) {
// XXX Should actually always try and continue if it failed.
} else if (j[0] === 's') {
this._value = Base.decode_check(Base.VER_FAMILY_SEED, j);
} else if (j.length === 32) {
this._value = this.parse_hex(j);
} else if (/^[0-9a-fA-f]{32}$/.test(j)) {
this.parse_hex(j);
// XXX Should also try 1751
} else {
this.parse_passphrase(j);

View File

@@ -82,8 +82,17 @@ describe('Message', function(){
});
it('should throw an error if given an invalid secret key', function(){
// Annoyingly non hex can be fed to the BigInteger(s, 16) constructor and
// it will parse as a number. Before the commit of this comment, this test
// involved a fixture of 32 chars, which was assumed to be hex. The test
// passed, but for the wrong wreasons. There was a bug in Seed.parse_json.
var secret_string = 'badsafRpB5euNL52PZPTSqrE9gvuFwTC';
// Seed.from_json only creates invalid seeds from empty strings or invalid
// base58 starting with an s, which it tries to base 58 decode/check sum.
// The rest will be assumed to be a passphrase.
// This is a bad b58 seed
var secret_string = 'sbadsafRpB5euNL52PZPTSqrE9gvuFwTC';
var hash = 'e865bcc63a86ef21585ac8340a7cc8590ed85175a2a718c6fb2bfb2715d13778';
assert.throws(function(){

View File

@@ -26,8 +26,9 @@ describe('Signing', function() {
assert(_isNaN(new Seed().parse_json('').to_json()));
});
it('hex string', function() {
// 32 0s is a valid hex repr of seed bytes
var str = new Array(33).join('0');
assert(_isNaN(new Seed().parse_json(str).to_json()));
assert.strictEqual((new Seed().parse_json(str).to_json()), 'sp6JS7f14BuwFY8Mw6bTtLKWauoUs');
});
it('passphrase', function() {
var str = new Array(60).join('0');