Package org.bitcoinj.crypto
Class HDKeyDerivation
java.lang.Object
org.bitcoinj.crypto.HDKeyDerivation
Implementation of the BIP 32
deterministic wallet child key generation algorithm.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
static class
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Child derivation may fail (although with extremely low probability); in such case it is re-attempted. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.
-
Field Details
-
MAX_CHILD_DERIVATION_ATTEMPTS
public static final int MAX_CHILD_DERIVATION_ATTEMPTSChild 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:
-
-
Method Details
-
createMasterPrivateKey
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)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
-
deriveChildKey
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
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. -
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
-