github.com/decred/dcrlnd@v0.7.6/keychain/hdkeyring_test.go (about)

     1  package keychain
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/decred/dcrd/chaincfg/chainhash"
     7  	"github.com/decred/dcrd/chaincfg/v3"
     8  	"github.com/decred/dcrd/hdkeychain/v3"
     9  )
    10  
    11  var (
    12  	testHDSeed = chainhash.Hash{
    13  		0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
    14  		0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
    15  		0x4f, 0x2f, 0x6f, 0x25, 0x98, 0xa3, 0xef, 0xb9,
    16  		0x69, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
    17  	}
    18  )
    19  
    20  // createTestHDKeyRing creates a test HDKeyRing implementation that works
    21  // purely in memory, using the default test HD seed as root master private key.
    22  func createTestHDKeyRing() *HDKeyRing {
    23  	// We can ignore errors here because they could only be for invalid
    24  	// keys/seeds and the default test seed does not produce errors.
    25  	master, _ := hdkeychain.NewMaster(testHDSeed[:], chaincfg.RegNetParams())
    26  	masterPubs := make(map[KeyFamily]*hdkeychain.ExtendedKey,
    27  		len(versionZeroKeyFamilies))
    28  	indexes := make(map[KeyFamily]uint32, len(versionZeroKeyFamilies))
    29  	for _, keyFam := range versionZeroKeyFamilies {
    30  		masterPubs[keyFam], _ = master.Child(uint32(keyFam))
    31  		indexes[keyFam] = 0
    32  	}
    33  
    34  	fetchMasterPriv := func(keyFam KeyFamily) (*hdkeychain.ExtendedKey, error) {
    35  		// We never neutered the keys in masterPubs, so they also
    36  		// contain the private key.
    37  		return masterPubs[keyFam], nil
    38  	}
    39  
    40  	nextIndex := func(keyFam KeyFamily) (uint32, error) {
    41  		index := indexes[keyFam]
    42  		indexes[keyFam] += 1
    43  		return index, nil
    44  	}
    45  
    46  	return NewHDKeyRing(masterPubs, fetchMasterPriv, nextIndex)
    47  }
    48  
    49  // TestHDKeyRingImpl tests whether the HDKeyRing implementation conforms to the
    50  // required interface spec.
    51  func TestHDKeyRingImpl(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	CheckKeyRingImpl(t,
    55  		func() (string, func(), KeyRing, error) {
    56  			return "hdkeyring", func() {}, createTestHDKeyRing(), nil
    57  		},
    58  	)
    59  }
    60  
    61  // TestHDSecretKeyRingImpl tests whether the HDKeyRing implementation conforms
    62  // to the required interface spec.
    63  func TestHDSecretKeyRingImpl(t *testing.T) {
    64  	t.Parallel()
    65  
    66  	CheckSecretKeyRingImpl(t,
    67  		func() (string, func(), SecretKeyRing, error) {
    68  			return "hdkeyring", func() {}, createTestHDKeyRing(), nil
    69  		},
    70  	)
    71  }
    72  
    73  func init() {
    74  	// We'll clamp the max range scan to constrain the run time of the
    75  	// private key scan test.
    76  	MaxKeyRangeScan = 3
    77  }