Repository: Bitmessage/PyBitmessage

Issue:
Error on send message via API
If you send a message to an address registered in the address book via API, an error will be returned.

```log
2021-12-11 02:50:58,436 - ERROR - too many values to unpack
Traceback (most recent call last):
  File "api.py", line 1514, in _dispatch
  File "api.py", line 1487, in _handle_request
  File "api.py", line 1137, in HandleSendMessage
ValueError: too many values to unpack
```

OS: Windows 10 21H1
PyBitmessage: 0.6.3.2 20211207


Discussion:
Could I see your code?
The first thing you can prepare is written in C, is that okay?
> The first thing you can prepare is written in C, is that okay?

I'm not afraid (;

The line number of your traceback seems to point to decoding of base64 in subject argument
https://github.com/teruteru128/study/blob/develop/src/bmsendmsg.c
https://github.com/teruteru128/libstudy/blob/master/src/bmapi.c
Hmm ... should I submit something made with python?
I think it's probably not difficult because it depends on whether there is a corresponding record in the address book using the XML-RPC API.

> The line number of your traceback seems to point to decoding of base64 in subject argument

Oh, I see it's really about the label: https://github.com/Bitmessage/PyBitmessage/blob/v0.6/src/api.py#L1137
> Hmm ... should I submit something made with python? I think it's probably not difficult because it depends on whether there is a corresponding record in the address book using the XML-RPC API.

Yes, if you can reproduce it in python. Don't you get this exception when using JSON-RPC? It seems not related.
Perhaps try to remove the comma in the line. Does it help?

Пт, 10 груд. 2021 20:23 користувач Teruteru ***@***.***> пише:

> Hmm ... should I submit something made with python?
> I think it's probably not difficult because it depends on whether there is
> a corresponding record in the address book using the XML-RPC API.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/Bitmessage/PyBitmessage/issues/1898#issuecomment-991194843>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AA6TVHCMMBA4ZFVNGXXTS5DUQJARPANCNFSM5JZW46PQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>

Um ... I'm sorry, I'm using PyBitmessage distributed as a binary on Windows, so I can't modify the source code and test it right away.
However, the indication of removing the comma seems correct.
OK, so I have to write a test and merge the fix or at least build a new
continuous release. Maybe tomorrow.

Пт, 10 груд. 2021 20:46 користувач Teruteru ***@***.***> пише:

> Um ... I'm sorry, I'm using PyBitmessage distributed as a binary on
> Windows, so I can't modify the source code and test it right away.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/Bitmessage/PyBitmessage/issues/1898#issuecomment-991210366>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AA6TVHASNI67VHCOH4XSHJTUQJDGVANCNFSM5JZW46PQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>

The source code has been updated. I think this makes it easier to see.
https://github.com/teruteru128/study/blob/9324573/src/bmsendmsg.c
https://github.com/teruteru128/libstudy/blob/d3475a2/src/bmapi.c#L88-L126
The `ValueError` is confusing me. However I was able to reproduce a scaled down version of the exception, maybe it will help (I don't have more time at the moment to look into it).

```
>>> r = [[[0, 0, 0], 0, 0]]
>>> toLabel, = r[0][0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
```

If I had to take a wild guess, I would say there are duplicate records in the address book, or the `label` column is stored in an unusual storage type (a `BLOB` instead of a `TEXT`, maybe). This type of problem should still be handled better in PyBM though.
Try bytes:
toLabel = b'test'

Сб, 11 груд. 2021 03:20 користувач Peter Šurda ***@***.***>
пише:

> The ValueError is confusing me. However I was able to reproduce a scaled
> down version of the exception, maybe it will help (I don't have more time
> at the moment to look into it).
>
> >>> r = [[[0, 0, 0], 0, 0]]
> >>> toLabel, = r[0][0]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: too many values to unpack
>
> If I had to take a wild guess, I would say there are duplicate records in
> the address book, or the label column is stored in an unusual storage
> type (a blob instead of a string, maybe). This type of problem should still
> be handled better in PyBM though.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <https://github.com/Bitmessage/PyBitmessage/issues/1898#issuecomment-991401902>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AA6TVHFBX7N3NUTAA6XKI6DUQKROPANCNFSM5JZW46PQ>
> .
> Triage notifications on the go with GitHub Mobile for iOS
> <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
> or Android
> <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
>
>

> Try bytes: `toLabel = b'test'`

Yep looks like you're right:

```
>>> r = [[b'sldfkjas']]
>>> toLabel, = r[0][0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
```
Well the same happens with a string, makes sense for python2. So the comma shouldn't be there, it's a bug.

Resolving patch:
diff --git a/src/api.py b/src/api.py
--- a/src/api.py
+++ b/src/api.py
@@ -1134,7 +1134,7 @@ def HandleSendMessage(
         queryreturn = sqlQuery(
             "SELECT label FROM addressbook WHERE address=?", toAddress)
         try:
-            toLabel, = queryreturn[0][0]
+            toLabel = queryreturn[0][0]
         except IndexError:
             pass
 


Test patch:
diff --git a/src/tests/test_api.py b/src/tests/test_api.py
--- a/src/tests/test_api.py
+++ b/src/tests/test_api.py
@@ -58,7 +58,8 @@ class TestAPI(TestAPIProto):
     _seed = base64.encodestring(sample_seed)
 
     def _add_random_address(self, label):
-        return self.api.createRandomAddress(base64.encodestring(label))
+        addr = self.api.createRandomAddress(base64.encodestring(label))
+        return addr
 
     def test_user_password(self):
         """Trying to connect with wrong username/password"""
@@ -243,6 +244,15 @@ def test_addressbook(self):
             entries['address'], sample_deterministic_addr4)
         self.assertEqual(
             base64.decodestring(entries['label']), 'tiger_4')
+        # Try sending to this address (#1898)
+        addr = self._add_random_address('random_2')
+        # TODO: it was never deleted
+        msg = base64.encodestring('test message')
+        msg_subject = base64.encodestring('test_subject')
+        result = self.api.sendMessage(
+            sample_deterministic_addr4, addr, msg_subject, msg)
+        self.assertNotRegexpMatches(result, r'^API Error')
+        self.api.deleteAddress(addr)
         # Remove known address
         self.api.deleteAddressBookEntry(sample_deterministic_addr4)
         # Addressbook should be empty again