2020-04-30 09:08:12 +02:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-08-26 00:58:28 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcutil/hdkeychain"
|
2020-04-30 09:08:12 +02:00
|
|
|
)
|
|
|
|
|
2020-08-11 21:53:04 +02:00
|
|
|
// defaultDBTimeout specifies the timeout value when opening the wallet
|
|
|
|
// database.
|
|
|
|
var defaultDBTimeout = 10 * time.Second
|
|
|
|
|
2020-04-30 09:08:12 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:16:15 +02:00
|
|
|
passphrase := []byte("hello world")
|
2020-04-30 09:08:12 +02:00
|
|
|
|
2020-08-11 21:53:04 +02:00
|
|
|
loader := NewLoader(
|
|
|
|
&chaincfg.TestNet3Params, dir, true, defaultDBTimeout, 250,
|
|
|
|
)
|
2022-09-29 00:16:15 +02:00
|
|
|
w, err := loader.CreateNewWallet(passphrase, seed, time.Now())
|
2020-04-30 09:08:12 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create wallet: %v", err)
|
|
|
|
}
|
|
|
|
chainClient := &mockChainClient{}
|
|
|
|
w.chainClient = chainClient
|
2022-09-29 00:16:15 +02:00
|
|
|
if err := w.Unlock(passphrase, time.After(10*time.Minute)); err != nil {
|
2020-04-30 09:08:12 +02:00
|
|
|
t.Fatalf("unable to unlock wallet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, cleanup
|
|
|
|
}
|