From 29023436857be2c2a91f759ba781427566919a3d Mon Sep 17 00:00:00 2001 From: Philemon Ukane Date: Wed, 3 Jan 2024 23:21:54 +0100 Subject: [PATCH] itswisdomagain review changes Signed-off-by: Philemon Ukane --- dexc/core.go | 4 - libwallet/assets/btc/dex-wallet.go | 156 +++++++++++++++++++++----- libwallet/assets/dcr/dex_wallet.go | 22 ++-- libwallet/assets_manager.go | 53 ++++----- ui/page/dcrdex/dex_onboarding_page.go | 1 - 5 files changed, 161 insertions(+), 75 deletions(-) diff --git a/dexc/core.go b/dexc/core.go index f477039c0..cf198d68e 100644 --- a/dexc/core.go +++ b/dexc/core.go @@ -22,10 +22,6 @@ const ( // settings map used to connect an existing Cryptopower wallet to the DEX // client. WalletIDConfigKey = "walletid" - // WalletAccountNameConfigKey is the key that holds the wallet account name - // in the settings map used to connect an existing Cryptopower wallet to the - // DEX client. - WalletAccountNameConfigKey = "account" // WalletAccountNumberConfigKey is the key that holds the wallet account // number in the settings map used to connect an existing Cryptopower wallet // to the DEX client. diff --git a/libwallet/assets/btc/dex-wallet.go b/libwallet/assets/btc/dex-wallet.go index d2f7de0be..1d974cc49 100644 --- a/libwallet/assets/btc/dex-wallet.go +++ b/libwallet/assets/btc/dex-wallet.go @@ -3,7 +3,7 @@ package btc -// Note: Most of the code here is a copy-pasta from: +// Note: Most of the code here is a copy-paste from: // https://github.com/decred/dcrdex/blob/master/client/asset/btc/spv.go import ( @@ -15,8 +15,10 @@ import ( "decred.org/dcrdex/client/asset" dexbtc "decred.org/dcrdex/client/asset/btc" "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/btcutil/gcs" + "github.com/btcsuite/btcd/btcutil/psbt" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" @@ -31,45 +33,124 @@ import ( // DEXWallet wraps *wallet.Wallet and implements dexbtc.BTCWallet. type DEXWallet struct { - *wallet.Wallet - acct dexbtc.XCWalletAccount + w *wallet.Wallet + acctNum int32 spvService *btcChainService } var _ dexbtc.BTCWallet = (*DEXWallet)(nil) // NewDEXWallet returns a new *DEXWallet. -func NewDEXWallet(acct dexbtc.XCWalletAccount, w *wallet.Wallet, nc *chain.NeutrinoClient) *DEXWallet { +func NewDEXWallet(w *wallet.Wallet, acctNum int32, nc *chain.NeutrinoClient) *DEXWallet { return &DEXWallet{ - Wallet: w, - acct: acct, + w: w, + acctNum: acctNum, spvService: &btcChainService{ NeutrinoClient: nc, }, } } -// The below methods are not implemented by *wallet.Wallet, so must be -// implemented by the BTCWallet implementation. +func (dw *DEXWallet) PublishTransaction(tx *wire.MsgTx, label string) error { + return dw.w.PublishTransaction(tx, label) +} -func (dw *DEXWallet) Start() (dexbtc.SPVService, error) { - return dw.spvService, nil +func (dw *DEXWallet) CalculateAccountBalances(account uint32, confirms int32) (wallet.Balances, error) { + return dw.w.CalculateAccountBalances(account, confirms) } -func (dw *DEXWallet) Birthday() time.Time { - return dw.Manager.Birthday() +func (dw *DEXWallet) ListUnspent(minconf, maxconf int32, acctName string) ([]*btcjson.ListUnspentResult, error) { + return dw.w.ListUnspent(minconf, maxconf, acctName) } -func (dw *DEXWallet) SyncedTo() waddrmgr.BlockStamp { - return dw.Wallet.Manager.SyncedTo() +func (dw *DEXWallet) FetchInputInfo(prevOut *wire.OutPoint) (*wire.MsgTx, *wire.TxOut, *psbt.Bip32Derivation, int64, error) { + return dw.w.FetchInputInfo(prevOut) +} + +func (dw *DEXWallet) ResetLockedOutpoints() { + dw.w.ResetLockedOutpoints() +} + +func (dw *DEXWallet) LockOutpoint(op wire.OutPoint) { + dw.w.LockOutpoint(op) +} + +func (dw *DEXWallet) UnlockOutpoint(op wire.OutPoint) { + dw.w.UnlockOutpoint(op) +} + +func (dw *DEXWallet) LockedOutpoints() []btcjson.TransactionInput { + return dw.w.LockedOutpoints() +} + +func (dw *DEXWallet) NewChangeAddress(account uint32, scope waddrmgr.KeyScope) (btcutil.Address, error) { + return dw.w.NewChangeAddress(account, scope) +} + +func (dw *DEXWallet) NewAddress(account uint32, scope waddrmgr.KeyScope) (btcutil.Address, error) { + return dw.w.NewAddress(account, scope) +} + +func (dw *DEXWallet) PrivKeyForAddress(a btcutil.Address) (*btcec.PrivateKey, error) { + return dw.w.PrivKeyForAddress(a) +} + +func (dw *DEXWallet) Unlock(passphrase []byte, lock <-chan time.Time) error { + return dw.w.Unlock(passphrase, lock) +} + +func (dw *DEXWallet) Lock() { + dw.w.Lock() +} + +func (dw *DEXWallet) Locked() bool { + return dw.w.Locked() +} + +func (dw *DEXWallet) SendOutputs(outputs []*wire.TxOut, keyScope *waddrmgr.KeyScope, account uint32, minconf int32, + satPerKb btcutil.Amount, coinSelectionStrategy wallet.CoinSelectionStrategy, label string) (*wire.MsgTx, error) { + return dw.w.SendOutputs(outputs, keyScope, account, minconf, satPerKb, coinSelectionStrategy, label) +} + +func (dw *DEXWallet) HaveAddress(a btcutil.Address) (bool, error) { + return dw.w.HaveAddress(a) +} + +func (dw *DEXWallet) WaitForShutdown() { + dw.w.WaitForShutdown() +} + +// currently unused +func (dw *DEXWallet) ChainSynced() bool { + return dw.w.ChainSynced() } +func (dw *DEXWallet) AccountProperties(scope waddrmgr.KeyScope, acct uint32) (*waddrmgr.AccountProperties, error) { + return dw.w.AccountProperties(scope, acct) +} + +// The below methods are not implemented by *wallet.Wallet, so must be +// implemented by the dexbtc.BTCWallet implementation. + +// AccountInfo returns the account information of the wallet for use by the +// exchange wallet. func (dw *DEXWallet) AccountInfo() dexbtc.XCWalletAccount { - return dw.acct + acct := dexbtc.XCWalletAccount{ + AccountNumber: uint32(dw.acctNum), + } + + accountName, err := dw.w.AccountName(GetScope(), acct.AccountNumber) + if err == nil { + acct.AccountName = accountName + } else { + log.Errorf("error checking selected DEX account name: %v", err) + } + + return acct } func (dw *DEXWallet) WalletTransaction(txHash *chainhash.Hash) (*wtxmgr.TxDetails, error) { - details, err := wallet.UnstableAPI(dw.Wallet).TxDetails(txHash) + details, err := wallet.UnstableAPI(dw.w).TxDetails(txHash) if err != nil { return nil, err } @@ -80,11 +161,15 @@ func (dw *DEXWallet) WalletTransaction(txHash *chainhash.Hash) (*wtxmgr.TxDetail return details, nil } +func (dw *DEXWallet) SyncedTo() waddrmgr.BlockStamp { + return dw.w.Manager.SyncedTo() +} + func (dw *DEXWallet) SignTx(tx *wire.MsgTx) error { var prevPkScripts [][]byte var inputValues []btcutil.Amount for _, txIn := range tx.TxIn { - _, txOut, _, _, err := dw.Wallet.FetchInputInfo(&txIn.PreviousOutPoint) + _, txOut, _, _, err := dw.w.FetchInputInfo(&txIn.PreviousOutPoint) if err != nil { return err } @@ -95,11 +180,11 @@ func (dw *DEXWallet) SignTx(tx *wire.MsgTx) error { txIn.SignatureScript = nil txIn.Witness = nil } - return txauthor.AddAllInputScripts(tx, prevPkScripts, inputValues, &secretSource{dw, dw.ChainParams()}) + return txauthor.AddAllInputScripts(tx, prevPkScripts, inputValues, &secretSource{dw, dw.w.ChainParams()}) } func (dw *DEXWallet) BlockNotifications(ctx context.Context) <-chan *dexbtc.BlockNotification { - cl := dw.Wallet.NtfnServer.TransactionNotifications() + cl := dw.w.NtfnServer.TransactionNotifications() ch := make(chan *dexbtc.BlockNotification, 1) go func() { defer cl.Done() @@ -124,6 +209,23 @@ func (dw *DEXWallet) BlockNotifications(ctx context.Context) <-chan *dexbtc.Bloc return ch } +func (dw *DEXWallet) RescanAsync() error { + return errors.New("RescanAsync not implemented for Cyptopower btc wallet") +} + +func (dw *DEXWallet) ForceRescan() {} +func (dw *DEXWallet) Start() (dexbtc.SPVService, error) { + return dw.spvService, nil +} +func (dw *DEXWallet) Stop() {} + +func (dw *DEXWallet) Reconfigure(*asset.WalletConfig, string) (bool, error) { + return false, errors.New("Reconfigure not supported for Cyptopower btc wallet") +} +func (dw *DEXWallet) Birthday() time.Time { + return dw.w.Manager.Birthday() +} + func (dw *DEXWallet) Peers() ([]*asset.WalletPeer, error) { peers := dw.spvService.CS.Peers() var walletPeers []*asset.WalletPeer @@ -146,14 +248,8 @@ func (dw *DEXWallet) RemovePeer(_ string) error { return errors.New("RemovePeer not implemented by DEX wallet") } -func (dw *DEXWallet) RescanAsync() error { - return errors.New("RescanAsync not implemented for Cyptopower btc wallet") -} - -func (dw *DEXWallet) ForceRescan() {} - -func (dw *DEXWallet) Reconfigure(*asset.WalletConfig, string) (bool, error) { - return false, errors.New("Reconfigure not supported for Cyptopower btc wallet") +func (dw *DEXWallet) ListSinceBlock(start, end, syncHeight int32) ([]btcjson.ListTransactionsResult, error) { + return dw.w.ListSinceBlock(start, end, syncHeight) } // btcChainService wraps *chain.NeutrinoClient in order to translate the @@ -201,7 +297,7 @@ func (s *btcChainService) Stop() error { // secretSource is used to locate keys and redemption scripts while signing a // transaction. secretSource satisfies the txauthor.SecretsSource interface. type secretSource struct { - w *DEXWallet + dexW *DEXWallet chainParams *chaincfg.Params } @@ -212,7 +308,7 @@ func (s *secretSource) ChainParams() *chaincfg.Params { // GetKey fetches a private key for the specified address. func (s *secretSource) GetKey(addr btcutil.Address) (*btcec.PrivateKey, bool, error) { - ma, err := s.w.Wallet.AddressInfo(addr) + ma, err := s.dexW.w.AddressInfo(addr) if err != nil { return nil, false, err } @@ -233,7 +329,7 @@ func (s *secretSource) GetKey(addr btcutil.Address) (*btcec.PrivateKey, bool, er // GetScript fetches the redemption script for the specified p2sh/p2wsh address. func (s *secretSource) GetScript(addr btcutil.Address) ([]byte, error) { - ma, err := s.w.Wallet.AddressInfo(addr) + ma, err := s.dexW.w.AddressInfo(addr) if err != nil { return nil, err } diff --git a/libwallet/assets/dcr/dex_wallet.go b/libwallet/assets/dcr/dex_wallet.go index c0a659c0c..217bae5d1 100644 --- a/libwallet/assets/dcr/dex_wallet.go +++ b/libwallet/assets/dcr/dex_wallet.go @@ -34,18 +34,18 @@ import ( // DEXWallet wraps *Asset and implements dexdcr.Wallet. type DEXWallet struct { *Asset - syncData *SyncData - tradingAccountName string + tradingAccountNumber int32 + syncData *SyncData } var _ dexdcr.Wallet = (*DEXWallet)(nil) // NewDEXWallet returns a new *DEXWallet. -func NewDEXWallet(tradingAccountName string, w *Asset, syncData *SyncData) *DEXWallet { +func NewDEXWallet(asset *Asset, tradingAccountNumber int32, syncData *SyncData) *DEXWallet { return &DEXWallet{ - Asset: w, - syncData: syncData, - tradingAccountName: tradingAccountName, + Asset: asset, + tradingAccountNumber: tradingAccountNumber, + syncData: syncData, } } @@ -68,8 +68,12 @@ func (dw *DEXWallet) SpvMode() bool { // Accounts returns the names of the accounts for use by the exchange wallet. func (dw *DEXWallet) Accounts() dexdcr.XCWalletAccounts { - accts := dexdcr.XCWalletAccounts{ - PrimaryAccount: dw.tradingAccountName, + var accts dexdcr.XCWalletAccounts + accountName, err := dw.AccountName(dw.tradingAccountNumber) + if err == nil { + accts.PrimaryAccount = accountName + } else { + log.Errorf("error checking selected DEX account name: %v", err) } if !dw.IsAccountMixerActive() { @@ -102,7 +106,7 @@ func (dw *DEXWallet) Accounts() dexdcr.XCWalletAccounts { return dexdcr.XCWalletAccounts{ PrimaryAccount: mixedAccName, UnmixedAccount: unMixedAcctName, - TradingAccount: dw.tradingAccountName, + TradingAccount: accts.PrimaryAccount, } } diff --git a/libwallet/assets_manager.go b/libwallet/assets_manager.go index c3db68ed3..a26e5ddb1 100644 --- a/libwallet/assets_manager.go +++ b/libwallet/assets_manager.go @@ -955,9 +955,9 @@ func (mgr *AssetsManager) PrepareDexSupportForDCRWallet() error { Description: "ID of existing wallet to use", }, { - Key: dexc.WalletAccountNameConfigKey, - DisplayName: "Wallet Account Name", - Description: "Account name of the selected wallet", + Key: dexc.WalletAccountNumberConfigKey, + DisplayName: "Wallet Account Number", + Description: "Account number of the selected wallet", }, } @@ -991,9 +991,15 @@ func (mgr *AssetsManager) PrepareDexSupportForDCRWallet() error { return nil, fmt.Errorf("cannot use watch only wallet for DEX trade") } - // Ensure the accountName exists. - accountName := settings[dexc.WalletAccountNameConfigKey] - if _, err = wallet.AccountNumber(accountName); err != nil { + // Ensure the account exists. + accountNumberStr := settings[dexc.WalletAccountNumberConfigKey] + acctNum, err := strconv.ParseInt(accountNumberStr, 10, 64) + if err != nil { + return nil, err + } + + accountNumber := int32(acctNum) + if _, err = wallet.AccountName(accountNumber); err != nil { return nil, fmt.Errorf("error checking selected DEX account: %w", err) } @@ -1002,7 +1008,7 @@ func (mgr *AssetsManager) PrepareDexSupportForDCRWallet() error { return nil, fmt.Errorf("DEX wallet not supported for %s", walletParams.Name) } - return dcr.NewDEXWallet(accountName, dcrAsset, dcrAsset.SyncData()), nil + return dcr.NewDEXWallet(dcrAsset, accountNumber, dcrAsset.SyncData()), nil } err := dexDcr.RegisterCustomWallet(walletMaker, def) @@ -1014,7 +1020,7 @@ func (mgr *AssetsManager) PrepareDexSupportForDCRWallet() error { } // PrepareDexSupportForBTCWallet sets up the DEX client to allow using a -// cyptopower btc wallet with DEX core. +// Cyptopower btc wallet with DEX core. func (mgr *AssetsManager) PrepareDexSupportForBTCWallet() error { // Build a custom wallet definition with custom config options for use by // the dexbtc.ExchangeWalletSPV. @@ -1024,11 +1030,6 @@ func (mgr *AssetsManager) PrepareDexSupportForBTCWallet() error { DisplayName: "Wallet ID", Description: "ID of existing wallet to use", }, - { - Key: dexc.WalletAccountNameConfigKey, - DisplayName: "Wallet Account Name", - Description: "Account name of the selected wallet", - }, { Key: dexc.WalletAccountNumberConfigKey, DisplayName: "Wallet Account Number", @@ -1066,29 +1067,19 @@ func (mgr *AssetsManager) PrepareDexSupportForBTCWallet() error { return nil, fmt.Errorf("cannot use watch only wallet for DEX trade") } - acct := dexbtc.XCWalletAccount{ - AccountName: settings[dexc.WalletAccountNameConfigKey], - } - - // Ensure the wallet account name exists. - accountNumber, err := wallet.AccountNumber(acct.AccountName) + // Ensure the wallet account exists. + accountNumberStr := settings[dexc.WalletAccountNumberConfigKey] + acctNum, err := strconv.ParseInt(accountNumberStr, 10, 64) if err != nil { - return nil, fmt.Errorf("error checking selected DEX account name: %w", err) + return nil, err } - configAcctNumber, accountNumberStr := settings[dexc.WalletAccountNumberConfigKey], fmt.Sprint(accountNumber) - if configAcctNumber == accountNumberStr { - acctNumber, err := strconv.ParseInt(configAcctNumber, 10, 64) - if err != nil { - return nil, err - } - acct.AccountNumber = uint32(acctNumber) - } else { - return nil, fmt.Errorf("config account number for wallet account(%s) does not match actual account number, expected %s got %s", - acct.AccountName, accountNumberStr, configAcctNumber) + accountNumber := int32(acctNum) + if _, err = wallet.AccountName(accountNumber); err != nil { + return nil, fmt.Errorf("error checking selected DEX account name: %w", err) } - return btc.NewDEXWallet(acct, wallet.Internal().BTC, wallet.(*btc.Asset).NeutrinoClient()), nil + return btc.NewDEXWallet(wallet.Internal().BTC, accountNumber, wallet.(*btc.Asset).NeutrinoClient()), nil } err := dexbtc.RegisterCustomSPVWallet(btcWalletConstructor, def) diff --git a/ui/page/dcrdex/dex_onboarding_page.go b/ui/page/dcrdex/dex_onboarding_page.go index d3b520e9f..d58d4ae81 100644 --- a/ui/page/dcrdex/dex_onboarding_page.go +++ b/ui/page/dcrdex/dex_onboarding_page.go @@ -1048,7 +1048,6 @@ func (pg *DEXOnboarding) postBond() { selectedAcct := pg.bondSourceAccountSelector.SelectedAccount() cfg := map[string]string{ dexc.WalletIDConfigKey: fmt.Sprintf("%d", asset.GetWalletID()), - dexc.WalletAccountNameConfigKey: selectedAcct.Name, dexc.WalletAccountNumberConfigKey: fmt.Sprint(selectedAcct.AccountNumber), }