From b2526f0b4d600f7b3eb5cb186327b9ba635e6323 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Thu, 11 Dec 2014 21:22:54 +0100 Subject: [PATCH] Throw when trying to set creation time of a DeterministicKey that is a leaf in the hierarchy. It would be needlessly stored, but unreadable. --- .../java/org/bitcoinj/crypto/DeterministicKey.java | 12 ++++++++++++ .../org/bitcoinj/crypto/ChildKeyDerivationTest.java | 1 - 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java index 1accec32..b71c0c06 100644 --- a/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java +++ b/core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java @@ -441,6 +441,18 @@ public class DeterministicKey extends ECKey { return super.getCreationTimeSeconds(); } + /** + * The creation time of a deterministic key is equal to that of its parent, unless this key is the root of a tree. + * Thus, setting the creation time on a leaf is forbidden. + */ + @Override + public void setCreationTimeSeconds(long newCreationTimeSeconds) { + if (parent != null) + throw new IllegalStateException("Creation time can only be set on root keys."); + else + super.setCreationTimeSeconds(newCreationTimeSeconds); + } + /** * Verifies equality of all fields but NOT the parent pointer (thus the same key derived in two separate heirarchy * objects will equal each other. diff --git a/core/src/test/java/org/bitcoinj/crypto/ChildKeyDerivationTest.java b/core/src/test/java/org/bitcoinj/crypto/ChildKeyDerivationTest.java index 567e9db7..e7ed68b6 100644 --- a/core/src/test/java/org/bitcoinj/crypto/ChildKeyDerivationTest.java +++ b/core/src/test/java/org/bitcoinj/crypto/ChildKeyDerivationTest.java @@ -199,7 +199,6 @@ public class ChildKeyDerivationTest { // Creation time can't survive the xpub serialization format unfortunately. key1.setCreationTimeSeconds(0); - key2.setCreationTimeSeconds(0); NetworkParameters params = MainNetParams.get(); {