def test_hashing_name_id(): assert hashing.name_id( 'aeternity.chain' ) == 'nm_S4ofw6861biSJrXgHuJPo7VotLbrY8P9ngTLvgrRwbDEA3svc' assert hashing.name_id( 'apeunit.chain' ) == 'nm_vXDbXQHeSqLUwXdYMioZdg4i1AizRR6kH5bzj16zzUN7gdFri' assert hashing.name_id( 'abc.chain') != 'nm_S4ofw6861biSJrXgHuJPo7VotLbrY8P9ngTLvgrRwbDEA3svc'
def delegate_name_revoke_signature(self, account: Account, contract_id: str, name: str): """ Helper to generate a signature to delegate a name revoke to a contract. Args: account: the account authorizing the transaction contract_id: the if of the contract executing the transaction name: the name being revoked Returns: the signature to use for delegation """ return self._delegate_common(account, hashing.name_id(name), contract_id)
def spend_by_name(self, account: Account, recipient_name: str, amount: int, payload: str = "", fee: int = defaults.FEE, tx_ttl: int = defaults.TX_TTL): """ Create and execute a spend to name_id transaction """ if utils.is_valid_aens_name(recipient_name): name_id = hashing.name_id(recipient_name) return self.spend(account, name_id, amount, payload, fee, tx_ttl) raise TypeError("Invalid AENS name. Please provide a valid AENS name.")
def claim(self, account, name_salt, preclaim_tx_hash, fee=defaults.FEE, tx_ttl=defaults.TX_TTL): self.preclaim_salt = name_salt # get the preclaim height try: pre_claim_tx = self.client.get_transaction_by_hash( hash=preclaim_tx_hash) self.preclaimed_block_height = pre_claim_tx.block_height except OpenAPIClientException: raise MissingPreclaim( f"Preclaim transaction {preclaim_tx_hash} not found") # if the commitment_id mismatch pre_claim_commitment_id = pre_claim_tx.tx.commitment_id commitment_id, _ = hashing.commitment_id(self.domain, salt=name_salt) if pre_claim_commitment_id != commitment_id: raise NameCommitmentIdMismatch( f"Committment id mismatch, wanted {pre_claim_commitment_id} got {commitment_id}" ) # if the transaction has not been mined if self.preclaimed_block_height <= 0: raise NameTooEarlyClaim( f"The pre-claim transaction has not been mined yet") # get the current height current_height = self.client.get_current_key_block_height() safe_height = self.preclaimed_block_height + self.client.config.key_block_confirmation_num if current_height < safe_height: raise NameTooEarlyClaim( f"It is not safe to execute the name claim before height {safe_height}, current height: {current_height}" ) # name encode name name = hashing.name_id(self.domain) # get the transaction builder txb = self.client.tx_builder # get the account nonce and ttl nonce, ttl = self.client._get_nonce_ttl(account.get_address(), tx_ttl) # create transaction tx = txb.tx_name_claim(account.get_address(), name, self.preclaim_salt, fee, ttl, nonce) # sign the transaction tx_signed = self.client.sign_transaction(account, tx.tx) # post the transaction to the chain self.client.broadcast_transaction(tx_signed.tx, tx_signed.hash) # update status self.status = AEName.Status.CLAIMED return tx_signed
def __init__(self, domain, client): if not utils.is_valid_aens_name(domain): raise ValueError("Invalid domain ", domain) self.client = client self.domain = domain.lower() self.name_id = hashing.name_id(domain) self.status = NameStatus.UNKNOWN # set after preclaimed: self.preclaimed_block_height = None self.preclaimed_tx_hash = None self.preclaimed_commitment_hash = None self.preclaim_salt = None # set after claimed self.name_ttl = 0 self.pointers = None
def spend(self, account: Account, recipient_id: str, amount, payload: str = "", fee: int = defaults.FEE, tx_ttl: int = defaults.TX_TTL) -> transactions.TxObject: """ Create and execute a spend transaction, automatically retrieve the nonce for the siging account and calculate the absolut ttl. :param account: the account signing the spend transaction (sender) :param recipient_id: the recipient address or name_id :param amount: the amount to spend :param payload: the payload for the transaction :param fee: the fee for the transaction (automatically calculated if not provided) :param tx_ttl: the transaction ttl expressed in relative number of blocks :return: the TxObject of the transaction :raises TypeError: if the recipient_id is not a valid name_id or address """ if utils.is_valid_aens_name(recipient_id): recipient_id = hashing.name_id(recipient_id) elif not utils.is_valid_hash(recipient_id, prefix="ak"): raise TypeError("Invalid recipient_id. Please provide a valid AENS name or account pub_key.") # parse amount and fee amount, fee = utils._amounts_to_aettos(amount, fee) # retrieve the nonce account.nonce = self.get_next_nonce(account.get_address()) if account.nonce == 0 else account.nonce + 1 # retrieve ttl tx_ttl = self.compute_absolute_ttl(tx_ttl) # build the transaction tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # get the signature tx = self.sign_transaction(account, tx) # post the signed transaction transaction self.broadcast_transaction(tx) return tx
def transfer_funds(self, account: Account, recipient_id: str, percentage: float, payload: str = "", tx_ttl: int = defaults.TX_TTL, fee: int = defaults.FEE, include_fee=True): """ Create and execute a spend transaction """ if utils.is_valid_aens_name(recipient_id): recipient_id = hashing.name_id(recipient_id) elif not utils.is_valid_hash(recipient_id, prefix="ak"): raise TypeError("Invalid recipient_id. Please provide a valid AENS name or account pub_key.") if percentage < 0 or percentage > 1: raise ValueError(f"Percentage should be a number between 0 and 1, got {percentage}") # parse amounts fee = utils.amount_to_aettos(fee) # retrieve the balance account_on_chain = self.get_account_by_pubkey(pubkey=account.get_address()) request_transfer_amount = int(account_on_chain.balance * percentage) # retrieve the nonce account.nonce = account_on_chain.nonce + 1 # retrieve ttl tx_ttl = self.compute_absolute_ttl(tx_ttl) # build the transaction tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, request_transfer_amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # if the request_transfer_amount should include the fee keep calculating the fee if include_fee: amount = request_transfer_amount while (amount + tx.data.fee) > request_transfer_amount: amount = request_transfer_amount - tx.data.fee tx = self.tx_builder.tx_spend(account.get_address(), recipient_id, amount, payload, fee, tx_ttl.absolute_ttl, account.nonce) # execute the transaction tx = self.sign_transaction(account, tx) # post the transaction self.broadcast_transaction(tx) return tx