Skip to content

Commit

Permalink
client/asset/dcr: add package-level func for setting custom wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Oct 20, 2021
1 parent 688b72a commit 67663b4
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 131 deletions.
49 changes: 35 additions & 14 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ var (
IsBoolean: true,
},
}
// WalletInfo defines some general information about a Decred wallet.
WalletInfo = &asset.WalletInfo{
// DefaultWalletInfo defines some general information about a Decred wallet.
DefaultWalletInfo = &asset.WalletInfo{
Name: "Decred",
Units: "atoms",
Version: version,
DefaultConfigPath: defaultConfigPath,
ConfigOpts: configOpts,
}
// walletInfo is the DefaultWalletInfo or a custom *asset.WalletInfo if one
// is provided via the RegisterCustomWallet function.
walletInfo = DefaultWalletInfo
)

// outPoint is the hash and output index of a transaction output.
Expand Down Expand Up @@ -325,7 +328,7 @@ type Driver struct{}

// Setup creates the DCR exchange wallet. Start the wallet with its Run method.
func (d *Driver) Setup(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
return NewWallet(cfg, logger, network, &rpcWallet{})
return NewWallet(cfg, logger, network)
}

// DecodeCoinID creates a human-readable representation of a coin ID for Decred.
Expand All @@ -341,7 +344,7 @@ func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
// ExchangeWallet instance may have different DefaultFeeRate set, so use
// (*ExchangeWallet).Info when possible.
func (d *Driver) Info() *asset.WalletInfo {
return WalletInfo
return walletInfo
}

func init() {
Expand Down Expand Up @@ -404,11 +407,10 @@ type findRedemptionResult struct {
var _ asset.Wallet = (*ExchangeWallet)(nil)

// NewWallet is the exported constructor by which the DEX will import the
// exchange wallet. The wallet will shut down when the provided context is
// canceled.
func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network, wallet Wallet) (*ExchangeWallet, error) {
// exchange wallet.
func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (*ExchangeWallet, error) {
// loadConfig will set fields if defaults are used and set the chainParams
// package variable.
// variable.
walletCfg, chainParams, err := loadConfig(cfg.Settings, network)
if err != nil {
return nil, err
Expand All @@ -419,10 +421,17 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network,
return nil, err
}

dcr.wallet = wallet
err = dcr.wallet.Initialize(cfg, walletCfg, chainParams, logger)
if err != nil {
return nil, err
// Set dcr.wallet using either the default rpcWallet or a custom wallet.
if customWalletConstructor == nil {
dcr.wallet, err = newRPCWallet(walletCfg, chainParams, logger)
if err != nil {
return nil, err
}
} else {
dcr.wallet, err = customWalletConstructor(walletCfg, chainParams, logger)
if err != nil {
return nil, fmt.Errorf("custom wallet setup error: %v", err)
}
}

return dcr, nil
Expand Down Expand Up @@ -473,7 +482,7 @@ func unconnectedWallet(cfg *asset.WalletConfig, dcrCfg *Config, chainParams *cha

// Info returns basic information about the wallet and asset.
func (dcr *ExchangeWallet) Info() *asset.WalletInfo {
return WalletInfo
return walletInfo
}

// var logup uint32
Expand Down Expand Up @@ -510,6 +519,14 @@ func (dcr *ExchangeWallet) Connect(ctx context.Context) (*sync.WaitGroup, error)
}
}()

curnet, err := dcr.wallet.Network(ctx)
if err != nil {
return nil, fmt.Errorf("unable to fetch wallet network: %w", err)
}
if curnet != dcr.chainParams.Net {
return nil, fmt.Errorf("unexpected wallet network %s, expected %s", curnet, dcr.chainParams.Net)
}

// Initialize the best block.
dcr.tipMtx.Lock()
dcr.currentTip, err = dcr.getBestBlock(ctx)
Expand Down Expand Up @@ -2403,7 +2420,11 @@ func msgTxToHex(msgTx *wire.MsgTx) (string, error) {
// signTx attempts to sign all transaction inputs. If it fails to completely
// sign the transaction, it is an error and a nil *wire.MsgTx is returned.
func (dcr *ExchangeWallet) signTx(baseTx *wire.MsgTx) (*wire.MsgTx, error) {
res, err := dcr.wallet.SignRawTransaction(dcr.ctx, baseTx)
txHex, err := msgTxToHex(baseTx)
if err != nil {
return nil, fmt.Errorf("failed to encode MsgTx: %w", err)
}
res, err := dcr.wallet.SignRawTransaction(dcr.ctx, txHex)
if err != nil {
return nil, fmt.Errorf("signrawtransaction error: %w", err)
}
Expand Down
8 changes: 6 additions & 2 deletions client/asset/dcr/dcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func tNewWallet() (*ExchangeWallet, *tRPCClient, func(), error) {
return nil, nil, nil, err
}
wallet.wallet = &rpcWallet{
node: client,
rpcClient: client,
}
wallet.ctx = walletCtx

Expand Down Expand Up @@ -330,6 +330,10 @@ func newTRPCClient() *tRPCClient {
}
}

func (c *tRPCClient) GetCurrentNet(context.Context) (wire.CurrencyNet, error) {
return tChainParams.Net, nil
}

func (c *tRPCClient) EstimateSmartFee(_ context.Context, confirmations int64, mode chainjson.EstimateSmartFeeMode) (*chainjson.EstimateSmartFeeResult, error) {
if c.estFeeErr != nil {
return nil, c.estFeeErr
Expand Down Expand Up @@ -2297,7 +2301,7 @@ func TestSyncStatus(t *testing.T) {
node.blockchainInfoErr = nil

wallet.wallet = &rpcWallet{
node: node,
rpcClient: node,
tipAtConnect: 100,
}
node.blockchainInfo = &chainjson.GetBlockChainInfoResult{
Expand Down
Loading

0 comments on commit 67663b4

Please sign in to comment.