@@ -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
0 commit comments