From c2af7acc312fc766f0380f720644eb4ccc209f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sa=C5=82aban?= Date: Mon, 9 Sep 2019 12:57:06 +0200 Subject: [PATCH] Update .new_address() method to return subaddress index --- docs/source/release_notes.rst | 6 +++++ docs/source/wallet.rst | 7 ++--- monero/account.py | 3 ++- monero/backends/jsonrpc.py | 2 +- .../test_new_address-00-get_accounts.json | 26 +++++++++++++++++++ ..._new_address-10-new_address_account_0.json | 8 ++++++ ..._new_address-20-new_address_account_1.json | 8 ++++++ tests/test_jsonrpcwallet.py | 21 ++++++++++++++- 8 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 tests/data/test_jsonrpcwallet/test_new_address-00-get_accounts.json create mode 100644 tests/data/test_jsonrpcwallet/test_new_address-10-new_address_account_0.json create mode 100644 tests/data/test_jsonrpcwallet/test_new_address-20-new_address_account_1.json diff --git a/docs/source/release_notes.rst b/docs/source/release_notes.rst index d7e415c..aa8e3cb 100644 --- a/docs/source/release_notes.rst +++ b/docs/source/release_notes.rst @@ -6,6 +6,12 @@ Release Notes With version 0.6 the package name on PyPi has changed from `monero-python` to just `monero`. +Backward-incompatible changes: + + 1. The ``.new_address()`` method of both ``Wallet`` and ``Account`` returns a 2-element tuple of + (`subaddress`, `index`) where the additional element is the index of the subaddress within + current account. + 0.5 --- diff --git a/docs/source/wallet.rst b/docs/source/wallet.rst index 572f99b..c6f191b 100644 --- a/docs/source/wallet.rst +++ b/docs/source/wallet.rst @@ -50,12 +50,13 @@ Creating accounts and addresses Every wallet can have separate accounts and each account can have numerous addresses. The ``Wallet.new_account()`` and ``Account.new_address()`` will -create new instances. +create new instances, then return a tuple consisting of the subaddress itself, +and the subaddress index within the account. .. code-block:: python In [9]: w.new_address() - Out[9]: BenuGf8eyVhjZwdcxEJY1MHrUfqHjPvE3d7Pi4XY5vQz53VnVpB38bCBsf8AS5rJuZhuYrqdG9URc2eFoCNPwLXtLENT4R7 + Out[9]: (BenuGf8eyVhjZwdcxEJY1MHrUfqHjPvE3d7Pi4XY5vQz53VnVpB38bCBsf8AS5rJuZhuYrqdG9URc2eFoCNPwLXtLENT4R7, 1) In [10]: w.get_addresses() Out[10]: @@ -72,7 +73,7 @@ create new instances. Out[13]: Bhd3PRVCnq5T5jjNey2hDSM8DxUgFpNjLUrKAa2iYVhYX71RuCGTekDKZKXoJPAGL763kEXaDSAsvDYb8bV77YT7Jo19GKY In [14]: w.accounts[1].new_address() - Out[14]: Bbz5uCtnn3Gaj1YAizaHw1FPeJ6T7kk7uQoeY48SWjezEAyrWScozLxYbqGxsV5L6VJkvw5VwECAuLVJKQtHpA3GFXJNPYu + Out[14]: (Bbz5uCtnn3Gaj1YAizaHw1FPeJ6T7kk7uQoeY48SWjezEAyrWScozLxYbqGxsV5L6VJkvw5VwECAuLVJKQtHpA3GFXJNPYu, 1) In [15]: w.accounts[1].get_addresses() Out[15]: diff --git a/monero/account.py b/monero/account.py index dd1e2b9..607072d 100644 --- a/monero/account.py +++ b/monero/account.py @@ -64,7 +64,8 @@ class Account(object): Creates a new address. :param label: address label as `str` - :rtype: :class:`SubAddress ` + :rtype: tuple of subaddress, subaddress index (minor): + (:class:`SubAddress `, `int`) """ return self._backend.new_address(account=self.index, label=label) diff --git a/monero/backends/jsonrpc.py b/monero/backends/jsonrpc.py index c79f692..9fe6576 100644 --- a/monero/backends/jsonrpc.py +++ b/monero/backends/jsonrpc.py @@ -174,7 +174,7 @@ class JSONRPCWallet(object): def new_address(self, account=0, label=None): _address = self.raw_request( 'create_address', {'account_index': account, 'label': label}) - return SubAddress(_address['address']) + return SubAddress(_address['address']), _address['address_index'] def balances(self, account=0): _balance = self.raw_request('getbalance', {'account_index': account}) diff --git a/tests/data/test_jsonrpcwallet/test_new_address-00-get_accounts.json b/tests/data/test_jsonrpcwallet/test_new_address-00-get_accounts.json new file mode 100644 index 0000000..5334e6d --- /dev/null +++ b/tests/data/test_jsonrpcwallet/test_new_address-00-get_accounts.json @@ -0,0 +1,26 @@ +{ + "id": 0, + "jsonrpc": "2.0", + "result": { + "subaddress_accounts": [ + { + "account_index": 0, + "balance": 111141601989972, + "base_address": "56cXYWG13YKaT9z1aEy2hb9TZNnxrW3zE9S4nTQVDux5Qq7UYsmjuux3Zstxkorj9HAufyWLU3FwHW4uERQF6tkeUVogGN3", + "label": "Primary account", + "tag": "", + "unlocked_balance": 111141601989972 + }, + { + "account_index": 1, + "balance": 1000000000000, + "base_address": "79kTZg96pMf2Dt9rLEWnLzTUB8XC1wMhxaJyxa79hJu6bK9CfFnfbSL1GJNZbqhv9xPqJhRj2Yfb7QUWa2zeEw56H4KiUfN", + "label": "Untitled account", + "tag": "", + "unlocked_balance": 1000000000000 + } + ], + "total_balance": 112141601989972, + "total_unlocked_balance": 112141601989972 + } +} diff --git a/tests/data/test_jsonrpcwallet/test_new_address-10-new_address_account_0.json b/tests/data/test_jsonrpcwallet/test_new_address-10-new_address_account_0.json new file mode 100644 index 0000000..fcfae34 --- /dev/null +++ b/tests/data/test_jsonrpcwallet/test_new_address-10-new_address_account_0.json @@ -0,0 +1,8 @@ +{ + "id": 0, + "jsonrpc": "2.0", + "result": { + "address": "7AEBRUmNcjhUjiqdVpeKKYiAVZ216AYdhBFx8UUfjPhWdKujoosnsUtHCohLcYWUXFdNiqnBsMmCFCyDkSmat3Ys4H4yHUp", + "address_index": 232 + } +} diff --git a/tests/data/test_jsonrpcwallet/test_new_address-20-new_address_account_1.json b/tests/data/test_jsonrpcwallet/test_new_address-20-new_address_account_1.json new file mode 100644 index 0000000..3a6a1bf --- /dev/null +++ b/tests/data/test_jsonrpcwallet/test_new_address-20-new_address_account_1.json @@ -0,0 +1,8 @@ +{ + "id": 0, + "jsonrpc": "2.0", + "result": { + "address": "74PY6ZcC4N5U4MN4aTPQE58d35zGsFYK17NBx9GA66Bc37V5q6UU7eG6uLExg5m2TH5DJZc9BuEs64jEStTSV2PNLt2Y3wb", + "address_index": 3 + } +} diff --git a/tests/test_jsonrpcwallet.py b/tests/test_jsonrpcwallet.py index a639fce..a16f9f2 100644 --- a/tests/test_jsonrpcwallet.py +++ b/tests/test_jsonrpcwallet.py @@ -7,7 +7,7 @@ except ImportError: from mock import patch, Mock from monero.wallet import Wallet -from monero.address import BaseAddress, Address +from monero.address import BaseAddress, Address, SubAddress from monero.seed import Seed from monero.transaction import IncomingPayment, OutgoingPayment, Transaction from monero.backends.jsonrpc import JSONRPCWallet @@ -165,6 +165,25 @@ class JSONRPCWalletTestCase(JSONTestCase): self.assertEqual('account 1', w.accounts[1].label) self.assertEqual([acc0, acc1], w.accounts) + @responses.activate + def test_new_address(self): + responses.add(responses.POST, self.jsonrpc_url, + json=self._read('test_new_address-00-get_accounts.json'), + status=200) + responses.add(responses.POST, self.jsonrpc_url, + json=self._read('test_new_address-10-new_address_account_0.json'), + status=200) + responses.add(responses.POST, self.jsonrpc_url, + json=self._read('test_new_address-20-new_address_account_1.json'), + status=200) + w = Wallet(JSONRPCWallet()) + subaddr, index = w.new_address() + self.assertIsInstance(subaddr, SubAddress) + self.assertIsInstance(index, int) + subaddr, index = w.accounts[1].new_address() + self.assertIsInstance(subaddr, SubAddress) + self.assertIsInstance(index, int) + @patch('monero.backends.jsonrpc.requests.post') def test_incoming_confirmed(self, mock_post): mock_post.return_value.status_code = 200