EncryptableItem: rename creationTime() method from getCreationTime()

This commit is contained in:
Andreas Schildbach 2023-03-18 11:23:05 +01:00
parent 45f87d8339
commit b309308076
15 changed files with 40 additions and 40 deletions

View File

@ -308,7 +308,7 @@ public class DeterministicKey extends ECKey {
EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey);
DeterministicKey key = new DeterministicKey(childNumberPath, chainCode, keyCrypter, pub, encryptedPrivateKey, newParent);
if (newParent == null) {
Optional<Instant> creationTime = getCreationTime();
Optional<Instant> creationTime = this.creationTime();
if (creationTime.isPresent())
key.setCreationTime(creationTime.get());
else
@ -388,7 +388,7 @@ public class DeterministicKey extends ECKey {
if (!Arrays.equals(key.getPubKey(), getPubKey()))
throw new KeyCrypterException.PublicPrivateMismatch("Provided AES key is wrong");
if (parent == null) {
Optional<Instant> creationTime = getCreationTime();
Optional<Instant> creationTime = this.creationTime();
if (creationTime.isPresent())
key.setCreationTime(creationTime.get());
else
@ -705,14 +705,14 @@ public class DeterministicKey extends ECKey {
/**
* The creation time of a deterministic key is equal to that of its parent, unless this key is the root of a tree
* in which case the time is stored alongside the key as per normal, see {@link ECKey#getCreationTime()}.
* in which case the time is stored alongside the key as per normal, see {@link ECKey#creationTime()}.
*/
@Override
public Optional<Instant> getCreationTime() {
public Optional<Instant> creationTime() {
if (parent != null)
return parent.getCreationTime();
return parent.creationTime();
else
return super.getCreationTime();
return super.creationTime();
}
/**
@ -776,7 +776,7 @@ public class DeterministicKey extends ECKey {
helper.add("pub", ByteUtils.formatHex(pub.getEncoded()));
helper.add("chainCode", ByteUtils.formatHex(chainCode));
helper.add("path", getPathAsString());
Optional<Instant> creationTime = getCreationTime();
Optional<Instant> creationTime = this.creationTime();
if (!creationTime.isPresent())
helper.add("creationTimeSeconds", "unknown");
else if (parent != null)

View File

@ -122,7 +122,7 @@ public class ECKey implements EncryptableItem {
private static final Comparator<byte[]> LEXICOGRAPHICAL_COMPARATOR = ByteUtils.arrayUnsignedComparator();
/** Sorts oldest keys first, newest last. */
public static final Comparator<ECKey> AGE_COMPARATOR = Comparator.comparing(ecKey -> ecKey.getCreationTime().orElse(Instant.EPOCH));
public static final Comparator<ECKey> AGE_COMPARATOR = Comparator.comparing(ecKey -> ecKey.creationTime().orElse(Instant.EPOCH));
/** Compares by extracting pub key as a {@code byte[]} and using a lexicographic comparator */
public static final Comparator<ECKey> PUBKEY_COMPARATOR = Comparator.comparing(ECKey::getPubKey, LEXICOGRAPHICAL_COMPARATOR);
@ -1080,7 +1080,7 @@ public class ECKey implements EncryptableItem {
* that data.
*/
@Override
public Optional<Instant> getCreationTime() {
public Optional<Instant> creationTime() {
return Optional.ofNullable(creationTime);
}

View File

@ -43,12 +43,12 @@ public interface EncryptableItem {
Protos.Wallet.EncryptionType getEncryptionType();
/** Returns the time at which this encryptable item was first created/derived, or empty of unknown. */
Optional<Instant> getCreationTime();
Optional<Instant> creationTime();
/** @deprecated use {@link #getCreationTime()} */
/** @deprecated use {@link #creationTime()} */
@Deprecated
default long getCreationTimeSeconds() {
Optional<Instant> creationTime = getCreationTime();
Optional<Instant> creationTime = creationTime();
return creationTime.isPresent() ? creationTime.get().getEpochSecond() : 0;
}
}

View File

@ -339,13 +339,13 @@ public class WalletAppKit extends AbstractIdleService {
// Initialize the chain file with a checkpoint to speed up first-run sync.
Instant time;
if (restoreFromSeed != null) {
time = restoreFromSeed.getCreationTime().orElse(Instant.EPOCH);
time = restoreFromSeed.creationTime().orElse(Instant.EPOCH);
if (chainFileExists) {
log.info("Clearing the chain file in preparation for restore.");
vStore.clear();
}
} else if (restoreFromKey != null) {
time = restoreFromKey.getCreationTime().orElse(Instant.EPOCH);
time = restoreFromKey.creationTime().orElse(Instant.EPOCH);
if (chainFileExists) {
log.info("Clearing the chain file in preparation for restore.");
vStore.clear();

View File

@ -295,7 +295,7 @@ public class BasicKeyChain implements EncryptableKeyChain {
lock.lock();
try {
return hashToKeys.values().stream()
.map(key -> key.getCreationTime().orElse(Instant.EPOCH))
.map(key -> key.creationTime().orElse(Instant.EPOCH))
.min(Instant::compareTo)
.orElse(Instant.MAX);
} finally {
@ -348,7 +348,7 @@ public class BasicKeyChain implements EncryptableKeyChain {
/*package*/ static Protos.Key.Builder serializeEncryptableItem(EncryptableItem item) {
Protos.Key.Builder proto = Protos.Key.newBuilder();
item.getCreationTime().ifPresent(creationTime -> proto.setCreationTimestamp(creationTime.toEpochMilli()));
item.creationTime().ifPresent(creationTime -> proto.setCreationTimestamp(creationTime.toEpochMilli()));
if (item.isEncrypted() && item.getEncryptedData() != null) {
// The encrypted data can be missing for an "encrypted" key in the case of a deterministic wallet for
// which the leaf keys chain to an encrypted parent and rederive their private keys on the fly. In that
@ -629,9 +629,9 @@ public class BasicKeyChain implements EncryptableKeyChain {
try {
ECKey oldest = null;
for (ECKey key : hashToKeys.values()) {
Instant keyTime = key.getCreationTime().orElse(Instant.EPOCH);
Instant keyTime = key.creationTime().orElse(Instant.EPOCH);
if (keyTime.isAfter(time)) {
if (oldest == null || oldest.getCreationTime().orElse(Instant.EPOCH).isAfter(keyTime))
if (oldest == null || oldest.creationTime().orElse(Instant.EPOCH).isAfter(keyTime))
oldest = key;
}
}
@ -654,7 +654,7 @@ public class BasicKeyChain implements EncryptableKeyChain {
try {
List<ECKey> results = new LinkedList<>();
for (ECKey key : hashToKeys.values()) {
Instant keyTime = key.getCreationTime().orElse(Instant.EPOCH);
Instant keyTime = key.creationTime().orElse(Instant.EPOCH);
if (keyTime.isBefore(time)) {
results.add(key);
}

View File

@ -397,7 +397,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
basicKeyChain = new BasicKeyChain(crypter);
if (!seed.isEncrypted()) {
rootKey = HDKeyDerivation.createMasterPrivateKey(Objects.requireNonNull(seed.getSeedBytes()));
Optional<Instant> creationTime = seed.getCreationTime();
Optional<Instant> creationTime = seed.creationTime();
if (creationTime.isPresent())
rootKey.setCreationTime(creationTime.get());
else
@ -726,8 +726,8 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
@Override
public Instant earliestKeyCreationTime() {
return (seed != null ?
seed.getCreationTime() :
getWatchingKey().getCreationTime()
seed.creationTime() :
getWatchingKey().creationTime()
).orElse(Instant.EPOCH);
}
@ -1452,7 +1452,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
builder.append("Seed is encrypted\n");
}
builder.append("Seed birthday: ");
Optional<Instant> seedCreationTime = seed.getCreationTime();
Optional<Instant> seedCreationTime = seed.creationTime();
if (seedCreationTime.isPresent())
builder.append(seedCreationTime.get().getEpochSecond()).append(" [")
.append(TimeUtils.dateTimeFormat(seedCreationTime.get())).append("]");
@ -1461,7 +1461,7 @@ public class DeterministicKeyChain implements EncryptableKeyChain {
builder.append("\n");
} else {
builder.append("Key birthday: ");
Optional<Instant> watchingKeyCreationTime = watchingKey.getCreationTime();
Optional<Instant> watchingKeyCreationTime = watchingKey.creationTime();
if (watchingKeyCreationTime.isPresent())
builder.append(watchingKeyCreationTime.get().getEpochSecond()).append(" [")
.append(TimeUtils.dateTimeFormat(watchingKeyCreationTime.get())).append("]");

View File

@ -268,7 +268,7 @@ public class DeterministicSeed implements EncryptableItem {
}
@Override
public Optional<Instant> getCreationTime() {
public Optional<Instant> creationTime() {
return Optional.ofNullable(creationTime);
}

View File

@ -82,7 +82,7 @@ public class KeyTimeCoinSelector implements CoinSelector {
continue;
}
Objects.requireNonNull(controllingKey, "Coin selector given output as candidate for which we lack the key");
if (controllingKey.getCreationTime().orElse(Instant.EPOCH).compareTo(time) >= 0) continue;
if (controllingKey.creationTime().orElse(Instant.EPOCH).compareTo(time) >= 0) continue;
// It's older than the cutoff time so select.
gathered.push(output);
if (gathered.size() >= MAX_SIMULTANEOUS_INPUTS) {

View File

@ -481,7 +481,7 @@ public class Wallet extends BaseTaggableObject
ScriptType outputScriptType, ChildNumber accountNumber) {
DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(masterKey, accountNumber);
accountKey = accountKey.dropParent();
Optional<Instant> creationTime = masterKey.getCreationTime();
Optional<Instant> creationTime = masterKey.creationTime();
if (creationTime.isPresent())
accountKey.setCreationTime(creationTime.get());
else
@ -3620,7 +3620,7 @@ public class Wallet extends BaseTaggableObject
/**
* Returns the earliest creation time of keys or watched scripts in this wallet, ie the min
* of {@link ECKey#getCreationTime()}. This can return {@link Instant#EPOCH} if at least one key does
* of {@link ECKey#creationTime()}. This can return {@link Instant#EPOCH} if at least one key does
* not have that data (e.g. is an imported key with unknown timestamp). <p>
*
* This method is most often used in conjunction with {@link PeerGroup#setFastCatchupTime(Instant)} in order to
@ -5435,7 +5435,7 @@ public class Wallet extends BaseTaggableObject
/** Returns whether the keys creation time is before the key rotation time, if one was set. */
public boolean isKeyRotating(ECKey key) {
Instant keyRotationTime = vKeyRotationTime;
return keyRotationTime != null && key.getCreationTime().orElse(Instant.EPOCH).isBefore(keyRotationTime);
return keyRotationTime != null && key.creationTime().orElse(Instant.EPOCH).isBefore(keyRotationTime);
}
/**

View File

@ -340,12 +340,12 @@ public class ECKeyTest {
public void testUnencryptedCreate() {
TimeUtils.setMockClock();
ECKey key = new ECKey();
Optional<Instant> time = key.getCreationTime();
Optional<Instant> time = key.creationTime();
assertTrue(time.isPresent());
assertTrue(!key.isEncrypted());
byte[] originalPrivateKeyBytes = key.getPrivKeyBytes();
ECKey encryptedKey = key.encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1));
assertEquals(time, encryptedKey.getCreationTime());
assertEquals(time, encryptedKey.creationTime());
assertTrue(encryptedKey.isEncrypted());
assertNull(encryptedKey.getSecretBytes());
key = encryptedKey.decrypt(keyCrypter.deriveKey(PASSWORD1));

View File

@ -125,7 +125,7 @@ public class WalletProtobufSerializerTest {
ECKey foundKey = wallet1.findKeyFromPubKeyHash(myKey.getPubKeyHash(), null);
assertArrayEquals(myKey.getPubKey(), foundKey.getPubKey());
assertArrayEquals(myKey.getPrivKeyBytes(), foundKey.getPrivKeyBytes());
assertEquals(myKey.getCreationTime(), foundKey.getCreationTime());
assertEquals(myKey.creationTime(), foundKey.creationTime());
assertEquals(mScriptCreationTime.truncatedTo(ChronoUnit.MILLIS),
wallet1.getWatchedScripts().get(0).getCreationTime().get());
assertEquals(1, wallet1.getWatchedScripts().size());
@ -343,7 +343,7 @@ public class WalletProtobufSerializerTest {
ECKey foundKey = wallet1.findKeyFromPubKeyHash(myKey.getPubKeyHash(), null);
assertArrayEquals(myKey.getPubKey(), foundKey.getPubKey());
assertArrayEquals(myKey.getPrivKeyBytes(), foundKey.getPrivKeyBytes());
assertEquals(myKey.getCreationTime(), foundKey.getCreationTime());
assertEquals(myKey.creationTime(), foundKey.creationTime());
}
@Test
@ -354,9 +354,9 @@ public class WalletProtobufSerializerTest {
Wallet wallet2 = roundTrip(wallet);
Wallet wallet3 = roundTrip(wallet2);
assertEquals(xpub, wallet.getWatchingKey().serializePubB58(TESTNET.network()));
assertEquals(creationTime, wallet.getWatchingKey().getCreationTime().get());
assertEquals(creationTime, wallet2.getWatchingKey().getCreationTime().get());
assertEquals(creationTime, wallet3.getWatchingKey().getCreationTime().get());
assertEquals(creationTime, wallet.getWatchingKey().creationTime().get());
assertEquals(creationTime, wallet2.getWatchingKey().creationTime().get());
assertEquals(creationTime, wallet3.getWatchingKey().creationTime().get());
assertEquals(creationTime, wallet.earliestKeyCreationTime());
assertEquals(creationTime, wallet2.earliestKeyCreationTime());
assertEquals(creationTime, wallet3.earliestKeyCreationTime());

View File

@ -656,7 +656,7 @@ public class DeterministicKeyChainTest {
//Simulate Wallet.fromMasterKey(params, coinLevelKey, 0)
DeterministicKey accountKey = HDKeyDerivation.deriveChildKey(coinLevelKey, new ChildNumber(0, true));
accountKey = accountKey.dropParent();
accountKey.setCreationTime(watchingKey.getCreationTime().get());
accountKey.setCreationTime(watchingKey.creationTime().get());
KeyChainGroup group = KeyChainGroup.builder(NetworkParameters.of(network)).addChain(DeterministicKeyChain.builder().spend(accountKey)
.outputScriptType(bip44chain.getOutputScriptType()).build()).build();
DeterministicKeyChain fromMasterKeyChain = group.getActiveKeyChain();

View File

@ -42,7 +42,7 @@ public class BackupToMnemonicSeed {
DeterministicSeed seed = wallet.getKeyChainSeed();
System.out.println("seed: " + seed.toString());
System.out.println("creation time: " + seed.getCreationTime().get().getEpochSecond());
System.out.println("creation time: " + seed.creationTime().get().getEpochSecond());
System.out.println("mnemonicCode: " + InternalUtils.SPACE_JOINER.join(seed.getMnemonicCode()));
}
}

View File

@ -38,7 +38,7 @@ public class WalletLoadTest {
Context.propagate(new Context());
Wallet wallet = Wallet.loadFromFile(walletFile);
Instant creation = wallet.getKeyChainSeed().getCreationTime().get();
Instant creation = wallet.getKeyChainSeed().creationTime().get();
assertEquals(testWalletCreation, creation, "unexpected creation timestamp");
String mnemonic = wallet.getKeyChainSeed().getMnemonicString();

View File

@ -88,7 +88,7 @@ public class WalletSettingsController implements OverlayController<WalletSetting
}
// Set the date picker to show the birthday of this wallet.
Instant creationTime = seed.getCreationTime().get();
Instant creationTime = seed.creationTime().get();
LocalDate origDate = creationTime.atZone(ZoneId.systemDefault()).toLocalDate();
datePicker.setValue(origDate);