/** * Returns true is it is possible to create new fresh receive addresses, false otherwise */ public boolean canCreateFreshReceiveAddress() { lock.lock(); try { DeterministicKey currentUnusedKey = keys.getCurrentUnusedKey(RECEIVE_FUNDS); int maximumKeyIndex = SimpleHDKeyChain.LOOKAHEAD - 1; // If there are used keys if (!addressesStatus.isEmpty()) { int lastUsedKeyIndex = 0; // Find the last used key index for (Map.Entry<AbstractAddress, String> entry : addressesStatus.entrySet()) { if (entry.getValue() == null) continue; DeterministicKey usedKey = keys.findKeyFromPubHash(getHash160(entry.getKey())); if (usedKey != null && keys.isExternal(usedKey) && usedKey.getChildNumber().num() > lastUsedKeyIndex) { lastUsedKeyIndex = usedKey.getChildNumber().num(); } } maximumKeyIndex = lastUsedKeyIndex + SimpleHDKeyChain.LOOKAHEAD; } log.info("Maximum key index for new key is {}", maximumKeyIndex); // If we exceeded the BIP44 look ahead threshold return currentUnusedKey.getChildNumber().num() < maximumKeyIndex; } finally { lock.unlock(); } }
@Test public void externalKeyCheck() { assertFalse(chain.isExternal(chain.getKey(KeyChain.KeyPurpose.CHANGE))); assertTrue(chain.isExternal(chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS))); }