Repository: rotki/rotki

Issue:
Importing BTC accounts via XPUB fails 
## Problem Definition

Importing BTC accounts via xpub and derivation path hangs forever and fails adding any account.
I tried several times and restarted Rotki without improvement.
After clicking the "save" button, it becomes disabled and that is pretty much it.
The dialog does not ever vanish (even after waiting a long time...). Clicking Cancel (the only option) ends up (obviously) with the xpub accounts not added.

NOTE: it may be related to the fact that I initially added the xpub and forgot the derivation path (I initially missed the location where it could be added), so I added the account, removed it, etc... I guess that may be causing the issue.

## Logs

I checked both backend and electron logs and they contain nothing related to the issue.

### System Description

Operating system: MacOS Catalina
Rotki version: 1.10.1



Discussion:
I tried with a "never seen" xpub and got the same issue.
Hmm that should not happen. Derivation path is not needed and is a very advanced option, which is probably not what you want to do.

We always need an "account" xpub from either a native segwit, segwit or legacy type. Adding the derivation path should not be needed.

Where are you getting the xpub from? What type is it?
I figured how to get around the issue. I was using the xpub from the Ledger Live but the values provided there do not work.
I think however that if Rokti is unhappy, it should show and error of some sort. Right now it seems to hang doing nothing.

The workaround I found is to import the account in electrum and then get the xpub (ypub in that case...) from there.
Then indeed, without providing a derivation key, it works. I had a few collisions since I did add the single addresses manually before but cleaning those up was not an issue.

I see. You need to know the type of xpub you have.

You can also use the xpub you get from ledger live directly, but have to choose the type correctly (in this case p2sh_p2wpkh, also known as segwit). In electrum speak it's ypub.

There is no way for rotki (or any other software) to detect an error here. There is no wrong xpub. Just xpub with no used addresses yet, which is not really an error.


> I think however that if Rokti is unhappy, it should show and error of some sort. Right now it seems to hang doing nothing

Yes that would indeed be a bug. Hanging is not good.

 I wonder how I can reproduce it. Last time I tried with a user's xpub from ledger live and wrong xpub type, it just generates addresses, finds no used ones and stops.
At first, since I did not find where to enter the derivation path, I provided the xpub from Ledger Live.
It did add the account and obviously showed me a 0 balance as expected.
I then started playing with various derivation paths including those shown in Ledger Live.
They look like `44'/0'/0'/0/4`. I tried adding `m` in front but nothing helped. Even `m/44'/0'/0' did not work if I remember right.

So I agree that using the "wrong" xpub should show me the wrong accounts (and thus a very very likely 0.00 balance) but it should not hang.

I could reproduce using:
![image](https://user-images.githubusercontent.com/738724/102718478-94ba1100-42e8-11eb-8164-2c1c9fbbdba4.png)

- xpub: `xpub00000000001111111111222222222233333333334444444444555555555566666666667777777777888888888899999999990123456`
- derivation path: `44'/0'/0'/0/4`

PS: Don't share my xpub 🤣 


Thanks a lot! Will try to reproduce.

> PS: Don't share my xpub

:thinking: 

Resolving patch:
diff --git a/rotkehlchen/chain/bitcoin/hdkey.py b/rotkehlchen/chain/bitcoin/hdkey.py
--- a/rotkehlchen/chain/bitcoin/hdkey.py
+++ b/rotkehlchen/chain/bitcoin/hdkey.py
@@ -161,7 +161,11 @@ def from_xpub(
         May raise:
         - XPUBError if there is a problem with decoding the xpub
         """
-        xpub_bytes = b58decode(xpub)
+        try:
+            xpub_bytes = b58decode(xpub)
+        except ValueError as e:
+            raise XPUBError(f'Given XPUB {xpub} is not b58 encoded') from e
+
         if len(xpub_bytes) < 78:
             raise XPUBError(f'Given XPUB {xpub} is too small')
 


Test patch: