waddrmgr: convert unit tests to package-level tests

In this commit, we convert our unit tests to have package-level access.
We do this as an effort to reduce test code duplication when we
introduce migration tests which require access to specific unexported
functions/methods.
This commit is contained in:
Wilmer Paulino 2018-11-05 23:24:38 -08:00
parent 6d43b2e29b
commit c2d6f1a8b3
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F
2 changed files with 168 additions and 165 deletions

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package waddrmgr_test package waddrmgr
import ( import (
"encoding/hex" "encoding/hex"
@ -13,7 +13,6 @@ import (
"time" "time"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb" _ "github.com/btcsuite/btcwallet/walletdb/bdb"
) )
@ -34,7 +33,7 @@ var (
// fastScrypt are parameters used throughout the tests to speed up the // fastScrypt are parameters used throughout the tests to speed up the
// scrypt operations. // scrypt operations.
fastScrypt = &waddrmgr.ScryptOptions{ fastScrypt = &ScryptOptions{
N: 16, N: 16,
R: 8, R: 8,
P: 1, P: 1,
@ -55,7 +54,7 @@ var (
pubKey: hexToBytes("02d8f88468c5a2e8e1815faf555f59cbd1979e3dbdf823f80c271b6fb70d2d519b"), pubKey: hexToBytes("02d8f88468c5a2e8e1815faf555f59cbd1979e3dbdf823f80c271b6fb70d2d519b"),
privKey: hexToBytes("c27d6581b92785834b381fa697c4b0ffc4574b495743722e0acb7601b1b68b99"), privKey: hexToBytes("c27d6581b92785834b381fa697c4b0ffc4574b495743722e0acb7601b1b68b99"),
privKeyWIF: "L3jmpy54Pc7MLXTN2mL8Xas7BJziwKaUGmgnXXzgGbVRdiAniXZk", privKeyWIF: "L3jmpy54Pc7MLXTN2mL8Xas7BJziwKaUGmgnXXzgGbVRdiAniXZk",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 0, Branch: 0,
Index: 0, Index: 0,
@ -70,7 +69,7 @@ var (
pubKey: hexToBytes("02b9c175b908624f8a8eaac227d0e8c77c0eec327b8c512ad1b8b7a4b5b676971f"), pubKey: hexToBytes("02b9c175b908624f8a8eaac227d0e8c77c0eec327b8c512ad1b8b7a4b5b676971f"),
privKey: hexToBytes("18f3b191019e83878a81557abebb2afda199e31d22e150d8bf4df4561671be6c"), privKey: hexToBytes("18f3b191019e83878a81557abebb2afda199e31d22e150d8bf4df4561671be6c"),
privKeyWIF: "Kx4DNid19W8sjNFN3uPqQE7UYnCqyEp7unCvdkf2LrVUFpnDtwpB", privKeyWIF: "Kx4DNid19W8sjNFN3uPqQE7UYnCqyEp7unCvdkf2LrVUFpnDtwpB",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 0, Branch: 0,
Index: 1, Index: 1,
@ -85,7 +84,7 @@ var (
pubKey: hexToBytes("0329faddf1254d490d6add49e2b08cf52b561038c72baec0edb3cfacff71ff1021"), pubKey: hexToBytes("0329faddf1254d490d6add49e2b08cf52b561038c72baec0edb3cfacff71ff1021"),
privKey: hexToBytes("ccb8f6305b73136b363644b647f6efc0fd27b6b7d9c11c7e560662ed38db7b34"), privKey: hexToBytes("ccb8f6305b73136b363644b647f6efc0fd27b6b7d9c11c7e560662ed38db7b34"),
privKeyWIF: "L45fWF6Yd736fDohuB97vwRRLdQQJr3ZGvbokk9ubiT7aNrg7tTn", privKeyWIF: "L45fWF6Yd736fDohuB97vwRRLdQQJr3ZGvbokk9ubiT7aNrg7tTn",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 0, Branch: 0,
Index: 2, Index: 2,
@ -100,7 +99,7 @@ var (
pubKey: hexToBytes("03d738324e2f0ce42e46975d7f8c7117c1670e3d7912b0291aea452add99674774"), pubKey: hexToBytes("03d738324e2f0ce42e46975d7f8c7117c1670e3d7912b0291aea452add99674774"),
privKey: hexToBytes("d6bc8ff768814fede2adcdb74826bd846924341b3862e3b6e31cdc084e992940"), privKey: hexToBytes("d6bc8ff768814fede2adcdb74826bd846924341b3862e3b6e31cdc084e992940"),
privKeyWIF: "L4R8XyxYQyPSpTwj8w96tM86a6j3QA9jbRPj3RA7DVTVWk71ndeP", privKeyWIF: "L4R8XyxYQyPSpTwj8w96tM86a6j3QA9jbRPj3RA7DVTVWk71ndeP",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 0, Branch: 0,
Index: 3, Index: 3,
@ -115,7 +114,7 @@ var (
pubKey: hexToBytes("03a917acd5cd5b6f544b43f1921a35677e4d5320e5d2add2056039b4b44fdf905e"), pubKey: hexToBytes("03a917acd5cd5b6f544b43f1921a35677e4d5320e5d2add2056039b4b44fdf905e"),
privKey: hexToBytes("8563ade061110e03aee50695ffc5cb1c06c8310bde0a3674257c853c966968c0"), privKey: hexToBytes("8563ade061110e03aee50695ffc5cb1c06c8310bde0a3674257c853c966968c0"),
privKeyWIF: "L1h16Hunxomww4FrpyQP2iFmWNgG7U1u3awp6Vd3s2uGf7v5VU8c", privKeyWIF: "L1h16Hunxomww4FrpyQP2iFmWNgG7U1u3awp6Vd3s2uGf7v5VU8c",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 0, Branch: 0,
Index: 4, Index: 4,
@ -130,7 +129,7 @@ var (
pubKey: hexToBytes("020a1290b997c0a234a95213962e7edcb761c7360f0230f698a1a3e71c37047bb0"), pubKey: hexToBytes("020a1290b997c0a234a95213962e7edcb761c7360f0230f698a1a3e71c37047bb0"),
privKey: hexToBytes("fe4f855fcf059ec6ddf7b25f63b19aa49c771d1fcb9850b68ae3d65e20657a60"), privKey: hexToBytes("fe4f855fcf059ec6ddf7b25f63b19aa49c771d1fcb9850b68ae3d65e20657a60"),
privKeyWIF: "L5k4HivqXvohxBMpuwD38iUgi6uewffwZny91ZNYfM39RXH2x3QR", privKeyWIF: "L5k4HivqXvohxBMpuwD38iUgi6uewffwZny91ZNYfM39RXH2x3QR",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 1, Branch: 1,
Index: 0, Index: 0,
@ -145,7 +144,7 @@ var (
pubKey: hexToBytes("03f79bbde32af42dde98195f011d95982602fcd0dab657fe4a1f49f9d5ada1e02d"), pubKey: hexToBytes("03f79bbde32af42dde98195f011d95982602fcd0dab657fe4a1f49f9d5ada1e02d"),
privKey: hexToBytes("bfef521317c65b018ae7e6d7ecc3aa700d5d0f7ea84d567be9270382d0b5e3e6"), privKey: hexToBytes("bfef521317c65b018ae7e6d7ecc3aa700d5d0f7ea84d567be9270382d0b5e3e6"),
privKeyWIF: "L3eomUajnTDM3Pc8GU47qqXUFuCjvpqY7NYN9mH3x1ZFjDgiY4BU", privKeyWIF: "L3eomUajnTDM3Pc8GU47qqXUFuCjvpqY7NYN9mH3x1ZFjDgiY4BU",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 1, Branch: 1,
Index: 1, Index: 1,
@ -160,7 +159,7 @@ var (
pubKey: hexToBytes("023ded84afe4fe91b52b45c3deb26fd263f749cbc27747dc964dae9e0739cbc579"), pubKey: hexToBytes("023ded84afe4fe91b52b45c3deb26fd263f749cbc27747dc964dae9e0739cbc579"),
privKey: hexToBytes("f506dffd4494c24006df7a35f3291f7ca0297a1a431557a1339bfed6f48738ca"), privKey: hexToBytes("f506dffd4494c24006df7a35f3291f7ca0297a1a431557a1339bfed6f48738ca"),
privKeyWIF: "L5S1bVQUPqQb1Su82fLoSpnGCjcPfdAQE1pJxWRopJSBdYNDHESv", privKeyWIF: "L5S1bVQUPqQb1Su82fLoSpnGCjcPfdAQE1pJxWRopJSBdYNDHESv",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 1, Branch: 1,
Index: 2, Index: 2,
@ -175,7 +174,7 @@ var (
pubKey: hexToBytes("030f1e79f06824e10a259914ec310528bb2d5b8d6356341fe9dff55498591af6af"), pubKey: hexToBytes("030f1e79f06824e10a259914ec310528bb2d5b8d6356341fe9dff55498591af6af"),
privKey: hexToBytes("b3629de8ef6a275b4ffae41aa2bbbc2952eb92282ea6402435abbb010ecc1fb8"), privKey: hexToBytes("b3629de8ef6a275b4ffae41aa2bbbc2952eb92282ea6402435abbb010ecc1fb8"),
privKeyWIF: "L3EQsGeEnyXmKaux54cG4DQeCSQDvGuvEuy3W2ss4geum7AtWaHw", privKeyWIF: "L3EQsGeEnyXmKaux54cG4DQeCSQDvGuvEuy3W2ss4geum7AtWaHw",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 1, Branch: 1,
Index: 3, Index: 3,
@ -190,7 +189,7 @@ var (
pubKey: hexToBytes("0317d7182e26b6ca3e0f3db531c474b9cab7a763a75eabff2e14ac92f62a793238"), pubKey: hexToBytes("0317d7182e26b6ca3e0f3db531c474b9cab7a763a75eabff2e14ac92f62a793238"),
privKey: hexToBytes("ca747a7ef815ea0dbe68655272cecbfbd65f2a109019a9ed28e0d3dcaffe05c3"), privKey: hexToBytes("ca747a7ef815ea0dbe68655272cecbfbd65f2a109019a9ed28e0d3dcaffe05c3"),
privKeyWIF: "L41Frac75RPbTELKzw1EGC2qCkdveiVumpmsyX4daAvyyCMxit1W", privKeyWIF: "L41Frac75RPbTELKzw1EGC2qCkdveiVumpmsyX4daAvyyCMxit1W",
derivationInfo: waddrmgr.DerivationPath{ derivationInfo: DerivationPath{
Account: 0, Account: 0,
Branch: 1, Branch: 1,
Index: 4, Index: 4,
@ -209,11 +208,13 @@ var (
// checkManagerError ensures the passed error is a ManagerError with an error // checkManagerError ensures the passed error is a ManagerError with an error
// code that matches the passed error code. // code that matches the passed error code.
func checkManagerError(t *testing.T, testName string, gotErr error, wantErrCode waddrmgr.ErrorCode) bool { func checkManagerError(t *testing.T, testName string, gotErr error,
merr, ok := gotErr.(waddrmgr.ManagerError) wantErrCode ErrorCode) bool {
merr, ok := gotErr.(ManagerError)
if !ok { if !ok {
t.Errorf("%s: unexpected error type - got %T, want %T", t.Errorf("%s: unexpected error type - got %T, want %T",
testName, gotErr, waddrmgr.ManagerError{}) testName, gotErr, ManagerError{})
return false return false
} }
if merr.ErrorCode != wantErrCode { if merr.ErrorCode != wantErrCode {
@ -255,7 +256,7 @@ func emptyDB(t *testing.T) (tearDownFunc func(), db walletdb.DB) {
// setupManager creates a new address manager and returns a teardown function // setupManager creates a new address manager and returns a teardown function
// that should be invoked to ensure it is closed and removed upon completion. // that should be invoked to ensure it is closed and removed upon completion.
func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *waddrmgr.Manager) { func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *Manager) {
// Create a new manager in a temp directory. // Create a new manager in a temp directory.
dirName, err := ioutil.TempDir("", "mgrtest") dirName, err := ioutil.TempDir("", "mgrtest")
if err != nil { if err != nil {
@ -272,14 +273,14 @@ func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *waddr
if err != nil { if err != nil {
return err return err
} }
err = waddrmgr.Create( err = Create(
ns, seed, pubPassphrase, privPassphrase, ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{}, &chaincfg.MainNetParams, fastScrypt, time.Time{},
) )
if err != nil { if err != nil {
return err return err
} }
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if err != nil { if err != nil {

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC // Use of this source code is governed by an ISC
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package waddrmgr_test package waddrmgr
import ( import (
"bytes" "bytes"
@ -17,7 +17,6 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/snacl" "github.com/btcsuite/btcwallet/snacl"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
) )
@ -34,10 +33,10 @@ func newHash(hexStr string) *chainhash.Hash {
return hash return hash
} }
// failingSecretKeyGen is a waddrmgr.SecretKeyGenerator that always returns // failingSecretKeyGen is a SecretKeyGenerator that always returns
// snacl.ErrDecryptFailed. // snacl.ErrDecryptFailed.
func failingSecretKeyGen(passphrase *[]byte, func failingSecretKeyGen(passphrase *[]byte,
config *waddrmgr.ScryptOptions) (*snacl.SecretKey, error) { config *ScryptOptions) (*snacl.SecretKey, error) {
return nil, snacl.ErrDecryptFailed return nil, snacl.ErrDecryptFailed
} }
@ -52,8 +51,8 @@ func failingSecretKeyGen(passphrase *[]byte,
type testContext struct { type testContext struct {
t *testing.T t *testing.T
db walletdb.DB db walletdb.DB
rootManager *waddrmgr.Manager rootManager *Manager
manager *waddrmgr.ScopedKeyManager manager *ScopedKeyManager
account uint32 account uint32
create bool create bool
unlocked bool unlocked bool
@ -81,7 +80,7 @@ type expectedAddr struct {
privKey []byte privKey []byte
privKeyWIF string privKeyWIF string
script []byte script []byte
derivationInfo waddrmgr.DerivationPath derivationInfo DerivationPath
} }
// testNamePrefix is a helper to return a prefix to show for test errors based // testNamePrefix is a helper to return a prefix to show for test errors based
@ -103,7 +102,7 @@ func testNamePrefix(tc *testContext) string {
// will also be tested, otherwise, the functions which deal with private data // will also be tested, otherwise, the functions which deal with private data
// are checked to ensure they return the correct error. // are checked to ensure they return the correct error.
func testManagedPubKeyAddress(tc *testContext, prefix string, func testManagedPubKeyAddress(tc *testContext, prefix string,
gotAddr waddrmgr.ManagedPubKeyAddress, wantAddr *expectedAddr) bool { gotAddr ManagedPubKeyAddress, wantAddr *expectedAddr) bool {
// Ensure pubkey is the expected value for the managed address. // Ensure pubkey is the expected value for the managed address.
var gpubBytes []byte var gpubBytes []byte
@ -152,7 +151,7 @@ func testManagedPubKeyAddress(tc *testContext, prefix string,
case tc.watchingOnly: case tc.watchingOnly:
// Confirm expected watching-only error. // Confirm expected watching-only error.
testName := fmt.Sprintf("%s PrivKey", prefix) testName := fmt.Sprintf("%s PrivKey", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrWatchingOnly) { if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
return false return false
} }
case tc.unlocked: case tc.unlocked:
@ -170,7 +169,7 @@ func testManagedPubKeyAddress(tc *testContext, prefix string,
default: default:
// Confirm expected locked error. // Confirm expected locked error.
testName := fmt.Sprintf("%s PrivKey", prefix) testName := fmt.Sprintf("%s PrivKey", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrLocked) { if !checkManagerError(tc.t, testName, err, ErrLocked) {
return false return false
} }
} }
@ -184,7 +183,7 @@ func testManagedPubKeyAddress(tc *testContext, prefix string,
case tc.watchingOnly: case tc.watchingOnly:
// Confirm expected watching-only error. // Confirm expected watching-only error.
testName := fmt.Sprintf("%s ExportPrivKey", prefix) testName := fmt.Sprintf("%s ExportPrivKey", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrWatchingOnly) { if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
return false return false
} }
case tc.unlocked: case tc.unlocked:
@ -202,7 +201,7 @@ func testManagedPubKeyAddress(tc *testContext, prefix string,
default: default:
// Confirm expected locked error. // Confirm expected locked error.
testName := fmt.Sprintf("%s ExportPrivKey", prefix) testName := fmt.Sprintf("%s ExportPrivKey", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrLocked) { if !checkManagerError(tc.t, testName, err, ErrLocked) {
return false return false
} }
} }
@ -223,7 +222,9 @@ func testManagedPubKeyAddress(tc *testContext, prefix string,
// When the test context indicates the manager is unlocked, the private data // When the test context indicates the manager is unlocked, the private data
// will also be tested, otherwise, the functions which deal with private data // will also be tested, otherwise, the functions which deal with private data
// are checked to ensure they return the correct error. // are checked to ensure they return the correct error.
func testManagedScriptAddress(tc *testContext, prefix string, gotAddr waddrmgr.ManagedScriptAddress, wantAddr *expectedAddr) bool { func testManagedScriptAddress(tc *testContext, prefix string,
gotAddr ManagedScriptAddress, wantAddr *expectedAddr) bool {
// Ensure script is the expected value for the managed address. // Ensure script is the expected value for the managed address.
// Ensure script is the expected value for the managed address. Since // Ensure script is the expected value for the managed address. Since
// this is only available when the manager is unlocked, also check for // this is only available when the manager is unlocked, also check for
@ -233,7 +234,7 @@ func testManagedScriptAddress(tc *testContext, prefix string, gotAddr waddrmgr.M
case tc.watchingOnly: case tc.watchingOnly:
// Confirm expected watching-only error. // Confirm expected watching-only error.
testName := fmt.Sprintf("%s Script", prefix) testName := fmt.Sprintf("%s Script", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrWatchingOnly) { if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
return false return false
} }
case tc.unlocked: case tc.unlocked:
@ -250,7 +251,7 @@ func testManagedScriptAddress(tc *testContext, prefix string, gotAddr waddrmgr.M
default: default:
// Confirm expected locked error. // Confirm expected locked error.
testName := fmt.Sprintf("%s Script", prefix) testName := fmt.Sprintf("%s Script", prefix)
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrLocked) { if !checkManagerError(tc.t, testName, err, ErrLocked) {
return false return false
} }
} }
@ -266,7 +267,9 @@ func testManagedScriptAddress(tc *testContext, prefix string, gotAddr waddrmgr.M
// When the test context indicates the manager is unlocked, the private data // When the test context indicates the manager is unlocked, the private data
// will also be tested, otherwise, the functions which deal with private data // will also be tested, otherwise, the functions which deal with private data
// are checked to ensure they return the correct error. // are checked to ensure they return the correct error.
func testAddress(tc *testContext, prefix string, gotAddr waddrmgr.ManagedAddress, wantAddr *expectedAddr) bool { func testAddress(tc *testContext, prefix string, gotAddr ManagedAddress,
wantAddr *expectedAddr) bool {
if gotAddr.Account() != tc.account { if gotAddr.Account() != tc.account {
tc.t.Errorf("ManagedAddress.Account: unexpected account - got "+ tc.t.Errorf("ManagedAddress.Account: unexpected account - got "+
"%d, want %d", gotAddr.Account(), tc.account) "%d, want %d", gotAddr.Account(), tc.account)
@ -307,12 +310,12 @@ func testAddress(tc *testContext, prefix string, gotAddr waddrmgr.ManagedAddress
} }
switch addr := gotAddr.(type) { switch addr := gotAddr.(type) {
case waddrmgr.ManagedPubKeyAddress: case ManagedPubKeyAddress:
if !testManagedPubKeyAddress(tc, prefix, addr, wantAddr) { if !testManagedPubKeyAddress(tc, prefix, addr, wantAddr) {
return false return false
} }
case waddrmgr.ManagedScriptAddress: case ManagedScriptAddress:
if !testManagedScriptAddress(tc, prefix, addr, wantAddr) { if !testManagedScriptAddress(tc, prefix, addr, wantAddr) {
return false return false
} }
@ -327,10 +330,10 @@ func testAddress(tc *testContext, prefix string, gotAddr waddrmgr.ManagedAddress
// and unlocked. // and unlocked.
func testExternalAddresses(tc *testContext) bool { func testExternalAddresses(tc *testContext) bool {
prefix := testNamePrefix(tc) + " testExternalAddresses" prefix := testNamePrefix(tc) + " testExternalAddresses"
var addrs []waddrmgr.ManagedAddress var addrs []ManagedAddress
if tc.create { if tc.create {
prefix := prefix + " NextExternalAddresses" prefix := prefix + " NextExternalAddresses"
var addrs []waddrmgr.ManagedAddress var addrs []ManagedAddress
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
var err error var err error
@ -365,7 +368,7 @@ func testExternalAddresses(tc *testContext) bool {
// Ensure the last external address is the expected one. // Ensure the last external address is the expected one.
leaPrefix := prefix + " LastExternalAddress" leaPrefix := prefix + " LastExternalAddress"
var lastAddr waddrmgr.ManagedAddress var lastAddr ManagedAddress
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -395,7 +398,7 @@ func testExternalAddresses(tc *testContext) bool {
} }
prefix := fmt.Sprintf("%s Address #%d", prefix, i) prefix := fmt.Sprintf("%s Address #%d", prefix, i)
var addr waddrmgr.ManagedAddress var addr ManagedAddress
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -480,7 +483,7 @@ func testInternalAddresses(tc *testContext) bool {
} }
prefix := testNamePrefix(tc) + " testInternalAddresses" prefix := testNamePrefix(tc) + " testInternalAddresses"
var addrs []waddrmgr.ManagedAddress var addrs []ManagedAddress
if tc.create { if tc.create {
prefix := prefix + " NextInternalAddress" prefix := prefix + " NextInternalAddress"
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
@ -517,7 +520,7 @@ func testInternalAddresses(tc *testContext) bool {
// Ensure the last internal address is the expected one. // Ensure the last internal address is the expected one.
liaPrefix := prefix + " LastInternalAddress" liaPrefix := prefix + " LastInternalAddress"
var lastAddr waddrmgr.ManagedAddress var lastAddr ManagedAddress
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -547,7 +550,7 @@ func testInternalAddresses(tc *testContext) bool {
} }
prefix := fmt.Sprintf("%s Address #%d", prefix, i) prefix := fmt.Sprintf("%s Address #%d", prefix, i)
var addr waddrmgr.ManagedAddress var addr ManagedAddress
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -618,9 +621,9 @@ func testLocking(tc *testContext) bool {
// should be ErrLocked or ErrWatchingOnly depending on the type of the // should be ErrLocked or ErrWatchingOnly depending on the type of the
// address manager. // address manager.
err := tc.rootManager.Lock() err := tc.rootManager.Lock()
wantErrCode := waddrmgr.ErrLocked wantErrCode := ErrLocked
if tc.watchingOnly { if tc.watchingOnly {
wantErrCode = waddrmgr.ErrWatchingOnly wantErrCode = ErrWatchingOnly
} }
if !checkManagerError(tc.t, "Lock", err, wantErrCode) { if !checkManagerError(tc.t, "Lock", err, wantErrCode) {
return false return false
@ -635,7 +638,7 @@ func testLocking(tc *testContext) bool {
return tc.rootManager.Unlock(ns, privPassphrase) return tc.rootManager.Unlock(ns, privPassphrase)
}) })
if tc.watchingOnly { if tc.watchingOnly {
if !checkManagerError(tc.t, "Unlock", err, waddrmgr.ErrWatchingOnly) { if !checkManagerError(tc.t, "Unlock", err, ErrWatchingOnly) {
return false return false
} }
} else if err != nil { } else if err != nil {
@ -655,7 +658,7 @@ func testLocking(tc *testContext) bool {
return tc.rootManager.Unlock(ns, privPassphrase) return tc.rootManager.Unlock(ns, privPassphrase)
}) })
if tc.watchingOnly { if tc.watchingOnly {
if !checkManagerError(tc.t, "Unlock2", err, waddrmgr.ErrWatchingOnly) { if !checkManagerError(tc.t, "Unlock2", err, ErrWatchingOnly) {
return false return false
} }
} else if err != nil { } else if err != nil {
@ -673,9 +676,9 @@ func testLocking(tc *testContext) bool {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
return tc.rootManager.Unlock(ns, []byte("invalidpassphrase")) return tc.rootManager.Unlock(ns, []byte("invalidpassphrase"))
}) })
wantErrCode = waddrmgr.ErrWrongPassphrase wantErrCode = ErrWrongPassphrase
if tc.watchingOnly { if tc.watchingOnly {
wantErrCode = waddrmgr.ErrWatchingOnly wantErrCode = ErrWatchingOnly
} }
if !checkManagerError(tc.t, "Unlock", err, wantErrCode) { if !checkManagerError(tc.t, "Unlock", err, wantErrCode) {
return false return false
@ -700,7 +703,7 @@ func testImportPrivateKey(tc *testContext) bool {
tests := []struct { tests := []struct {
name string name string
in string in string
blockstamp waddrmgr.BlockStamp blockstamp BlockStamp
expected expectedAddr expected expectedAddr
}{ }{
{ {
@ -750,7 +753,7 @@ func testImportPrivateKey(tc *testContext) bool {
} }
// Only import the private keys when in the create phase of testing. // Only import the private keys when in the create phase of testing.
tc.account = waddrmgr.ImportedAddrAccount tc.account = ImportedAddrAccount
prefix := testNamePrefix(tc) + " testImportPrivateKey" prefix := testNamePrefix(tc) + " testImportPrivateKey"
if tc.create { if tc.create {
for i, test := range tests { for i, test := range tests {
@ -761,7 +764,7 @@ func testImportPrivateKey(tc *testContext) bool {
"error: %v", prefix, i, test.name, err) "error: %v", prefix, i, test.name, err)
continue continue
} }
var addr waddrmgr.ManagedPubKeyAddress var addr ManagedPubKeyAddress
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
var err error var err error
@ -802,7 +805,7 @@ func testImportPrivateKey(tc *testContext) bool {
} }
taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix, taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix,
i, test.name) i, test.name)
var ma waddrmgr.ManagedAddress var ma ManagedAddress
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -866,7 +869,7 @@ func testImportScript(tc *testContext) bool {
tests := []struct { tests := []struct {
name string name string
in []byte in []byte
blockstamp waddrmgr.BlockStamp blockstamp BlockStamp
expected expectedAddr expected expectedAddr
}{ }{
{ {
@ -923,7 +926,7 @@ func testImportScript(tc *testContext) bool {
} }
// Only import the scripts when in the create phase of testing. // Only import the scripts when in the create phase of testing.
tc.account = waddrmgr.ImportedAddrAccount tc.account = ImportedAddrAccount
prefix := testNamePrefix(tc) prefix := testNamePrefix(tc)
if tc.create { if tc.create {
for i, test := range tests { for i, test := range tests {
@ -931,7 +934,7 @@ func testImportScript(tc *testContext) bool {
prefix := fmt.Sprintf("%s ImportScript #%d (%s)", prefix, prefix := fmt.Sprintf("%s ImportScript #%d (%s)", prefix,
i, test.name) i, test.name)
var addr waddrmgr.ManagedScriptAddress var addr ManagedScriptAddress
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
var err error var err error
@ -970,7 +973,7 @@ func testImportScript(tc *testContext) bool {
} }
taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix, taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix,
i, test.name) i, test.name)
var ma waddrmgr.ManagedAddress var ma ManagedAddress
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
@ -1107,27 +1110,27 @@ func testChangePassphrase(tc *testContext) bool {
// that intentionally errors. // that intentionally errors.
testName := "ChangePassphrase (public) with invalid new secret key" testName := "ChangePassphrase (public) with invalid new secret key"
oldKeyGen := waddrmgr.SetSecretKeyGen(failingSecretKeyGen) oldKeyGen := SetSecretKeyGen(failingSecretKeyGen)
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return tc.rootManager.ChangePassphrase( return tc.rootManager.ChangePassphrase(
ns, pubPassphrase, pubPassphrase2, false, fastScrypt, ns, pubPassphrase, pubPassphrase2, false, fastScrypt,
) )
}) })
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrCrypto) { if !checkManagerError(tc.t, testName, err, ErrCrypto) {
return false return false
} }
// Attempt to change public passphrase with invalid old passphrase. // Attempt to change public passphrase with invalid old passphrase.
testName = "ChangePassphrase (public) with invalid old passphrase" testName = "ChangePassphrase (public) with invalid old passphrase"
waddrmgr.SetSecretKeyGen(oldKeyGen) SetSecretKeyGen(oldKeyGen)
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return tc.rootManager.ChangePassphrase( return tc.rootManager.ChangePassphrase(
ns, []byte("bogus"), pubPassphrase2, false, fastScrypt, ns, []byte("bogus"), pubPassphrase2, false, fastScrypt,
) )
}) })
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrWrongPassphrase) { if !checkManagerError(tc.t, testName, err, ErrWrongPassphrase) {
return false return false
} }
@ -1172,9 +1175,9 @@ func testChangePassphrase(tc *testContext) bool {
ns, []byte("bogus"), privPassphrase2, true, fastScrypt, ns, []byte("bogus"), privPassphrase2, true, fastScrypt,
) )
}) })
wantErrCode := waddrmgr.ErrWrongPassphrase wantErrCode := ErrWrongPassphrase
if tc.watchingOnly { if tc.watchingOnly {
wantErrCode = waddrmgr.ErrWatchingOnly wantErrCode = ErrWatchingOnly
} }
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
@ -1251,8 +1254,10 @@ func testNewAccount(tc *testContext) bool {
_, err := tc.manager.NewAccount(ns, "test") _, err := tc.manager.NewAccount(ns, "test")
return err return err
}) })
if !checkManagerError(tc.t, "Create account in watching-only mode", err, if !checkManagerError(
waddrmgr.ErrWatchingOnly) { tc.t, "Create account in watching-only mode", err,
ErrWatchingOnly,
) {
tc.manager.Close() tc.manager.Close()
return false return false
} }
@ -1264,8 +1269,9 @@ func testNewAccount(tc *testContext) bool {
_, err := tc.manager.NewAccount(ns, "test") _, err := tc.manager.NewAccount(ns, "test")
return err return err
}) })
if !checkManagerError(tc.t, "Create account when wallet is locked", err, if !checkManagerError(
waddrmgr.ErrLocked) { tc.t, "Create account when wallet is locked", err, ErrLocked,
) {
tc.manager.Close() tc.manager.Close()
return false return false
} }
@ -1313,7 +1319,7 @@ func testNewAccount(tc *testContext) bool {
_, err := tc.manager.NewAccount(ns, testName) _, err := tc.manager.NewAccount(ns, testName)
return err return err
}) })
wantErrCode := waddrmgr.ErrDuplicateAccount wantErrCode := ErrDuplicateAccount
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1324,7 +1330,7 @@ func testNewAccount(tc *testContext) bool {
_, err := tc.manager.NewAccount(ns, testName) _, err := tc.manager.NewAccount(ns, testName)
return err return err
}) })
wantErrCode = waddrmgr.ErrInvalidAccount wantErrCode = ErrInvalidAccount
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1334,7 +1340,7 @@ func testNewAccount(tc *testContext) bool {
_, err := tc.manager.NewAccount(ns, testName) _, err := tc.manager.NewAccount(ns, testName)
return err return err
}) })
wantErrCode = waddrmgr.ErrInvalidAccount wantErrCode = ErrInvalidAccount
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1346,8 +1352,8 @@ func testNewAccount(tc *testContext) bool {
func testLookupAccount(tc *testContext) bool { func testLookupAccount(tc *testContext) bool {
// Lookup accounts created earlier in testNewAccount // Lookup accounts created earlier in testNewAccount
expectedAccounts := map[string]uint32{ expectedAccounts := map[string]uint32{
waddrmgr.TstDefaultAccountName: waddrmgr.DefaultAccountNum, TstDefaultAccountName: DefaultAccountNum,
waddrmgr.ImportedAddrAccountName: waddrmgr.ImportedAddrAccount, ImportedAddrAccountName: ImportedAddrAccount,
} }
for acctName, expectedAccount := range expectedAccounts { for acctName, expectedAccount := range expectedAccounts {
var account uint32 var account uint32
@ -1375,7 +1381,7 @@ func testLookupAccount(tc *testContext) bool {
_, err := tc.manager.LookupAccount(ns, testName) _, err := tc.manager.LookupAccount(ns, testName)
return err return err
}) })
wantErrCode := waddrmgr.ErrAccountNotFound wantErrCode := ErrAccountNotFound
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1476,7 +1482,7 @@ func testRenameAccount(tc *testContext) bool {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return tc.manager.RenameAccount(ns, tc.account, testName) return tc.manager.RenameAccount(ns, tc.account, testName)
}) })
wantErrCode := waddrmgr.ErrDuplicateAccount wantErrCode := ErrDuplicateAccount
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1486,7 +1492,7 @@ func testRenameAccount(tc *testContext) bool {
_, err := tc.manager.LookupAccount(ns, acctName) _, err := tc.manager.LookupAccount(ns, acctName)
return err return err
}) })
wantErrCode = waddrmgr.ErrAccountNotFound wantErrCode = ErrAccountNotFound
if !checkManagerError(tc.t, testName, err, wantErrCode) { if !checkManagerError(tc.t, testName, err, wantErrCode) {
return false return false
} }
@ -1503,7 +1509,7 @@ func testForEachAccount(tc *testContext) bool {
expectedAccounts = append(expectedAccounts, 2) expectedAccounts = append(expectedAccounts, 2)
} }
// Imported account // Imported account
expectedAccounts = append(expectedAccounts, waddrmgr.ImportedAddrAccount) expectedAccounts = append(expectedAccounts, ImportedAddrAccount)
var accounts []uint32 var accounts []uint32
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
@ -1542,11 +1548,11 @@ func testForEachAccountAddress(tc *testContext) bool {
expectedAddrMap[expectedAddrs[i].address] = &expectedAddrs[i] expectedAddrMap[expectedAddrs[i].address] = &expectedAddrs[i]
} }
var addrs []waddrmgr.ManagedAddress var addrs []ManagedAddress
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error { err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
return tc.manager.ForEachAccountAddress(ns, tc.account, return tc.manager.ForEachAccountAddress(ns, tc.account,
func(maddr waddrmgr.ManagedAddress) error { func(maddr ManagedAddress) error {
addrs = append(addrs, maddr) addrs = append(addrs, maddr)
return nil return nil
}) })
@ -1629,11 +1635,11 @@ func testWatchingOnly(tc *testContext) bool {
defer db.Close() defer db.Close()
// Open the manager using the namespace and convert it to watching-only. // Open the manager using the namespace and convert it to watching-only.
var mgr *waddrmgr.Manager var mgr *Manager
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if err != nil { if err != nil {
@ -1652,7 +1658,7 @@ func testWatchingOnly(tc *testContext) bool {
// Run all of the manager API tests against the converted manager and // Run all of the manager API tests against the converted manager and
// close it. We'll also retrieve the default scope (BIP0044) from the // close it. We'll also retrieve the default scope (BIP0044) from the
// manager in order to use. // manager in order to use.
scopedMgr, err := mgr.FetchScopedKeyManager(waddrmgr.KeyScopeBIP0044) scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
if err != nil { if err != nil {
tc.t.Errorf("unable to fetch bip 44 scope %v", err) tc.t.Errorf("unable to fetch bip 44 scope %v", err)
return false return false
@ -1672,7 +1678,7 @@ func testWatchingOnly(tc *testContext) bool {
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if err != nil { if err != nil {
@ -1681,7 +1687,7 @@ func testWatchingOnly(tc *testContext) bool {
} }
defer mgr.Close() defer mgr.Close()
scopedMgr, err = mgr.FetchScopedKeyManager(waddrmgr.KeyScopeBIP0044) scopedMgr, err = mgr.FetchScopedKeyManager(KeyScopeBIP0044)
if err != nil { if err != nil {
tc.t.Errorf("unable to fetch bip 44 scope %v", err) tc.t.Errorf("unable to fetch bip 44 scope %v", err)
return false return false
@ -1712,7 +1718,7 @@ func testSync(tc *testContext) bool {
tc.t.Errorf("SetSyncedTo unexpected err on nil: %v", err) tc.t.Errorf("SetSyncedTo unexpected err on nil: %v", err)
return false return false
} }
blockStamp := waddrmgr.BlockStamp{ blockStamp := BlockStamp{
Height: 0, Height: 0,
Hash: *chaincfg.MainNetParams.GenesisHash, Hash: *chaincfg.MainNetParams.GenesisHash,
} }
@ -1730,7 +1736,7 @@ func testSync(tc *testContext) bool {
tc.t.Errorf("%v", err) tc.t.Errorf("%v", err)
return false return false
} }
blockStamp = waddrmgr.BlockStamp{ blockStamp = BlockStamp{
Height: 1, Height: 1,
Hash: *latestHash, Hash: *latestHash,
Timestamp: time.Unix(1234, 0), Timestamp: time.Unix(1234, 0),
@ -1766,28 +1772,28 @@ func TestManager(t *testing.T) {
// returned. // returned.
err := walletdb.View(db, func(tx walletdb.ReadTx) error { err := walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
_, err := waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) _, err := Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if !checkManagerError(t, "Open non-existant", err, waddrmgr.ErrNoExist) { if !checkManagerError(t, "Open non-existant", err, ErrNoExist) {
return return
} }
// Create a new manager. // Create a new manager.
var mgr *waddrmgr.Manager var mgr *Manager
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey) ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
if err != nil { if err != nil {
return err return err
} }
err = waddrmgr.Create( err = Create(
ns, seed, pubPassphrase, privPassphrase, ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{}, &chaincfg.MainNetParams, fastScrypt, time.Time{},
) )
if err != nil { if err != nil {
return err return err
} }
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if err != nil { if err != nil {
@ -1802,17 +1808,19 @@ func TestManager(t *testing.T) {
// returned. // returned.
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return waddrmgr.Create(ns, seed, pubPassphrase, privPassphrase, return Create(
&chaincfg.MainNetParams, fastScrypt, time.Time{}) ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{},
)
}) })
if !checkManagerError(t, "Create existing", err, waddrmgr.ErrAlreadyExists) { if !checkManagerError(t, "Create existing", err, ErrAlreadyExists) {
mgr.Close() mgr.Close()
return return
} }
// Run all of the manager API tests in create mode and close the // Run all of the manager API tests in create mode and close the
// manager after they've completed // manager after they've completed
scopedMgr, err := mgr.FetchScopedKeyManager(waddrmgr.KeyScopeBIP0044) scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
if err != nil { if err != nil {
t.Fatalf("unable to fetch default scope: %v", err) t.Fatalf("unable to fetch default scope: %v", err)
} }
@ -1829,23 +1837,23 @@ func TestManager(t *testing.T) {
// Ensure the expected error is returned if the latest manager version // Ensure the expected error is returned if the latest manager version
// constant is bumped without writing code to actually do the upgrade. // constant is bumped without writing code to actually do the upgrade.
*waddrmgr.TstLatestMgrVersion++ *TstLatestMgrVersion++
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
_, err := waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) _, err := Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if !checkManagerError(t, "Upgrade needed", err, waddrmgr.ErrUpgrade) { if !checkManagerError(t, "Upgrade needed", err, ErrUpgrade) {
return return
} }
*waddrmgr.TstLatestMgrVersion-- *TstLatestMgrVersion--
// Open the manager and run all the tests again in open mode which // Open the manager and run all the tests again in open mode which
// avoids reinserting new addresses like the create mode tests do. // avoids reinserting new addresses like the create mode tests do.
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err return err
}) })
if err != nil { if err != nil {
@ -1854,7 +1862,7 @@ func TestManager(t *testing.T) {
} }
defer mgr.Close() defer mgr.Close()
scopedMgr, err = mgr.FetchScopedKeyManager(waddrmgr.KeyScopeBIP0044) scopedMgr, err = mgr.FetchScopedKeyManager(KeyScopeBIP0044)
if err != nil { if err != nil {
t.Fatalf("unable to fetch default scope: %v", err) t.Fatalf("unable to fetch default scope: %v", err)
} }
@ -1895,7 +1903,7 @@ func TestEncryptDecryptErrors(t *testing.T) {
teardown, db, mgr := setupManager(t) teardown, db, mgr := setupManager(t)
defer teardown() defer teardown()
invalidKeyType := waddrmgr.CryptoKeyType(0xff) invalidKeyType := CryptoKeyType(0xff)
if _, err := mgr.Encrypt(invalidKeyType, []byte{}); err == nil { if _, err := mgr.Encrypt(invalidKeyType, []byte{}); err == nil {
t.Fatalf("Encrypt accepted an invalid key type!") t.Fatalf("Encrypt accepted an invalid key type!")
} }
@ -1911,13 +1919,13 @@ func TestEncryptDecryptErrors(t *testing.T) {
var err error var err error
// Now the mgr is locked and encrypting/decrypting with private // Now the mgr is locked and encrypting/decrypting with private
// keys should fail. // keys should fail.
_, err = mgr.Encrypt(waddrmgr.CKTPrivate, []byte{}) _, err = mgr.Encrypt(CKTPrivate, []byte{})
checkManagerError(t, "encryption with private key fails when manager is locked", checkManagerError(t, "encryption with private key fails when manager is locked",
err, waddrmgr.ErrLocked) err, ErrLocked)
_, err = mgr.Decrypt(waddrmgr.CKTPrivate, []byte{}) _, err = mgr.Decrypt(CKTPrivate, []byte{})
checkManagerError(t, "decryption with private key fails when manager is locked", checkManagerError(t, "decryption with private key fails when manager is locked",
err, waddrmgr.ErrLocked) err, ErrLocked)
// Unlock the manager for these tests // Unlock the manager for these tests
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
@ -1929,16 +1937,16 @@ func TestEncryptDecryptErrors(t *testing.T) {
} }
// Make sure to cover the ErrCrypto error path in Encrypt. // Make sure to cover the ErrCrypto error path in Encrypt.
waddrmgr.TstRunWithFailingCryptoKeyPriv(mgr, func() { TstRunWithFailingCryptoKeyPriv(mgr, func() {
_, err = mgr.Encrypt(waddrmgr.CKTPrivate, []byte{}) _, err = mgr.Encrypt(CKTPrivate, []byte{})
}) })
checkManagerError(t, "failed encryption", err, waddrmgr.ErrCrypto) checkManagerError(t, "failed encryption", err, ErrCrypto)
// Make sure to cover the ErrCrypto error path in Decrypt. // Make sure to cover the ErrCrypto error path in Decrypt.
waddrmgr.TstRunWithFailingCryptoKeyPriv(mgr, func() { TstRunWithFailingCryptoKeyPriv(mgr, func() {
_, err = mgr.Decrypt(waddrmgr.CKTPrivate, []byte{}) _, err = mgr.Decrypt(CKTPrivate, []byte{})
}) })
checkManagerError(t, "failed decryption", err, waddrmgr.ErrCrypto) checkManagerError(t, "failed decryption", err, ErrCrypto)
} }
// TestEncryptDecrypt ensures that encrypting and decrypting data with the // TestEncryptDecrypt ensures that encrypting and decrypting data with the
@ -1960,10 +1968,10 @@ func TestEncryptDecrypt(t *testing.T) {
t.Fatal("Attempted to unlock the manager, but failed:", err) t.Fatal("Attempted to unlock the manager, but failed:", err)
} }
keyTypes := []waddrmgr.CryptoKeyType{ keyTypes := []CryptoKeyType{
waddrmgr.CKTPublic, CKTPublic,
waddrmgr.CKTPrivate, CKTPrivate,
waddrmgr.CKTScript, CKTScript,
} }
for _, keyType := range keyTypes { for _, keyType := range keyTypes {
@ -1994,13 +2002,13 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// We'll start the test by creating a new root manager that will be // We'll start the test by creating a new root manager that will be
// used for the duration of the test. // used for the duration of the test.
var mgr *waddrmgr.Manager var mgr *Manager
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey) ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
if err != nil { if err != nil {
return err return err
} }
err = waddrmgr.Create( err = Create(
ns, seed, pubPassphrase, privPassphrase, ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{}, &chaincfg.MainNetParams, fastScrypt, time.Time{},
) )
@ -2008,9 +2016,7 @@ func TestScopedKeyManagerManagement(t *testing.T) {
return err return err
} }
mgr, err = waddrmgr.Open( mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
ns, pubPassphrase, &chaincfg.MainNetParams,
)
if err != nil { if err != nil {
return err return err
} }
@ -2023,7 +2029,7 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// All the default scopes should have been created and loaded into // All the default scopes should have been created and loaded into
// memory upon initial opening. // memory upon initial opening.
for _, scope := range waddrmgr.DefaultKeyScopes { for _, scope := range DefaultKeyScopes {
_, err := mgr.FetchScopedKeyManager(scope) _, err := mgr.FetchScopedKeyManager(scope)
if err != nil { if err != nil {
t.Fatalf("unable to fetch scope %v: %v", scope, err) t.Fatalf("unable to fetch scope %v: %v", scope, err)
@ -2036,14 +2042,14 @@ func TestScopedKeyManagerManagement(t *testing.T) {
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
for _, scope := range waddrmgr.DefaultKeyScopes { for _, scope := range DefaultKeyScopes {
sMgr, err := mgr.FetchScopedKeyManager(scope) sMgr, err := mgr.FetchScopedKeyManager(scope)
if err != nil { if err != nil {
t.Fatalf("unable to fetch scope %v: %v", scope, err) t.Fatalf("unable to fetch scope %v: %v", scope, err)
} }
externalAddr, err := sMgr.NextExternalAddresses( externalAddr, err := sMgr.NextExternalAddresses(
ns, waddrmgr.DefaultAccountNum, 1, ns, DefaultAccountNum, 1,
) )
if err != nil { if err != nil {
t.Fatalf("unable to derive external addr: %v", err) t.Fatalf("unable to derive external addr: %v", err)
@ -2051,14 +2057,14 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// The external address should match the prescribed // The external address should match the prescribed
// addr schema for this scoped key manager. // addr schema for this scoped key manager.
if externalAddr[0].AddrType() != waddrmgr.ScopeAddrMap[scope].ExternalAddrType { if externalAddr[0].AddrType() != ScopeAddrMap[scope].ExternalAddrType {
t.Fatalf("addr type mismatch: expected %v, got %v", t.Fatalf("addr type mismatch: expected %v, got %v",
externalAddr[0].AddrType(), externalAddr[0].AddrType(),
waddrmgr.ScopeAddrMap[scope].ExternalAddrType) ScopeAddrMap[scope].ExternalAddrType)
} }
internalAddr, err := sMgr.NextInternalAddresses( internalAddr, err := sMgr.NextInternalAddresses(
ns, waddrmgr.DefaultAccountNum, 1, ns, DefaultAccountNum, 1,
) )
if err != nil { if err != nil {
t.Fatalf("unable to derive internal addr: %v", err) t.Fatalf("unable to derive internal addr: %v", err)
@ -2066,10 +2072,10 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// Similarly, the internal address should match the // Similarly, the internal address should match the
// prescribed addr schema for this scoped key manager. // prescribed addr schema for this scoped key manager.
if internalAddr[0].AddrType() != waddrmgr.ScopeAddrMap[scope].InternalAddrType { if internalAddr[0].AddrType() != ScopeAddrMap[scope].InternalAddrType {
t.Fatalf("addr type mismatch: expected %v, got %v", t.Fatalf("addr type mismatch: expected %v, got %v",
internalAddr[0].AddrType(), internalAddr[0].AddrType(),
waddrmgr.ScopeAddrMap[scope].InternalAddrType) ScopeAddrMap[scope].InternalAddrType)
} }
} }
@ -2081,15 +2087,15 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// Now that the manager is open, we'll create a "test" scope that we'll // Now that the manager is open, we'll create a "test" scope that we'll
// be utilizing for the remainder of the test. // be utilizing for the remainder of the test.
testScope := waddrmgr.KeyScope{ testScope := KeyScope{
Purpose: 99, Purpose: 99,
Coin: 0, Coin: 0,
} }
addrSchema := waddrmgr.ScopeAddrSchema{ addrSchema := ScopeAddrSchema{
ExternalAddrType: waddrmgr.NestedWitnessPubKey, ExternalAddrType: NestedWitnessPubKey,
InternalAddrType: waddrmgr.WitnessPubKey, InternalAddrType: WitnessPubKey,
} }
var scopedMgr *waddrmgr.ScopedKeyManager var scopedMgr *ScopedKeyManager
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
@ -2110,21 +2116,21 @@ func TestScopedKeyManagerManagement(t *testing.T) {
t.Fatalf("attempt to read created mgr failed: %v", err) t.Fatalf("attempt to read created mgr failed: %v", err)
} }
var externalAddr, internalAddr []waddrmgr.ManagedAddress var externalAddr, internalAddr []ManagedAddress
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
// We'll now create a new external address to ensure we // We'll now create a new external address to ensure we
// retrieve the proper type. // retrieve the proper type.
externalAddr, err = scopedMgr.NextExternalAddresses( externalAddr, err = scopedMgr.NextExternalAddresses(
ns, waddrmgr.DefaultAccountNum, 1, ns, DefaultAccountNum, 1,
) )
if err != nil { if err != nil {
t.Fatalf("unable to derive external addr: %v", err) t.Fatalf("unable to derive external addr: %v", err)
} }
internalAddr, err = scopedMgr.NextInternalAddresses( internalAddr, err = scopedMgr.NextInternalAddresses(
ns, waddrmgr.DefaultAccountNum, 1, ns, DefaultAccountNum, 1,
) )
if err != nil { if err != nil {
t.Fatalf("unable to derive internal addr: %v", err) t.Fatalf("unable to derive internal addr: %v", err)
@ -2136,9 +2142,9 @@ func TestScopedKeyManagerManagement(t *testing.T) {
} }
// Ensure that the type of the address matches as expected. // Ensure that the type of the address matches as expected.
if externalAddr[0].AddrType() != waddrmgr.NestedWitnessPubKey { if externalAddr[0].AddrType() != NestedWitnessPubKey {
t.Fatalf("addr type mismatch: expected %v, got %v", t.Fatalf("addr type mismatch: expected %v, got %v",
waddrmgr.NestedWitnessPubKey, externalAddr[0].AddrType()) NestedWitnessPubKey, externalAddr[0].AddrType())
} }
_, ok := externalAddr[0].Address().(*btcutil.AddressScriptHash) _, ok := externalAddr[0].Address().(*btcutil.AddressScriptHash)
if !ok { if !ok {
@ -2147,9 +2153,9 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// We'll also create an internal address and ensure that the types // We'll also create an internal address and ensure that the types
// match up properly. // match up properly.
if internalAddr[0].AddrType() != waddrmgr.WitnessPubKey { if internalAddr[0].AddrType() != WitnessPubKey {
t.Fatalf("addr type mismatch: expected %v, got %v", t.Fatalf("addr type mismatch: expected %v, got %v",
waddrmgr.WitnessPubKey, internalAddr[0].AddrType()) WitnessPubKey, internalAddr[0].AddrType())
} }
_, ok = internalAddr[0].Address().(*btcutil.AddressWitnessPubKeyHash) _, ok = internalAddr[0].Address().(*btcutil.AddressWitnessPubKeyHash)
if !ok { if !ok {
@ -2162,7 +2168,7 @@ func TestScopedKeyManagerManagement(t *testing.T) {
err = walletdb.View(db, func(tx walletdb.ReadTx) error { err = walletdb.View(db, func(tx walletdb.ReadTx) error {
ns := tx.ReadBucket(waddrmgrNamespaceKey) ns := tx.ReadBucket(waddrmgrNamespaceKey)
var err error var err error
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams) mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
if err != nil { if err != nil {
return err return err
} }
@ -2183,12 +2189,12 @@ func TestScopedKeyManagerManagement(t *testing.T) {
// If we fetch the last generated external address, it should map // If we fetch the last generated external address, it should map
// exactly to the address that we just generated. // exactly to the address that we just generated.
var lastAddr waddrmgr.ManagedAddress var lastAddr ManagedAddress
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
lastAddr, err = scopedMgr.LastExternalAddress( lastAddr, err = scopedMgr.LastExternalAddress(
ns, waddrmgr.DefaultAccountNum, ns, DefaultAccountNum,
) )
if err != nil { if err != nil {
return err return err
@ -2205,7 +2211,7 @@ func TestScopedKeyManagerManagement(t *testing.T) {
} }
// After the restart, all the default scopes should be been re-loaded. // After the restart, all the default scopes should be been re-loaded.
for _, scope := range waddrmgr.DefaultKeyScopes { for _, scope := range DefaultKeyScopes {
_, err := mgr.FetchScopedKeyManager(scope) _, err := mgr.FetchScopedKeyManager(scope)
if err != nil { if err != nil {
t.Fatalf("unable to fetch scope %v: %v", scope, err) t.Fatalf("unable to fetch scope %v: %v", scope, err)
@ -2245,13 +2251,13 @@ func TestRootHDKeyNeutering(t *testing.T) {
// We'll start the test by creating a new root manager that will be // We'll start the test by creating a new root manager that will be
// used for the duration of the test. // used for the duration of the test.
var mgr *waddrmgr.Manager var mgr *Manager
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey) ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
if err != nil { if err != nil {
return err return err
} }
err = waddrmgr.Create( err = Create(
ns, seed, pubPassphrase, privPassphrase, ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{}, &chaincfg.MainNetParams, fastScrypt, time.Time{},
) )
@ -2259,9 +2265,7 @@ func TestRootHDKeyNeutering(t *testing.T) {
return err return err
} }
mgr, err = waddrmgr.Open( mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
ns, pubPassphrase, &chaincfg.MainNetParams,
)
if err != nil { if err != nil {
return err return err
} }
@ -2275,13 +2279,13 @@ func TestRootHDKeyNeutering(t *testing.T) {
// With the root manager open, we'll now create a new scoped manager // With the root manager open, we'll now create a new scoped manager
// for usage within this test. // for usage within this test.
testScope := waddrmgr.KeyScope{ testScope := KeyScope{
Purpose: 99, Purpose: 99,
Coin: 0, Coin: 0,
} }
addrSchema := waddrmgr.ScopeAddrSchema{ addrSchema := ScopeAddrSchema{
ExternalAddrType: waddrmgr.NestedWitnessPubKey, ExternalAddrType: NestedWitnessPubKey,
InternalAddrType: waddrmgr.WitnessPubKey, InternalAddrType: WitnessPubKey,
} }
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
@ -2309,7 +2313,7 @@ func TestRootHDKeyNeutering(t *testing.T) {
// If we try to create *another* scope, this should fail, as the root // If we try to create *another* scope, this should fail, as the root
// key is no longer in the database. // key is no longer in the database.
testScope = waddrmgr.KeyScope{ testScope = KeyScope{
Purpose: 100, Purpose: 100,
Coin: 0, Coin: 0,
} }
@ -2339,13 +2343,13 @@ func TestNewRawAccount(t *testing.T) {
// We'll start the test by creating a new root manager that will be // We'll start the test by creating a new root manager that will be
// used for the duration of the test. // used for the duration of the test.
var mgr *waddrmgr.Manager var mgr *Manager
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey) ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
if err != nil { if err != nil {
return err return err
} }
err = waddrmgr.Create( err = Create(
ns, seed, pubPassphrase, privPassphrase, ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{}, &chaincfg.MainNetParams, fastScrypt, time.Time{},
) )
@ -2353,9 +2357,7 @@ func TestNewRawAccount(t *testing.T) {
return err return err
} }
mgr, err = waddrmgr.Open( mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
ns, pubPassphrase, &chaincfg.MainNetParams,
)
if err != nil { if err != nil {
return err return err
} }
@ -2369,9 +2371,9 @@ func TestNewRawAccount(t *testing.T) {
// Now that we have the manager created, we'll fetch one of the default // Now that we have the manager created, we'll fetch one of the default
// scopes for usage within this test. // scopes for usage within this test.
scopedMgr, err := mgr.FetchScopedKeyManager(waddrmgr.KeyScopeBIP0084) scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0084)
if err != nil { if err != nil {
t.Fatalf("unable to fetch scope %v: %v", waddrmgr.KeyScopeBIP0084, err) t.Fatalf("unable to fetch scope %v: %v", KeyScopeBIP0084, err)
} }
// With the scoped manager retrieved, we'll attempt to create a new raw // With the scoped manager retrieved, we'll attempt to create a new raw
@ -2387,7 +2389,7 @@ func TestNewRawAccount(t *testing.T) {
// With the account created, we should be able to derive new addresses // With the account created, we should be able to derive new addresses
// from the account. // from the account.
var accountAddrNext waddrmgr.ManagedAddress var accountAddrNext ManagedAddress
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
@ -2407,11 +2409,11 @@ func TestNewRawAccount(t *testing.T) {
// Additionally, we should be able to manually derive specific target // Additionally, we should be able to manually derive specific target
// keys. // keys.
var accountTargetAddr waddrmgr.ManagedAddress var accountTargetAddr ManagedAddress
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
keyPath := waddrmgr.DerivationPath{ keyPath := DerivationPath{
Account: accountNum, Account: accountNum,
Branch: 0, Branch: 0,
Index: 0, Index: 0,