Package org.bitcoinj.crypto
Class HDKeyDerivation
- java.lang.Object
-
- org.bitcoinj.crypto.HDKeyDerivation
-
public final class HDKeyDerivation extends java.lang.Object
Implementation of the BIP 32 deterministic wallet child key generation algorithm.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
HDKeyDerivation.PublicDeriveMode
static class
HDKeyDerivation.RawKeyBytes
-
Field Summary
Fields Modifier and Type Field Description static int
MAX_CHILD_DERIVATION_ATTEMPTS
Child derivation may fail (although with extremely low probability); in such case it is re-attempted.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static DeterministicKey
createMasterPrivateKey(byte[] seed)
Generates a new deterministic key from the given seed, which can be any arbitrary byte array.static DeterministicKey
createMasterPrivKeyFromBytes(byte[] privKeyBytes, byte[] chainCode)
static DeterministicKey
createMasterPubKeyFromBytes(byte[] pubKeyBytes, byte[] chainCode)
static DeterministicKey
deriveChildKey(DeterministicKey parent, int childNumber)
Derives a key given the "extended" child number, ie.static DeterministicKey
deriveChildKey(DeterministicKey parent, ChildNumber childNumber)
static HDKeyDerivation.RawKeyBytes
deriveChildKeyBytesFromPrivate(DeterministicKey parent, ChildNumber childNumber)
static HDKeyDerivation.RawKeyBytes
deriveChildKeyBytesFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode)
static DeterministicKey
deriveChildKeyFromPrivate(DeterministicKey parent, ChildNumber childNumber)
static DeterministicKey
deriveChildKeyFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode)
static DeterministicKey
deriveThisOrNextChildKey(DeterministicKey parent, int childNumber)
Derives a key of the "extended" child number, ie.static java.util.stream.Stream<DeterministicKey>
generate(DeterministicKey parent, int childNumber)
Generate an infinite stream ofDeterministicKey
s from the given parent and index.
-
-
-
Field Detail
-
MAX_CHILD_DERIVATION_ATTEMPTS
public static final int MAX_CHILD_DERIVATION_ATTEMPTS
Child derivation may fail (although with extremely low probability); in such case it is re-attempted. This is the maximum number of re-attempts (to avoid an infinite loop in case of bugs etc.).- See Also:
- Constant Field Values
-
-
Method Detail
-
createMasterPrivateKey
public static DeterministicKey createMasterPrivateKey(byte[] seed) throws HDDerivationException
Generates a new deterministic key from the given seed, which can be any arbitrary byte array. However resist the temptation to use a string as the seed - any key derived from a password is likely to be weak and easily broken by attackers (this is not theoretical, people have had money stolen that way). This method checks that the given seed is at least 64 bits long.- Throws:
HDDerivationException
- if generated master key is invalid (private key not between 0 and n inclusive)java.lang.IllegalArgumentException
- if the seed is less than 8 bytes and could be brute forced
-
createMasterPrivKeyFromBytes
public static DeterministicKey createMasterPrivKeyFromBytes(byte[] privKeyBytes, byte[] chainCode) throws HDDerivationException
- Throws:
HDDerivationException
- if privKeyBytes is invalid (not between 0 and n inclusive).
-
createMasterPubKeyFromBytes
public static DeterministicKey createMasterPubKeyFromBytes(byte[] pubKeyBytes, byte[] chainCode)
-
deriveChildKey
public static DeterministicKey deriveChildKey(DeterministicKey parent, int childNumber)
Derives a key given the "extended" child number, ie. the 0x80000000 bit of the value that you pass forchildNumber
will determine whether to use hardened derivation or not. Consider whether your code would benefit from the clarity of the equivalent, but explicit, form of this method that takes aChildNumber
rather than anint
, for example:deriveChildKey(parent, new ChildNumber(childNumber, true))
where the value of the hardened bit ofchildNumber
is zero.
-
deriveThisOrNextChildKey
public static DeterministicKey deriveThisOrNextChildKey(DeterministicKey parent, int childNumber)
Derives a key of the "extended" child number, ie. with the 0x80000000 bit specifying whether to use hardened derivation or not. If derivation fails, tries a next child.
-
generate
public static java.util.stream.Stream<DeterministicKey> generate(DeterministicKey parent, int childNumber)
Generate an infinite stream ofDeterministicKey
s from the given parent and index.Note: Use
Stream.limit(long)
to get the desired number of keys.- Parameters:
parent
- The parent keychildNumber
- The index of the first child to supply/generate- Returns:
- A stream of
DeterministicKey
s
-
deriveChildKey
public static DeterministicKey deriveChildKey(DeterministicKey parent, ChildNumber childNumber) throws HDDerivationException
- Throws:
HDDerivationException
- if private derivation is attempted for a public-only parent key, or if the resulting derived key is invalid (eg. private key == 0).
-
deriveChildKeyFromPrivate
public static DeterministicKey deriveChildKeyFromPrivate(DeterministicKey parent, ChildNumber childNumber) throws HDDerivationException
- Throws:
HDDerivationException
-
deriveChildKeyBytesFromPrivate
public static HDKeyDerivation.RawKeyBytes deriveChildKeyBytesFromPrivate(DeterministicKey parent, ChildNumber childNumber) throws HDDerivationException
- Throws:
HDDerivationException
-
deriveChildKeyFromPublic
public static DeterministicKey deriveChildKeyFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode) throws HDDerivationException
- Throws:
HDDerivationException
-
deriveChildKeyBytesFromPublic
public static HDKeyDerivation.RawKeyBytes deriveChildKeyBytesFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode) throws HDDerivationException
- Throws:
HDDerivationException
-
-