/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * Create an encrypted private key with the keyCrypter and the AES key supplied. * This method returns a new encrypted key and leaves the original unchanged. * * @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create). * @return encryptedKey */ public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); final byte[] privKeyBytes = getPrivKeyBytes(); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey); ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, getPubKey()); result.setCreationTimeSeconds(creationTimeSeconds); return result; }
/** * Create an encrypted private key with the keyCrypter and the AES key supplied. * This method returns a new encrypted key and leaves the original unchanged. * * @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create). * @return encryptedKey */ public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); final byte[] privKeyBytes = getPrivKeyBytes(); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey); ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, getPubKey()); result.setCreationTimeSeconds(creationTimeSeconds); return result; }
/** * Create an encrypted private key with the keyCrypter and the AES key supplied. * This method returns a new encrypted key and leaves the original unchanged. * * @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create). * @return encryptedKey */ public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); final byte[] privKeyBytes = getPrivKeyBytes(); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey); ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, getPubKey()); result.setCreationTimeSeconds(creationTimeSeconds); return result; }
/** * Create an encrypted private key with the keyCrypter and the AES key supplied. * This method returns a new encrypted key and leaves the original unchanged. * * @param keyCrypter The keyCrypter that specifies exactly how the encrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached as it is slow to create). * @return encryptedKey */ public ECKey encrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); final byte[] privKeyBytes = getPrivKeyBytes(); EncryptedData encryptedPrivateKey = keyCrypter.encrypt(privKeyBytes, aesKey); ECKey result = ECKey.fromEncrypted(encryptedPrivateKey, keyCrypter, getPubKey()); result.setCreationTimeSeconds(creationTimeSeconds); return result; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
ecKey = ECKey.fromPublicOnly(pub); ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000); importKeyLocked(ecKey);
ecKey = ECKey.fromPublicOnly(pub); ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000); importKeyLocked(ecKey);
ecKey = ECKey.fromPublicOnly(pub); ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000); importKeyLocked(ecKey);
ecKey = ECKey.fromPublicOnly(pub); ecKey.setCreationTimeSeconds(key.getCreationTimestamp() / 1000); importKeyLocked(ecKey);
/** * Constructor an HD address. * * @param params NetworkParameters * @param cKey deterministic key for this address * @param child index of this address in its chain */ public HDAddress(NetworkParameters params, DeterministicKey cKey, int child) { this.params = params; childNum = child; DeterministicKey dk = HDKeyDerivation.deriveChildKey(cKey, new ChildNumber(childNum, false)); // compressed WIF private key format if (dk.hasPrivKey()) { byte[] prepended0Byte = ArrayUtils.addAll(new byte[1], dk.getPrivKeyBytes()); ecKey = ECKey.fromPrivate(new BigInteger(prepended0Byte), true); } else { ecKey = ECKey.fromPublicOnly(dk.getPubKey()); } long now = Utils.now().getTime() / 1000; // use Unix time (in seconds) ecKey.setCreationTimeSeconds(now); pubKey = ecKey.getPubKey(); pubKeyHash = ecKey.getPubKeyHash(); strPath = dk.getPathAsString(); }
@Before public void setUp() throws Exception { BriefLogFormatter.initVerbose(); Context ctx = new Context(PARAMS); myWatchedKey = new ECKey(); myWallet = new Wallet(PARAMS); myKey = new ECKey(); myKey.setCreationTimeSeconds(123456789L); myWallet.importKey(myKey); myAddress = myKey.toAddress(PARAMS); myWallet = new Wallet(PARAMS); myWallet.importKey(myKey); mScriptCreationTime = new Date().getTime() / 1000 - 1234; myWallet.addWatchedAddress(myWatchedKey.toAddress(PARAMS), mScriptCreationTime); myWallet.setDescription(WALLET_DESCRIPTION); }
@Test public void testWalletCatchupTime() throws Exception { // Check the fast catchup time was initialized to something around the current runtime minus a week. // The wallet was already added to the peer in setup. final int WEEK = 86400 * 7; final long now = Utils.currentTimeSeconds(); peerGroup.start(); assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000); Wallet w2 = new Wallet(PARAMS); ECKey key1 = new ECKey(); key1.setCreationTimeSeconds(now - 86400); // One day ago. w2.importKey(key1); peerGroup.addWallet(w2); peerGroup.waitForJobQueue(); assertEquals(peerGroup.getFastCatchupTimeSecs(), now - 86400 - WEEK); // Adding a key to the wallet should update the fast catchup time, but asynchronously and in the background // due to the need to avoid complicated lock inversions. ECKey key2 = new ECKey(); key2.setCreationTimeSeconds(now - 100000); w2.importKey(key2); peerGroup.waitForJobQueue(); assertEquals(peerGroup.getFastCatchupTimeSecs(), now - WEEK - 100000); }
@Test public void earliestKeyTime() throws Exception { long now = Utils.currentTimeSeconds(); // mock long yesterday = now - 86400; assertEquals(now, group.getEarliestKeyCreationTime()); Utils.rollMockClock(10000); group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); Utils.rollMockClock(10000); group.freshKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); // Check that all keys are assumed to be created at the same instant the seed is. assertEquals(now, group.getEarliestKeyCreationTime()); ECKey key = new ECKey(); key.setCreationTimeSeconds(yesterday); group.importKeys(key); assertEquals(yesterday, group.getEarliestKeyCreationTime()); }