3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-15 19:55:51 +00:00

Make BasicKeyChain.isWatching return a state enum instead and use it to simplify KeyChainGroup.isWatching()

This commit is contained in:
Mike Hearn 2015-04-16 16:41:33 +01:00
parent ed6821ed15
commit 54a6316243
2 changed files with 44 additions and 43 deletions

View File

@ -230,16 +230,23 @@ public class BasicKeyChain implements EncryptableKeyChain {
return pubkeyToKeys.size(); return pubkeyToKeys.size();
} }
/** Whether this basic key chain is empty, full of regular (usable for signing) keys, or full of watching keys. */
public enum State {
EMPTY,
WATCHING,
REGULAR
}
/** /**
* Returns whether this chain consists entirely of watching keys (unencrypted keys with no private part). Mixed * Returns whether this chain consists of pubkey only (watching) keys, regular keys (usable for signing), or
* chains are forbidden. Null means the chain is empty. * has no keys in it yet at all (thus we cannot tell).
*/ */
public Boolean isWatching() { public State isWatching() {
lock.lock(); lock.lock();
try { try {
if (hashToKeys.isEmpty()) if (hashToKeys.isEmpty())
return null; return State.EMPTY;
return isWatching; return isWatching ? State.WATCHING : State.REGULAR;
} finally { } finally {
lock.unlock(); lock.unlock();
} }

View File

@ -17,31 +17,21 @@
package org.bitcoinj.wallet; package org.bitcoinj.wallet;
import com.google.common.base.*;
import com.google.common.collect.*;
import com.google.protobuf.*;
import org.bitcoinj.core.*; import org.bitcoinj.core.*;
import org.bitcoinj.crypto.ChildNumber; import org.bitcoinj.crypto.*;
import org.bitcoinj.crypto.DeterministicKey; import org.bitcoinj.script.*;
import org.bitcoinj.crypto.KeyCrypter; import org.bitcoinj.store.*;
import org.bitcoinj.crypto.LinuxSecureRandom; import org.bitcoinj.utils.*;
import org.bitcoinj.script.Script; import org.slf4j.*;
import org.bitcoinj.script.ScriptBuilder; import org.spongycastle.crypto.params.*;
import org.bitcoinj.store.UnreadableWalletException;
import org.bitcoinj.utils.ListenerRegistration;
import org.bitcoinj.utils.Threading;
import com.google.common.base.Joiner; import javax.annotation.*;
import com.google.common.collect.HashMultimap; import java.security.*;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.google.protobuf.ByteString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.params.KeyParameter;
import javax.annotation.Nullable;
import java.security.SecureRandom;
import java.util.*; import java.util.*;
import java.util.concurrent.Executor; import java.util.concurrent.*;
import static com.google.common.base.Preconditions.*; import static com.google.common.base.Preconditions.*;
@ -555,24 +545,28 @@ public class KeyChainGroup implements KeyBag {
* Returns whether this chain has only watching keys (unencrypted keys with no private part). Mixed chains are * Returns whether this chain has only watching keys (unencrypted keys with no private part). Mixed chains are
* forbidden. * forbidden.
* *
* @throws IllegalStateException * @throws IllegalStateException if there are no keys, or if there is a mix between watching and non-watching keys.
* if there are no keys, or if there is a mix between watching and non-watching keys.
*/ */
public boolean isWatching() { public boolean isWatching() {
Boolean basicChainIsWatching = basic.isWatching(); BasicKeyChain.State basicState = basic.isWatching();
Boolean deterministicChainIsWatching = !chains.isEmpty() ? getActiveKeyChain().isWatching() : null; BasicKeyChain.State activeState = BasicKeyChain.State.EMPTY;
if (basicChainIsWatching == null && deterministicChainIsWatching == null) if (!chains.isEmpty()) {
throw new IllegalStateException("No keys"); if (getActiveKeyChain().isWatching())
if (basicChainIsWatching == null && deterministicChainIsWatching != null) activeState = BasicKeyChain.State.WATCHING;
return deterministicChainIsWatching; else
if (basicChainIsWatching != null && deterministicChainIsWatching == null) activeState = BasicKeyChain.State.REGULAR;
return basicChainIsWatching; }
if (basicChainIsWatching == deterministicChainIsWatching) if (basicState == BasicKeyChain.State.EMPTY) {
return deterministicChainIsWatching; if (activeState == BasicKeyChain.State.EMPTY)
if (basicChainIsWatching && !deterministicChainIsWatching) throw new IllegalStateException("Empty key chain group: cannot answer isWatching() query");
throw new IllegalStateException("Basic chain is watching, deterministic chain is not"); return activeState == BasicKeyChain.State.WATCHING;
else } else if (activeState == BasicKeyChain.State.EMPTY)
throw new IllegalStateException("Basic chain is not watching, deterministic chain is"); return basicState == BasicKeyChain.State.WATCHING;
else {
if (activeState != basicState)
throw new IllegalStateException("Mix of watching and non-watching keys in wallet");
return activeState == BasicKeyChain.State.WATCHING;
}
} }
/** Returns the key crypter or null if the group is not encrypted. */ /** Returns the key crypter or null if the group is not encrypted. */