wallet/test: extract testWallet generation into separate function
This commit is contained in:
parent
3809a6d553
commit
3465d2ecc6
2 changed files with 49 additions and 29 deletions
|
@ -6,16 +6,12 @@ package wallet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/txscript"
|
"github.com/btcsuite/btcd/txscript"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/btcsuite/btcutil/hdkeychain"
|
|
||||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||||
"github.com/btcsuite/btcwallet/walletdb"
|
"github.com/btcsuite/btcwallet/walletdb"
|
||||||
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
||||||
|
@ -26,31 +22,8 @@ import (
|
||||||
// request a dry run of the txToOutputs call. It also makes sure a subsequent
|
// request a dry run of the txToOutputs call. It also makes sure a subsequent
|
||||||
// non-dry run call produces a similar transaction to the dry-run.
|
// non-dry run call produces a similar transaction to the dry-run.
|
||||||
func TestTxToOutputsDryRun(t *testing.T) {
|
func TestTxToOutputsDryRun(t *testing.T) {
|
||||||
// Set up a wallet.
|
w, cleanup := testWallet(t)
|
||||||
dir, err := ioutil.TempDir("", "createtx_test")
|
defer cleanup()
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create db dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(dir)
|
|
||||||
|
|
||||||
seed, err := hdkeychain.GenerateSeed(hdkeychain.MinSeedBytes)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create seed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pubPass := []byte("hello")
|
|
||||||
privPass := []byte("world")
|
|
||||||
|
|
||||||
loader := NewLoader(&chaincfg.TestNet3Params, dir, true, 250)
|
|
||||||
w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create wallet: %v", err)
|
|
||||||
}
|
|
||||||
chainClient := &mockChainClient{}
|
|
||||||
w.chainClient = chainClient
|
|
||||||
if err := w.Unlock(privPass, time.After(10*time.Minute)); err != nil {
|
|
||||||
t.Fatalf("unable to unlock wallet: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an address we can use to send some coins to.
|
// Create an address we can use to send some coins to.
|
||||||
addr, err := w.CurrentAddress(0, waddrmgr.KeyScopeBIP0044)
|
addr, err := w.CurrentAddress(0, waddrmgr.KeyScopeBIP0044)
|
||||||
|
|
47
wallet/example_test.go
Normal file
47
wallet/example_test.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package wallet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/btcsuite/btcutil/hdkeychain"
|
||||||
|
)
|
||||||
|
|
||||||
|
// testWallet creates a test wallet and unlocks it.
|
||||||
|
func testWallet(t *testing.T) (*Wallet, func()) {
|
||||||
|
// Set up a wallet.
|
||||||
|
dir, err := ioutil.TempDir("", "test_wallet")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to create db dir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup := func() {
|
||||||
|
if err := os.RemoveAll(dir); err != nil {
|
||||||
|
t.Fatalf("could not cleanup test: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
seed, err := hdkeychain.GenerateSeed(hdkeychain.MinSeedBytes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create seed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pubPass := []byte("hello")
|
||||||
|
privPass := []byte("world")
|
||||||
|
|
||||||
|
loader := NewLoader(&chaincfg.TestNet3Params, dir, true, 250)
|
||||||
|
w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create wallet: %v", err)
|
||||||
|
}
|
||||||
|
chainClient := &mockChainClient{}
|
||||||
|
w.chainClient = chainClient
|
||||||
|
if err := w.Unlock(privPass, time.After(10*time.Minute)); err != nil {
|
||||||
|
t.Fatalf("unable to unlock wallet: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w, cleanup
|
||||||
|
}
|
Loading…
Reference in a new issue