@@ -153,7 +153,7 @@ def load_account(self, name, account):
153
153
new_account = Account .import_from_account (account , address = address , name = name )
154
154
return new_account
155
155
156
- def setup_account (self , name , import_account ):
156
+ def setup_account (self , name , import_account , register_account = None ):
157
157
"""
158
158
159
159
Convenience method to create or load an account based on the account name.
@@ -162,10 +162,12 @@ def setup_account(self, name, import_account):
162
162
163
163
:param str name: name of the account to create or load
164
164
:param Account account: :class:`.Account` object to import
165
+ :param Account register_account: Optional :class:`.Account` object to use for registration of the account
165
166
166
167
:results: :class:`.Account` object with the address and name set, if not found then return None
167
168
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.
169
171
170
172
171
173
.. code-block:: python
@@ -177,41 +179,73 @@ def setup_account(self, name, import_account):
177
179
my_account
178
180
179
181
"""
182
+
183
+ # check to see if we can resolve the account name
180
184
if self .resolve_account_name (name ):
185
+ # if so just load the account
181
186
account = self .load_account (name , import_account )
182
187
else :
188
+ # new name , so first create the account
183
189
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 )
187
195
return account
188
196
189
- def register_account_name (self , name , account ):
197
+ def register_account_name (self , name , address_account , account = None ):
190
198
"""
191
199
192
- Register an account address with an account name.
200
+ Register or update an account address with an account name.
193
201
194
202
This call will submit to the CNS (Convex Name Service), a name in the format
195
203
"`account.<your_name>`". You need to have some convex balance in your account, and
196
204
a valid account address.
197
205
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
200
211
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
201
220
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
210
227
211
228
"""
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 )
215
249
216
250
def send (self , transaction , account , language = None , sequence_retry_count = 20 ):
217
251
"""
@@ -551,9 +585,6 @@ def get_account_info(self, address_account):
551
585
'isLibrary': False, 'isActor': False, 'allowance': 0,
552
586
'sequence': 0, 'type': 'user'}
553
587
554
-
555
-
556
-
557
588
"""
558
589
if is_address (address_account ):
559
590
address = to_address (address_account )
@@ -572,8 +603,31 @@ def get_account_info(self, address_account):
572
603
return result
573
604
574
605
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
+ """
575
616
return self ._registry .resolve_address (f'account.{ name } ' )
576
617
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
+
577
631
def _transaction_prepare (self , address , transaction , language = None , sequence_number = None ):
578
632
"""
579
633
@@ -654,3 +708,7 @@ def language(self):
654
708
655
709
"""
656
710
return self ._language
711
+
712
+ @property
713
+ def registry (self ):
714
+ return self ._registry
0 commit comments