Skip to content

Commit

Permalink
client/core: Check deposit addr in wallet reconfigure
Browse files Browse the repository at this point in the history
Rather than search for addresses in contracts, use the deposit address
to determine if the new wallet is the same as the old one.

This allows changing the wallet before their is any swap, when the swap
contract script is still empty.
  • Loading branch information
JoeGruffins committed Sep 14, 2021
1 parent 7f9793e commit aee00c8
Showing 1 changed file with 12 additions and 57 deletions.
69 changes: 12 additions & 57 deletions client/core/core.go
Expand Up @@ -1933,12 +1933,13 @@ func (c *Core) ReconfigureWallet(appPW, newWalletPW []byte, assetID uint32, cfg
return newError(missingWalletErr, "%d -> %s wallet not found",
assetID, unbip(assetID))
}
oldDepositAddr := oldWallet.currentDepositAddress()
dbWallet := &db.Wallet{
AssetID: oldWallet.AssetID,
Settings: cfg,
Balance: &db.Balance{}, // in case retrieving new balance after connect fails
EncryptedPW: oldWallet.encPW(),
Address: oldWallet.currentDepositAddress(),
Address: oldDepositAddr,
}
// Reload the wallet with the new settings.
wallet, err := c.loadWallet(dbWallet)
Expand All @@ -1959,66 +1960,20 @@ func (c *Core) ReconfigureWallet(appPW, newWalletPW []byte, assetID uint32, cfg
// If there are active trades, make sure they can be settled by the
// keys held within the new wallet.
sameWallet := func() error {
ownsAddr := func(addr string) error {
owns, err := wallet.OwnsAddress(addr)
hasActiveTrade := false
for _, dc := range c.dexConnections() {
if dc.hasActiveAssetOrders(wallet.AssetID) {
hasActiveTrade = true
break
}
}
if hasActiveTrade {
owns, err := wallet.OwnsAddress(oldDepositAddr)
if err != nil {
return err
}
if !owns {
return fmt.Errorf("new wallet does not own address found in active trades: %v", addr)
}
return nil
}
for _, dc := range c.dexConnections() {
maybeDifferentWallet := false
for _, trade := range dc.trackedTrades() {
if !trade.isActive() {
continue
}
waID := wallet.AssetID
// If the to asset, check if we own an
// contract.Address.
if trade.wallets.toAsset.ID == waID {
if err := ownsAddr(trade.Trade().SwapAddress()); err != nil {
return err
}
// Assume all trade addresses are
// owned by the new wallet if one is.
return nil
}
// If the from asset, check if we own a
// refund address for a match if any exist.
if trade.wallets.fromAsset.ID == waID {
for _, match := range trade.matches {
script := match.MetaData.Proof.Script
if len(script) == 0 {
continue
}
addr, err := wallet.RefundAddress(script)
if err != nil {
return err
}
if err := ownsAddr(addr); err != nil {
return err
}
// Assume all refund addresses
// are owned by the new
// wallet if one is.
return nil
}
// If we did not find a refund address,
// we cannot be sure that this is the
// same wallet.
//
// TODO: Implement a way to check that
// these accounts are the same or not.
maybeDifferentWallet = true
}
}
if maybeDifferentWallet {
return errors.New("unable to change wallets with active trades. " +
"trades with this wallet must match or be canceled in " +
"order to change settings")
return errors.New("new wallet does not own old deposit address")
}
}
return nil
Expand Down

0 comments on commit aee00c8

Please sign in to comment.