Skip to content

Commit 41aacb0

Browse files
committed
cleanup account name registry
1 parent 70bc105 commit 41aacb0

File tree

4 files changed

+123
-31
lines changed

4 files changed

+123
-31
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Change log
22

3+
### 0.1.2
4+
+ Improve registration of account names and resolving account names from the CNS registry
5+
36
### 0.1.1
47
+ Incease minimum topup_account funds to 10,000,000
58
+ Allow to register and load accounts using a name

convex_api/convex_api.py

+81-23
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def load_account(self, name, account):
153153
new_account = Account.import_from_account(account, address=address, name=name)
154154
return new_account
155155

156-
def setup_account(self, name, import_account):
156+
def setup_account(self, name, import_account, register_account=None):
157157
"""
158158
159159
Convenience method to create or load an account based on the account name.
@@ -162,10 +162,12 @@ def setup_account(self, name, import_account):
162162
163163
:param str name: name of the account to create or load
164164
:param Account account: :class:`.Account` object to import
165+
:param Account register_account: Optional :class:`.Account` object to use for registration of the account
165166
166167
:results: :class:`.Account` object with the address and name set, if not found then return None
167168
168-
**Note** This method calls the :meth:`.topup_account` method to get enougth funds to register an account name.
169+
**Note** If you do not provide a register_account, then this method calls the
170+
:meth:`.topup_account` method to get enougth funds to register an account name.
169171
170172
171173
.. code-block:: python
@@ -177,41 +179,73 @@ def setup_account(self, name, import_account):
177179
my_account
178180
179181
"""
182+
183+
# check to see if we can resolve the account name
180184
if self.resolve_account_name(name):
185+
# if so just load the account
181186
account = self.load_account(name, import_account)
182187
else:
188+
# new name , so first create the account
183189
account = self.create_account(account=import_account)
184-
self.topup_account(account)
185-
self.register_account_name(name, account)
186-
self.topup_account(account)
190+
if not register_account:
191+
# make sure we have enougth funds to do the registration
192+
self.topup_account(account)
193+
register_account = account
194+
account = self.register_account_name(name, account, register_account)
187195
return account
188196

189-
def register_account_name(self, name, account):
197+
def register_account_name(self, name, address_account, account=None):
190198
"""
191199
192-
Register an account address with an account name.
200+
Register or update an account address with an account name.
193201
194202
This call will submit to the CNS (Convex Name Service), a name in the format
195203
"`account.<your_name>`". You need to have some convex balance in your account, and
196204
a valid account address.
197205
198-
:param str name: name of the account to register
199-
:param Account account: :class:`.Account` object to register the account name
206+
:param str name: name of the account to register.
207+
:param number|Account account_address: Account or address to register.
208+
:param Account account: :class:`.Account` object to register the account name.
209+
210+
.. code-block:: python
200211
212+
>>> # load the register account
213+
>>> register_account = convex.load_account('register_account', import_account)
214+
>>> account = convex.create_account(import_account)
215+
>>> print(account.address)
216+
1024
217+
>>> account = convex.register_account('my_new_account', account.address, register_account)
218+
>>> print(account.address)
219+
1024
201220
202-
>>> # create a new account
203-
>>> account = convex.create_account()
204-
>>> # add some convex tokens to the account
205-
>>> convex.topup_account(account)
206-
10000000
207-
>>> account = convex.register_account('my_new_account', account)
208-
>>> print(account.name)
209-
my_new_account
221+
# or you can call with only one account, this will use the address of that account
222+
>>> print(register_account.address)
223+
404
224+
>>> account = convex.register_account('my_new_account', register_account)
225+
>>> print(account.address)
226+
404
210227
211228
"""
212-
if account.address:
213-
self._registry.register(f'account.{name}', account.address, account)
214-
return Account.import_from_account(account, address=account.address, name=name)
229+
230+
# is the address_account field only an address?
231+
if is_address(address_account):
232+
address = to_address(address_account)
233+
else:
234+
# if account then use the account address, and also see if we can use it for the
235+
# registration
236+
address = address_account.address
237+
if account is None:
238+
account = address_account
239+
240+
# we must have a valid account to do the registration
241+
if not account:
242+
raise ValueError('you need to provide a registration account to register an account name')
243+
244+
if not address:
245+
raise ValueError('You need to provide a valid address to register an account name')
246+
247+
self._registry.register(f'account.{name}', address, account)
248+
return Account.import_from_account(account, address=address, name=name)
215249

