Skip to content

Commit 23e473b

Browse files
committed
[FEATURE] make maxLoops in seed.get_key optional
default to 1 or the index of the requested account +1
1 parent 0dfd3a0 commit 23e473b

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/js/ripple/seed.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,22 @@ function SHA256_RIPEMD160(bits) {
9393
*
9494
* {Uint160} (from_json able), specifies the address matching the KeyPair
9595
* that is desired.
96+
*
97+
* @param maxLoops (optional)
98+
* {Number} specifies the amount of attempts taken to generate
99+
* a matching KeyPair
96100
*/
97-
Seed.prototype.get_key = function (account) {
101+
Seed.prototype.get_key = function (account, maxLoops) {
98102
var account_number = 0, address;
103+
var max_loops = maxLoops || 1;
99104

100105
if (!this.is_valid()) {
101106
throw new Error('Cannot generate keys from invalid seed!');
102107
}
103108
if (account) {
104109
if (typeof account === 'number') {
105110
account_number = account;
111+
max_loops = account_number+1;
106112
} else {
107113
address = UInt160.from_json(account);
108114
}
@@ -121,9 +127,9 @@ Seed.prototype.get_key = function (account) {
121127

122128
var sec;
123129
var key_pair;
124-
var max_loops = 1000; // TODO
125130

126131
do {
132+
127133
i = 0;
128134

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

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

146-
return key_pair;
151+
} while (address && !key_pair.get_address().equals(address));
152+
return key_pair;
147153
};
148154

149155
exports.Seed = Seed;

test/seed-test.js

+26-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var config = require('./testutils').get_config();
55

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

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

2928
function assert_helper(seed_json, address_or_nth, expected) {
3029
var seed = Seed.from_json(seed_json);
31-
var keypair = seed.get_key(address_or_nth);
32-
assert.strictEqual(keypair.to_hex_pub(),
33-
expected);
30+
var keypair = seed.get_key(address_or_nth, 500);
31+
assert.strictEqual(keypair.to_hex_pub(), expected);
3432
}
33+
3534
for (var nth = 0; nth < test_data.length; nth++) {
3635
var seed_json = test_data[nth][0];
3736
var address = test_data[nth][1];
@@ -47,7 +46,30 @@ describe('Seed', function() {
4746
// This isn't too bad as it only needs to generate one keypair `seq`
4847
assert_helper(seed_json, nth_for_seed, expected);
4948
};
49+
5050
});
51+
52+
it('should return the key_pair for a valid account and secret pair', function() {
53+
var address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
54+
var seed = Seed.from_json('shsWGZcmZz6YsWWmcnpfr6fLTdtFV');
55+
var keyPair = seed.get_key(address);
56+
assert.strictEqual(keyPair.get_address().to_json(), address);
57+
assert.strictEqual(keyPair.to_hex_pub(), '02F89EAEC7667B30F33D0687BBA86C3FE2A08CCA40A9186C5BDE2DAA6FA97A37D8');
58+
});
59+
60+
it('should not find a KeyPair for a secret that does not belong to the given account', function() {
61+
var address = 'r3GgMwvgvP8h4yVWvjH1dPZNvC37TjzBBE';
62+
var secret = 'snoPBrXtMeMyMHUVTgbuqAfg1SUTb';
63+
var seed = Seed.from_json('snoPBrXtMeMyMHUVTgbuqAfg1SUTb');
64+
try {
65+
seed.get_key(address);
66+
assert(false, 'should throw an error');
67+
} catch(e) {
68+
assert.strictEqual(e.message, 'Too many loops looking for KeyPair yielding '+address+' from '+secret);
69+
}
70+
71+
});
72+
5173
});
5274

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

0 commit comments

Comments
 (0)