Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asset{btc/ltc}: Add custom wallet constructor for BTC and LTC #2636

Merged
merged 6 commits into from
Jan 12, 2024

Conversation

ukane-philemon
Copy link
Contributor

@ukane-philemon ukane-philemon commented Dec 21, 2023

This a follow-up of https://github.com/decred/dcrdex/pull/1227/files#diff-cd95defa7320b76287f90152b40ee5ec5edec59ca9af2eb9579ecda539ece20a which introduced custom wallet constructors.

These changes are required by the Cryptopower project to provide dex core with a custom built-in wallet.

Closes #2637

Copy link
Member

@itswisdomagain itswisdomagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks straightforward.

client/asset/btc/btc.go Outdated Show resolved Hide resolved
client/asset/dcr/wallet.go Outdated Show resolved Hide resolved
Comment on lines 253 to 255
cl SPVService
acctNum uint32
acctName string
dir string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just populate these during initialization with a single call to AccountInfo instead of calling it every time?

Copy link
Contributor Author

@ukane-philemon ukane-philemon Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhmm, okay.

EDIT: Some consumers might have an interface for wallet renaming(like Cryptopower), saving this might cause some unexpected problems, I think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

account renaming*

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you.

Comment on lines 671 to 674
if def.Type == availableWallets.Type {
return fmt.Errorf("(%q): %w", def.Type, asset.ErrWalletTypeAlreadyRegistered)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're just adopting the existing pattern from dcr, but this should really be a panic, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yh. Currently, the only consumer “might” call this function multiple times but apart from that, I think we can let consumers handle this error anyhow they deem fit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a panic is appropriate. The consumer can use something like an atomic.Bool to track if the custom type has been registered and avoid repeat registration attempts.

Comment on lines 161 to 181
// customSPVWalletConstructors are functions for setting up custom
// implementations of the btc.BTCWallet interface that may be used by the
// ExchangeWalletSPV instead of the default spv implementation.
var customSPVWalletConstructors = map[string]btc.CustomSPVWalletConstructor{}

// RegisterCustomSPVWallet registers a function that should be used in creating
// a btc.BTCWallet implementation that the ExchangeWalletSPV will use in place
// of the default spv wallet implementation. External consumers can use this
// function to provide alternative btc.BTCWallet implementations, and must do so
// before attempting to create an ExchangeWalletSPV instance of this type.
func RegisterCustomSPVWallet(constructor btc.CustomSPVWalletConstructor, def *asset.WalletDefinition) error {
for _, availableWallets := range WalletInfo.AvailableWallets {
if def.Type == availableWallets.Type {
return fmt.Errorf("(%q): %w", def.Type, asset.ErrWalletTypeAlreadyRegistered)
}
}
customSPVWalletConstructors[def.Type] = constructor
WalletInfo.AvailableWallets = append(WalletInfo.AvailableWallets, def)
return nil
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does bch use the btc RegisterCustomSPVWallet but ltc has it's own and duplicates the btc constructor code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RegisterCustomSPVWallet was not implemented for bch cuz Cryptopower only supports btc, ltc and dcr atm.

@ukane-philemon ukane-philemon force-pushed the add-custom-wallet-constructor branch 3 times, most recently from 2decf1c to beacdac Compare January 4, 2024 13:33
@ukane-philemon ukane-philemon force-pushed the add-custom-wallet-constructor branch from beacdac to c615fb9 Compare January 7, 2024 00:12
Copy link
Member

@buck54321 buck54321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ukane-philemon
Copy link
Contributor Author

ukane-philemon commented Jan 11, 2024

How about just one? buck54321/[email protected]:dcrdex:onereg

Thanks for the review and suggestion, I think we still need to register the wallet in ‘WalletInfo.AvailableWallets’ for each asset.

@buck54321 buck54321 merged commit 2201c69 into decred:master Jan 12, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add custom wallet constructor for BTC and LTC
4 participants