public class BasicKeyChain extends java.lang.Object implements EncryptableKeyChain
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.Modifier and Type | Class and Description |
---|---|
static class |
BasicKeyChain.State
Whether this basic key chain is empty, full of regular (usable for signing) keys, or full of watching keys.
|
KeyChain.KeyPurpose
Constructor and Description |
---|
BasicKeyChain() |
BasicKeyChain(KeyCrypter crypter) |
Modifier and Type | Method and Description |
---|---|
void |
addEventListener(KeyChainEventListener listener)
Adds a listener for events that are run when keys are added, on the user thread.
|
void |
addEventListener(KeyChainEventListener listener,
java.util.concurrent.Executor executor)
Adds a listener for events that are run when keys are added, on the given executor.
|
boolean |
checkAESKey(org.bouncycastle.crypto.params.KeyParameter aesKey)
Check whether the AES key can decrypt the first encrypted key in the wallet.
|
boolean |
checkPassword(java.lang.CharSequence password)
Returns whether the given password is correct for this key chain.
|
ECKey |
findKeyFromPubHash(byte[] pubKeyHash) |
ECKey |
findKeyFromPubKey(byte[] pubKey) |
java.util.List<ECKey> |
findKeysBefore(long timeSecs)
Returns a list of all ECKeys created after the given UNIX time.
|
ECKey |
findOldestKeyAfter(long timeSecs)
Returns the first ECKey created after the given UNIX time, or null if there is none.
|
static BasicKeyChain |
fromProtobufEncrypted(java.util.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 BasicKeyChain |
fromProtobufUnencrypted(java.util.List<Protos.Key> keys)
Returns a new BasicKeyChain that contains all basic, ORIGINAL type keys extracted from the list.
|
long |
getEarliestKeyCreationTime()
Returns the earliest creation time of keys in this chain, in seconds since the epoch.
|
BloomFilter |
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.
|
ECKey |
getKey(KeyChain.KeyPurpose ignored)
Obtains a key intended for the given purpose.
|
KeyCrypter |
getKeyCrypter()
Returns the
KeyCrypter in use or null if the key chain is not encrypted. |
java.util.List<ECKey> |
getKeys()
Returns a copy of the list of keys that this chain is managing.
|
java.util.List<ECKey> |
getKeys(KeyChain.KeyPurpose purpose,
int numberOfKeys)
Obtains a number of key/s intended for the given purpose.
|
java.util.List<ListenerRegistration<KeyChainEventListener>> |
getListeners() |
boolean |
hasKey(ECKey key)
Returns true if the given key is in the chain.
|
void |
importKey(ECKey key)
Imports a key to the key chain.
|
int |
importKeys(ECKey... keys) |
int |
importKeys(java.util.List<? extends ECKey> keys) |
BasicKeyChain.State |
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).
|
int |
numBloomFilterEntries()
Returns the number of elements this chain wishes to insert into the Bloom filter.
|
int |
numKeys()
Returns the number of keys this key chain manages.
|
boolean |
removeEventListener(KeyChainEventListener listener)
Removes a listener for events that are run when keys are added.
|
boolean |
removeKey(ECKey key)
Removes the given key from the keychain.
|
java.util.List<Protos.Key> |
serializeToProtobuf()
Returns a list of keys serialized to the bitcoinj protobuf format.
|
BasicKeyChain |
toDecrypted(java.lang.CharSequence password)
Decrypts the key chain with the given password.
|
BasicKeyChain |
toDecrypted(org.bouncycastle.crypto.params.KeyParameter aesKey)
Decrypt the key chain with the given AES key and whatever
KeyCrypter is already set. |
BasicKeyChain |
toEncrypted(java.lang.CharSequence password)
Convenience wrapper around
toEncrypted(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. |
BasicKeyChain |
toEncrypted(KeyCrypter keyCrypter,
org.bouncycastle.crypto.params.KeyParameter aesKey)
Encrypt the wallet using the KeyCrypter and the AES key.
|
java.lang.String |
toString(boolean includePrivateKeys,
org.bouncycastle.crypto.params.KeyParameter aesKey,
NetworkParameters params) |
public BasicKeyChain()
public BasicKeyChain(@Nullable KeyCrypter crypter)
@Nullable public KeyCrypter getKeyCrypter()
KeyCrypter
in use or null if the key chain is not encrypted.getKeyCrypter
in interface EncryptableKeyChain
public ECKey getKey(@Nullable KeyChain.KeyPurpose ignored)
KeyChain
public java.util.List<ECKey> getKeys(@Nullable KeyChain.KeyPurpose purpose, int numberOfKeys)
KeyChain
public java.util.List<ECKey> getKeys()
public int importKeys(ECKey... keys)
public int importKeys(java.util.List<? extends ECKey> keys)
public void importKey(ECKey key)
public ECKey findKeyFromPubHash(byte[] pubKeyHash)
public ECKey findKeyFromPubKey(byte[] pubKey)
public boolean hasKey(ECKey key)
KeyChain
public int numKeys()
KeyChain
public BasicKeyChain.State isWatching()
public boolean removeKey(ECKey key)
public long getEarliestKeyCreationTime()
KeyChain
Returns 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_VALUE
is returned.
getEarliestKeyCreationTime
in interface KeyChain
public java.util.List<ListenerRegistration<KeyChainEventListener>> getListeners()
public java.util.List<Protos.Key> serializeToProtobuf()
KeyChain
serializeToProtobuf
in interface KeyChain
public static BasicKeyChain fromProtobufUnencrypted(java.util.List<Protos.Key> keys) throws UnreadableWalletException
UnreadableWalletException
public static BasicKeyChain fromProtobufEncrypted(java.util.List<Protos.Key> keys, KeyCrypter crypter) throws UnreadableWalletException
UnreadableWalletException.BadPassword
- if the password doesn't seem to matchUnreadableWalletException
- if the data structures are corrupted/inconsistentpublic void addEventListener(KeyChainEventListener listener)
KeyChain
addEventListener
in interface KeyChain
public void addEventListener(KeyChainEventListener listener, java.util.concurrent.Executor executor)
KeyChain
addEventListener
in interface KeyChain
public boolean removeEventListener(KeyChainEventListener listener)
KeyChain
removeEventListener
in interface KeyChain
public BasicKeyChain toEncrypted(java.lang.CharSequence password)
toEncrypted(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
in interface EncryptableKeyChain
public BasicKeyChain toEncrypted(KeyCrypter keyCrypter, org.bouncycastle.crypto.params.KeyParameter aesKey)
KeyCrypterScrypt
.toEncrypted
in interface EncryptableKeyChain
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)KeyCrypterException
- Thrown if the wallet encryption fails. If so, the wallet state is unchanged.public BasicKeyChain toDecrypted(java.lang.CharSequence password)
EncryptableKeyChain
EncryptableKeyChain.toDecrypted(KeyParameter)
for details.toDecrypted
in interface EncryptableKeyChain
public BasicKeyChain toDecrypted(org.bouncycastle.crypto.params.KeyParameter aesKey)
EncryptableKeyChain
KeyCrypter
is already set. Note that if you
just want to spend money from an encrypted wallet, don't decrypt the whole thing first. Instead, set the
SendRequest.aesKey
field before asking the wallet to build the send.toDecrypted
in interface EncryptableKeyChain
aesKey
- AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming to
create from a password)public boolean checkPassword(java.lang.CharSequence password)
checkPassword
in interface EncryptableKeyChain
java.lang.IllegalStateException
- if the chain is not encrypted at all.public boolean checkAESKey(org.bouncycastle.crypto.params.KeyParameter aesKey)
checkAESKey
in interface EncryptableKeyChain
public BloomFilter getFilter(int size, double falsePositiveRate, long tweak)
KeyChain
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. 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 BloomFilter
which can be BloomFilter.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.BloomFilter(int, double, long)
for a brief
explanation of anonymity when using bloom filters, and for the meaning of these parameters.
public int numBloomFilterEntries()
KeyChain
KeyChain.getFilter(int, double, long)
should be at least this large.numBloomFilterEntries
in interface KeyChain
@Nullable public ECKey findOldestKeyAfter(long timeSecs)
public java.util.List<ECKey> findKeysBefore(long timeSecs)
public java.lang.String toString(boolean includePrivateKeys, @Nullable org.bouncycastle.crypto.params.KeyParameter aesKey, NetworkParameters params)