Skip to content

Commit

Permalink
[FEATURE] make maxLoops in seed.get_key optional
Browse files Browse the repository at this point in the history
default to 1 or the index of the requested account +1
  • Loading branch information
geertweening committed Oct 3, 2014
1 parent 0dfd3a0 commit 23e473b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/js/ripple/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,22 @@ function SHA256_RIPEMD160(bits) {
*
* {Uint160} (from_json able), specifies the address matching the KeyPair
* that is desired.
*
* @param maxLoops (optional)
* {Number} specifies the amount of attempts taken to generate
* a matching KeyPair
*/
Seed.prototype.get_key = function (account) {
Seed.prototype.get_key = function (account, maxLoops) {
var account_number = 0, address;
var max_loops = maxLoops || 1;

if (!this.is_valid()) {
throw new Error('Cannot generate keys from invalid seed!');
}
if (account) {
if (typeof account === 'number') {
account_number = account;
max_loops = account_number+1;
} else {
address = UInt160.from_json(account);
}
Expand All @@ -121,9 +127,9 @@ Seed.prototype.get_key = function (account) {

var sec;
var key_pair;
var max_loops = 1000; // TODO

do {

i = 0;

do {
Expand All @@ -135,15 +141,15 @@ Seed.prototype.get_key = function (account) {
sec = sec.add(private_gen).mod(curve.r);
key_pair = KeyPair.from_bn_secret(sec);

if (--max_loops <= 0) {
if (max_loops-- <= 0) {
// We are almost certainly looking for an account that would take same
// value of $too_long {forever, ...}
throw new Error('Too many loops looking for KeyPair yielding '+
address.to_json() +' from ' + this.to_json());
};
} while (address && !key_pair.get_address().equals(address));
}

return key_pair;
} while (address && !key_pair.get_address().equals(address));
return key_pair;
};

exports.Seed = Seed;
30 changes: 26 additions & 4 deletions test/seed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var config = require('./testutils').get_config();

describe('Seed', function() {
it('can generate many addresses', function () {
var seed = Seed.from_json("masterpassphrase");

var test_data = [
// Format:
Expand All @@ -28,10 +27,10 @@ describe('Seed', function() {

function assert_helper(seed_json, address_or_nth, expected) {
var seed = Seed.from_json(seed_json);
var keypair = seed.get_key(address_or_nth);
assert.strictEqual(keypair.to_hex_pub(),
expected);
var keypair = seed.get_key(address_or_nth, 500);
assert.strictEqual(keypair.to_hex_pub(), expected);
}

for (var nth = 0; nth < test_data.length; nth++) {
var seed_json = test_data[nth][0];
var address = test_data[nth][1];
Expand All @@ -47,7 +46,30 @@ describe('Seed', function() {
// This isn't too bad as it only needs to generate one keypair `seq`
assert_helper(seed_json, nth_for_seed, expected);
};

});

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);
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');
try {
seed.get_key(address);
assert(false, 'should throw an error');
} catch(e) {
assert.strictEqual(e.message, 'Too many loops looking for KeyPair yielding '+address+' from '+secret);
}

});

});

// vim:sw=2:sts=2:ts=8:et

0 comments on commit 23e473b

Please sign in to comment.