diff --git a/client/asset/dcr/dcr.go b/client/asset/dcr/dcr.go index 27c1f173e6..3690448ad8 100644 --- a/client/asset/dcr/dcr.go +++ b/client/asset/dcr/dcr.go @@ -348,9 +348,7 @@ func (d *Driver) DecodeCoinID(coinID []byte) (string, error) { return fmt.Sprintf("%v:%d", txid, vout), err } -// Info returns basic information about the wallet and asset. WARNING: An -// ExchangeWallet instance may have different DefaultFeeRate set, so use -// (*ExchangeWallet).Info when possible. +// Info returns basic information about the wallet and asset. func (d *Driver) Info() *asset.WalletInfo { return WalletInfo } @@ -430,23 +428,23 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) } // Set dcr.wallet using either the default rpcWallet or a custom wallet. - if customWalletConstructor == nil { - dcr.wallet, err = newRPCWallet(walletCfg, chainParams, logger) + if makeCustomWallet, ok := customWalletConstructors[cfg.Type]; ok { + dcr.wallet, err = makeCustomWallet(cfg, chainParams, logger) if err != nil { - return nil, err + return nil, fmt.Errorf("custom wallet setup error: %v", err) } } else { - dcr.wallet, err = customWalletConstructor(walletCfg, chainParams, logger) + dcr.wallet, err = newRPCWallet(walletCfg, chainParams, logger) if err != nil { - return nil, fmt.Errorf("custom wallet setup error: %v", err) + return nil, err } } return dcr, nil } -// unconnectedWallet returns an ExchangeWallet without a node. The node should -// be set before use. +// unconnectedWallet returns an ExchangeWallet without a base wallet. The wallet +// should be set before use. func unconnectedWallet(cfg *asset.WalletConfig, dcrCfg *Config, chainParams *chaincfg.Params, logger dex.Logger) (*ExchangeWallet, error) { // If set in the user config, the fallback fee will be in units of DCR/kB. // Convert to atoms/B. diff --git a/client/asset/dcr/rpcwallet.go b/client/asset/dcr/rpcwallet.go index 7f1a0e92fc..6910adbbd3 100644 --- a/client/asset/dcr/rpcwallet.go +++ b/client/asset/dcr/rpcwallet.go @@ -250,15 +250,15 @@ func (w *rpcWallet) Disconnected() bool { return w.rpcConnector.Disconnected() } -// Disconnected returns true if the rpc client is not connected. +// Network returns the network of the connected wallet. // Part of the Wallet interface. func (w *rpcWallet) Network(ctx context.Context) (wire.CurrencyNet, error) { net, err := w.rpcClient.GetCurrentNet(ctx) return net, translateRPCCancelErr(err) } -// NotifyOnTipChange registers a callback function that the should be invoked -// when the wallet sees new mainchain blocks. The return value indicates if this +// NotifyOnTipChange registers a callback function that should be invoked when +// the wallet sees new mainchain blocks. The return value indicates if this // notification can be provided. // Part of the Wallet interface. func (w *rpcWallet) NotifyOnTipChange(ctx context.Context, cb TipChangeCallback) bool { @@ -525,7 +525,7 @@ func (w *rpcWallet) AddressPrivKey(ctx context.Context, address stdaddr.Address) type anylist []interface{} // rpcClientRawRequest is used to marshal parameters and send requests to the -// RPC server via rpcClient.RawRequest. If `thing` is non-nil, the resul will +// RPC server via rpcClient.RawRequest. If `thing` is non-nil, the result will // be marshaled into `thing`. func (w *rpcWallet) rpcClientRawRequest(ctx context.Context, method string, args anylist, thing interface{}) error { params := make([]json.RawMessage, 0, len(args)) diff --git a/client/asset/dcr/wallet.go b/client/asset/dcr/wallet.go index 86a6f35ab2..a2faad1868 100644 --- a/client/asset/dcr/wallet.go +++ b/client/asset/dcr/wallet.go @@ -21,31 +21,36 @@ import ( // WalletConstructor defines a function that can be invoked to create a custom // implementation of the Wallet interface. -type WalletConstructor func(cfg *Config, chainParams *chaincfg.Params, logger dex.Logger) (Wallet, error) +type WalletConstructor func(cfg *asset.WalletConfig, chainParams *chaincfg.Params, logger dex.Logger) (Wallet, error) -// customWalletConstructor is a function that can be invoked when setting up -// the ExchangeWallet to set up a custom implementation of the Wallet interface -// which will be used instead of the default rpcWallet implementation. -var customWalletConstructor WalletConstructor +// customWalletConstructors are functions for setting up custom implementations +// of the Wallet interface that may be used by the ExchangeWallet instead of the +// default rpcWallet implementation. +var customWalletConstructors map[string]WalletConstructor -// RegisterCustomWallet specifies the function that should be used in creating -// a wallet implementation that the ExchangeWallet will use in place of the -// default rpcWallet implementation. External consumers can use this function -// to provide alternative Wallet implementations, and must do so before an -// ExchangeWallet instance is created. +// RegisterCustomWallet registers a function that should be used in creating a +// Wallet implementation that the ExchangeWallet of the specified type will use +// in place of the default rpcWallet implementation. External consumers can use +// this function to provide alternative Wallet implementations, and must do so +// before attempting to create an ExchangeWallet instance of this type. func RegisterCustomWallet(constructor WalletConstructor, def *asset.WalletDefinition) error { for _, availableWallets := range WalletInfo.AvailableWallets { if def.Type == availableWallets.Type { return fmt.Errorf("already support %q wallets", def.Type) } } - customWalletConstructor = constructor + customWalletConstructors[def.Type] = constructor WalletInfo.AvailableWallets = append(WalletInfo.AvailableWallets, def) return nil } type TipChangeCallback func(*chainhash.Hash, int64, error) +// Wallet defines methods that the ExchangeWallet uses for communicating with +// a Decred wallet and blockchain. +// TODO: Where possible, replace walletjson and chainjson return types with +// other types that define fewer fields e.g. *chainjson.TxRawResult with +// *wire.MsgTx. type Wallet interface { // Connect establishes a connection to the wallet. Connect(ctx context.Context) error diff --git a/client/asset/driver.go b/client/asset/driver.go index 20cc1adf0f..734341785e 100644 --- a/client/asset/driver.go +++ b/client/asset/driver.go @@ -133,8 +133,6 @@ func Assets() map[uint32]RegisteredAsset { } // Info returns the WalletInfo for the specified asset, if supported. -// WARNING: An ExchangeWallet instance may have different config values set, -// so use (*ExchangeWallet).Info when possible. func Info(assetID uint32) (*WalletInfo, error) { driversMtx.RLock() drv, ok := drivers[assetID]