3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-12 18:25:51 +00:00

DeterministicKeyChain.Builder: Make seedCreationTimeSecs() a parameter of entropy(), as all other forms of root come with their own creation time.

This commit is contained in:
Andreas Schildbach 2019-01-26 19:15:06 +01:00
parent f40e16aac4
commit 04433862cd
3 changed files with 9 additions and 19 deletions

View File

@ -164,7 +164,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
protected SecureRandom random;
protected int bits = DeterministicSeed.DEFAULT_SEED_ENTROPY_BITS;
protected String passphrase;
protected long seedCreationTimeSecs;
protected long creationTimeSecs;
protected byte[] entropy;
protected DeterministicSeed seed;
protected DeterministicKey watchingKey = null;
@ -183,8 +183,9 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
* if the starting entropy is the same. You should provide the creation time in seconds since the UNIX epoch for the
* seed: this lets us know from what part of the chain we can expect to see derived keys appear.
*/
public T entropy(byte[] entropy) {
public T entropy(byte[] entropy, long creationTimeSecs) {
this.entropy = entropy;
this.creationTimeSecs = creationTimeSecs;
return self();
}
@ -227,11 +228,6 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
return self();
}
public T seedCreationTimeSecs(long seedCreationTimeSecs) {
this.seedCreationTimeSecs = seedCreationTimeSecs;
return self();
}
/** The passphrase to use with the generated mnemonic, or null if you would like to use the default empty string. Currently must be the empty string. */
public T passphrase(String passphrase) {
// FIXME support non-empty passphrase
@ -258,15 +254,12 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
DeterministicKeyChain chain;
if (random != null) {
// Default passphrase to "" if not specified
checkState(seedCreationTimeSecs == 0);
chain = new DeterministicKeyChain(new DeterministicSeed(random, bits, getPassphrase()), null, accountPath);
} else if (entropy != null) {
chain = new DeterministicKeyChain(new DeterministicSeed(entropy, getPassphrase(), seedCreationTimeSecs), null, accountPath);
chain = new DeterministicKeyChain(new DeterministicSeed(entropy, getPassphrase(), creationTimeSecs), null, accountPath);
} else if (seed != null) {
seed.setCreationTimeSeconds(seedCreationTimeSecs);
chain = new DeterministicKeyChain(seed, null, accountPath);
} else {
watchingKey.setCreationTimeSeconds(seedCreationTimeSecs);
chain = new DeterministicKeyChain(watchingKey);
}

View File

@ -104,15 +104,12 @@ public class MarriedKeyChain extends DeterministicKeyChain {
MarriedKeyChain chain;
if (random != null) {
checkState(seedCreationTimeSecs == 0);
chain = new MarriedKeyChain(new DeterministicSeed(random, bits, getPassphrase()), null, accountPath);
} else if (entropy != null) {
chain = new MarriedKeyChain(new DeterministicSeed(entropy, getPassphrase(), seedCreationTimeSecs), null, accountPath);
chain = new MarriedKeyChain(new DeterministicSeed(entropy, getPassphrase(), creationTimeSecs), null, accountPath);
} else if (seed != null) {
seed.setCreationTimeSeconds(seedCreationTimeSecs);
chain = new MarriedKeyChain(seed, null, accountPath);
} else {
watchingKey.setCreationTimeSeconds(seedCreationTimeSecs);
chain = new MarriedKeyChain(watchingKey);
}
chain.addFollowingAccountKeys(followingKeys, threshold);

View File

@ -106,7 +106,7 @@ public class DeterministicKeyChainTest {
public void deriveAccountOne() throws Exception {
long secs = 1389353062L;
DeterministicKeyChain chain1 = DeterministicKeyChain.builder().accountPath(ImmutableList.of(ChildNumber.ONE))
.entropy(ENTROPY).seedCreationTimeSecs(secs).build();
.entropy(ENTROPY, secs).build();
ECKey key1 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
ECKey key2 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
@ -127,7 +127,7 @@ public class DeterministicKeyChainTest {
public void serializeAccountOne() throws Exception {
long secs = 1389353062L;
DeterministicKeyChain chain1 = DeterministicKeyChain.builder().accountPath(ImmutableList.of(ChildNumber.ONE))
.entropy(ENTROPY).seedCreationTimeSecs(secs).build();
.entropy(ENTROPY, secs).build();
ECKey key1 = chain1.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
final Address address = LegacyAddress.fromBase58(UNITTEST, "n2nHHRHs7TiZScTuVhZUkzZfTfVgGYwy6X");
@ -514,8 +514,8 @@ public class DeterministicKeyChainTest {
public void spendingChainAccountTwo() throws UnreadableWalletException {
Utils.setMockClock();
long secs = 1389353062L;
chain = DeterministicKeyChain.builder().accountPath(ImmutableList.of(new ChildNumber(2, true))).entropy(ENTROPY)
.seedCreationTimeSecs(secs).build();
chain = DeterministicKeyChain.builder().accountPath(ImmutableList.of(new ChildNumber(2, true)))
.entropy(ENTROPY, secs).build();
DeterministicKey firstReceiveKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey secondReceiveKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey firstChangeKey = chain.getKey(KeyChain.KeyPurpose.CHANGE);