Class BasicKeyChain
- All Implemented Interfaces:
EncryptableKeyChain,KeyChain
KeyChain that implements the simplest model possible: it can have keys imported into it, and just acts as
a dumb bag of keys. It will, left to its own devices, always return the same key for usage by the wallet, although
it will automatically add one to itself if it's empty or if encryption is requested.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumWhether this basic key chain is empty, full of regular (usable for signing) keys, or full of watching keys.Nested classes/interfaces inherited from interface org.bitcoinj.wallet.KeyChain
KeyChain.KeyPurpose -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddEventListener(KeyChainEventListener listener) Adds a listener for events that are run when keys are added, on the user thread.voidaddEventListener(KeyChainEventListener listener, Executor executor) Adds a listener for events that are run when keys are added, on the given executor.booleancheckAESKey(org.bouncycastle.crypto.params.KeyParameter aesKey) Check whether the AES key can decrypt the first encrypted key in the wallet.booleancheckPassword(CharSequence password) Returns whether the given password is correct for this key chain.findKeyFromPubHash(byte[] pubKeyHash) findKeyFromPubKey(byte[] pubKey) findKeysBefore(long timeSecs) Returns a list of all ECKeys created after the given UNIX time.findOldestKeyAfter(long timeSecs) Returns the first ECKey created after the given UNIX time, or null if there is none.static BasicKeyChainfromProtobufEncrypted(List<Protos.Key> keys, KeyCrypter crypter) Returns a new BasicKeyChain that contains all basic, ORIGINAL type keys and also any encrypted keys extracted from the list.static BasicKeyChainReturns a new BasicKeyChain that contains all basic, ORIGINAL type keys extracted from the list.longReturns the earliest creation time of keys in this chain, in seconds since the epoch.getFilter(int size, double falsePositiveRate, long tweak) Gets a bloom filter that contains all of the public keys from this chain, and which will provide the given false-positive rate if it has size elements.getKey(KeyChain.KeyPurpose ignored) Obtains a key intended for the given purpose.Returns theKeyCrypterin use or null if the key chain is not encrypted.getKeys()Returns a copy of the list of keys that this chain is managing.getKeys(KeyChain.KeyPurpose purpose, int numberOfKeys) Obtains a number of key/s intended for the given purpose.booleanReturns true if the given key is in the chain.voidImports a key to the key chain.intimportKeys(List<? extends ECKey> keys) intimportKeys(ECKey... keys) Returns whether this chain consists of pubkey only (watching) keys, regular keys (usable for signing), or has no keys in it yet at all (thus we cannot tell).intReturns the number of elements this chain wishes to insert into the Bloom filter.intnumKeys()Returns the number of keys this key chain manages.booleanremoveEventListener(KeyChainEventListener listener) Removes a listener for events that are run when keys are added.booleanRemoves the given key from the keychain.Returns a list of keys serialized to the bitcoinj protobuf format.toDecrypted(CharSequence password) Decrypts the key chain with the given password.toDecrypted(org.bouncycastle.crypto.params.KeyParameter aesKey) Decrypt the key chain with the given AES key and whateverKeyCrypteris already set.toEncrypted(CharSequence password) Convenience wrapper aroundtoEncrypted(KeyCrypter, org.bouncycastle.crypto.params.KeyParameter)which uses the default Scrypt key derivation algorithm and parameters, derives a key from the given password and returns the created key.toEncrypted(KeyCrypter keyCrypter, org.bouncycastle.crypto.params.KeyParameter aesKey) Encrypt the wallet using the KeyCrypter and the AES key.toString(boolean includePrivateKeys, org.bouncycastle.crypto.params.KeyParameter aesKey, NetworkParameters params)
-
Constructor Details
-
BasicKeyChain
public BasicKeyChain() -
BasicKeyChain
-
-
Method Details
-
getKeyCrypter
Returns theKeyCrypterin use or null if the key chain is not encrypted.- Specified by:
getKeyCrypterin interfaceEncryptableKeyChain
-
getKey
Description copied from interface:KeyChainObtains a key intended for the given purpose. The chain may create a new key, derive one, or re-use an old one. -
getKeys
Description copied from interface:KeyChainObtains a number of key/s intended for the given purpose. The chain may create new key/s, derive, or re-use an old one. -
getKeys
Returns a copy of the list of keys that this chain is managing. -
importKeys
-
importKeys
-
importKey
Imports a key to the key chain. If key is present in the key chain, ignore it. -
findKeyFromPubHash
-
findKeyFromPubKey
-
hasKey
Description copied from interface:KeyChainReturns true if the given key is in the chain. -
numKeys
public int numKeys()Description copied from interface:KeyChainReturns the number of keys this key chain manages. -
isWatching
Returns whether this chain consists of pubkey only (watching) keys, regular keys (usable for signing), or has no keys in it yet at all (thus we cannot tell). -
removeKey
Removes the given key from the keychain. Be very careful with this - losing a private key destroys the money associated with it.- Returns:
- Whether the key was removed or not.
-
getEarliestKeyCreationTime
public long getEarliestKeyCreationTime()Description copied from interface:KeyChainReturns the earliest creation time of keys in this chain, in seconds since the epoch. This can return zero if at least one key does not have that data (was created before key timestamping was implemented). If there are no keys in the wallet,
Long.MAX_VALUEis returned.- Specified by:
getEarliestKeyCreationTimein interfaceKeyChain
-
getListeners
-
serializeToProtobuf
Description copied from interface:KeyChainReturns a list of keys serialized to the bitcoinj protobuf format.- Specified by:
serializeToProtobufin interfaceKeyChain
-
fromProtobufUnencrypted
public static BasicKeyChain fromProtobufUnencrypted(List<Protos.Key> keys) throws UnreadableWalletException Returns a new BasicKeyChain that contains all basic, ORIGINAL type keys extracted from the list. Unrecognised key types are ignored.- Throws:
UnreadableWalletException
-
fromProtobufEncrypted
public static BasicKeyChain fromProtobufEncrypted(List<Protos.Key> keys, KeyCrypter crypter) throws UnreadableWalletException Returns a new BasicKeyChain that contains all basic, ORIGINAL type keys and also any encrypted keys extracted from the list. Unrecognised key types are ignored.- Throws:
UnreadableWalletException.BadPassword- if the password doesn't seem to matchUnreadableWalletException- if the data structures are corrupted/inconsistent
-
addEventListener
Description copied from interface:KeyChainAdds a listener for events that are run when keys are added, on the user thread.- Specified by:
addEventListenerin interfaceKeyChain
-
addEventListener
Description copied from interface:KeyChainAdds a listener for events that are run when keys are added, on the given executor.- Specified by:
addEventListenerin interfaceKeyChain
-
removeEventListener
Description copied from interface:KeyChainRemoves a listener for events that are run when keys are added.- Specified by:
removeEventListenerin interfaceKeyChain
-
toEncrypted
Convenience wrapper aroundtoEncrypted(KeyCrypter, org.bouncycastle.crypto.params.KeyParameter)which uses the default Scrypt key derivation algorithm and parameters, derives a key from the given password and returns the created key.- Specified by:
toEncryptedin interfaceEncryptableKeyChain- Returns:
- The derived key, in case you wish to cache it for future use.
-
toEncrypted
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, org.bouncycastle.crypto.params.KeyParameter aesKey) Encrypt the wallet using the KeyCrypter and the AES key. A good default KeyCrypter to use isKeyCrypterScrypt.- Specified by:
toEncryptedin interfaceEncryptableKeyChain- Parameters:
keyCrypter- The KeyCrypter that specifies how to encrypt/ decrypt a keyaesKey- AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming to create from a password)- Throws:
KeyCrypterException- Thrown if the wallet encryption fails. If so, the wallet state is unchanged.
-
toDecrypted
Description copied from interface:EncryptableKeyChainDecrypts the key chain with the given password. SeeEncryptableKeyChain.toDecrypted(KeyParameter)for details.- Specified by:
toDecryptedin interfaceEncryptableKeyChain
-
toDecrypted
Description copied from interface:EncryptableKeyChainDecrypt the key chain with the given AES key and whateverKeyCrypteris already set. Note that if you just want to spend money from an encrypted wallet, don't decrypt the whole thing first. Instead, set theSendRequest.aesKeyfield before asking the wallet to build the send.- Specified by:
toDecryptedin interfaceEncryptableKeyChain- Parameters:
aesKey- AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming to create from a password)
-
checkPassword
Returns whether the given password is correct for this key chain.- Specified by:
checkPasswordin interfaceEncryptableKeyChain- Throws:
IllegalStateException- if the chain is not encrypted at all.
-
checkAESKey
public boolean checkAESKey(org.bouncycastle.crypto.params.KeyParameter aesKey) Check whether the AES key can decrypt the first encrypted key in the wallet.- Specified by:
checkAESKeyin interfaceEncryptableKeyChain- Returns:
- true if AES key supplied can decrypt the first encrypted private key in the wallet, false otherwise.
-
getFilter
Description copied from interface:KeyChainGets a bloom filter that contains all of the public keys from this chain, and which will provide the given false-positive rate if it has size elements. Keep in mind that you will get 2 elements in the bloom filter for each key in the key chain, for the public key and the hash of the public key (address form). For this reason size should be at least 2x the result of
KeyChain.numKeys().This is used to generate a
BloomFilterwhich can beBloomFilter.merge(BloomFilter)d with another. It could also be used if you have a specific target for the filter's size.See the docs for
BloomFilter(int, double, long)for a brief explanation of anonymity when using bloom filters, and for the meaning of these parameters. -
numBloomFilterEntries
public int numBloomFilterEntries()Description copied from interface:KeyChainReturns the number of elements this chain wishes to insert into the Bloom filter. The size passed toKeyChain.getFilter(int, double, long)should be at least this large.- Specified by:
numBloomFilterEntriesin interfaceKeyChain
-
findOldestKeyAfter
Returns the first ECKey created after the given UNIX time, or null if there is none. -
findKeysBefore
Returns a list of all ECKeys created after the given UNIX time. -
toString
public String toString(boolean includePrivateKeys, @Nullable org.bouncycastle.crypto.params.KeyParameter aesKey, NetworkParameters params)
-