2015-12-01 19:44:58 +01:00
|
|
|
// Copyright (c) 2014-2015 The btcsuite developers
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-10-29 07:43:29 +01:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-01-17 01:19:02 +01:00
|
|
|
"time"
|
2014-10-29 07:43:29 +01:00
|
|
|
|
2021-08-26 00:58:28 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
|
|
|
"github.com/lbryio/lbcwallet/internal/prompt"
|
|
|
|
"github.com/lbryio/lbcwallet/wallet"
|
|
|
|
"github.com/lbryio/lbcwallet/walletdb"
|
|
|
|
_ "github.com/lbryio/lbcwallet/walletdb/bdb"
|
2014-10-29 07:43:29 +01:00
|
|
|
)
|
|
|
|
|
2015-04-02 20:13:38 +02:00
|
|
|
// networkDir returns the directory name of a network directory to hold wallet
|
|
|
|
// files.
|
|
|
|
func networkDir(dataDir string, chainParams *chaincfg.Params) string {
|
|
|
|
netname := chainParams.Name
|
|
|
|
|
|
|
|
// For now, we must always name the testnet data directory as "testnet"
|
|
|
|
// and not "testnet3" or any other version, as the chaincfg testnet3
|
2021-03-24 14:43:24 +01:00
|
|
|
// parameters will likely be switched to being named "testnet3" in the
|
2015-04-02 20:13:38 +02:00
|
|
|
// future. This is done to future proof that change, and an upgrade
|
|
|
|
// plan to move the testnet3 data directory can be worked out later.
|
|
|
|
if chainParams.Net == wire.TestNet3 {
|
|
|
|
netname = "testnet"
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(dataDir, netname)
|
|
|
|
}
|
|
|
|
|
2014-10-29 07:43:29 +01:00
|
|
|
// createWallet prompts the user for information needed to generate a new wallet
|
|
|
|
// and generates the wallet accordingly. The new wallet will reside at the
|
|
|
|
// provided path.
|
|
|
|
func createWallet(cfg *config) error {
|
2016-05-05 16:29:29 +02:00
|
|
|
dbDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
|
2020-08-11 21:53:44 +02:00
|
|
|
loader := wallet.NewLoader(
|
|
|
|
activeNet.Params, dbDir, true, cfg.DBTimeout, 250,
|
|
|
|
)
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
|
2022-09-29 00:16:15 +02:00
|
|
|
// Start by prompting for the passphrase.
|
2014-10-29 07:43:29 +01:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
2022-09-29 00:16:15 +02:00
|
|
|
privPass, err := prompt.Passphrase(reader)
|
2014-10-29 07:43:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ascertain the wallet generation seed. This will either be an
|
|
|
|
// automatically generated value the user has already confirmed or a
|
|
|
|
// value the user has entered which has already been validated.
|
2022-08-20 01:16:46 +02:00
|
|
|
seed, bday, err := prompt.Seed(reader)
|
2014-10-29 07:43:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Creating the wallet...")
|
2022-09-29 00:16:15 +02:00
|
|
|
w, err := loader.CreateNewWallet(privPass, seed, bday)
|
2014-10-29 07:43:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-01 19:15:32 +02:00
|
|
|
|
Modernize the RPC server.
This is a rather monolithic commit that moves the old RPC server to
its own package (rpc/legacyrpc), introduces a new RPC server using
gRPC (rpc/rpcserver), and provides the ability to defer wallet loading
until request at a later time by an RPC (--noinitialload).
The legacy RPC server remains the default for now while the new gRPC
server is not enabled by default. Enabling the new server requires
setting a listen address (--experimenalrpclisten). This experimental
flag is used to effectively feature gate the server until it is ready
to use as a default. Both RPC servers can be run at the same time,
but require binding to different listen addresses.
In theory, with the legacy RPC server now living in its own package it
should become much easier to unit test the handlers. This will be
useful for any future changes to the package, as compatibility with
Core's wallet is still desired.
Type safety has also been improved in the legacy RPC server. Multiple
handler types are now used for methods that do and do not require the
RPC client as a dependency. This can statically help prevent nil
pointer dereferences, and was very useful for catching bugs during
refactoring.
To synchronize the wallet loading process between the main package
(the default) and through the gRPC WalletLoader service (with the
--noinitialload option), as well as increasing the loose coupling of
packages, a new wallet.Loader type has been added. All creating and
loading of existing wallets is done through a single Loader instance,
and callbacks can be attached to the instance to run after the wallet
has been opened. This is how the legacy RPC server is associated with
a loaded wallet, even after the wallet is loaded by a gRPC method in a
completely unrelated package.
Documentation for the new RPC server has been added to the
rpc/documentation directory. The documentation includes a
specification for the new RPC API, addresses how to make changes to
the server implementation, and provides short example clients in
several different languages.
Some of the new RPC methods are not implementated exactly as described
by the specification. These are considered bugs with the
implementation, not the spec. Known bugs are commented as such.
2015-06-01 21:57:50 +02:00
|
|
|
w.Manager.Close()
|
2022-08-20 01:16:46 +02:00
|
|
|
|
|
|
|
fmt.Println("The wallet has been created successfully with birthday:", bday.Format(time.UnixDate))
|
|
|
|
|
2014-10-29 07:43:29 +01:00
|
|
|
return nil
|
|
|
|
}
|
2015-03-04 18:48:34 +01:00
|
|
|
|
|
|
|
// createSimulationWallet is intended to be called from the rpcclient
|
|
|
|
// and used to create a wallet for actors involved in simulations.
|
|
|
|
func createSimulationWallet(cfg *config) error {
|
|
|
|
// Simulation wallet password is 'password'.
|
|
|
|
privPass := []byte("password")
|
|
|
|
|
2016-05-05 16:29:29 +02:00
|
|
|
netDir := networkDir(cfg.AppDataDir.Value, activeNet.Params)
|
2015-03-04 18:48:34 +01:00
|
|
|
|
|
|
|
// Create the wallet.
|
2020-11-06 17:51:23 +01:00
|
|
|
dbPath := filepath.Join(netDir, wallet.WalletDBName)
|
2015-03-04 18:48:34 +01:00
|
|
|
fmt.Println("Creating the wallet...")
|
|
|
|
|
|
|
|
// Create the wallet database backed by bolt db.
|
2020-08-11 21:53:44 +02:00
|
|
|
db, err := walletdb.Create("bdb", dbPath, true, cfg.DBTimeout)
|
2015-03-04 18:48:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2016-03-17 15:05:11 +01:00
|
|
|
// Create the wallet.
|
2022-09-29 00:16:15 +02:00
|
|
|
err = wallet.Create(db, privPass, nil, activeNet.Params, time.Now())
|
2015-03-04 18:48:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("The wallet has been created successfully.")
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-02 20:13:38 +02:00
|
|
|
|
2015-04-06 21:03:24 +02:00
|
|
|
// checkCreateDir checks that the path exists and is a directory.
|
|
|
|
// If path does not exist, it is created.
|
|
|
|
func checkCreateDir(path string) error {
|
|
|
|
if fi, err := os.Stat(path); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Attempt data directory creation
|
|
|
|
if err = os.MkdirAll(path, 0700); err != nil {
|
|
|
|
return fmt.Errorf("cannot create directory: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("error checking directory: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return fmt.Errorf("path '%s' is not a directory", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|