From 5ffa719c3882fd2ec1e8b9f4978066701c31a343 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Mon, 12 Jun 2017 02:02:13 +0200 Subject: [PATCH] Expose hdkeychain.Depth() - returning number of derivations since the root --- hdkeychain/extendedkey.go | 8 ++++++++ hdkeychain/extendedkey_test.go | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/hdkeychain/extendedkey.go b/hdkeychain/extendedkey.go index b634cd0..53486ad 100644 --- a/hdkeychain/extendedkey.go +++ b/hdkeychain/extendedkey.go @@ -171,6 +171,14 @@ func (k *ExtendedKey) IsPrivate() bool { 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 // this one was derived. func (k *ExtendedKey) ParentFingerprint() uint32 { diff --git a/hdkeychain/extendedkey_test.go b/hdkeychain/extendedkey_test.go index 850256f..d37c53c 100644 --- a/hdkeychain/extendedkey_test.go +++ b/hdkeychain/extendedkey_test.go @@ -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() if privStr != test.wantPriv { t.Errorf("Serialize #%d (%s): mismatched serialized "+ @@ -1042,6 +1047,10 @@ func TestMaximumDepth(t *testing.T) { } 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) if err != nil { t.Fatalf("Child: unexpected error: %v", err)