/** * Get the currently latest unused address by purpose. */ @VisibleForTesting BitAddress currentAddress(SimpleHDKeyChain.KeyPurpose purpose) { lock.lock(); try { return BitAddress.from(type, keys.getCurrentUnusedKey(purpose)); } finally { lock.unlock(); subscribeToAddressesIfNeeded(); } }
/** * 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 testSingleAddressWallet() throws Exception { ECKey key = pocket.keys.getCurrentUnusedKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); BitWalletSingleKey bitWalletSingleKey = new BitWalletSingleKey(DOGE, key); bitWalletSingleKey.onConnection(getBlockchainConnection(DOGE)); assertEquals(DOGE.value("10"), bitWalletSingleKey.getBalance()); }