mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
Simplify error handling with waddrmgr.IsError.
This commit is contained in:
parent
39cab6087a
commit
9d5abaf14e
3 changed files with 11 additions and 49 deletions
51
rpcserver.go
51
rpcserver.go
|
@ -172,41 +172,6 @@ func (c *websocketClient) send(b []byte) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// isManagerLockedError returns whether or not the passed error is due to the
|
|
||||||
// address manager being locked.
|
|
||||||
func isManagerLockedError(err error) bool {
|
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
|
||||||
return ok && merr.ErrorCode == waddrmgr.ErrLocked
|
|
||||||
}
|
|
||||||
|
|
||||||
// isManagerWrongPassphraseError returns whether or not the passed error is due
|
|
||||||
// to the address manager being provided with an invalid passprhase.
|
|
||||||
func isManagerWrongPassphraseError(err error) bool {
|
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
|
||||||
return ok && merr.ErrorCode == waddrmgr.ErrWrongPassphrase
|
|
||||||
}
|
|
||||||
|
|
||||||
// isManagerDuplicateAddressError returns whether or not the passed error is due to a
|
|
||||||
// duplicate item being provided to the address manager.
|
|
||||||
func isManagerDuplicateAddressError(err error) bool {
|
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
|
||||||
return ok && merr.ErrorCode == waddrmgr.ErrDuplicateAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
// isManagerAddressNotFoundError returns whether or not the passed error is due to a
|
|
||||||
// the address not being found.
|
|
||||||
func isManagerAddressNotFoundError(err error) bool {
|
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
|
||||||
return ok && merr.ErrorCode == waddrmgr.ErrAddressNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// isManagerAccountNotFoundError returns whether or not the passed error is due
|
|
||||||
// to the account not being found.
|
|
||||||
func isManagerAccountNotFoundError(err error) bool {
|
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
|
||||||
return ok && merr.ErrorCode == waddrmgr.ErrAccountNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseListeners splits the list of listen addresses passed in addrs into
|
// parseListeners splits the list of listen addresses passed in addrs into
|
||||||
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
|
// IPv4 and IPv6 slices and returns them. This allows easy creation of the
|
||||||
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
// listeners on the correct interface "tcp4" and "tcp6". It also properly
|
||||||
|
@ -1592,7 +1557,7 @@ func DumpPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (in
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := w.DumpWIFPrivateKey(addr)
|
key, err := w.DumpWIFPrivateKey(addr)
|
||||||
if isManagerLockedError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
|
||||||
// Address was found, but the private key isn't
|
// Address was found, but the private key isn't
|
||||||
// accessible.
|
// accessible.
|
||||||
return nil, &ErrWalletUnlockNeeded
|
return nil, &ErrWalletUnlockNeeded
|
||||||
|
@ -1605,7 +1570,7 @@ func DumpPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (in
|
||||||
// TODO: finish this to match bitcoind by writing the dump to a file.
|
// TODO: finish this to match bitcoind by writing the dump to a file.
|
||||||
func DumpWallet(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (interface{}, error) {
|
func DumpWallet(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (interface{}, error) {
|
||||||
keys, err := w.DumpPrivKeys()
|
keys, err := w.DumpPrivKeys()
|
||||||
if isManagerLockedError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
|
||||||
return nil, &ErrWalletUnlockNeeded
|
return nil, &ErrWalletUnlockNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1848,10 +1813,10 @@ func ImportPrivKey(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (
|
||||||
// Import the private key, handling any errors.
|
// Import the private key, handling any errors.
|
||||||
_, err = w.ImportPrivateKey(wif, nil, *cmd.Rescan)
|
_, err = w.ImportPrivateKey(wif, nil, *cmd.Rescan)
|
||||||
switch {
|
switch {
|
||||||
case isManagerDuplicateAddressError(err):
|
case waddrmgr.IsError(err, waddrmgr.ErrDuplicateAddress):
|
||||||
// Do not return duplicate key errors to the client.
|
// Do not return duplicate key errors to the client.
|
||||||
return nil, nil
|
return nil, nil
|
||||||
case isManagerLockedError(err):
|
case waddrmgr.IsError(err, waddrmgr.ErrLocked):
|
||||||
return nil, &ErrWalletUnlockNeeded
|
return nil, &ErrWalletUnlockNeeded
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1893,7 +1858,7 @@ func CreateNewAccount(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = w.Manager.NewAccount(cmd.Account)
|
_, err = w.Manager.NewAccount(cmd.Account)
|
||||||
if isManagerLockedError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
|
||||||
return nil, &btcjson.RPCError{
|
return nil, &btcjson.RPCError{
|
||||||
Code: btcjson.ErrRPCWalletUnlockNeeded,
|
Code: btcjson.ErrRPCWalletUnlockNeeded,
|
||||||
Message: "Creating an account requires the wallet to be unlocked. " +
|
Message: "Creating an account requires the wallet to be unlocked. " +
|
||||||
|
@ -2599,7 +2564,7 @@ func sendPairs(w *wallet.Wallet, amounts map[string]btcutil.Amount,
|
||||||
if err == wallet.ErrNonPositiveAmount {
|
if err == wallet.ErrNonPositiveAmount {
|
||||||
return "", ErrNeedPositiveAmount
|
return "", ErrNeedPositiveAmount
|
||||||
}
|
}
|
||||||
if isManagerLockedError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrLocked) {
|
||||||
return "", &ErrWalletUnlockNeeded
|
return "", &ErrWalletUnlockNeeded
|
||||||
}
|
}
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
|
@ -3111,7 +3076,7 @@ func ValidateAddress(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{})
|
||||||
|
|
||||||
ainfo, err := w.Manager.Address(addr)
|
ainfo, err := w.Manager.Address(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if isManagerAddressNotFoundError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
|
||||||
// No additional information available about the address.
|
// No additional information available about the address.
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -3251,7 +3216,7 @@ func WalletPassphraseChange(w *wallet.Wallet, chainSvr *chain.Client, icmd inter
|
||||||
|
|
||||||
err := w.ChangePassphrase([]byte(cmd.OldPassphrase),
|
err := w.ChangePassphrase([]byte(cmd.OldPassphrase),
|
||||||
[]byte(cmd.NewPassphrase))
|
[]byte(cmd.NewPassphrase))
|
||||||
if isManagerWrongPassphraseError(err) {
|
if waddrmgr.IsError(err, waddrmgr.ErrWrongPassphrase) {
|
||||||
return nil, &btcjson.RPCError{
|
return nil, &btcjson.RPCError{
|
||||||
Code: btcjson.ErrRPCWalletPassphraseIncorrect,
|
Code: btcjson.ErrRPCWalletPassphraseIncorrect,
|
||||||
Message: "Incorrect passphrase",
|
Message: "Incorrect passphrase",
|
||||||
|
|
|
@ -191,8 +191,7 @@ func (w *Wallet) addRelevantTx(rec *wtxmgr.TxRecord, block *wtxmgr.BlockMeta) er
|
||||||
|
|
||||||
// Missing addresses are skipped. Other errors should
|
// Missing addresses are skipped. Other errors should
|
||||||
// be propagated.
|
// be propagated.
|
||||||
code := err.(waddrmgr.ManagerError).ErrorCode
|
if !waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
|
||||||
if code != waddrmgr.ErrAddressNotFound {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -752,8 +752,7 @@ func (w *Wallet) CurrentAddress(account uint32) (btcutil.Address, error) {
|
||||||
addr, err := w.Manager.LastExternalAddress(account)
|
addr, err := w.Manager.LastExternalAddress(account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If no address exists yet, create the first external address
|
// If no address exists yet, create the first external address
|
||||||
merr, ok := err.(waddrmgr.ManagerError)
|
if waddrmgr.IsError(err, waddrmgr.ErrAddressNotFound) {
|
||||||
if ok && merr.ErrorCode == waddrmgr.ErrAddressNotFound {
|
|
||||||
return w.NewAddress(account)
|
return w.NewAddress(account)
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -1349,8 +1348,7 @@ func (w *Wallet) ExportWatchingWallet(pubPass string) (string, error) {
|
||||||
// Only return the error is it's not because it's already
|
// Only return the error is it's not because it's already
|
||||||
// watching-only. When it is already watching-only, the code
|
// watching-only. When it is already watching-only, the code
|
||||||
// just falls through to the export below.
|
// just falls through to the export below.
|
||||||
if merr, ok := err.(waddrmgr.ManagerError); ok &&
|
if !waddrmgr.IsError(err, waddrmgr.ErrWatchingOnly) {
|
||||||
merr.ErrorCode != waddrmgr.ErrWatchingOnly {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue