Repository: fetchai/tools-pocketbook

Issue:
Unknown address generates cryptic error
If you do a transfer to an address you do not have in the book, it generates this error:

```
Error: Unable to parse address, incorrect size
```

... instead of something like "Unknown address 'octopus'"


Comments:

Patch:
diff --git a/pocketbook/cli.py b/pocketbook/cli.py
index 27187da..6424598 100644
--- a/pocketbook/cli.py
+++ b/pocketbook/cli.py
@@ -11,7 +11,7 @@ from .commands.list import run_list
 from .commands.transfer import run_transfer
 from .commands.rename import run_rename
 from .disclaimer import display_disclaimer
-from .utils import NetworkUnavailableError
+from .utils import NetworkUnavailableError, checked_address
 
 
 def parse_commandline():
@@ -33,7 +33,7 @@ def parse_commandline():
 
     parser_add = subparsers.add_parser('add', help='Adds an address to the address book')
     parser_add.add_argument('name', help='The name of the key')
-    parser_add.add_argument('address', type=Address, help='The account address')
+    parser_add.add_argument('address', type=checked_address, help='The account address')
     parser_add.set_defaults(handler=run_add)
 
     parser_transfer = subparsers.add_parser('transfer')
diff --git a/pocketbook/utils.py b/pocketbook/utils.py
index 7298c64..a46f608 100644
--- a/pocketbook/utils.py
+++ b/pocketbook/utils.py
@@ -1,3 +1,4 @@
+from fetchai.ledger.crypto import Address
 
 
 class NetworkUnavailableError(Exception):
@@ -23,3 +24,11 @@ def create_api(name: str):
         pass
 
     raise NetworkUnavailableError()
+
+
+def checked_address(address):
+    try:
+        return Address(address)
+    except:
+        raise RuntimeError('Unable to convert {} into and address. The address needs to be a base58 encoded value'.format(address))
+


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index a0eee66..a752b53 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,7 +1,10 @@
 import unittest
 from unittest.mock import MagicMock, patch
 
-from pocketbook.utils import get_balance, get_stake, NetworkUnavailableError
+from fetchai.ledger.crypto import Address, Entity
+
+from pocketbook.utils import get_balance, get_stake, NetworkUnavailableError, checked_address
+
 
 class UtilsTests(unittest.TestCase):
 
@@ -14,7 +17,6 @@ class UtilsTests(unittest.TestCase):
 
         api.tokens.balance.assert_called_once_with('some address')
 
-
     def test_get_stake(self):
         api = MagicMock()
         api.tokens.stake.return_value = 50000000000
@@ -26,7 +28,6 @@ class UtilsTests(unittest.TestCase):
 
     @patch('fetchai.ledger.api.LedgerApi')
     def test_create_api(self, MockLedgerApi):
-
         # normal use
         from pocketbook.utils import create_api
         create_api('super-duper-net')
@@ -36,10 +37,23 @@ class UtilsTests(unittest.TestCase):
 
     @patch('fetchai.ledger.api.LedgerApi', side_effect=[RuntimeError('Bad Error')])
     def test_error_on_create_api(self, MockLedgerApi):
-
         # internal error case
         from pocketbook.utils import create_api
         with self.assertRaises(NetworkUnavailableError):
             create_api('super-duper-net')
 
-        MockLedgerApi.assert_called_once_with(network='super-duper-net')
\ No newline at end of file
+        MockLedgerApi.assert_called_once_with(network='super-duper-net')
+
+    def test_valid_address(self):
+        entity = Entity()
+        address = Address(entity)
+
+        recovered_address = checked_address(str(address))
+
+        self.assertEqual(str(recovered_address), str(address))
+
+    def test_invalid_address_exception(self):
+        with self.assertRaises(RuntimeError) as ctx:
+            checked_address('foo-bar-baz')
+        self.assertEqual(str(ctx.exception),
+                         'Unable to convert foo-bar-baz into and address. The address needs to be a base58 encoded value')