Class HDPath
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<ChildNumber>
-
- org.bitcoinj.crypto.HDPath
-
- All Implemented Interfaces:
java.lang.Iterable<ChildNumber>
,java.util.Collection<ChildNumber>
,java.util.List<ChildNumber>
public class HDPath extends java.util.AbstractList<ChildNumber>
HD Key derivation path.HDPath
can be used to represent a full path or a relative path. ThehasPrivateKey
boolean
is used for rendering toString
but (at present) not much else. It defaults tofalse
which is the preferred setting for a relative path.HDPath
is immutable and uses theCollections.UnmodifiableList
type internally.It implements
java.util.List<ChildNumber>
to ease migration from the previous implementation. When anHDPath
is returned you can treat it as aList<ChildNumber>
where necessary in your code. Although it is recommended to use theHDPath
type for clarity and for access toHDPath
-specific functionality.Note that it is possible for
HDPath
to be an empty list.Take note of the overloaded factory methods
M()
andm()
. These can be used to very concisely create HDPath objects (especially when statically imported.)
-
-
Field Summary
Fields Modifier and Type Field Description static HDPath
BIP44_PARENT
Partial path with BIP44 purposestatic HDPath
BIP84_PARENT
Partial path with BIP84 purposestatic HDPath
BIP86_PARENT
Partial path with BIP86 purposeprotected boolean
hasPrivateKey
protected java.util.List<ChildNumber>
unmodifiableList
-
Constructor Summary
Constructors Constructor Description HDPath(boolean hasPrivateKey, java.util.List<ChildNumber> list)
Constructs a path for a public or private key.HDPath(java.util.List<ChildNumber> list)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<HDPath>
ancestors()
Return a list of all ancestors of this pathjava.util.List<HDPath>
ancestors(boolean includeSelf)
Return a list of all ancestors of this pathstatic HDPath
deserialize(java.util.List<java.lang.Integer> integerList)
Deserialize a list of integers into an HDPath (internal use only)HDPath
extend(java.util.List<ChildNumber> path2)
Extend the path by appending a relative path.HDPath
extend(ChildNumber child1, ChildNumber... children)
Extend the path by appending additional ChildNumber objects.HDPath
extend(HDPath path2)
Extend the path by appending a relative path.ChildNumber
get(int index)
boolean
hasPrivateKey()
Is this a path to a private key?java.util.List<ChildNumber>
list()
Return a simple list ofChildNumber
static HDPath
m()
Returns an empty path for a private key.static HDPath
m(java.util.List<ChildNumber> list)
Returns a path for a private key.static HDPath
m(ChildNumber childNumber)
Returns a path for a private key.static HDPath
m(ChildNumber... children)
Returns a path for a private key.static HDPath
M()
Returns an empty path for a public key.static HDPath
M(java.util.List<ChildNumber> list)
Returns a path for a public key.static HDPath
M(ChildNumber childNumber)
Returns a path for a public key.static HDPath
M(ChildNumber... children)
Returns a path for a public key.HDPath
parent()
Return the parent path.static HDPath
parsePath(java.lang.String path)
Create an HDPath from a path string.int
size()
java.lang.String
toString()
-
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
-
-
-
-
Field Detail
-
hasPrivateKey
protected final boolean hasPrivateKey
-
unmodifiableList
protected final java.util.List<ChildNumber> unmodifiableList
-
BIP44_PARENT
public static final HDPath BIP44_PARENT
Partial path with BIP44 purpose
-
BIP84_PARENT
public static final HDPath BIP84_PARENT
Partial path with BIP84 purpose
-
BIP86_PARENT
public static final HDPath BIP86_PARENT
Partial path with BIP86 purpose
-
-
Constructor Detail
-
HDPath
public HDPath(boolean hasPrivateKey, java.util.List<ChildNumber> list)
Constructs a path for a public or private key. Should probably be a private constructor.- Parameters:
hasPrivateKey
- Whether it is a path to a private key or notlist
- List of children in the path
-
HDPath
@Deprecated public HDPath(java.util.List<ChildNumber> list)
Constructs a path for a public key.- Parameters:
list
- List of children in the path
-
-
Method Detail
-
deserialize
public static HDPath deserialize(java.util.List<java.lang.Integer> integerList)
Deserialize a list of integers into an HDPath (internal use only)- Parameters:
integerList
- A list of integers (what we use in ProtoBuf for an HDPath)- Returns:
- a deserialized HDPath (hasPrivateKey is false/unknown)
-
M
public static HDPath M(java.util.List<ChildNumber> list)
Returns a path for a public key.- Parameters:
list
- List of children in the path
-
M
public static HDPath M()
Returns an empty path for a public key.
-
M
public static HDPath M(ChildNumber childNumber)
Returns a path for a public key.- Parameters:
childNumber
- Single child in path
-
M
public static HDPath M(ChildNumber... children)
Returns a path for a public key.- Parameters:
children
- Children in the path
-
m
public static HDPath m(java.util.List<ChildNumber> list)
Returns a path for a private key.- Parameters:
list
- List of children in the path
-
m
public static HDPath m()
Returns an empty path for a private key.
-
m
public static HDPath m(ChildNumber childNumber)
Returns a path for a private key.- Parameters:
childNumber
- Single child in path
-
m
public static HDPath m(ChildNumber... children)
Returns a path for a private key.- Parameters:
children
- Children in the path
-
parsePath
public static HDPath parsePath(@Nonnull java.lang.String path)
Create an HDPath from a path string. The path string is a human-friendly representation of the deterministic path. For example: "44H / 0H / 0H / 1 / 1" Where a letter "H" means hardened key. Spaces are ignored.
-
hasPrivateKey
public boolean hasPrivateKey()
Is this a path to a private key?- Returns:
- true if yes, false if no or a partial path
-
extend
public HDPath extend(ChildNumber child1, ChildNumber... children)
Extend the path by appending additional ChildNumber objects.- Parameters:
child1
- the first child to appendchildren
- zero or more additional children to append- Returns:
- A new immutable path
-
extend
public HDPath extend(HDPath path2)
Extend the path by appending a relative path.- Parameters:
path2
- the relative path to append- Returns:
- A new immutable path
-
extend
public HDPath extend(java.util.List<ChildNumber> path2)
Extend the path by appending a relative path.- Parameters:
path2
- the relative path to append- Returns:
- A new immutable path
-
list
public java.util.List<ChildNumber> list()
Return a simple list ofChildNumber
- Returns:
- an unmodifiable list of
ChildNumber
-
parent
public HDPath parent()
Return the parent path.Note that this method defines the parent of a root path as the empty path and the parent of the empty path as the empty path. This behavior is what one would expect of an unmodifiable, copy-on-modify list. If you need to check for edge cases, you can use
AbstractCollection.isEmpty()
before or after usingHDPath#parent()
- Returns:
- parent path (which can be empty -- see above)
-
ancestors
public java.util.List<HDPath> ancestors()
Return a list of all ancestors of this path- Returns:
- unmodifiable list of ancestors
-
ancestors
public java.util.List<HDPath> ancestors(boolean includeSelf)
Return a list of all ancestors of this path- Parameters:
includeSelf
- true if include path for self- Returns:
- unmodifiable list of ancestors
-
get
public ChildNumber get(int index)
- Specified by:
get
in interfacejava.util.List<ChildNumber>
- Specified by:
get
in classjava.util.AbstractList<ChildNumber>
-
size
public int size()
- Specified by:
size
in interfacejava.util.Collection<ChildNumber>
- Specified by:
size
in interfacejava.util.List<ChildNumber>
- Specified by:
size
in classjava.util.AbstractCollection<ChildNumber>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.util.AbstractCollection<ChildNumber>
-
-