public class BasicKeyChain extends 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,
Executor executor)
Adds a listener for events that are run when keys are added, on the given executor.
|
boolean |
checkAESKey(org.spongycastle.crypto.params.KeyParameter aesKey)
Check whether the AES key can decrypt the first encrypted key in the wallet.
|
boolean |
checkPassword(CharSequence password)
Returns whether the given password is correct for this key chain.
|
ECKey |
findKeyFromPubHash(byte[] pubkeyHash) |
ECKey |
findKeyFromPubKey(byte[] pubkey) |
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(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(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. |
List<ECKey> |
getKeys()
Returns a copy of the list of keys that this chain is managing.
|
List<ECKey> |
getKeys(KeyChain.KeyPurpose purpose,
int numberOfKeys)
Obtains a number of key/s intended for the given purpose.
|
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(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.
|
List<Protos.Key> |
serializeToProtobuf()
Returns a list of keys serialized to the bitcoinj protobuf format.
|
BasicKeyChain |
toDecrypted(CharSequence password)
Decrypts the key chain with the given password.
|
BasicKeyChain |
toDecrypted(org.spongycastle.crypto.params.KeyParameter aesKey)
Decrypt the key chain with the given AES key and whatever
KeyCrypter is already set. |
BasicKeyChain |
toEncrypted(CharSequence password)
Convenience wrapper around
toEncrypted(org.bitcoinj.crypto.KeyCrypter,
org.spongycastle.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.spongycastle.crypto.params.KeyParameter aesKey)
Encrypt the wallet using the KeyCrypter and the AES key.
|
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 EncryptableKeyChainpublic ECKey getKey(@Nullable KeyChain.KeyPurpose ignored)
KeyChainpublic List<ECKey> getKeys(@Nullable KeyChain.KeyPurpose purpose, int numberOfKeys)
KeyChainpublic List<ECKey> getKeys()
public int importKeys(ECKey... keys)
public void importKey(ECKey key)
public ECKey findKeyFromPubHash(byte[] pubkeyHash)
public ECKey findKeyFromPubKey(byte[] pubkey)
public boolean hasKey(ECKey key)
KeyChainpublic int numKeys()
KeyChainpublic BasicKeyChain.State isWatching()
public boolean removeKey(ECKey key)
public long getEarliestKeyCreationTime()
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_VALUE is returned.
getEarliestKeyCreationTime in interface KeyChainpublic List<ListenerRegistration<KeyChainEventListener>> getListeners()
public List<Protos.Key> serializeToProtobuf()
KeyChainserializeToProtobuf in interface KeyChainpublic static BasicKeyChain fromProtobufUnencrypted(List<Protos.Key> keys) throws UnreadableWalletException
UnreadableWalletExceptionpublic static BasicKeyChain fromProtobufEncrypted(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)
KeyChainaddEventListener in interface KeyChainpublic void addEventListener(KeyChainEventListener listener, Executor executor)
KeyChainaddEventListener in interface KeyChainpublic boolean removeEventListener(KeyChainEventListener listener)
KeyChainremoveEventListener in interface KeyChainpublic BasicKeyChain toEncrypted(CharSequence password)
toEncrypted(org.bitcoinj.crypto.KeyCrypter,
org.spongycastle.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 EncryptableKeyChainpublic BasicKeyChain toEncrypted(KeyCrypter keyCrypter, org.spongycastle.crypto.params.KeyParameter aesKey)
KeyCrypterScrypt.toEncrypted in interface EncryptableKeyChainkeyCrypter - 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(CharSequence password)
EncryptableKeyChainEncryptableKeyChain.toDecrypted(org.spongycastle.crypto.params.KeyParameter)
for details.toDecrypted in interface EncryptableKeyChainpublic BasicKeyChain toDecrypted(org.spongycastle.crypto.params.KeyParameter aesKey)
EncryptableKeyChainKeyCrypter 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
Wallet.SendRequest.aesKey field before asking the wallet to build the send.toDecrypted in interface EncryptableKeyChainaesKey - AES key to use (normally created using KeyCrypter#deriveKey and cached as it is time consuming to
create from a password)public boolean checkPassword(CharSequence password)
checkPassword in interface EncryptableKeyChainIllegalStateException - if the chain is not encrypted at all.public boolean checkAESKey(org.spongycastle.crypto.params.KeyParameter aesKey)
checkAESKey in interface EncryptableKeyChainpublic BloomFilter getFilter(int size, double falsePositiveRate, long tweak)
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 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()
KeyChainKeyChain.getFilter(int, double, long) should be at least this large.numBloomFilterEntries in interface KeyChain@Nullable public ECKey findOldestKeyAfter(long timeSecs)
Copyright © 2015. All rights reserved.