Package org.bitcoinj.crypto
Class HDKeyDerivation
- java.lang.Object
 - 
- org.bitcoinj.crypto.HDKeyDerivation
 
 
- 
public final class HDKeyDerivation extends java.lang.ObjectImplementation of the BIP 32 deterministic wallet child key generation algorithm. 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classHDKeyDerivation.PublicDeriveModestatic classHDKeyDerivation.RawKeyBytes 
- 
Field Summary
Fields Modifier and Type Field Description static intMAX_CHILD_DERIVATION_ATTEMPTSChild 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 DeterministicKeycreateMasterPrivateKey(byte[] seed)Generates a new deterministic key from the given seed, which can be any arbitrary byte array.static DeterministicKeycreateMasterPrivKeyFromBytes(byte[] privKeyBytes, byte[] chainCode)static DeterministicKeycreateMasterPubKeyFromBytes(byte[] pubKeyBytes, byte[] chainCode)static DeterministicKeyderiveChildKey(DeterministicKey parent, int childNumber)Derives a key given the "extended" child number, ie.static DeterministicKeyderiveChildKey(DeterministicKey parent, ChildNumber childNumber)static HDKeyDerivation.RawKeyBytesderiveChildKeyBytesFromPrivate(DeterministicKey parent, ChildNumber childNumber)static HDKeyDerivation.RawKeyBytesderiveChildKeyBytesFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode)static DeterministicKeyderiveChildKeyFromPrivate(DeterministicKey parent, ChildNumber childNumber)static DeterministicKeyderiveChildKeyFromPublic(DeterministicKey parent, ChildNumber childNumber, HDKeyDerivation.PublicDeriveMode mode)static DeterministicKeyderiveThisOrNextChildKey(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 ofDeterministicKeys 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 forchildNumberwill 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 aChildNumberrather than anint, for example:deriveChildKey(parent, new ChildNumber(childNumber, true))where the value of the hardened bit ofchildNumberis 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 ofDeterministicKeys 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 
DeterministicKeys 
 
- 
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 (e.g. 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
 
 - 
 
 -