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"
|
|
|
|
|
2014-10-01 19:15:32 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2015-04-02 20:13:38 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2014-10-01 19:15:32 +02:00
|
|
|
"github.com/btcsuite/btcutil"
|
2015-05-27 16:09:22 +02:00
|
|
|
"github.com/btcsuite/btcwallet/internal/legacy/keystore"
|
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
|
|
|
"github.com/btcsuite/btcwallet/internal/prompt"
|
2014-10-29 07:43:29 +01:00
|
|
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
2015-04-02 20:13:38 +02:00
|
|
|
"github.com/btcsuite/btcwallet/wallet"
|
2014-10-29 07:43:29 +01:00
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
// paramaters will likely be switched to being named "testnet3" in the
|
|
|
|
// 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-01 19:15:32 +02:00
|
|
|
// convertLegacyKeystore converts all of the addresses in the passed legacy
|
|
|
|
// key store to the new waddrmgr.Manager format. Both the legacy keystore and
|
|
|
|
// the new manager must be unlocked.
|
|
|
|
func convertLegacyKeystore(legacyKeyStore *keystore.Store, manager *waddrmgr.Manager) error {
|
|
|
|
netParams := legacyKeyStore.Net()
|
|
|
|
blockStamp := waddrmgr.BlockStamp{
|
|
|
|
Height: 0,
|
|
|
|
Hash: *netParams.GenesisHash,
|
|
|
|
}
|
|
|
|
for _, walletAddr := range legacyKeyStore.ActiveAddresses() {
|
|
|
|
switch addr := walletAddr.(type) {
|
|
|
|
case keystore.PubKeyAddress:
|
|
|
|
privKey, err := addr.PrivKey()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("WARN: Failed to obtain private key "+
|
|
|
|
"for address %v: %v\n", addr.Address(),
|
|
|
|
err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
wif, err := btcutil.NewWIF((*btcec.PrivateKey)(privKey),
|
|
|
|
netParams, addr.Compressed())
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("WARN: Failed to create wallet "+
|
|
|
|
"import format for address %v: %v\n",
|
|
|
|
addr.Address(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = manager.ImportPrivateKey(wif, &blockStamp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("WARN: Failed to import private "+
|
|
|
|
"key for address %v: %v\n",
|
|
|
|
addr.Address(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
case keystore.ScriptAddress:
|
|
|
|
_, err := manager.ImportScript(addr.Script(), &blockStamp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("WARN: Failed to import "+
|
|
|
|
"pay-to-script-hash script for "+
|
|
|
|
"address %v: %v\n", addr.Address(), err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Printf("WARN: Skipping unrecognized legacy "+
|
|
|
|
"keystore type: %T\n", addr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
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
|
|
|
dbDir := networkDir(cfg.DataDir, activeNet.Params)
|
|
|
|
loader := wallet.NewLoader(activeNet.Params, dbDir)
|
|
|
|
|
2014-10-01 19:15:32 +02:00
|
|
|
// When there is a legacy keystore, open it now to ensure any errors
|
|
|
|
// don't end up exiting the process after the user has spent time
|
|
|
|
// entering a bunch of information.
|
|
|
|
netDir := networkDir(cfg.DataDir, activeNet.Params)
|
|
|
|
keystorePath := filepath.Join(netDir, keystore.Filename)
|
|
|
|
var legacyKeyStore *keystore.Store
|
2015-11-25 06:21:40 +01:00
|
|
|
_, err := os.Stat(keystorePath)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
// A stat error not due to a non-existant file should be
|
|
|
|
// returned to the caller.
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
// Keystore file exists.
|
2014-10-01 19:15:32 +02:00
|
|
|
legacyKeyStore, err = keystore.OpenDir(netDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start by prompting for the private passphrase. When there is an
|
|
|
|
// existing keystore, the user will be promped for that passphrase,
|
|
|
|
// otherwise they will be prompted for a new one.
|
2014-10-29 07:43:29 +01:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
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
|
|
|
privPass, err := prompt.PrivatePass(reader, legacyKeyStore)
|
2014-10-29 07:43:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// When there exists a legacy keystore, unlock it now and set up a
|
|
|
|
// callback to import all keystore keys into the new walletdb
|
|
|
|
// wallet
|
|
|
|
if legacyKeyStore != nil {
|
|
|
|
err = legacyKeyStore.Unlock(privPass)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Import the addresses in the legacy keystore to the new wallet if
|
|
|
|
// any exist, locking each wallet again when finished.
|
2016-03-11 01:35:35 +01:00
|
|
|
loader.RunAfterLoad(func(w *wallet.Wallet) {
|
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
|
|
|
defer legacyKeyStore.Lock()
|
|
|
|
|
|
|
|
fmt.Println("Importing addresses from existing wallet...")
|
|
|
|
|
|
|
|
err := w.Manager.Unlock(privPass)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERR: Failed to unlock new wallet "+
|
|
|
|
"during old wallet key import: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer w.Manager.Lock()
|
|
|
|
|
|
|
|
err = convertLegacyKeystore(legacyKeyStore, w.Manager)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERR: Failed to import keys from old "+
|
|
|
|
"wallet format: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the legacy key store.
|
|
|
|
err = os.Remove(keystorePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("WARN: Failed to remove legacy wallet "+
|
|
|
|
"from'%s'\n", keystorePath)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-10-29 07:43:29 +01:00
|
|
|
// Ascertain the public passphrase. This will either be a value
|
|
|
|
// specified by the user or the default hard-coded public passphrase if
|
|
|
|
// the user does not want the additional public data encryption.
|
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
|
|
|
pubPass, err := prompt.PublicPass(reader, privPass,
|
|
|
|
[]byte(wallet.InsecurePubPassphrase), []byte(cfg.WalletPass))
|
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.
|
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
|
|
|
seed, err := prompt.Seed(reader)
|
2014-10-29 07:43:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Creating the wallet...")
|
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, err := loader.CreateNewWallet(pubPass, privPass, seed)
|
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()
|
2014-10-29 07:43:29 +01:00
|
|
|
fmt.Println("The wallet has been created successfully.")
|
|
|
|
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")
|
|
|
|
|
|
|
|
// Public passphrase is the default.
|
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
|
|
|
pubPass := []byte(wallet.InsecurePubPassphrase)
|
2015-03-04 18:48:34 +01:00
|
|
|
|
|
|
|
netDir := networkDir(cfg.DataDir, activeNet.Params)
|
|
|
|
|
|
|
|
// Create the wallet.
|
|
|
|
dbPath := filepath.Join(netDir, walletDbName)
|
|
|
|
fmt.Println("Creating the wallet...")
|
|
|
|
|
|
|
|
// Create the wallet database backed by bolt db.
|
|
|
|
db, err := walletdb.Create("bdb", dbPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2016-03-17 15:05:11 +01:00
|
|
|
// Create the wallet.
|
|
|
|
err = wallet.Create(db, pubPass, privPass, nil, activeNet.Params)
|
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
|
|
|
|
}
|