common_test.go raw

   1  package waddrmgr_test
   2  
   3  import (
   4  	"encoding/hex"
   5  	"io/ioutil"
   6  	"os"
   7  	"path/filepath"
   8  	"testing"
   9  	"time"
  10  	
  11  	"github.com/p9c/p9/pkg/addrmgr"
  12  	"github.com/p9c/p9/pkg/chaincfg"
  13  	"github.com/p9c/p9/pkg/waddrmgr"
  14  	"github.com/p9c/p9/pkg/walletdb"
  15  	_ "github.com/p9c/p9/pkg/walletdb/bdb"
  16  )
  17  
  18  var (
  19  	// seed is the master seed used throughout the tests.
  20  	seed = []byte{
  21  		0x2a, 0x64, 0xdf, 0x08, 0x5e, 0xef, 0xed, 0xd8, 0xbf,
  22  		0xdb, 0xb3, 0x31, 0x76, 0xb5, 0xba, 0x2e, 0x62, 0xe8,
  23  		0xbe, 0x8b, 0x56, 0xc8, 0x83, 0x77, 0x95, 0x59, 0x8b,
  24  		0xb6, 0xc4, 0x40, 0xc0, 0x64,
  25  	}
  26  	pubPassphrase   = []byte("_DJr{fL4H0O}*-0\n:V1izc)(6BomK")
  27  	privPassphrase  = []byte("81lUHXnOMZ@?XXd7O9xyDIWIbXX-lj")
  28  	pubPassphrase2  = []byte("-0NV4P~VSJBWbunw}%<Z]fuGpbN[ZI")
  29  	privPassphrase2 = []byte("~{<]08%6!-?2s<$(8$8:f(5[4/!/{Y")
  30  	// fastScrypt are parameters used throughout the tests to speed up the
  31  	// scrypt operations.
  32  	fastScrypt = &waddrmgr.ScryptOptions{
  33  		N: 16,
  34  		R: 8,
  35  		P: 1,
  36  	}
  37  	// waddrmgrNamespaceKey is the namespace key for the waddrmgr package.
  38  	waddrmgrNamespaceKey = []byte("waddrmgrNamespace")
  39  	// expectedAddrs is the list of all expected addresses generated from the seed.
  40  	expectedAddrs = []expectedAddr{
  41  		{
  42  			address:     "14wtcepMNiEazuN7YosWY8bwD9tcCtxXRB",
  43  			addressHash: hexToBytes("2b49ecd0cf72006173e6e95acf416b6735b5f889"),
  44  			internal:    false,
  45  			compressed:  true,
  46  			imported:    false,
  47  			pubKey:      hexToBytes("02d8f88468c5a2e8e1815faf555f59cbd1979e3dbdf823f80c271b6fb70d2d519b"),
  48  			privKey:     hexToBytes("c27d6581b92785834b381fa697c4b0ffc4574b495743722e0acb7601b1b68b99"),
  49  			privKeyWIF:  "L3jmpy54Pc7MLXTN2mL8Xas7BJziwKaUGmgnXXzgGbVRdiAniXZk",
  50  			derivationInfo: waddrmgr.DerivationPath{
  51  				Account: 0,
  52  				Branch:  0,
  53  				Index:   0,
  54  			},
  55  		},
  56  		{
  57  			address:     "1N3D8jy2aQuUsKBsDgZ6ZPTVR9VhHgJYpE",
  58  			addressHash: hexToBytes("e6c59a1542138d1bf08f45cd18899557cf56b356"),
  59  			internal:    false,
  60  			compressed:  true,
  61  			imported:    false,
  62  			pubKey:      hexToBytes("02b9c175b908624f8a8eaac227d0e8c77c0eec327b8c512ad1b8b7a4b5b676971f"),
  63  			privKey:     hexToBytes("18f3b191019e83878a81557abebb2afda199e31d22e150d8bf4df4561671be6c"),
  64  			privKeyWIF:  "Kx4DNid19W8sjNFN3uPqQE7UYnCqyEp7unCvdkf2LrVUFpnDtwpB",
  65  			derivationInfo: waddrmgr.DerivationPath{
  66  				Account: 0,
  67  				Branch:  0,
  68  				Index:   1,
  69  			},
  70  		},
  71  		{
  72  			address:     "1VTfwD4iHre2bMrR9qGiJMwoiZGQZ8e6s",
  73  			addressHash: hexToBytes("0561e9373986965b647a57a09718e9c050215cfe"),
  74  			internal:    false,
  75  			compressed:  true,
  76  			imported:    false,
  77  			pubKey:      hexToBytes("0329faddf1254d490d6add49e2b08cf52b561038c72baec0edb3cfacff71ff1021"),
  78  			privKey:     hexToBytes("ccb8f6305b73136b363644b647f6efc0fd27b6b7d9c11c7e560662ed38db7b34"),
  79  			privKeyWIF:  "L45fWF6Yd736fDohuB97vwRRLdQQJr3ZGvbokk9ubiT7aNrg7tTn",
  80  			derivationInfo: waddrmgr.DerivationPath{
  81  				Account: 0,
  82  				Branch:  0,
  83  				Index:   2,
  84  			},
  85  		},
  86  		{
  87  			address:     "13TdEj4ehUuYFiSaB47eLVBwM2XhAhrK2J",
  88  			addressHash: hexToBytes("1af950be02584ca230b7078cec0cfd38dd71b468"),
  89  			internal:    false,
  90  			compressed:  true,
  91  			imported:    false,
  92  			pubKey:      hexToBytes("03d738324e2f0ce42e46975d7f8c7117c1670e3d7912b0291aea452add99674774"),
  93  			privKey:     hexToBytes("d6bc8ff768814fede2adcdb74826bd846924341b3862e3b6e31cdc084e992940"),
  94  			privKeyWIF:  "L4R8XyxYQyPSpTwj8w96tM86a6j3QA9jbRPj3RA7DVTVWk71ndeP",
  95  			derivationInfo: waddrmgr.DerivationPath{
  96  				Account: 0,
  97  				Branch:  0,
  98  				Index:   3,
  99  			},
 100  		},
 101  		{
 102  			address:     "1LTjSghkBecT59VjEKke331HxVdqcFwUDa",
 103  			addressHash: hexToBytes("d578a267a7174c6ba7f76b0ab2397ce0ba0c5c3c"),
 104  			internal:    false,
 105  			compressed:  true,
 106  			imported:    false,
 107  			pubKey:      hexToBytes("03a917acd5cd5b6f544b43f1921a35677e4d5320e5d2add2056039b4b44fdf905e"),
 108  			privKey:     hexToBytes("8563ade061110e03aee50695ffc5cb1c06c8310bde0a3674257c853c966968c0"),
 109  			privKeyWIF:  "L1h16Hunxomww4FrpyQP2iFmWNgG7U1u3awp6Vd3s2uGf7v5VU8c",
 110  			derivationInfo: waddrmgr.DerivationPath{
 111  				Account: 0,
 112  				Branch:  0,
 113  				Index:   4,
 114  			},
 115  		},
 116  		{
 117  			address:     "15HNivzKhsLaMs1qRdQN1ifoJYUnJ2xW9z",
 118  			addressHash: hexToBytes("2ef94abb9ee8f785d087c3ec8d6ee467e92d0d0a"),
 119  			internal:    true,
 120  			compressed:  true,
 121  			imported:    false,
 122  			pubKey:      hexToBytes("020a1290b997c0a234a95213962e7edcb761c7360f0230f698a1a3e71c37047bb0"),
 123  			privKey:     hexToBytes("fe4f855fcf059ec6ddf7b25f63b19aa49c771d1fcb9850b68ae3d65e20657a60"),
 124  			privKeyWIF:  "L5k4HivqXvohxBMpuwD38iUgi6uewffwZny91ZNYfM39RXH2x3QR",
 125  			derivationInfo: waddrmgr.DerivationPath{
 126  				Account: 0,
 127  				Branch:  1,
 128  				Index:   0,
 129  			},
 130  		},
 131  		{
 132  			address:     "1LJpGrAP1vWHuvfHqmUutQqFVYca2qwxhy",
 133  			addressHash: hexToBytes("d3c8ec46891f599bfeaa4c25918bfb3d46ea334c"),
 134  			internal:    true,
 135  			compressed:  true,
 136  			imported:    false,
 137  			pubKey:      hexToBytes("03f79bbde32af42dde98195f011d95982602fcd0dab657fe4a1f49f9d5ada1e02d"),
 138  			privKey:     hexToBytes("bfef521317c65b018ae7e6d7ecc3aa700d5d0f7ea84d567be9270382d0b5e3e6"),
 139  			privKeyWIF:  "L3eomUajnTDM3Pc8GU47qqXUFuCjvpqY7NYN9mH3x1ZFjDgiY4BU",
 140  			derivationInfo: waddrmgr.DerivationPath{
 141  				Account: 0,
 142  				Branch:  1,
 143  				Index:   1,
 144  			},
 145  		},
 146  		{
 147  			address:     "13NhXy2nCLMwNug1TZ6uwaWnxp3uTqdDQq",
 148  			addressHash: hexToBytes("1a0ad2a04fde3b2afe068057591e1871c289c4b8"),
 149  			internal:    true,
 150  			compressed:  true,
 151  			imported:    false,
 152  			pubKey:      hexToBytes("023ded84afe4fe91b52b45c3deb26fd263f749cbc27747dc964dae9e0739cbc579"),
 153  			privKey:     hexToBytes("f506dffd4494c24006df7a35f3291f7ca0297a1a431557a1339bfed6f48738ca"),
 154  			privKeyWIF:  "L5S1bVQUPqQb1Su82fLoSpnGCjcPfdAQE1pJxWRopJSBdYNDHESv",
 155  			derivationInfo: waddrmgr.DerivationPath{
 156  				Account: 0,
 157  				Branch:  1,
 158  				Index:   2,
 159  			},
 160  		},
 161  		{
 162  			address:     "1AY6yAHvojvpFcevAichLMnJfxgE8eSe4N",
 163  			addressHash: hexToBytes("689b0249c628265215fd1de6142d5d5594eb8dc2"),
 164  			internal:    true,
 165  			compressed:  true,
 166  			imported:    false,
 167  			pubKey:      hexToBytes("030f1e79f06824e10a259914ec310528bb2d5b8d6356341fe9dff55498591af6af"),
 168  			privKey:     hexToBytes("b3629de8ef6a275b4ffae41aa2bbbc2952eb92282ea6402435abbb010ecc1fb8"),
 169  			privKeyWIF:  "L3EQsGeEnyXmKaux54cG4DQeCSQDvGuvEuy3W2ss4geum7AtWaHw",
 170  			derivationInfo: waddrmgr.DerivationPath{
 171  				Account: 0,
 172  				Branch:  1,
 173  				Index:   3,
 174  			},
 175  		},
 176  		{
 177  			address:     "1Jc7An3JqjzRQULVr6Wh3iYR7miB6WPJCD",
 178  			addressHash: hexToBytes("c11dd8a3577978807a0453febedee2994a6144d4"),
 179  			internal:    true,
 180  			compressed:  true,
 181  			imported:    false,
 182  			pubKey:      hexToBytes("0317d7182e26b6ca3e0f3db531c474b9cab7a763a75eabff2e14ac92f62a793238"),
 183  			privKey:     hexToBytes("ca747a7ef815ea0dbe68655272cecbfbd65f2a109019a9ed28e0d3dcaffe05c3"),
 184  			privKeyWIF:  "L41Frac75RPbTELKzw1EGC2qCkdveiVumpmsyX4daAvyyCMxit1W",
 185  			derivationInfo: waddrmgr.DerivationPath{
 186  				Account: 0,
 187  				Branch:  1,
 188  				Index:   4,
 189  			},
 190  		},
 191  	}
 192  	// expectedExternalAddrs is the list of expected external addresses generated
 193  	// from the seed
 194  	expectedExternalAddrs = expectedAddrs[:5]
 195  	// expectedInternalAddrs is the list of expected internal addresses generated
 196  	// from the seed
 197  	expectedInternalAddrs = expectedAddrs[5:]
 198  )
 199  
 200  // checkManagerError ensures the passed error is a ManagerError with an error
 201  // code that matches the passed error code.
 202  func checkManagerError(t *testing.T, testName string, gotErr error, wantErrCode waddrmgr.ErrorCode) bool {
 203  	merr, ok := gotErr.(waddrmgr.ManagerError)
 204  	if !ok {
 205  		t.Errorf("%s: unexpected error type - got %T, want %T",
 206  			testName, gotErr, waddrmgr.ManagerError{},
 207  		)
 208  		return false
 209  	}
 210  	if merr.ErrorCode != wantErrCode {
 211  		t.Errorf("%s: unexpected error code - got %s (%s), want %s",
 212  			testName, merr.ErrorCode, merr.Description, wantErrCode,
 213  		)
 214  		return false
 215  	}
 216  	return true
 217  }
 218  
 219  // hexToBytes is a wrapper around hex.DecodeString that panics if there is an
 220  // error. It MUST only be used with hard coded values in the tests.
 221  func hexToBytes(origHex string) []byte {
 222  	buf, e := hex.DecodeString(origHex)
 223  	if e != nil {
 224  		panic(e)
 225  	}
 226  	return buf
 227  }
 228  func emptyDB(t *testing.T) (tearDownFunc func(), db walletdb.DB) {
 229  	var dirName string
 230  	var e error
 231  	if dirName, e = ioutil.TempDir("", "mgrtest"); addrmgr.E.Chk(e) {
 232  		t.Fatalf("Failed to create db temp dir: %v", e)
 233  	}
 234  	dbPath := filepath.Join(dirName, "mgrtest.db")
 235  	if db, e = walletdb.Create("bdb", dbPath); addrmgr.E.Chk(e) {
 236  		_ = os.RemoveAll(dirName)
 237  		t.Fatalf("createDbNamespace: unexpected error: %v", e)
 238  	}
 239  	tearDownFunc = func() {
 240  		if e := db.Close(); addrmgr.E.Chk(e) {
 241  		}
 242  		if e := os.RemoveAll(dirName); addrmgr.E.Chk(e) {
 243  		}
 244  	}
 245  	return
 246  }
 247  
 248  // setupManager creates a new address manager and returns a teardown function
 249  // that should be invoked to ensure it is closed and removed upon completion.
 250  func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *waddrmgr.Manager) {
 251  	// Create a new manager in a temp directory.
 252  	dirName, e := ioutil.TempDir("", "mgrtest")
 253  	if e != nil {
 254  		t.Fatalf("Failed to create db temp dir: %v", e)
 255  	}
 256  	dbPath := filepath.Join(dirName, "mgrtest.db")
 257  	db, e = walletdb.Create("bdb", dbPath)
 258  	if e != nil {
 259  		_ = os.RemoveAll(dirName)
 260  		t.Fatalf("createDbNamespace: unexpected error: %v", e)
 261  	}
 262  	e = walletdb.Update(db, func(tx walletdb.ReadWriteTx) (e error) {
 263  		ns, e := tx.CreateTopLevelBucket(waddrmgrNamespaceKey)
 264  		if e != nil {
 265  			return e
 266  		}
 267  		e = waddrmgr.Create(
 268  			ns, seed, pubPassphrase, privPassphrase,
 269  			&chaincfg.MainNetParams, fastScrypt, time.Time{},
 270  		)
 271  		if e != nil {
 272  			return e
 273  		}
 274  		mgr, e = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams)
 275  		return e
 276  	},
 277  	)
 278  	if e != nil {
 279  		func() {
 280  			if e = db.Close(); addrmgr.E.Chk(e) {
 281  			}
 282  		}()
 283  		_ = os.RemoveAll(dirName)
 284  		t.Fatalf("Failed to create Manager: %v", e)
 285  	}
 286  	tearDownFunc = func() {
 287  		mgr.Close()
 288  		if e := db.Close(); addrmgr.E.Chk(e) {
 289  		}
 290  		_ = os.RemoveAll(dirName)
 291  	}
 292  	return tearDownFunc, db, mgr
 293  }
 294