2016-08-08 21:49:09 +02:00
|
|
|
// Copyright (c) 2014-2016 The btcsuite developers
|
2015-12-01 19:44:58 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-08-08 22:43:50 +02:00
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
package waddrmgr
|
2014-08-08 22:43:50 +02:00
|
|
|
|
|
|
|
import (
|
2018-02-14 06:34:50 +01:00
|
|
|
"bytes"
|
2021-12-02 15:10:24 +01:00
|
|
|
"crypto/sha256"
|
2014-08-08 22:43:50 +02:00
|
|
|
"encoding/hex"
|
2018-11-06 08:50:51 +01:00
|
|
|
"errors"
|
2014-08-08 22:43:50 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2018-03-05 18:44:41 +01:00
|
|
|
"time"
|
2014-08-08 22:43:50 +02:00
|
|
|
|
2018-08-14 03:45:45 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2021-08-26 00:58:28 +02:00
|
|
|
"github.com/lbryio/lbcd/btcec"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
|
|
|
btcutil "github.com/lbryio/lbcutil"
|
|
|
|
"github.com/lbryio/lbcutil/hdkeychain"
|
|
|
|
"github.com/lbryio/lbcwallet/snacl"
|
|
|
|
"github.com/lbryio/lbcwallet/walletdb"
|
2021-08-14 01:13:06 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-08-08 22:43:50 +02:00
|
|
|
)
|
|
|
|
|
2018-11-06 08:50:51 +01:00
|
|
|
// failingCryptoKey is an implementation of the EncryptorDecryptor interface
|
|
|
|
// with intentionally fails when attempting to encrypt or decrypt with it.
|
|
|
|
type failingCryptoKey struct {
|
|
|
|
cryptoKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypt intenionally returns a failure when invoked to test error paths.
|
|
|
|
//
|
|
|
|
// This is part of the EncryptorDecryptor interface implementation.
|
|
|
|
func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
|
|
|
|
return nil, errors.New("failed to encrypt")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt intenionally returns a failure when invoked to test error paths.
|
|
|
|
//
|
|
|
|
// This is part of the EncryptorDecryptor interface implementation.
|
|
|
|
func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
|
|
|
|
return nil, errors.New("failed to decrypt")
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
// failingSecretKeyGen is a SecretKeyGenerator that always returns
|
2018-09-01 01:28:38 +02:00
|
|
|
// snacl.ErrDecryptFailed.
|
|
|
|
func failingSecretKeyGen(passphrase *[]byte,
|
2018-11-06 08:24:38 +01:00
|
|
|
config *ScryptOptions) (*snacl.SecretKey, error) {
|
2018-09-01 01:28:38 +02:00
|
|
|
return nil, snacl.ErrDecryptFailed
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// testContext is used to store context information about a running test which
|
|
|
|
// is passed into helper functions. The useSpends field indicates whether or
|
|
|
|
// not the spend data should be empty or figure it out based on the specific
|
|
|
|
// test blocks provided. This is needed because the first loop where the blocks
|
|
|
|
// are inserted, the tests are running against the latest block and therefore
|
|
|
|
// none of the outputs can be spent yet. However, on subsequent runs, all
|
|
|
|
// blocks have been inserted and therefore some of the transaction outputs are
|
|
|
|
// spent.
|
|
|
|
type testContext struct {
|
2021-02-17 02:01:12 +01:00
|
|
|
t *testing.T
|
|
|
|
caseName string
|
|
|
|
db walletdb.DB
|
|
|
|
rootManager *Manager
|
|
|
|
manager *ScopedKeyManager
|
|
|
|
internalAccount uint32
|
|
|
|
create bool
|
|
|
|
unlocked bool
|
|
|
|
watchingOnly bool
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
// addrType is the type of address being tested
|
|
|
|
type addrType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
addrPubKeyHash addrType = iota
|
|
|
|
addrScriptHash
|
|
|
|
)
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// expectedAddr is used to house the expected return values from a managed
|
|
|
|
// address. Not all fields for used for all managed address types.
|
|
|
|
type expectedAddr struct {
|
2021-12-02 15:10:24 +01:00
|
|
|
address string
|
|
|
|
addressHash []byte
|
|
|
|
internal bool
|
|
|
|
compressed bool
|
|
|
|
imported bool
|
|
|
|
pubKey []byte
|
|
|
|
privKey []byte
|
|
|
|
privKeyWIF string
|
|
|
|
script []byte
|
|
|
|
derivationInfo DerivationPath
|
|
|
|
scriptNotSecret bool
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testNamePrefix is a helper to return a prefix to show for test errors based
|
|
|
|
// on the state of the test context.
|
|
|
|
func testNamePrefix(tc *testContext) string {
|
|
|
|
prefix := "Open "
|
|
|
|
if tc.create {
|
|
|
|
prefix = "Create "
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:01:12 +01:00
|
|
|
return fmt.Sprintf("(%s) %s account #%d", tc.caseName, prefix, tc.internalAccount)
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testManagedPubKeyAddress ensures the data returned by all exported functions
|
|
|
|
// provided by the passed managed p ublic key address matches the corresponding
|
|
|
|
// fields in the provided expected address.
|
|
|
|
//
|
|
|
|
// When the test context indicates the manager is unlocked, the private data
|
|
|
|
// will also be tested, otherwise, the functions which deal with private data
|
|
|
|
// are checked to ensure they return the correct error.
|
2018-02-14 06:34:50 +01:00
|
|
|
func testManagedPubKeyAddress(tc *testContext, prefix string,
|
2018-11-06 08:24:38 +01:00
|
|
|
gotAddr ManagedPubKeyAddress, wantAddr *expectedAddr) bool {
|
2018-02-14 06:34:50 +01:00
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// Ensure pubkey is the expected value for the managed address.
|
|
|
|
var gpubBytes []byte
|
|
|
|
if gotAddr.Compressed() {
|
|
|
|
gpubBytes = gotAddr.PubKey().SerializeCompressed()
|
|
|
|
} else {
|
|
|
|
gpubBytes = gotAddr.PubKey().SerializeUncompressed()
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gpubBytes, wantAddr.pubKey) {
|
|
|
|
tc.t.Errorf("%s PubKey: unexpected public key - got %x, want "+
|
|
|
|
"%x", prefix, gpubBytes, wantAddr.pubKey)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure exported pubkey string is the expected value for the managed
|
|
|
|
// address.
|
|
|
|
gpubHex := gotAddr.ExportPubKey()
|
|
|
|
wantPubHex := hex.EncodeToString(wantAddr.pubKey)
|
|
|
|
if gpubHex != wantPubHex {
|
|
|
|
tc.t.Errorf("%s ExportPubKey: unexpected public key - got %s, "+
|
|
|
|
"want %s", prefix, gpubHex, wantPubHex)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:45:45 +02:00
|
|
|
// Ensure that the derivation path has been properly re-set after the
|
|
|
|
// address was read from disk.
|
|
|
|
_, gotAddrPath, ok := gotAddr.DerivationInfo()
|
|
|
|
if !ok && !gotAddr.Imported() {
|
|
|
|
tc.t.Errorf("%s PubKey: non-imported address has empty "+
|
|
|
|
"derivation info", prefix)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
expectedDerivationInfo := wantAddr.derivationInfo
|
|
|
|
if gotAddrPath != expectedDerivationInfo {
|
|
|
|
tc.t.Errorf("%s PubKey: wrong derivation info: expected %v, "+
|
|
|
|
"got %v", prefix, spew.Sdump(gotAddrPath),
|
|
|
|
spew.Sdump(expectedDerivationInfo))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// Ensure private key is the expected value for the managed address.
|
|
|
|
// Since this is only available when the manager is unlocked, also check
|
|
|
|
// for the expected error when the manager is locked.
|
|
|
|
gotPrivKey, err := gotAddr.PrivKey()
|
|
|
|
switch {
|
|
|
|
case tc.watchingOnly:
|
|
|
|
// Confirm expected watching-only error.
|
|
|
|
testName := fmt.Sprintf("%s PrivKey", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case tc.unlocked:
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s PrivKey: unexpected error - got %v",
|
|
|
|
prefix, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
gpriv := gotPrivKey.Serialize()
|
|
|
|
if !reflect.DeepEqual(gpriv, wantAddr.privKey) {
|
|
|
|
tc.t.Errorf("%s PrivKey: unexpected private key - "+
|
|
|
|
"got %x, want %x", prefix, gpriv, wantAddr.privKey)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Confirm expected locked error.
|
|
|
|
testName := fmt.Sprintf("%s PrivKey", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrLocked) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure exported private key in Wallet Import Format (WIF) is the
|
|
|
|
// expected value for the managed address. Since this is only available
|
|
|
|
// when the manager is unlocked, also check for the expected error when
|
|
|
|
// the manager is locked.
|
|
|
|
gotWIF, err := gotAddr.ExportPrivKey()
|
|
|
|
switch {
|
|
|
|
case tc.watchingOnly:
|
|
|
|
// Confirm expected watching-only error.
|
|
|
|
testName := fmt.Sprintf("%s ExportPrivKey", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
case tc.unlocked:
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s ExportPrivKey: unexpected error - "+
|
|
|
|
"got %v", prefix, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if gotWIF.String() != wantAddr.privKeyWIF {
|
|
|
|
tc.t.Errorf("%s ExportPrivKey: unexpected WIF - got "+
|
|
|
|
"%v, want %v", prefix, gotWIF.String(),
|
|
|
|
wantAddr.privKeyWIF)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Confirm expected locked error.
|
|
|
|
testName := fmt.Sprintf("%s ExportPrivKey", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrLocked) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 03:45:45 +02:00
|
|
|
// Imported addresses should return a nil derivation info.
|
|
|
|
if _, _, ok := gotAddr.DerivationInfo(); gotAddr.Imported() && ok {
|
|
|
|
tc.t.Errorf("%s Imported: expected nil derivation info", prefix)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testManagedScriptAddress ensures the data returned by all exported functions
|
|
|
|
// provided by the passed managed script address matches the corresponding
|
|
|
|
// fields in the provided expected address.
|
|
|
|
//
|
|
|
|
// When the test context indicates the manager is unlocked, the private data
|
|
|
|
// will also be tested, otherwise, the functions which deal with private data
|
|
|
|
// are checked to ensure they return the correct error.
|
2018-11-06 08:24:38 +01:00
|
|
|
func testManagedScriptAddress(tc *testContext, prefix string,
|
|
|
|
gotAddr ManagedScriptAddress, wantAddr *expectedAddr) bool {
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// Ensure script is the expected value for the managed address.
|
|
|
|
// Ensure script is the expected value for the managed address. Since
|
|
|
|
// this is only available when the manager is unlocked, also check for
|
|
|
|
// the expected error when the manager is locked.
|
|
|
|
gotScript, err := gotAddr.Script()
|
|
|
|
switch {
|
2021-12-02 15:10:24 +01:00
|
|
|
case tc.watchingOnly && !wantAddr.scriptNotSecret:
|
2014-08-08 22:43:50 +02:00
|
|
|
// Confirm expected watching-only error.
|
|
|
|
testName := fmt.Sprintf("%s Script", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrWatchingOnly) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-12-02 15:10:24 +01:00
|
|
|
|
|
|
|
// Either the manger is unlocked or the script is not considered to
|
|
|
|
// be secret and is encrypted with the public key.
|
|
|
|
case tc.unlocked || wantAddr.scriptNotSecret:
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s Script: unexpected error - got %v",
|
|
|
|
prefix, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(gotScript, wantAddr.script) {
|
|
|
|
tc.t.Errorf("%s Script: unexpected script - got %x, "+
|
|
|
|
"want %x", prefix, gotScript, wantAddr.script)
|
|
|
|
return false
|
|
|
|
}
|
2021-12-02 15:10:24 +01:00
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
default:
|
|
|
|
// Confirm expected locked error.
|
|
|
|
testName := fmt.Sprintf("%s Script", prefix)
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrLocked) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testAddress ensures the data returned by all exported functions provided by
|
|
|
|
// the passed managed address matches the corresponding fields in the provided
|
|
|
|
// expected address. It also type asserts the managed address to determine its
|
|
|
|
// specific type and calls the corresponding testing functions accordingly.
|
|
|
|
//
|
|
|
|
// When the test context indicates the manager is unlocked, the private data
|
|
|
|
// will also be tested, otherwise, the functions which deal with private data
|
|
|
|
// are checked to ensure they return the correct error.
|
2018-11-06 08:24:38 +01:00
|
|
|
func testAddress(tc *testContext, prefix string, gotAddr ManagedAddress,
|
|
|
|
wantAddr *expectedAddr) bool {
|
|
|
|
|
2021-02-17 02:01:12 +01:00
|
|
|
if gotAddr.InternalAccount() != tc.internalAccount {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("ManagedAddress.Account: unexpected account - got "+
|
2021-02-17 02:01:12 +01:00
|
|
|
"%d, want %d", gotAddr.InternalAccount(), tc.internalAccount)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotAddr.Address().EncodeAddress() != wantAddr.address {
|
|
|
|
tc.t.Errorf("%s EncodeAddress: unexpected address - got %s, "+
|
|
|
|
"want %s", prefix, gotAddr.Address().EncodeAddress(),
|
|
|
|
wantAddr.address)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(gotAddr.AddrHash(), wantAddr.addressHash) {
|
|
|
|
tc.t.Errorf("%s AddrHash: unexpected address hash - got %x, "+
|
|
|
|
"want %x", prefix, gotAddr.AddrHash(),
|
|
|
|
wantAddr.addressHash)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotAddr.Internal() != wantAddr.internal {
|
|
|
|
tc.t.Errorf("%s Internal: unexpected internal flag - got %v, "+
|
|
|
|
"want %v", prefix, gotAddr.Internal(), wantAddr.internal)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotAddr.Compressed() != wantAddr.compressed {
|
|
|
|
tc.t.Errorf("%s Compressed: unexpected compressed flag - got "+
|
|
|
|
"%v, want %v", prefix, gotAddr.Compressed(),
|
|
|
|
wantAddr.compressed)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotAddr.Imported() != wantAddr.imported {
|
|
|
|
tc.t.Errorf("%s Imported: unexpected imported flag - got %v, "+
|
|
|
|
"want %v", prefix, gotAddr.Imported(), wantAddr.imported)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch addr := gotAddr.(type) {
|
2018-11-06 08:24:38 +01:00
|
|
|
case ManagedPubKeyAddress:
|
2014-08-08 22:43:50 +02:00
|
|
|
if !testManagedPubKeyAddress(tc, prefix, addr, wantAddr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
case ManagedScriptAddress:
|
2014-08-08 22:43:50 +02:00
|
|
|
if !testManagedScriptAddress(tc, prefix, addr, wantAddr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testExternalAddresses tests several facets of external addresses such as
|
|
|
|
// generating multiple addresses via NextExternalAddresses, ensuring they can be
|
|
|
|
// retrieved by Address, and that they work properly when the manager is locked
|
|
|
|
// and unlocked.
|
|
|
|
func testExternalAddresses(tc *testContext) bool {
|
|
|
|
prefix := testNamePrefix(tc) + " testExternalAddresses"
|
2018-11-06 08:24:38 +01:00
|
|
|
var addrs []ManagedAddress
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.create {
|
|
|
|
prefix := prefix + " NextExternalAddresses"
|
2018-11-06 08:24:38 +01:00
|
|
|
var addrs []ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
addrs, err = tc.manager.NextExternalAddresses(
|
|
|
|
ns, tc.internalAccount, 5,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
if len(addrs) != len(expectedExternalAddrs) {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("%s: unexpected number of addresses - got "+
|
|
|
|
"%d, want %d", prefix, len(addrs),
|
2015-04-11 18:08:48 +02:00
|
|
|
len(expectedExternalAddrs))
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a closure to test the results since the same tests need to be
|
|
|
|
// repeated with the manager locked and unlocked.
|
|
|
|
testResults := func() bool {
|
|
|
|
// Ensure the returned addresses are the expected ones. When
|
|
|
|
// not in the create phase, there will be no addresses in the
|
|
|
|
// addrs slice, so this really only runs during the first phase
|
|
|
|
// of the tests.
|
|
|
|
for i := 0; i < len(addrs); i++ {
|
|
|
|
prefix := fmt.Sprintf("%s ExternalAddress #%d", prefix, i)
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, prefix, addrs[i], &expectedExternalAddrs[i]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the last external address is the expected one.
|
|
|
|
leaPrefix := prefix + " LastExternalAddress"
|
2018-11-06 08:24:38 +01:00
|
|
|
var lastAddr ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
lastAddr, err = tc.manager.LastExternalAddress(
|
|
|
|
ns, tc.internalAccount,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", leaPrefix, err)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, leaPrefix, lastAddr, &expectedExternalAddrs[len(expectedExternalAddrs)-1]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, use the Address API to retrieve each of the expected new
|
|
|
|
// addresses and ensure they're accurate.
|
2015-02-06 06:51:37 +01:00
|
|
|
chainParams := tc.manager.ChainParams()
|
2015-04-11 18:08:48 +02:00
|
|
|
for i := 0; i < len(expectedExternalAddrs); i++ {
|
|
|
|
pkHash := expectedExternalAddrs[i].addressHash
|
2018-02-14 06:34:50 +01:00
|
|
|
utilAddr, err := btcutil.NewAddressPubKeyHash(
|
|
|
|
pkHash, chainParams,
|
|
|
|
)
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s NewAddressPubKeyHash #%d: "+
|
|
|
|
"unexpected error: %v", prefix, i, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fmt.Sprintf("%s Address #%d", prefix, i)
|
2018-11-06 08:24:38 +01:00
|
|
|
var addr ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
addr, err = tc.manager.Address(ns, utilAddr)
|
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix,
|
|
|
|
err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, prefix, addr, &expectedExternalAddrs[i]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since the manager is locked at this point, the public address
|
|
|
|
// information is tested and the private functions are checked to ensure
|
|
|
|
// they return the expected error.
|
|
|
|
if !testResults() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after this point involves retesting with an unlocked
|
|
|
|
// address manager which is not possible for watching-only mode, so
|
|
|
|
// just exit now in that case.
|
|
|
|
if tc.watchingOnly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the manager and retest all of the addresses to ensure the
|
|
|
|
// private information is valid as well.
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
if !testResults() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relock the manager for future tests.
|
2018-02-14 06:34:50 +01:00
|
|
|
if err := tc.rootManager.Lock(); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Lock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = false
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testInternalAddresses tests several facets of internal addresses such as
|
|
|
|
// generating multiple addresses via NextInternalAddresses, ensuring they can be
|
|
|
|
// retrieved by Address, and that they work properly when the manager is locked
|
|
|
|
// and unlocked.
|
|
|
|
func testInternalAddresses(tc *testContext) bool {
|
|
|
|
// When the address manager is not in watching-only mode, unlocked it
|
|
|
|
// first to ensure that address generation works correctly when the
|
|
|
|
// address manager is unlocked and then locked later. These tests
|
|
|
|
// reverse the order done in the external tests which starts with a
|
|
|
|
// locked manager and unlock it afterwards.
|
|
|
|
if !tc.watchingOnly {
|
|
|
|
// Unlock the manager and retest all of the addresses to ensure the
|
|
|
|
// private information is valid as well.
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := testNamePrefix(tc) + " testInternalAddresses"
|
2018-11-06 08:24:38 +01:00
|
|
|
var addrs []ManagedAddress
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.create {
|
|
|
|
prefix := prefix + " NextInternalAddress"
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
addrs, err = tc.manager.NextInternalAddresses(
|
|
|
|
ns, tc.internalAccount, 5,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
if len(addrs) != len(expectedInternalAddrs) {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("%s: unexpected number of addresses - got "+
|
|
|
|
"%d, want %d", prefix, len(addrs),
|
2015-04-11 18:08:48 +02:00
|
|
|
len(expectedInternalAddrs))
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a closure to test the results since the same tests need to be
|
|
|
|
// repeated with the manager locked and unlocked.
|
|
|
|
testResults := func() bool {
|
|
|
|
// Ensure the returned addresses are the expected ones. When
|
|
|
|
// not in the create phase, there will be no addresses in the
|
|
|
|
// addrs slice, so this really only runs during the first phase
|
|
|
|
// of the tests.
|
|
|
|
for i := 0; i < len(addrs); i++ {
|
|
|
|
prefix := fmt.Sprintf("%s InternalAddress #%d", prefix, i)
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, prefix, addrs[i], &expectedInternalAddrs[i]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the last internal address is the expected one.
|
|
|
|
liaPrefix := prefix + " LastInternalAddress"
|
2018-11-06 08:24:38 +01:00
|
|
|
var lastAddr ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
lastAddr, err = tc.manager.LastInternalAddress(
|
|
|
|
ns, tc.internalAccount,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", liaPrefix, err)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, liaPrefix, lastAddr, &expectedInternalAddrs[len(expectedInternalAddrs)-1]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, use the Address API to retrieve each of the expected new
|
|
|
|
// addresses and ensure they're accurate.
|
2015-02-06 06:51:37 +01:00
|
|
|
chainParams := tc.manager.ChainParams()
|
2015-04-11 18:08:48 +02:00
|
|
|
for i := 0; i < len(expectedInternalAddrs); i++ {
|
|
|
|
pkHash := expectedInternalAddrs[i].addressHash
|
2018-02-14 06:34:50 +01:00
|
|
|
utilAddr, err := btcutil.NewAddressPubKeyHash(
|
|
|
|
pkHash, chainParams,
|
|
|
|
)
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s NewAddressPubKeyHash #%d: "+
|
|
|
|
"unexpected error: %v", prefix, i, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fmt.Sprintf("%s Address #%d", prefix, i)
|
2018-11-06 08:24:38 +01:00
|
|
|
var addr ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
addr, err = tc.manager.Address(ns, utilAddr)
|
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix,
|
|
|
|
err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
if !testAddress(tc, prefix, addr, &expectedInternalAddrs[i]) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// The address manager could either be locked or unlocked here depending
|
|
|
|
// on whether or not it's a watching-only manager. When it's unlocked,
|
|
|
|
// this will test both the public and private address data are accurate.
|
|
|
|
// When it's locked, it must be watching-only, so only the public
|
|
|
|
// address information is tested and the private functions are checked
|
|
|
|
// to ensure they return the expected ErrWatchingOnly error.
|
|
|
|
if !testResults() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after this point involves locking the address manager and
|
|
|
|
// retesting the addresses with a locked manager. However, for
|
|
|
|
// watching-only mode, this has already happened, so just exit now in
|
|
|
|
// that case.
|
|
|
|
if tc.watchingOnly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the manager and retest all of the addresses to ensure the
|
|
|
|
// public information remains valid and the private functions return
|
|
|
|
// the expected error.
|
2018-02-14 06:34:50 +01:00
|
|
|
if err := tc.rootManager.Lock(); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Lock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = false
|
2021-03-24 14:43:24 +01:00
|
|
|
return testResults()
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testLocking tests the basic locking semantics of the address manager work
|
|
|
|
// as expected. Other tests ensure addresses behave as expected under locked
|
|
|
|
// and unlocked conditions.
|
|
|
|
func testLocking(tc *testContext) bool {
|
|
|
|
if tc.unlocked {
|
|
|
|
tc.t.Error("testLocking called with an unlocked manager")
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
if !tc.rootManager.IsLocked() {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Error("IsLocked: returned false on locked manager")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locking an already lock manager should return an error. The error
|
|
|
|
// should be ErrLocked or ErrWatchingOnly depending on the type of the
|
|
|
|
// address manager.
|
2018-02-14 06:34:50 +01:00
|
|
|
err := tc.rootManager.Lock()
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode := ErrLocked
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.watchingOnly {
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrWatchingOnly
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
if !checkManagerError(tc.t, "Lock", err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure unlocking with the correct passphrase doesn't return any
|
|
|
|
// unexpected errors and the manager properly reports it is unlocked.
|
|
|
|
// Since watching-only address managers can't be unlocked, also ensure
|
|
|
|
// the correct error for that case.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.watchingOnly {
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, "Unlock", err, ErrWatchingOnly) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
if !tc.watchingOnly && tc.rootManager.IsLocked() {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Error("IsLocked: returned true on unlocked manager")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlocking the manager again is allowed. Since watching-only address
|
|
|
|
// managers can't be unlocked, also ensure the correct error for that
|
|
|
|
// case.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.watchingOnly {
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, "Unlock2", err, ErrWatchingOnly) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
if !tc.watchingOnly && tc.rootManager.IsLocked() {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Error("IsLocked: returned true on unlocked manager")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlocking the manager with an invalid passphrase must result in an
|
|
|
|
// error and a locked manager.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, []byte("invalidpassphrase"))
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrWrongPassphrase
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.watchingOnly {
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrWatchingOnly
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
if !checkManagerError(tc.t, "Unlock", err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
if !tc.rootManager.IsLocked() {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Error("IsLocked: manager is unlocked after failed unlock " +
|
|
|
|
"attempt")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testImportPrivateKey tests that importing private keys works properly. It
|
|
|
|
// ensures they can be retrieved by Address after they have been imported and
|
|
|
|
// the addresses give the expected values when the manager is locked and
|
|
|
|
// unlocked.
|
|
|
|
//
|
|
|
|
// This function expects the manager is already locked when called and returns
|
|
|
|
// with the manager locked.
|
|
|
|
func testImportPrivateKey(tc *testContext) bool {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
in string
|
2018-11-06 08:24:38 +01:00
|
|
|
blockstamp BlockStamp
|
2014-08-08 22:43:50 +02:00
|
|
|
expected expectedAddr
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wif for uncompressed pubkey address",
|
|
|
|
in: "5HueCGU8rMjxEXxiPuD5BDku4MkFqeZyd4dZ1jvhTVqvbTLvyTJ",
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "1GAehh7TsJAHuUAeKZcXf5CnwuGuGgyX2S",
|
|
|
|
addressHash: hexToBytes("a65d1a239d4ec666643d350c7bb8fc44d2881128"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: false,
|
|
|
|
pubKey: hexToBytes("04d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3" +
|
|
|
|
"d66a2c5f10780d95b7df42645cd85228a6fb29940e858e7e558" +
|
|
|
|
"42ae2bd115d1ed7cc0e82d934e929c97648cb0a"),
|
|
|
|
privKey: hexToBytes("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d"),
|
|
|
|
// privKeyWIF is set to the in field during tests
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wif for compressed pubkey address",
|
|
|
|
in: "KwdMAjGmerYanjeui5SHS7JkmpZvVipYvB2LJGU1ZxJwYvP98617",
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "1LoVGDgRs9hTfTNJNuXKSpywcbdvwRXpmK",
|
|
|
|
addressHash: hexToBytes("d9351dcbad5b8f3b8bfa2f2cdc85c28118ca9326"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: true,
|
|
|
|
pubKey: hexToBytes("02d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645c"),
|
|
|
|
privKey: hexToBytes("0c28fca386c7a227600b2fe50b7cae11ec86d3bf1fbe471be89827e19d72aa1d"),
|
|
|
|
// privKeyWIF is set to the in field during tests
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// The manager must be unlocked to import a private key, however a
|
|
|
|
// watching-only manager can't be unlocked.
|
|
|
|
if !tc.watchingOnly {
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only import the private keys when in the create phase of testing.
|
2021-02-17 02:01:12 +01:00
|
|
|
tc.internalAccount = ImportedAddrAccount
|
2014-08-08 22:43:50 +02:00
|
|
|
prefix := testNamePrefix(tc) + " testImportPrivateKey"
|
|
|
|
if tc.create {
|
|
|
|
for i, test := range tests {
|
2021-03-24 14:43:24 +01:00
|
|
|
test := test
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
test.expected.privKeyWIF = test.in
|
|
|
|
wif, err := btcutil.DecodeWIF(test.in)
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s DecodeWIF #%d (%s): unexpected "+
|
|
|
|
"error: %v", prefix, i, test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
var addr ManagedPubKeyAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-03-24 14:43:24 +01:00
|
|
|
addr, err = tc.manager.ImportPrivateKey(
|
|
|
|
ns, wif, &test.blockstamp,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s ImportPrivateKey #%d (%s): "+
|
|
|
|
"unexpected error: %v", prefix, i,
|
|
|
|
test.name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !testAddress(tc, prefix+" ImportPrivateKey", addr,
|
|
|
|
&test.expected) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a closure to test the results since the same tests need to be
|
|
|
|
// repeated with the manager unlocked and locked.
|
2015-02-06 06:51:37 +01:00
|
|
|
chainParams := tc.manager.ChainParams()
|
2014-08-08 22:43:50 +02:00
|
|
|
testResults := func() bool {
|
|
|
|
failed := false
|
|
|
|
for i, test := range tests {
|
|
|
|
test.expected.privKeyWIF = test.in
|
|
|
|
|
|
|
|
// Use the Address API to retrieve each of the expected
|
|
|
|
// new addresses and ensure they're accurate.
|
|
|
|
utilAddr, err := btcutil.NewAddressPubKeyHash(
|
2015-02-06 06:51:37 +01:00
|
|
|
test.expected.addressHash, chainParams)
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s NewAddressPubKeyHash #%d (%s): "+
|
|
|
|
"unexpected error: %v", prefix, i,
|
|
|
|
test.name, err)
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix,
|
|
|
|
i, test.name)
|
2018-11-06 08:24:38 +01:00
|
|
|
var ma ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
ma, err = tc.manager.Address(ns, utilAddr)
|
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", taPrefix,
|
|
|
|
err)
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !testAddress(tc, taPrefix, ma, &test.expected) {
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !failed
|
|
|
|
}
|
|
|
|
|
|
|
|
// The address manager could either be locked or unlocked here depending
|
|
|
|
// on whether or not it's a watching-only manager. When it's unlocked,
|
|
|
|
// this will test both the public and private address data are accurate.
|
|
|
|
// When it's locked, it must be watching-only, so only the public
|
|
|
|
// address information is tested and the private functions are checked
|
|
|
|
// to ensure they return the expected ErrWatchingOnly error.
|
|
|
|
if !testResults() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after this point involves locking the address manager and
|
|
|
|
// retesting the addresses with a locked manager. However, for
|
|
|
|
// watching-only mode, this has already happened, so just exit now in
|
|
|
|
// that case.
|
|
|
|
if tc.watchingOnly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the manager and retest all of the addresses to ensure the
|
|
|
|
// private information returns the expected error.
|
2018-02-14 06:34:50 +01:00
|
|
|
if err := tc.rootManager.Lock(); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Lock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = false
|
2021-03-24 14:43:24 +01:00
|
|
|
return testResults()
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// testImportScript tests that importing scripts works properly. It ensures
|
|
|
|
// they can be retrieved by Address after they have been imported and the
|
|
|
|
// addresses give the expected values when the manager is locked and unlocked.
|
|
|
|
//
|
|
|
|
// This function expects the manager is already locked when called and returns
|
|
|
|
// with the manager locked.
|
|
|
|
func testImportScript(tc *testContext) bool {
|
|
|
|
tests := []struct {
|
2021-12-02 15:10:24 +01:00
|
|
|
name string
|
|
|
|
in []byte
|
|
|
|
isWitness bool
|
|
|
|
witnessVersion byte
|
|
|
|
isSecretScript bool
|
|
|
|
blockstamp BlockStamp
|
|
|
|
expected expectedAddr
|
2014-08-08 22:43:50 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "p2sh uncompressed pubkey",
|
|
|
|
in: hexToBytes("41048b65a0e6bb200e6dac05e74281b1ab9a41e8" +
|
|
|
|
"0006d6b12d8521e09981da97dd96ac72d24d1a7d" +
|
|
|
|
"ed9493a9fc20fdb4a714808f0b680f1f1d935277" +
|
|
|
|
"48b5e3f629ffac"),
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "3MbyWAu9UaoBewR3cArF1nwf4aQgVwzrA5",
|
|
|
|
addressHash: hexToBytes("da6e6a632d96dc5530d7b3c9f3017725d023093e"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: false,
|
|
|
|
// script is set to the in field during tests.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2sh multisig",
|
|
|
|
in: hexToBytes("524104cb9c3c222c5f7a7d3b9bd152f363a0b6d5" +
|
|
|
|
"4c9eb312c4d4f9af1e8551b6c421a6a4ab0e2910" +
|
|
|
|
"5f24de20ff463c1c91fcf3bf662cdde4783d4799" +
|
|
|
|
"f787cb7c08869b4104ccc588420deeebea22a7e9" +
|
|
|
|
"00cc8b68620d2212c374604e3487ca08f1ff3ae1" +
|
|
|
|
"2bdc639514d0ec8612a2d3c519f084d9a00cbbe3" +
|
|
|
|
"b53d071e9b09e71e610b036aa24104ab47ad1939" +
|
|
|
|
"edcb3db65f7fedea62bbf781c5410d3f22a7a3a5" +
|
|
|
|
"6ffefb2238af8627363bdf2ed97c1f89784a1aec" +
|
|
|
|
"db43384f11d2acc64443c7fc299cef0400421a53ae"),
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "34CRZpt8j81rgh9QhzuBepqPi4cBQSjhjr",
|
|
|
|
addressHash: hexToBytes("1b800cec1fe92222f36a502c139bed47c5959715"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: false,
|
|
|
|
// script is set to the in field during tests.
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 15:10:24 +01:00
|
|
|
{
|
|
|
|
name: "p2wsh multisig",
|
|
|
|
isWitness: true,
|
|
|
|
witnessVersion: 0,
|
|
|
|
isSecretScript: true,
|
|
|
|
in: hexToBytes("52210305a662958b547fe25a71cd28fc7ef1c2" +
|
|
|
|
"ad4a79b12f34fc40137824b88e61199d21038552c09d9" +
|
|
|
|
"a709c8cbba6e472307d3f8383f46181895a76e01e258f" +
|
|
|
|
"09033b4a7821029dd72aba87324af59508380f9564d34" +
|
|
|
|
"b0f7b20d864d186e7d0428c9ea241c61653ae"),
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "bc1q0jljr70qchwtk3ag0w3gyg9mjhg4c95xr7h8ezhvdrfgppcpz4esfdl9an",
|
|
|
|
addressHash: hexToBytes("7cbf21f9e0c5dcbb47a87ba28220bb95d15c16861fae7c8aec68d28087011573"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: true,
|
|
|
|
// script is set to the in field during tests.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2wsh multisig as watch-only address",
|
|
|
|
isWitness: true,
|
|
|
|
witnessVersion: 0,
|
|
|
|
isSecretScript: false,
|
|
|
|
in: hexToBytes("52210305a662958b547fe25a71cd28fc7ef1c2" +
|
|
|
|
"ad4a79b12f34fc40137824b88e61199d21038552c09d9" +
|
|
|
|
"a709c8cbba6e472307d3f8383f46181895a76e01e258f" +
|
|
|
|
"09033b4a7821024794b65a83e9ba415096e59abc4d4d1" +
|
|
|
|
"1710968e52bf5eec56fe0e5bdb3d3ec0e53ae"),
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "bc1q3a79gkjulrsgp864yskp4d5zmwm49xsdrfwvdypkqtlpj7spd3fqrl5nes",
|
|
|
|
addressHash: hexToBytes("8f7c545a5cf8e0809f55242c1ab682dbb7529a0d1a5cc6903602fe197a016c52"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: true,
|
|
|
|
scriptNotSecret: true,
|
|
|
|
// script is set to the in field during tests.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "p2tr multisig",
|
|
|
|
isWitness: true,
|
|
|
|
witnessVersion: 1,
|
|
|
|
isSecretScript: true,
|
|
|
|
in: hexToBytes("52210305a662958b547fe25a71cd28fc7ef1c2" +
|
|
|
|
"ad4a79b12f34fc40137824b88e61199d21038552c09d9" +
|
|
|
|
"a709c8cbba6e472307d3f8383f46181895a76e01e258f" +
|
|
|
|
"09033b4a78210205ad9a838cff17d79fee2841bec72e9" +
|
|
|
|
"9b6fd4e62fd9214fcf845b1cf8438062053ae"),
|
|
|
|
expected: expectedAddr{
|
|
|
|
address: "bc1pc57jdm7kcnufnc339fvy2caflj6lkfeqasdfghftl7dd77dfpresqu7vep",
|
|
|
|
addressHash: hexToBytes("c53d26efd6c4f899e2312a584563a9fcb5fb2720ec1a945d2bff9adf79a908f3"),
|
|
|
|
internal: false,
|
|
|
|
imported: true,
|
|
|
|
compressed: true,
|
|
|
|
// script is set to the in field during tests.
|
|
|
|
},
|
|
|
|
},
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The manager must be unlocked to import a private key and also for
|
|
|
|
// testing private data. However, a watching-only manager can't be
|
|
|
|
// unlocked.
|
|
|
|
if !tc.watchingOnly {
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only import the scripts when in the create phase of testing.
|
2021-02-17 02:01:12 +01:00
|
|
|
tc.internalAccount = ImportedAddrAccount
|
2014-08-08 22:43:50 +02:00
|
|
|
prefix := testNamePrefix(tc)
|
|
|
|
if tc.create {
|
|
|
|
for i, test := range tests {
|
2021-03-24 14:43:24 +01:00
|
|
|
test := test
|
2014-08-08 22:43:50 +02:00
|
|
|
test.expected.script = test.in
|
|
|
|
prefix := fmt.Sprintf("%s ImportScript #%d (%s)", prefix,
|
|
|
|
i, test.name)
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
var addr ManagedScriptAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-12-02 15:10:24 +01:00
|
|
|
|
|
|
|
if test.isWitness {
|
|
|
|
addr, err = tc.manager.ImportWitnessScript(
|
|
|
|
ns, test.in, &test.blockstamp,
|
|
|
|
test.witnessVersion,
|
|
|
|
test.isSecretScript,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
addr, err = tc.manager.ImportScript(
|
|
|
|
ns, test.in, &test.blockstamp,
|
|
|
|
)
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix,
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !testAddress(tc, prefix, addr, &test.expected) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a closure to test the results since the same tests need to be
|
|
|
|
// repeated with the manager unlocked and locked.
|
2015-02-06 06:51:37 +01:00
|
|
|
chainParams := tc.manager.ChainParams()
|
2014-08-08 22:43:50 +02:00
|
|
|
testResults := func() bool {
|
|
|
|
failed := false
|
|
|
|
for i, test := range tests {
|
|
|
|
test.expected.script = test.in
|
|
|
|
|
|
|
|
// Use the Address API to retrieve each of the expected
|
|
|
|
// new addresses and ensure they're accurate.
|
2021-12-02 15:10:24 +01:00
|
|
|
var (
|
|
|
|
utilAddr btcutil.Address
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
switch {
|
|
|
|
case test.isWitness && test.witnessVersion == 0:
|
|
|
|
scriptHash := sha256.Sum256(test.in)
|
|
|
|
utilAddr, err = btcutil.NewAddressWitnessScriptHash(
|
|
|
|
scriptHash[:], chainParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
case test.isWitness && test.witnessVersion == 1:
|
|
|
|
scriptHash := sha256.Sum256(test.in)
|
|
|
|
utilAddr, err = btcutil.NewAddressTaproot(
|
|
|
|
scriptHash[:], chainParams,
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
utilAddr, err = btcutil.NewAddressScriptHash(
|
|
|
|
test.in, chainParams,
|
|
|
|
)
|
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s NewAddressScriptHash #%d (%s): "+
|
|
|
|
"unexpected error: %v", prefix, i,
|
|
|
|
test.name, err)
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
taPrefix := fmt.Sprintf("%s Address #%d (%s)", prefix,
|
|
|
|
i, test.name)
|
2018-11-06 08:24:38 +01:00
|
|
|
var ma ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
ma, err = tc.manager.Address(ns, utilAddr)
|
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", taPrefix,
|
|
|
|
err)
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !testAddress(tc, taPrefix, ma, &test.expected) {
|
|
|
|
failed = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return !failed
|
|
|
|
}
|
|
|
|
|
|
|
|
// The address manager could either be locked or unlocked here depending
|
|
|
|
// on whether or not it's a watching-only manager. When it's unlocked,
|
|
|
|
// this will test both the public and private address data are accurate.
|
|
|
|
// When it's locked, it must be watching-only, so only the public
|
|
|
|
// address information is tested and the private functions are checked
|
|
|
|
// to ensure they return the expected ErrWatchingOnly error.
|
|
|
|
if !testResults() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after this point involves locking the address manager and
|
|
|
|
// retesting the addresses with a locked manager. However, for
|
|
|
|
// watching-only mode, this has already happened, so just exit now in
|
|
|
|
// that case.
|
|
|
|
if tc.watchingOnly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the manager and retest all of the addresses to ensure the
|
|
|
|
// private information returns the expected error.
|
2018-02-14 06:34:50 +01:00
|
|
|
if err := tc.rootManager.Lock(); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Lock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = false
|
2021-03-24 14:43:24 +01:00
|
|
|
return testResults()
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
2015-01-26 13:08:14 +01:00
|
|
|
// testMarkUsed ensures used addresses are flagged as such.
|
2020-04-25 02:44:21 +02:00
|
|
|
func testMarkUsed(tc *testContext, doScript bool) bool {
|
2015-04-11 18:08:48 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
typ addrType
|
|
|
|
in []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "managed address",
|
|
|
|
typ: addrPubKeyHash,
|
|
|
|
in: hexToBytes("2ef94abb9ee8f785d087c3ec8d6ee467e92d0d0a"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "script address",
|
|
|
|
typ: addrScriptHash,
|
|
|
|
in: hexToBytes("da6e6a632d96dc5530d7b3c9f3017725d023093e"),
|
|
|
|
},
|
2015-01-26 13:08:14 +01:00
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
prefix := fmt.Sprintf("(%s) MarkUsed", tc.caseName)
|
2015-01-26 13:08:14 +01:00
|
|
|
chainParams := tc.manager.ChainParams()
|
2015-04-11 18:08:48 +02:00
|
|
|
for i, test := range tests {
|
2021-03-24 14:43:24 +01:00
|
|
|
i, test := i, test
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
if !doScript && test.typ == addrScriptHash {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
addrHash := test.in
|
2015-01-26 13:08:14 +01:00
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
var addr btcutil.Address
|
|
|
|
var err error
|
|
|
|
switch test.typ {
|
|
|
|
case addrPubKeyHash:
|
|
|
|
addr, err = btcutil.NewAddressPubKeyHash(addrHash, chainParams)
|
|
|
|
case addrScriptHash:
|
|
|
|
addr, err = btcutil.NewAddressScriptHashFromHash(addrHash, chainParams)
|
|
|
|
default:
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s #%d: NewAddress unexpected error: %v", prefix, i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
maddr, err := tc.manager.Address(ns, addr)
|
2015-04-11 18:08:48 +02:00
|
|
|
if err != nil {
|
2017-01-17 01:19:02 +01:00
|
|
|
tc.t.Errorf("%s #%d: Address unexpected error: %v", prefix, i, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if tc.create {
|
|
|
|
// Test that initially the address is not flagged as used
|
|
|
|
used := maddr.Used(ns)
|
|
|
|
if used != false {
|
|
|
|
tc.t.Errorf("%s #%d: unexpected used flag -- got "+
|
|
|
|
"%v, want %v", prefix, i, used, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = tc.manager.MarkUsed(ns, addr)
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s #%d: unexpected error: %v", prefix, i, err)
|
|
|
|
return nil
|
2015-04-11 18:08:48 +02:00
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
used := maddr.Used(ns)
|
|
|
|
if used != true {
|
2015-04-11 18:08:48 +02:00
|
|
|
tc.t.Errorf("%s #%d: unexpected used flag -- got "+
|
2017-01-17 01:19:02 +01:00
|
|
|
"%v, want %v", prefix, i, used, true)
|
2015-04-11 18:08:48 +02:00
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
return nil
|
|
|
|
})
|
2015-01-26 13:08:14 +01:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
tc.t.Errorf("(%s) Unexpected error %v", tc.caseName, err)
|
2015-01-26 13:08:14 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
2015-01-26 13:08:14 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-14 06:34:50 +01:00
|
|
|
// testChangePassphrase ensures changes both the public and private passphrases
|
2014-08-08 22:43:50 +02:00
|
|
|
// works as intended.
|
|
|
|
func testChangePassphrase(tc *testContext) bool {
|
2020-04-25 02:44:21 +02:00
|
|
|
pfx := fmt.Sprintf("(%s) ", tc.caseName)
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// Force an error when changing the passphrase due to failure to
|
|
|
|
// generate a new secret key by replacing the generation function one
|
|
|
|
// that intentionally errors.
|
2020-04-25 02:44:21 +02:00
|
|
|
testName := pfx + "ChangePassphrase (public) with invalid new secret key"
|
2014-10-24 08:56:54 +02:00
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
oldKeyGen := SetSecretKeyGen(failingSecretKeyGen)
|
2018-09-01 01:28:38 +02:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, pubPassphrase, pubPassphrase2, false, fastScrypt,
|
|
|
|
)
|
2014-10-24 08:56:54 +02:00
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrCrypto) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to change public passphrase with invalid old passphrase.
|
2020-04-25 02:44:21 +02:00
|
|
|
testName = pfx + "ChangePassphrase (public) with invalid old passphrase"
|
2018-11-06 08:24:38 +01:00
|
|
|
SetSecretKeyGen(oldKeyGen)
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, []byte("bogus"), pubPassphrase2, false, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, ErrWrongPassphrase) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the public passphrase.
|
2020-04-25 02:44:21 +02:00
|
|
|
testName = pfx + "ChangePassphrase (public)"
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, pubPassphrase, pubPassphrase2, false, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", testName, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:50:51 +01:00
|
|
|
// Ensure the public passphrase was successfully changed. We do this by
|
|
|
|
// being able to re-derive the public key with the new passphrase.
|
|
|
|
secretKey := snacl.SecretKey{Key: &snacl.CryptoKey{}}
|
|
|
|
secretKey.Parameters = tc.rootManager.masterKeyPub.Parameters
|
|
|
|
if err := secretKey.DeriveKey(&pubPassphrase2); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("%s: passphrase does not match", testName)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the private passphrase back to what it was.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, pubPassphrase2, pubPassphrase, false, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", testName, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to change private passphrase with invalid old passphrase.
|
|
|
|
// The error should be ErrWrongPassphrase or ErrWatchingOnly depending
|
|
|
|
// on the type of the address manager.
|
2020-04-25 02:44:21 +02:00
|
|
|
testName = pfx + "ChangePassphrase (private) with invalid old passphrase"
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, []byte("bogus"), privPassphrase2, true, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode := ErrWrongPassphrase
|
2014-08-08 22:43:50 +02:00
|
|
|
if tc.watchingOnly {
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrWatchingOnly
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
if !checkManagerError(tc.t, testName, err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything after this point involves testing that the private
|
|
|
|
// passphrase for the address manager can be changed successfully.
|
|
|
|
// This is not possible for watching-only mode, so just exit now in that
|
|
|
|
// case.
|
|
|
|
if tc.watchingOnly {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the private passphrase.
|
2020-04-25 02:44:21 +02:00
|
|
|
testName = pfx + "ChangePassphrase (private)"
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, privPassphrase, privPassphrase2, true, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", testName, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock the manager with the new passphrase to ensure it changed as
|
|
|
|
// expected.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.Unlock(ns, privPassphrase2)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("%s: failed to unlock with new private "+
|
|
|
|
"passphrase: %v", testName, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
|
|
|
|
// Change the private passphrase back to what it was while the manager
|
|
|
|
// is unlocked to ensure that path works properly as well.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.ChangePassphrase(
|
|
|
|
ns, privPassphrase2, privPassphrase, true, fastScrypt,
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%s: unexpected error: %v", testName, err)
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
if tc.rootManager.IsLocked() {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("%s: manager is locked", testName)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relock the manager for future tests.
|
2018-02-14 06:34:50 +01:00
|
|
|
if err := tc.rootManager.Lock(); err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("Lock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = false
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:54:26 +01:00
|
|
|
// testNewAccount tests the new account creation func of the address manager works
|
|
|
|
// as expected.
|
|
|
|
func testNewAccount(tc *testContext) bool {
|
|
|
|
if tc.watchingOnly {
|
|
|
|
// Creating new accounts in watching-only mode should return ErrWatchingOnly
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.NewAccount(ns, "test")
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(
|
|
|
|
tc.t, "Create account in watching-only mode", err,
|
|
|
|
ErrWatchingOnly,
|
|
|
|
) {
|
2014-12-12 09:54:26 +01:00
|
|
|
tc.manager.Close()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Creating new accounts when wallet is locked should return ErrLocked
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.NewAccount(ns, "test")
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
if !checkManagerError(
|
|
|
|
tc.t, "Create account when wallet is locked", err, ErrLocked,
|
|
|
|
) {
|
2014-12-12 09:54:26 +01:00
|
|
|
tc.manager.Close()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Unlock the wallet to decrypt cointype keys required
|
|
|
|
// to derive account keys
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
err := tc.rootManager.Unlock(ns, privPassphrase)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-12-12 09:54:26 +01:00
|
|
|
tc.t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tc.unlocked = true
|
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
testName := "acct-create"
|
2021-02-17 02:01:12 +01:00
|
|
|
expectedAccount := tc.internalAccount + 1
|
2014-12-12 09:54:26 +01:00
|
|
|
if !tc.create {
|
2015-04-11 18:08:48 +02:00
|
|
|
// Create a new account in open mode
|
|
|
|
testName = "acct-open"
|
2014-12-12 09:54:26 +01:00
|
|
|
expectedAccount++
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
var account uint32
|
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
account, err = tc.manager.NewAccount(ns, testName)
|
|
|
|
return err
|
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("NewAccount: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if account != expectedAccount {
|
|
|
|
tc.t.Errorf("NewAccount "+
|
|
|
|
"account mismatch -- got %d, "+
|
|
|
|
"want %d", account, expectedAccount)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
2014-12-12 09:54:26 +01:00
|
|
|
// Test duplicate account name error
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.NewAccount(ns, testName)
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode := ErrDuplicateAccount
|
2014-12-12 09:54:26 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test account name validation
|
2016-03-11 16:45:09 +01:00
|
|
|
testName = "" // Empty account names are not allowed
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.NewAccount(ns, testName)
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrInvalidAccount
|
2016-03-11 16:45:09 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
2015-04-16 18:54:56 +02:00
|
|
|
testName = "imported" // A reserved account name
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.NewAccount(ns, testName)
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrInvalidAccount
|
2021-03-24 14:43:24 +01:00
|
|
|
return checkManagerError(tc.t, testName, err, wantErrCode)
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
|
|
|
|
2015-04-11 18:08:48 +02:00
|
|
|
// testLookupAccount tests the basic account lookup func of the address manager
|
|
|
|
// works as expected.
|
2014-12-12 09:54:26 +01:00
|
|
|
func testLookupAccount(tc *testContext) bool {
|
|
|
|
// Lookup accounts created earlier in testNewAccount
|
|
|
|
expectedAccounts := map[string]uint32{
|
2018-11-06 08:50:51 +01:00
|
|
|
defaultAccountName: DefaultAccountNum,
|
2018-11-06 08:24:38 +01:00
|
|
|
ImportedAddrAccountName: ImportedAddrAccount,
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
2020-04-25 02:44:21 +02:00
|
|
|
|
|
|
|
var expectedLastAccount uint32 = 1
|
|
|
|
if !tc.create {
|
|
|
|
// Existing wallet manager will have 3 accounts
|
|
|
|
expectedLastAccount = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
return testLookupExpectedAccount(tc, expectedAccounts, expectedLastAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLookupExpectedAccount(tc *testContext, expectedAccounts map[string]uint32,
|
|
|
|
expectedLastAccount uint32) bool {
|
2021-03-24 14:43:24 +01:00
|
|
|
|
2014-12-12 09:54:26 +01:00
|
|
|
for acctName, expectedAccount := range expectedAccounts {
|
2021-03-24 14:43:24 +01:00
|
|
|
acctName := acctName
|
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
var account uint32
|
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
account, err = tc.manager.LookupAccount(ns, acctName)
|
|
|
|
return err
|
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("LookupAccount: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if account != expectedAccount {
|
|
|
|
tc.t.Errorf("LookupAccount "+
|
|
|
|
"account mismatch -- got %d, "+
|
|
|
|
"want %d", account, expectedAccount)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test account not found error
|
|
|
|
testName := "non existent account"
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.LookupAccount(ns, testName)
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode := ErrAccountNotFound
|
2014-12-12 09:54:26 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
|
|
|
// Test last account
|
2017-01-17 01:19:02 +01:00
|
|
|
var lastAccount uint32
|
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
lastAccount, err = tc.manager.LastAccount(ns)
|
|
|
|
return err
|
|
|
|
})
|
2021-03-24 14:43:24 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("LookupAccount: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
if lastAccount != expectedLastAccount {
|
|
|
|
tc.t.Errorf("LookupAccount "+
|
|
|
|
"account mismatch -- got %d, "+
|
|
|
|
"want %d", lastAccount, expectedLastAccount)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test account lookup for default account adddress
|
|
|
|
var expectedAccount uint32
|
|
|
|
for i, addr := range expectedAddrs {
|
|
|
|
addr, err := btcutil.NewAddressPubKeyHash(addr.addressHash,
|
|
|
|
tc.manager.ChainParams())
|
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("AddrAccount #%d: unexpected error: %v", i, err)
|
|
|
|
return false
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
var account uint32
|
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
|
|
|
account, err = tc.manager.AddrAccount(ns, addr)
|
|
|
|
return err
|
|
|
|
})
|
2015-04-11 18:08:48 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("AddrAccount #%d: unexpected error: %v", i, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if account != expectedAccount {
|
|
|
|
tc.t.Errorf("AddrAccount "+
|
|
|
|
"account mismatch -- got %d, "+
|
|
|
|
"want %d", account, expectedAccount)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2014-12-12 09:54:26 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRenameAccount tests the rename account func of the address manager works
|
|
|
|
// as expected.
|
|
|
|
func testRenameAccount(tc *testContext) bool {
|
2017-01-17 01:19:02 +01:00
|
|
|
var acctName string
|
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
acctName, err = tc.manager.AccountName(ns, tc.internalAccount)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("AccountName: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
testName := acctName + "-renamed"
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2021-02-17 02:01:12 +01:00
|
|
|
return tc.manager.RenameAccount(ns, tc.internalAccount, testName)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("RenameAccount: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
var newName string
|
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2021-02-17 02:01:12 +01:00
|
|
|
newName, err = tc.manager.AccountName(ns, tc.internalAccount)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("AccountName: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if newName != testName {
|
|
|
|
tc.t.Errorf("RenameAccount "+
|
|
|
|
"account name mismatch -- got %s, "+
|
|
|
|
"want %s", newName, testName)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test duplicate account name error
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2021-02-17 02:01:12 +01:00
|
|
|
return tc.manager.RenameAccount(ns, tc.internalAccount, testName)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode := ErrDuplicateAccount
|
2014-12-12 09:54:26 +01:00
|
|
|
if !checkManagerError(tc.t, testName, err, wantErrCode) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test old account name is no longer valid
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := tc.manager.LookupAccount(ns, acctName)
|
|
|
|
return err
|
|
|
|
})
|
2018-11-06 08:24:38 +01:00
|
|
|
wantErrCode = ErrAccountNotFound
|
2021-03-24 14:43:24 +01:00
|
|
|
return checkManagerError(tc.t, testName, err, wantErrCode)
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 19:22:59 +01:00
|
|
|
// testForEachAccount tests the retrieve all accounts func of the address
|
|
|
|
// manager works as expected.
|
|
|
|
func testForEachAccount(tc *testContext) bool {
|
|
|
|
prefix := testNamePrefix(tc) + " testForEachAccount"
|
2014-12-12 09:54:26 +01:00
|
|
|
expectedAccounts := []uint32{0, 1}
|
|
|
|
if !tc.create {
|
|
|
|
// Existing wallet manager will have 3 accounts
|
|
|
|
expectedAccounts = append(expectedAccounts, 2)
|
|
|
|
}
|
|
|
|
// Imported account
|
2018-11-06 08:24:38 +01:00
|
|
|
expectedAccounts = append(expectedAccounts, ImportedAddrAccount)
|
2015-03-26 19:22:59 +01:00
|
|
|
var accounts []uint32
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
return tc.manager.ForEachAccount(ns, func(account uint32) error {
|
|
|
|
accounts = append(accounts, account)
|
|
|
|
return nil
|
|
|
|
})
|
2015-03-26 19:22:59 +01:00
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
2015-03-26 19:22:59 +01:00
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
|
2014-12-12 09:54:26 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(accounts) != len(expectedAccounts) {
|
2015-03-26 19:22:59 +01:00
|
|
|
tc.t.Errorf("%s: unexpected number of accounts - got "+
|
|
|
|
"%d, want %d", prefix, len(accounts),
|
2014-12-12 09:54:26 +01:00
|
|
|
len(expectedAccounts))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, account := range accounts {
|
|
|
|
if expectedAccounts[i] != account {
|
2015-03-26 19:22:59 +01:00
|
|
|
tc.t.Errorf("%s #%d: "+
|
2014-12-12 09:54:26 +01:00
|
|
|
"account mismatch -- got %d, "+
|
2015-03-26 19:22:59 +01:00
|
|
|
"want %d", prefix, i, account, expectedAccounts[i])
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-03-26 19:22:59 +01:00
|
|
|
// testForEachAccountAddress tests that iterating through the given
|
|
|
|
// account addresses using the manager API works as expected.
|
|
|
|
func testForEachAccountAddress(tc *testContext) bool {
|
|
|
|
prefix := testNamePrefix(tc) + " testForEachAccountAddress"
|
2015-04-11 18:08:48 +02:00
|
|
|
// Make a map of expected addresses
|
|
|
|
expectedAddrMap := make(map[string]*expectedAddr, len(expectedAddrs))
|
|
|
|
for i := 0; i < len(expectedAddrs); i++ {
|
|
|
|
expectedAddrMap[expectedAddrs[i].address] = &expectedAddrs[i]
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
var addrs []ManagedAddress
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(tc.db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
2021-02-17 02:01:12 +01:00
|
|
|
return tc.manager.ForEachAccountAddress(ns, tc.internalAccount,
|
2018-11-06 08:24:38 +01:00
|
|
|
func(maddr ManagedAddress) error {
|
2017-01-17 01:19:02 +01:00
|
|
|
addrs = append(addrs, maddr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
2014-12-12 09:54:26 +01:00
|
|
|
if err != nil {
|
2015-04-11 18:08:48 +02:00
|
|
|
tc.t.Errorf("%s: unexpected error: %v", prefix, err)
|
2014-12-12 09:54:26 +01:00
|
|
|
return false
|
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
|
|
|
for i := 0; i < len(addrs); i++ {
|
|
|
|
prefix := fmt.Sprintf("%s: #%d", prefix, i)
|
|
|
|
gotAddr := addrs[i]
|
|
|
|
wantAddr := expectedAddrMap[gotAddr.Address().String()]
|
|
|
|
if !testAddress(tc, prefix, gotAddr, wantAddr) {
|
|
|
|
return false
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
delete(expectedAddrMap, gotAddr.Address().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(expectedAddrMap) != 0 {
|
|
|
|
tc.t.Errorf("%s: unexpected addresses -- got %d, want %d", prefix,
|
|
|
|
len(expectedAddrMap), 0)
|
|
|
|
return false
|
2014-12-12 09:54:26 +01:00
|
|
|
}
|
2015-04-11 18:08:48 +02:00
|
|
|
|
2014-12-12 09:54:26 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// testManagerAPI tests the functions provided by the Manager API as well as
|
|
|
|
// the ManagedAddress, ManagedPubKeyAddress, and ManagedScriptAddress
|
|
|
|
// interfaces.
|
2020-04-25 02:44:21 +02:00
|
|
|
func testManagerAPI(tc *testContext, caseCreatedWatchingOnly bool) {
|
|
|
|
if !caseCreatedWatchingOnly {
|
|
|
|
// Test API for normal create (w/ seed) case.
|
|
|
|
testLocking(tc)
|
|
|
|
testExternalAddresses(tc)
|
|
|
|
testInternalAddresses(tc)
|
|
|
|
testImportPrivateKey(tc)
|
|
|
|
testImportScript(tc)
|
|
|
|
testMarkUsed(tc, true)
|
|
|
|
testChangePassphrase(tc)
|
|
|
|
|
|
|
|
// Reset default account
|
2021-02-17 02:01:12 +01:00
|
|
|
tc.internalAccount = 0
|
2020-04-25 02:44:21 +02:00
|
|
|
testNewAccount(tc)
|
|
|
|
testLookupAccount(tc)
|
|
|
|
testForEachAccount(tc)
|
|
|
|
testForEachAccountAddress(tc)
|
|
|
|
|
|
|
|
// Rename account 1 "acct-create"
|
2021-02-17 02:01:12 +01:00
|
|
|
tc.internalAccount = 1
|
2020-04-25 02:44:21 +02:00
|
|
|
testRenameAccount(tc)
|
|
|
|
} else {
|
|
|
|
// Test API for created watch-only case.
|
|
|
|
testExternalAddresses(tc)
|
|
|
|
testInternalAddresses(tc)
|
|
|
|
testMarkUsed(tc, false)
|
|
|
|
testChangePassphrase(tc)
|
|
|
|
|
|
|
|
testNewAccount(tc)
|
|
|
|
expectedAccounts := map[string]uint32{
|
|
|
|
defaultAccountName: DefaultAccountNum,
|
|
|
|
}
|
|
|
|
testLookupExpectedAccount(tc, expectedAccounts, 0)
|
|
|
|
//testForEachAccount(tc)
|
|
|
|
testForEachAccountAddress(tc)
|
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
// testConvertWatchingOnly tests various facets of a watching-only address
|
2014-11-02 22:06:11 +01:00
|
|
|
// manager such as running the full set of API tests against a newly converted
|
|
|
|
// copy as well as when it is opened from an existing namespace.
|
2020-04-25 02:44:21 +02:00
|
|
|
func testConvertWatchingOnly(tc *testContext) bool {
|
|
|
|
// These tests check the case where the manager was not initially
|
|
|
|
// created watch-only, but converted to watch only ...
|
|
|
|
|
2014-11-02 22:06:11 +01:00
|
|
|
// Make a copy of the current database so the copy can be converted to
|
|
|
|
// watching only.
|
2014-08-08 22:43:50 +02:00
|
|
|
woMgrName := "mgrtestwo.bin"
|
|
|
|
_ = os.Remove(woMgrName)
|
2014-11-02 22:06:11 +01:00
|
|
|
fi, err := os.OpenFile(woMgrName, os.O_CREATE|os.O_RDWR, 0600)
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
2014-11-02 22:06:11 +01:00
|
|
|
tc.t.Errorf("%v", err)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2014-11-02 22:06:11 +01:00
|
|
|
if err := tc.db.Copy(fi); err != nil {
|
|
|
|
fi.Close()
|
|
|
|
tc.t.Errorf("%v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fi.Close()
|
2014-08-08 22:43:50 +02:00
|
|
|
defer os.Remove(woMgrName)
|
|
|
|
|
2014-11-02 22:06:11 +01:00
|
|
|
// Open the new database copy and get the address manager namespace.
|
2020-08-11 21:51:32 +02:00
|
|
|
db, err := walletdb.Open("bdb", woMgrName, true, defaultDBTimeout)
|
2014-11-02 22:06:11 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("openDbNamespace: unexpected error: %v", err)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2014-11-02 22:06:11 +01:00
|
|
|
defer db.Close()
|
2014-08-08 22:43:50 +02:00
|
|
|
|
2014-11-02 22:06:11 +01:00
|
|
|
// Open the manager using the namespace and convert it to watching-only.
|
2018-11-06 08:24:38 +01:00
|
|
|
var mgr *Manager
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-11-02 22:06:11 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("%v", err)
|
|
|
|
return false
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
return mgr.ConvertToWatchingOnly(ns)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-11-02 22:06:11 +01:00
|
|
|
tc.t.Errorf("%v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all of the manager API tests against the converted manager and
|
2018-02-14 06:34:50 +01:00
|
|
|
// close it. We'll also retrieve the default scope (BIP0044) from the
|
|
|
|
// manager in order to use.
|
2018-11-06 08:24:38 +01:00
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
2018-02-14 06:34:50 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("unable to fetch bip 44 scope %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
testManagerAPI(&testContext{
|
2021-02-17 02:01:12 +01:00
|
|
|
t: tc.t,
|
|
|
|
caseName: tc.caseName,
|
|
|
|
db: db,
|
|
|
|
rootManager: mgr,
|
|
|
|
manager: scopedMgr,
|
|
|
|
internalAccount: 0,
|
|
|
|
create: false,
|
|
|
|
watchingOnly: true,
|
2020-04-25 02:44:21 +02:00
|
|
|
}, false)
|
2014-08-08 22:43:50 +02:00
|
|
|
mgr.Close()
|
|
|
|
|
|
|
|
// Open the watching-only manager and run all the tests again.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("Open Watching-Only: unexpected error: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
scopedMgr, err = mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
2018-02-14 06:34:50 +01:00
|
|
|
if err != nil {
|
|
|
|
tc.t.Errorf("unable to fetch bip 44 scope %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
testManagerAPI(&testContext{
|
2021-02-17 02:01:12 +01:00
|
|
|
t: tc.t,
|
|
|
|
caseName: tc.caseName,
|
|
|
|
db: db,
|
|
|
|
rootManager: mgr,
|
|
|
|
manager: scopedMgr,
|
|
|
|
internalAccount: 0,
|
|
|
|
create: false,
|
|
|
|
watchingOnly: true,
|
2020-04-25 02:44:21 +02:00
|
|
|
}, false)
|
2014-08-08 22:43:50 +02:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// testSync tests various facets of setting the manager sync state.
|
|
|
|
func testSync(tc *testContext) bool {
|
2018-02-14 06:34:50 +01:00
|
|
|
// Ensure syncing the manager to nil results in the synced to state
|
|
|
|
// being the earliest block (genesis block in this case).
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.SetSyncedTo(ns, nil)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
tc.t.Errorf("(%s) SetSyncedTo unexpected err on nil: %v",
|
|
|
|
tc.caseName, err)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
blockStamp := BlockStamp{
|
2018-02-14 06:34:50 +01:00
|
|
|
Height: 0,
|
|
|
|
Hash: *chaincfg.MainNetParams.GenesisHash,
|
|
|
|
}
|
|
|
|
gotBlockStamp := tc.rootManager.SyncedTo()
|
2014-08-08 22:43:50 +02:00
|
|
|
if gotBlockStamp != blockStamp {
|
2020-04-25 02:44:21 +02:00
|
|
|
tc.t.Errorf("(%s) SyncedTo unexpected block stamp on nil -- "+
|
|
|
|
"got %v, want %v", tc.caseName, gotBlockStamp, blockStamp)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-14 06:34:50 +01:00
|
|
|
// If we update to a new more recent block time stamp, then upon
|
|
|
|
// retrieval it should be returned as the best known state.
|
|
|
|
latestHash, err := chainhash.NewHash(seed)
|
2017-01-17 01:19:02 +01:00
|
|
|
if err != nil {
|
2018-02-14 06:34:50 +01:00
|
|
|
tc.t.Errorf("%v", err)
|
2014-08-08 22:43:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
blockStamp = BlockStamp{
|
2018-03-05 18:44:41 +01:00
|
|
|
Height: 1,
|
|
|
|
Hash: *latestHash,
|
|
|
|
Timestamp: time.Unix(1234, 0),
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-02-14 06:34:50 +01:00
|
|
|
return tc.rootManager.SetSyncedTo(ns, &blockStamp)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2014-08-08 22:43:50 +02:00
|
|
|
tc.t.Errorf("SetSyncedTo unexpected err on nil: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
gotBlockStamp = tc.rootManager.SyncedTo()
|
2014-08-08 22:43:50 +02:00
|
|
|
if gotBlockStamp != blockStamp {
|
|
|
|
tc.t.Errorf("SyncedTo unexpected block stamp on nil -- "+
|
|
|
|
"got %v, want %v", gotBlockStamp, blockStamp)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestManager performs a full suite of tests against the address manager API.
|
|
|
|
// It makes use of a test context because the address manager is persistent and
|
|
|
|
// much of the testing involves having specific state.
|
2022-07-06 06:00:17 +02:00
|
|
|
func _TestManager(t *testing.T) {
|
2020-04-25 02:44:21 +02:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
createdWatchingOnly bool
|
2020-09-26 12:14:09 +02:00
|
|
|
rootKey *hdkeychain.ExtendedKey
|
2020-04-25 02:44:21 +02:00
|
|
|
privPassphrase []byte
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "created with seed",
|
|
|
|
createdWatchingOnly: false,
|
2020-09-26 12:14:09 +02:00
|
|
|
rootKey: rootKey,
|
2020-04-25 02:44:21 +02:00
|
|
|
privPassphrase: privPassphrase,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "created watch-only",
|
|
|
|
createdWatchingOnly: true,
|
2020-09-26 12:14:09 +02:00
|
|
|
rootKey: nil,
|
2020-04-25 02:44:21 +02:00
|
|
|
privPassphrase: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
// Need to wrap in a call so the defers work correctly.
|
2020-09-26 12:14:09 +02:00
|
|
|
testManagerCase(
|
|
|
|
t, test.name, test.createdWatchingOnly,
|
|
|
|
test.privPassphrase, test.rootKey,
|
|
|
|
)
|
2020-04-25 02:44:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testManagerCase(t *testing.T, caseName string,
|
2020-09-26 12:14:09 +02:00
|
|
|
caseCreatedWatchingOnly bool, casePrivPassphrase []byte,
|
|
|
|
caseKey *hdkeychain.ExtendedKey) {
|
2020-04-25 02:44:21 +02:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
2014-11-02 22:06:11 +01:00
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
if !caseCreatedWatchingOnly {
|
|
|
|
// Open manager that does not exist to ensure the expected error is
|
|
|
|
// returned.
|
|
|
|
err := walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if !checkManagerError(t, "Open non-existent", err, ErrNoExist) {
|
|
|
|
return
|
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new manager.
|
2018-11-06 08:24:38 +01:00
|
|
|
var mgr *Manager
|
2020-04-25 02:44:21 +02:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
2017-01-17 01:19:02 +01:00
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
err = Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, caseKey, pubPassphrase, casePrivPassphrase,
|
2018-04-19 05:04:41 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2020-04-25 02:44:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if caseCreatedWatchingOnly {
|
|
|
|
_, err = mgr.NewScopedKeyManager(
|
|
|
|
ns, KeyScopeBIP0044, ScopeAddrMap[KeyScopeBIP0044])
|
|
|
|
}
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2016-03-17 15:05:11 +01:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
t.Errorf("(%s) Create/Open: unexpected error: %v", caseName, err)
|
2016-03-17 15:05:11 +01:00
|
|
|
return
|
|
|
|
}
|
2014-11-02 22:06:11 +01:00
|
|
|
|
2014-08-08 22:43:50 +02:00
|
|
|
// NOTE: Not using deferred close here since part of the tests is
|
|
|
|
// explicitly closing the manager and then opening the existing one.
|
|
|
|
|
|
|
|
// Attempt to create the manager again to ensure the expected error is
|
|
|
|
// returned.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2018-11-06 08:24:38 +01:00
|
|
|
return Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, caseKey, pubPassphrase, casePrivPassphrase,
|
2018-11-06 08:24:38 +01:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
|
|
|
)
|
2017-01-17 01:19:02 +01:00
|
|
|
})
|
2020-04-25 02:44:21 +02:00
|
|
|
if !checkManagerError(t, fmt.Sprintf("(%s) Create existing", caseName),
|
|
|
|
err, ErrAlreadyExists) {
|
2014-08-08 22:43:50 +02:00
|
|
|
mgr.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
2018-02-14 06:34:50 +01:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
t.Fatalf("(%s) unable to fetch default scope: %v", caseName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if caseCreatedWatchingOnly {
|
|
|
|
accountKey := deriveTestAccountKey(t)
|
|
|
|
if accountKey == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acctKeyPub, err := accountKey.Neuter()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("(%s) Neuter: unexpected error: %v", caseName, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the default account
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err = scopedMgr.NewAccountWatchingOnly(
|
2021-02-15 23:38:23 +01:00
|
|
|
ns, defaultAccountName, acctKeyPub, 0, nil,
|
|
|
|
)
|
2020-04-25 02:44:21 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("NewAccountWatchingOnly: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2018-02-14 06:34:50 +01:00
|
|
|
}
|
2020-04-25 02:44:21 +02:00
|
|
|
|
|
|
|
// Run all of the manager API tests in create mode and close the
|
|
|
|
// manager after they've completed
|
2014-08-08 22:43:50 +02:00
|
|
|
testManagerAPI(&testContext{
|
2021-02-17 02:01:12 +01:00
|
|
|
t: t,
|
|
|
|
caseName: caseName,
|
|
|
|
db: db,
|
|
|
|
manager: scopedMgr,
|
|
|
|
rootManager: mgr,
|
|
|
|
internalAccount: 0,
|
|
|
|
create: true,
|
|
|
|
watchingOnly: caseCreatedWatchingOnly,
|
2020-04-25 02:44:21 +02:00
|
|
|
}, caseCreatedWatchingOnly)
|
2014-08-08 22:43:50 +02:00
|
|
|
mgr.Close()
|
|
|
|
|
|
|
|
// Open the manager and run all the tests again in open mode which
|
|
|
|
// avoids reinserting new addresses like the create mode tests do.
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2017-01-17 01:19:02 +01:00
|
|
|
return err
|
|
|
|
})
|
2014-08-08 22:43:50 +02:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
t.Errorf("(%s) Open: unexpected error: %v", caseName, err)
|
2014-08-08 22:43:50 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
scopedMgr, err = mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
2018-02-14 06:34:50 +01:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
t.Fatalf("(%s) unable to fetch default scope: %v", caseName, err)
|
2018-02-14 06:34:50 +01:00
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
tc := &testContext{
|
2021-02-17 02:01:12 +01:00
|
|
|
t: t,
|
|
|
|
caseName: caseName,
|
|
|
|
db: db,
|
|
|
|
manager: scopedMgr,
|
|
|
|
rootManager: mgr,
|
|
|
|
internalAccount: 0,
|
|
|
|
create: false,
|
|
|
|
watchingOnly: caseCreatedWatchingOnly,
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
2020-04-25 02:44:21 +02:00
|
|
|
testManagerAPI(tc, caseCreatedWatchingOnly)
|
2014-08-08 22:43:50 +02:00
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
if !caseCreatedWatchingOnly {
|
|
|
|
// Now that the address manager has been tested in both the newly
|
|
|
|
// created and opened modes, test a watching-only version.
|
|
|
|
testConvertWatchingOnly(tc)
|
|
|
|
}
|
2014-08-08 22:43:50 +02:00
|
|
|
|
|
|
|
// Ensure that the manager sync state functionality works as expected.
|
|
|
|
testSync(tc)
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
if !caseCreatedWatchingOnly {
|
|
|
|
// Unlock the manager so it can be closed with it unlocked to ensure
|
|
|
|
// it works without issue.
|
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
return mgr.Unlock(ns, casePrivPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unlock: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deriveTestAccountKey(t *testing.T) *hdkeychain.ExtendedKey {
|
|
|
|
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams)
|
2017-01-17 01:19:02 +01:00
|
|
|
if err != nil {
|
2020-04-25 02:44:21 +02:00
|
|
|
t.Errorf("NewMaster: unexpected error: %v", err)
|
|
|
|
return nil
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
2020-04-25 02:44:21 +02:00
|
|
|
scopeKey, err := deriveCoinTypeKey(masterKey, KeyScopeBIP0044)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("derive: unexpected error: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
accountKey, err := deriveAccountKey(scopeKey, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("derive: unexpected error: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return accountKey
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
2014-11-03 12:52:41 +01:00
|
|
|
|
2018-11-06 08:49:57 +01:00
|
|
|
// TestManagerIncorrectVersion ensures that that the manager cannot be accessed
|
|
|
|
// if its version does not match the latest version.
|
|
|
|
func TestManagerHigherVersion(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db, _ := setupManager(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-11-06 08:49:57 +01:00
|
|
|
// We'll update our manager's version to be one higher than the latest.
|
|
|
|
latestVersion := getLatestVersion()
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
if ns == nil {
|
|
|
|
return errors.New("top-level namespace does not exist")
|
|
|
|
}
|
|
|
|
return putManagerVersion(ns, latestVersion+1)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update manager version %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, upon attempting to open it without performing an upgrade, we
|
|
|
|
// should expect to see the error ErrUpgrade.
|
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if !checkManagerError(t, "Upgrade needed", err, ErrUpgrade) {
|
|
|
|
t.Fatalf("expected error ErrUpgrade, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also update it so that it is one lower than the latest.
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
if ns == nil {
|
|
|
|
return errors.New("top-level namespace does not exist")
|
|
|
|
}
|
|
|
|
return putManagerVersion(ns, latestVersion-1)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update manager version %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, upon attempting to open it without performing an upgrade to
|
|
|
|
// the latest version, we should also expect to see the error
|
|
|
|
// ErrUpgrade.
|
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
_, err := Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if !checkManagerError(t, "Upgrade needed", err, ErrUpgrade) {
|
|
|
|
t.Fatalf("expected error ErrUpgrade, got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// TestEncryptDecryptErrors ensures that errors which occur while encrypting and
|
|
|
|
// decrypting data return the expected errors.
|
2014-11-03 12:52:41 +01:00
|
|
|
func TestEncryptDecryptErrors(t *testing.T) {
|
2018-07-20 11:13:27 +02:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
teardown, db, mgr := setupManager(t)
|
2014-11-03 12:52:41 +01:00
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
invalidKeyType := CryptoKeyType(0xff)
|
2014-11-03 12:52:41 +01:00
|
|
|
if _, err := mgr.Encrypt(invalidKeyType, []byte{}); err == nil {
|
|
|
|
t.Fatalf("Encrypt accepted an invalid key type!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := mgr.Decrypt(invalidKeyType, []byte{}); err == nil {
|
|
|
|
t.Fatalf("Encrypt accepted an invalid key type!")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mgr.IsLocked() {
|
|
|
|
t.Fatal("Manager should be locked at this point.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
// Now the mgr is locked and encrypting/decrypting with private
|
|
|
|
// keys should fail.
|
2018-11-06 08:24:38 +01:00
|
|
|
_, err = mgr.Encrypt(CKTPrivate, []byte{})
|
2014-11-03 12:52:41 +01:00
|
|
|
checkManagerError(t, "encryption with private key fails when manager is locked",
|
2018-11-06 08:24:38 +01:00
|
|
|
err, ErrLocked)
|
2014-11-03 12:52:41 +01:00
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
_, err = mgr.Decrypt(CKTPrivate, []byte{})
|
2014-11-03 12:52:41 +01:00
|
|
|
checkManagerError(t, "decryption with private key fails when manager is locked",
|
2018-11-06 08:24:38 +01:00
|
|
|
err, ErrLocked)
|
2014-11-03 12:52:41 +01:00
|
|
|
|
|
|
|
// Unlock the manager for these tests
|
2017-01-17 01:19:02 +01:00
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-11-03 12:52:41 +01:00
|
|
|
t.Fatal("Attempted to unlock the manager, but failed:", err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:50:51 +01:00
|
|
|
// Make sure to cover the ErrCrypto error path in Encrypt and Decrypt.
|
|
|
|
// We'll use a mock private key that will fail upon running these
|
|
|
|
// methods.
|
|
|
|
mgr.cryptoKeyPriv = &failingCryptoKey{}
|
|
|
|
|
|
|
|
_, err = mgr.Encrypt(CKTPrivate, []byte{})
|
2018-11-06 08:24:38 +01:00
|
|
|
checkManagerError(t, "failed encryption", err, ErrCrypto)
|
2014-11-03 12:52:41 +01:00
|
|
|
|
2018-11-06 08:50:51 +01:00
|
|
|
_, err = mgr.Decrypt(CKTPrivate, []byte{})
|
2018-11-06 08:24:38 +01:00
|
|
|
checkManagerError(t, "failed decryption", err, ErrCrypto)
|
2014-11-03 12:52:41 +01:00
|
|
|
}
|
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// TestEncryptDecrypt ensures that encrypting and decrypting data with the
|
|
|
|
// the various crypto key types works as expected.
|
2014-11-03 12:52:41 +01:00
|
|
|
func TestEncryptDecrypt(t *testing.T) {
|
2018-07-20 11:13:27 +02:00
|
|
|
|
2017-01-17 01:19:02 +01:00
|
|
|
teardown, db, mgr := setupManager(t)
|
2014-11-03 12:52:41 +01:00
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2014-11-03 12:52:41 +01:00
|
|
|
plainText := []byte("this is a plaintext")
|
|
|
|
|
|
|
|
// Make sure address manager is unlocked
|
2017-01-17 01:19:02 +01:00
|
|
|
err := walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2014-11-03 12:52:41 +01:00
|
|
|
t.Fatal("Attempted to unlock the manager, but failed:", err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
keyTypes := []CryptoKeyType{
|
|
|
|
CKTPublic,
|
|
|
|
CKTPrivate,
|
|
|
|
CKTScript,
|
2014-11-03 12:52:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyType := range keyTypes {
|
|
|
|
cipherText, err := mgr.Encrypt(keyType, plainText)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to encrypt plaintext: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
decryptedCipherText, err := mgr.Decrypt(keyType, cipherText)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to decrypt plaintext: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(decryptedCipherText, plainText) {
|
|
|
|
t.Fatal("Got:", decryptedCipherText, ", want:", plainText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 06:35:17 +01:00
|
|
|
|
|
|
|
// TestScopedKeyManagerManagement tests that callers are able to properly
|
|
|
|
// create, retrieve, and utilize new scoped managers outside the set of default
|
|
|
|
// created scopes.
|
|
|
|
func TestScopedKeyManagerManagement(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-14 06:35:17 +01:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
2018-11-06 08:24:38 +01:00
|
|
|
var mgr *Manager
|
2018-02-14 06:35:17 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
err = Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, rootKey, pubPassphrase, privPassphrase,
|
2018-04-19 05:04:41 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create/open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the default scopes should have been created and loaded into
|
|
|
|
// memory upon initial opening.
|
2018-11-06 08:24:38 +01:00
|
|
|
for _, scope := range DefaultKeyScopes {
|
2018-02-14 06:35:17 +01:00
|
|
|
_, err := mgr.FetchScopedKeyManager(scope)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch scope %v: %v", scope, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, ensure that if we create an internal and external addrs for
|
|
|
|
// each of the default scope types, then they're derived according to
|
|
|
|
// their schema.
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
for _, scope := range DefaultKeyScopes {
|
2018-02-14 06:35:17 +01:00
|
|
|
sMgr, err := mgr.FetchScopedKeyManager(scope)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch scope %v: %v", scope, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
externalAddr, err := sMgr.NextExternalAddresses(
|
2018-11-06 08:24:38 +01:00
|
|
|
ns, DefaultAccountNum, 1,
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to derive external addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The external address should match the prescribed
|
|
|
|
// addr schema for this scoped key manager.
|
2018-11-06 08:24:38 +01:00
|
|
|
if externalAddr[0].AddrType() != ScopeAddrMap[scope].ExternalAddrType {
|
2018-02-14 06:35:17 +01:00
|
|
|
t.Fatalf("addr type mismatch: expected %v, got %v",
|
|
|
|
externalAddr[0].AddrType(),
|
2018-11-06 08:24:38 +01:00
|
|
|
ScopeAddrMap[scope].ExternalAddrType)
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
internalAddr, err := sMgr.NextInternalAddresses(
|
2018-11-06 08:24:38 +01:00
|
|
|
ns, DefaultAccountNum, 1,
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to derive internal addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Similarly, the internal address should match the
|
|
|
|
// prescribed addr schema for this scoped key manager.
|
2018-11-06 08:24:38 +01:00
|
|
|
if internalAddr[0].AddrType() != ScopeAddrMap[scope].InternalAddrType {
|
2018-02-14 06:35:17 +01:00
|
|
|
t.Fatalf("addr type mismatch: expected %v, got %v",
|
|
|
|
internalAddr[0].AddrType(),
|
2018-11-06 08:24:38 +01:00
|
|
|
ScopeAddrMap[scope].InternalAddrType)
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to read db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the manager is open, we'll create a "test" scope that we'll
|
|
|
|
// be utilizing for the remainder of the test.
|
2018-11-06 08:24:38 +01:00
|
|
|
testScope := KeyScope{
|
2018-02-14 06:35:17 +01:00
|
|
|
Purpose: 99,
|
|
|
|
Coin: 0,
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
addrSchema := ScopeAddrSchema{
|
|
|
|
ExternalAddrType: NestedWitnessPubKey,
|
|
|
|
InternalAddrType: WitnessPubKey,
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
var scopedMgr *ScopedKeyManager
|
2018-02-14 06:35:17 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
scopedMgr, err = mgr.NewScopedKeyManager(ns, testScope, addrSchema)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to read db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The manager was just created, we should be able to look it up within
|
|
|
|
// the root manager.
|
|
|
|
if _, err := mgr.FetchScopedKeyManager(testScope); err != nil {
|
|
|
|
t.Fatalf("attempt to read created mgr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
var externalAddr, internalAddr []ManagedAddress
|
2018-02-14 06:35:17 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
// We'll now create a new external address to ensure we
|
|
|
|
// retrieve the proper type.
|
|
|
|
externalAddr, err = scopedMgr.NextExternalAddresses(
|
2018-11-06 08:24:38 +01:00
|
|
|
ns, DefaultAccountNum, 1,
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to derive external addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
internalAddr, err = scopedMgr.NextInternalAddresses(
|
2018-11-06 08:24:38 +01:00
|
|
|
ns, DefaultAccountNum, 1,
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to derive internal addr: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the type of the address matches as expected.
|
2018-11-06 08:24:38 +01:00
|
|
|
if externalAddr[0].AddrType() != NestedWitnessPubKey {
|
2018-02-14 06:35:17 +01:00
|
|
|
t.Fatalf("addr type mismatch: expected %v, got %v",
|
2018-11-06 08:24:38 +01:00
|
|
|
NestedWitnessPubKey, externalAddr[0].AddrType())
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
_, ok := externalAddr[0].Address().(*btcutil.AddressScriptHash)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("wrong type: %T", externalAddr[0].Address())
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also create an internal address and ensure that the types
|
|
|
|
// match up properly.
|
2018-11-06 08:24:38 +01:00
|
|
|
if internalAddr[0].AddrType() != WitnessPubKey {
|
2018-02-14 06:35:17 +01:00
|
|
|
t.Fatalf("addr type mismatch: expected %v, got %v",
|
2018-11-06 08:24:38 +01:00
|
|
|
WitnessPubKey, internalAddr[0].AddrType())
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
_, ok = internalAddr[0].Address().(*btcutil.AddressWitnessPubKeyHash)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("wrong type: %T", externalAddr[0].Address())
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now simulate a restart by closing, then restarting the
|
|
|
|
// manager.
|
|
|
|
mgr.Close()
|
|
|
|
err = walletdb.View(db, func(tx walletdb.ReadTx) error {
|
|
|
|
ns := tx.ReadBucket(waddrmgrNamespaceKey)
|
|
|
|
var err error
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// We should be able to retrieve the new scoped manager that we just
|
|
|
|
// created.
|
|
|
|
scopedMgr, err = mgr.FetchScopedKeyManager(testScope)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("attempt to read created mgr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we fetch the last generated external address, it should map
|
|
|
|
// exactly to the address that we just generated.
|
2018-11-06 08:24:38 +01:00
|
|
|
var lastAddr ManagedAddress
|
2018-02-14 06:35:17 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
lastAddr, err = scopedMgr.LastExternalAddress(
|
2018-11-06 08:24:38 +01:00
|
|
|
ns, DefaultAccountNum,
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(lastAddr.AddrHash(), externalAddr[0].AddrHash()) {
|
|
|
|
t.Fatalf("mismatch addr hashes: expected %x, got %x",
|
|
|
|
externalAddr[0].AddrHash(), lastAddr.AddrHash())
|
|
|
|
}
|
|
|
|
|
|
|
|
// After the restart, all the default scopes should be been re-loaded.
|
2018-11-06 08:24:38 +01:00
|
|
|
for _, scope := range DefaultKeyScopes {
|
2018-02-14 06:35:17 +01:00
|
|
|
_, err := mgr.FetchScopedKeyManager(scope)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch scope %v: %v", scope, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, if we attempt to query the root manager for this last
|
|
|
|
// address, it should be able to locate the private key, etc.
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
_, err := mgr.Address(ns, lastAddr.Address())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to find addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mgr.MarkUsed(ns, lastAddr.Address())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to mark addr as "+
|
|
|
|
"used: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to find addr: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRootHDKeyNeutering tests that callers are unable to create new scoped
|
|
|
|
// managers once the root HD key has been deleted from the database.
|
|
|
|
func TestRootHDKeyNeutering(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-14 06:35:17 +01:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
2018-11-06 08:24:38 +01:00
|
|
|
var mgr *Manager
|
2018-02-14 06:35:17 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
err = Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, rootKey, pubPassphrase, privPassphrase,
|
2018-04-19 05:04:41 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create/open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// With the root manager open, we'll now create a new scoped manager
|
|
|
|
// for usage within this test.
|
2018-11-06 08:24:38 +01:00
|
|
|
testScope := KeyScope{
|
2018-02-14 06:35:17 +01:00
|
|
|
Purpose: 99,
|
|
|
|
Coin: 0,
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
addrSchema := ScopeAddrSchema{
|
|
|
|
ExternalAddrType: NestedWitnessPubKey,
|
|
|
|
InternalAddrType: WitnessPubKey,
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
2018-05-24 04:09:09 +02:00
|
|
|
_, err := mgr.NewScopedKeyManager(ns, testScope, addrSchema)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to read db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the manager created, we'll now neuter the root HD private key.
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
return mgr.NeuterRootKey(ns)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to read db: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we try to create *another* scope, this should fail, as the root
|
|
|
|
// key is no longer in the database.
|
2018-11-06 08:24:38 +01:00
|
|
|
testScope = KeyScope{
|
2018-02-14 06:35:17 +01:00
|
|
|
Purpose: 100,
|
|
|
|
Coin: 0,
|
|
|
|
}
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
2018-05-24 04:09:09 +02:00
|
|
|
_, err := mgr.NewScopedKeyManager(ns, testScope, addrSchema)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("new scoped manager creation should have failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNewRawAccount tests that callers are able to properly create, and use
|
|
|
|
// raw accounts created with only an account number, and not a string which is
|
|
|
|
// eventually mapped to an account number.
|
|
|
|
func TestNewRawAccount(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-02-14 06:35:17 +01:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
2018-11-06 08:24:38 +01:00
|
|
|
var mgr *Manager
|
2018-02-14 06:35:17 +01:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-06 08:24:38 +01:00
|
|
|
err = Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, rootKey, pubPassphrase, privPassphrase,
|
2018-04-19 05:04:41 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
2018-02-14 06:35:17 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create/open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// Now that we have the manager created, we'll fetch one of the default
|
|
|
|
// scopes for usage within this test.
|
2018-11-06 08:24:38 +01:00
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0084)
|
2018-02-14 06:35:17 +01:00
|
|
|
if err != nil {
|
2018-11-06 08:24:38 +01:00
|
|
|
t.Fatalf("unable to fetch scope %v: %v", KeyScopeBIP0084, err)
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// With the scoped manager retrieved, we'll attempt to create a new raw
|
|
|
|
// account by number.
|
|
|
|
const accountNum = 1000
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
return scopedMgr.NewRawAccount(ns, accountNum)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new account: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
testNewRawAccount(t, mgr, db, accountNum, scopedMgr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNewRawAccountWatchingOnly tests that callers are able to
|
|
|
|
// properly create, and use watching-only raw accounts created with
|
|
|
|
// only an account number, and not a string which is eventually mapped
|
|
|
|
// to an account number.
|
|
|
|
func TestNewRawAccountWatchingOnly(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
|
|
|
var mgr *Manager
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = Create(
|
|
|
|
ns, nil, pubPassphrase, nil,
|
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = mgr.NewScopedKeyManager(
|
|
|
|
ns, KeyScopeBIP0044, ScopeAddrMap[KeyScopeBIP0044])
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create/open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// Now that we have the manager created, we'll fetch one of the default
|
|
|
|
// scopes for usage within this test.
|
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch scope %v: %v", KeyScopeBIP0044, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey := deriveTestAccountKey(t)
|
|
|
|
if accountKey == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the scoped manager retrieved, we'll attempt to create a new raw
|
|
|
|
// account by number.
|
|
|
|
const accountNum = 1000
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2021-02-15 23:38:23 +01:00
|
|
|
return scopedMgr.NewRawAccountWatchingOnly(
|
|
|
|
ns, accountNum, accountKey, 0, nil,
|
|
|
|
)
|
2020-04-25 02:44:21 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new account: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testNewRawAccount(t, mgr, db, accountNum, scopedMgr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestNewRawAccountHybrid is similar to TestNewRawAccountWatchingOnly
|
|
|
|
// except that the manager is created normally with a seed. This test
|
|
|
|
// shows that watch-only accounts can be added to managers with
|
|
|
|
// non-watch-only accounts.
|
|
|
|
func TestNewRawAccountHybrid(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-04-25 02:44:21 +02:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
|
|
|
var mgr *Manager
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = Create(
|
2020-09-26 12:14:09 +02:00
|
|
|
ns, rootKey, pubPassphrase, privPassphrase,
|
2020-04-25 02:44:21 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("create/open: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// Now that we have the manager created, we'll fetch one of the default
|
|
|
|
// scopes for usage within this test.
|
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to fetch scope %v: %v", KeyScopeBIP0044, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
accountKey := deriveTestAccountKey(t)
|
|
|
|
if accountKey == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
acctKeyPub, err := accountKey.Neuter()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Neuter: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// With the scoped manager retrieved, we'll attempt to create a new raw
|
|
|
|
// account by number.
|
|
|
|
const accountNum = 1000
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
2021-02-15 23:38:23 +01:00
|
|
|
return scopedMgr.NewRawAccountWatchingOnly(
|
|
|
|
ns, accountNum, acctKeyPub, 0, nil,
|
|
|
|
)
|
2020-04-25 02:44:21 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create new account: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testNewRawAccount(t, mgr, db, accountNum, scopedMgr)
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:43:24 +01:00
|
|
|
func testNewRawAccount(t *testing.T, _ *Manager, db walletdb.DB,
|
2020-04-25 02:44:21 +02:00
|
|
|
accountNum uint32, scopedMgr *ScopedKeyManager) {
|
2018-02-14 06:35:17 +01:00
|
|
|
// With the account created, we should be able to derive new addresses
|
|
|
|
// from the account.
|
2018-11-06 08:24:38 +01:00
|
|
|
var accountAddrNext ManagedAddress
|
2020-04-25 02:44:21 +02:00
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
2018-02-14 06:35:17 +01:00
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
addrs, err := scopedMgr.NextExternalAddresses(
|
|
|
|
ns, accountNum, 1,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountAddrNext = addrs[0]
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionally, we should be able to manually derive specific target
|
|
|
|
// keys.
|
2018-11-06 08:24:38 +01:00
|
|
|
var accountTargetAddr ManagedAddress
|
2018-02-14 06:35:17 +01:00
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
2018-11-06 08:24:38 +01:00
|
|
|
keyPath := DerivationPath{
|
2021-02-17 02:01:12 +01:00
|
|
|
InternalAccount: accountNum,
|
|
|
|
Account: hdkeychain.HardenedKeyStart,
|
|
|
|
Branch: 0,
|
|
|
|
Index: 0,
|
2018-02-14 06:35:17 +01:00
|
|
|
}
|
|
|
|
accountTargetAddr, err = scopedMgr.DeriveFromKeyPath(
|
|
|
|
ns, keyPath,
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to derive addr: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The two keys we just derived should match up perfectly.
|
|
|
|
if accountAddrNext.AddrType() != accountTargetAddr.AddrType() {
|
|
|
|
t.Fatalf("wrong addr type: %v vs %v",
|
|
|
|
accountAddrNext.AddrType(), accountTargetAddr.AddrType())
|
|
|
|
}
|
|
|
|
if !bytes.Equal(accountAddrNext.AddrHash(), accountTargetAddr.AddrHash()) {
|
|
|
|
t.Fatalf("wrong pubkey hash: %x vs %x", accountAddrNext.AddrHash(),
|
|
|
|
accountTargetAddr.AddrHash())
|
|
|
|
}
|
|
|
|
}
|
2021-08-14 01:13:06 +02:00
|
|
|
|
|
|
|
// TestDeriveFromKeyPathCache tests that the DeriveFromKeyPathCache method will
|
|
|
|
// properly cache items in the cache, and return corresponding errors if the
|
|
|
|
// account isn't properly cached.
|
|
|
|
func TestDeriveFromKeyPathCache(t *testing.T) {
|
|
|
|
|
|
|
|
teardown, db := emptyDB(t)
|
|
|
|
defer teardown()
|
|
|
|
|
2022-07-06 07:08:22 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-08-14 01:13:06 +02:00
|
|
|
// We'll start the test by creating a new root manager that will be
|
|
|
|
// used for the duration of the test.
|
|
|
|
var mgr *Manager
|
|
|
|
err := walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns, err := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = Create(
|
2021-08-26 10:09:08 +02:00
|
|
|
ns, rootKey, pubPassphrase, privPassphrase,
|
2021-08-14 01:13:06 +02:00
|
|
|
&chaincfg.MainNetParams, fastScrypt, time.Time{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.Unlock(ns, privPassphrase)
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "create/open: unexpected error: %v", err)
|
|
|
|
|
|
|
|
defer mgr.Close()
|
|
|
|
|
|
|
|
// Now that we have the manager created, we'll fetch one of the default
|
|
|
|
// scopes for usage within this test.
|
|
|
|
scopedMgr, err := mgr.FetchScopedKeyManager(KeyScopeBIP0044)
|
|
|
|
require.NoError(
|
|
|
|
t, err, "unable to fetch scope %v: %v", KeyScopeBIP0044, err,
|
|
|
|
)
|
|
|
|
|
|
|
|
keyPath := DerivationPath{
|
|
|
|
InternalAccount: 0,
|
|
|
|
Account: hdkeychain.HardenedKeyStart,
|
|
|
|
Branch: 10,
|
|
|
|
Index: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our test starts here, we'll attempt to derive a new key using the
|
|
|
|
// cached method. This should fail at first since the account itself
|
|
|
|
// isn't cached.
|
|
|
|
_, err = scopedMgr.DeriveFromKeyPathCache(keyPath)
|
|
|
|
if !IsError(err, ErrAccountNotCached) {
|
|
|
|
t.Fatalf("didn't get account not cached error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we'll attempt to derive the key using the normal method that
|
|
|
|
// requires a database transaction.
|
|
|
|
var derivedKey *btcec.PrivateKey
|
|
|
|
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
|
|
|
|
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
|
|
|
|
|
|
|
|
managedAddr, err := scopedMgr.DeriveFromKeyPath(
|
|
|
|
ns, keyPath,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
derivedKey, err = managedAddr.(ManagedPubKeyAddress).PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err, "unable to derive addr: %v", err)
|
|
|
|
|
|
|
|
// Next attempt to read the key again from the cache, it should succeed
|
|
|
|
// this time.
|
|
|
|
cachedKey, err := scopedMgr.DeriveFromKeyPathCache(keyPath)
|
|
|
|
require.NoError(t, err, "account wasn't cached")
|
|
|
|
|
|
|
|
// We should be able to read the key again.
|
|
|
|
cachedKey2, err := scopedMgr.DeriveFromKeyPathCache(keyPath)
|
|
|
|
require.NoError(t, err, "account wasn't cached")
|
|
|
|
|
|
|
|
// All three keys we have now should match exactly.
|
|
|
|
require.Equal(t, cachedKey.Serialize(), cachedKey2.Serialize())
|
|
|
|
require.Equal(t, derivedKey.Serialize(), cachedKey2.Serialize())
|
|
|
|
}
|