216250
def send(self, transaction, account, language=None, sequence_retry_count=20):
217251
"""
@@ -551,9 +585,6 @@ def get_account_info(self, address_account):
551585
'isLibrary': False, 'isActor': False, 'allowance': 0,
552586
'sequence': 0, 'type': 'user'}
553587
554-
555-
556-
557588
"""
558589
if is_address(address_account):
559590
address = to_address(address_account)
@@ -572,8 +603,31 @@ def get_account_info(self, address_account):
572603
return result
573604

574605
def resolve_account_name(self, name):
606+
"""
607+
Resolves an account name to an address.
608+
:param string name Name of the account to resolve.
609+
610+
.. code-block:: python
611+
612+
>>> convex.resolve_account_name('my_account')
613+
405
614+
615+
"""
575616
return self._registry.resolve_address(f'account.{name}')
576617

618+
def resolve_name(self, name):
619+
"""
620+
Resolves any Convex Name Services to an address.
621+
:param string name Name of the the CNS Service.
622+
623+
.. code-block:: python
624+
625+
>>> convex.resolve_account_name('convex.nft-tokens')
626+
25
627+
628+
"""
629+
return self._registry.resolve_address(name)
630+
577631
def _transaction_prepare(self, address, transaction, language=None, sequence_number=None):
578632
"""
579633
@@ -654,3 +708,7 @@ def language(self):
654708
655709
"""
656710
return self._language
711+
712+
@property
713+
def registry(self):
714+
return self._registry

tests/conftest.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@ def test_account_info():
4343
}
4444

4545
@pytest.fixture(scope='module')
46-
def test_account(convex, test_account_info):
47-
import_account = Account.import_from_bytes(test_account_info['private_bytes'])
48-
if convex.resolve_account_name(TEST_ACCOUNT_NAME):
49-
account = convex.load_account(TEST_ACCOUNT_NAME, import_account)
50-
else:
51-
account = convex.create_account(account=import_account)
52-
convex.topup_account(account)
53-
convex.register_account_name(TEST_ACCOUNT_NAME, account)
46+
def import_account(test_account_info):
47+
return Account.import_from_bytes(test_account_info['private_bytes'])
48+
49+
@pytest.fixture(scope='module')
50+
def test_account(convex, import_account):
51+
account = convex.setup_account(TEST_ACCOUNT_NAME, import_account)
5452
convex.topup_account(account)
5553
return account
5654

tests/intergration/test_convex_api.py

+33
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ def test_convex_get_account_info(convex_url, test_account):
7272
assert(info)
7373
assert(info['balance'] == TEST_FUNDING_AMOUNT)
7474

75+
def test_convex_account_name_registry(convex_url, test_account, import_account):
76+
account_name = f'test.convex-api.{secrets.token_hex(4)}'
77+
convex = ConvexAPI(convex_url)
78+
79+
address = convex.resolve_account_name(account_name)
80+
assert(not address)
81+
82+
account = convex.load_account(account_name, import_account)
83+
assert(not account)
84+
85+
86+
register_account = convex.register_account_name(account_name, test_account)
87+
assert(register_account.address == test_account.address)
88+
89+
assert(convex.resolve_account_name(account_name) == test_account.address)
90+
91+
new_account = convex.create_account(import_account)
92+
register_account = convex.register_account_name(account_name, new_account, test_account)
93+
94+
assert(register_account.address == new_account.address)
95+
assert(convex.resolve_account_name(account_name) == new_account.address)
96+
97+
# clear the cache in the registry
98+
convex.registry.clear()
99+
assert(convex.resolve_account_name(account_name) == new_account.address)
100+
101+
102+
def test_convex_resolve_name(convex_url):
103+
convex = ConvexAPI(convex_url)
104+
address = convex.resolve_name('convex.nft-tokens')
105+
assert(address)
106+
107+
75108
def test_convex_api_send_basic_lisp(convex_url, test_account):
76109
convex = ConvexAPI(convex_url)
77110
request_amount = convex.request_funds(TEST_FUNDING_AMOUNT, test_account)

0 commit comments

Comments
 (0)