Expose hdkeychain.Depth() - returning number of derivations since the root

This commit is contained in:
Thomas Kerin 2017-06-12 02:02:13 +02:00
parent 61ec18f9c8
commit 5ffa719c38
No known key found for this signature in database
GPG key ID: A3E314FE5E0DAAE0
2 changed files with 17 additions and 0 deletions

View file

@ -171,6 +171,14 @@ func (k *ExtendedKey) IsPrivate() bool {
return k.isPrivate return k.isPrivate
} }
// Depth returns the current derivation level with respect to the root.
//
// The root key has depth zero, and the field has a maximum of 255 due to
// how depth is serialized.
func (k *ExtendedKey) Depth() uint8 {
return k.depth
}
// ParentFingerprint returns a fingerprint of the parent extended key from which // ParentFingerprint returns a fingerprint of the parent extended key from which
// this one was derived. // this one was derived.
func (k *ExtendedKey) ParentFingerprint() uint32 { func (k *ExtendedKey) ParentFingerprint() uint32 {

View file

@ -231,6 +231,11 @@ tests:
} }
} }
if extKey.Depth() != uint8(len(test.path)) {
t.Errorf("Depth of key %d should match fixture path")
continue
}
privStr := extKey.String() privStr := extKey.String()
if privStr != test.wantPriv { if privStr != test.wantPriv {
t.Errorf("Serialize #%d (%s): mismatched serialized "+ t.Errorf("Serialize #%d (%s): mismatched serialized "+
@ -1042,6 +1047,10 @@ func TestMaximumDepth(t *testing.T) {
} }
for i := uint8(0); i < math.MaxUint8; i++ { for i := uint8(0); i < math.MaxUint8; i++ {
if extKey.Depth() != i {
t.Fatalf("extendedkey depth %d should match expected value %d",
extKey.Depth(), i)
}
newKey, err := extKey.Child(1) newKey, err := extKey.Child(1)
if err != nil { if err != nil {
t.Fatalf("Child: unexpected error: %v", err) t.Fatalf("Child: unexpected error: %v", err)