def test_public_address_validator(self): # address too short self.assertFalse(isValidPublicAddress("aaa")) # address too long self.assertFalse(isValidPublicAddress("a" * 40)) # address with invalid checksum self.assertFalse( isValidPublicAddress("AZfFBeBqtJvaTK9JqG8uk6N7FppQY6byEA")) # valid address self.assertTrue( isValidPublicAddress("AZfFBeBqtJvaTK9JqG8uk6N7FppQY6byEg"))
def execute(self, arguments): asset_id = None from_addr = None watch_only = False do_count = False wallet = PromptData.Wallet arguments, from_addr_str = PromptUtils.get_from_addr(arguments) if from_addr_str: if not isValidPublicAddress(from_addr_str): print("Invalid address specified") return from_addr = wallet.ToScriptHash(from_addr_str) for item in arguments: if item == '--watch': watch_only = True elif item == '--count': do_count = True else: asset_id = PromptUtils.get_asset_id(wallet, item) return ShowUnspentCoins(wallet, asset_id, from_addr, watch_only, do_count)
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) < 4: print("Please specify the required parameters") return if len(arguments) > 5: # the 5th argument is the optional attributes, print("Too many parameters supplied. Please check your command") return addr = arguments[0] if not isValidPublicAddress(addr): print("Invalid address specified") return try: from_addr = wallet.ToScriptHash(addr) except ValueError as e: print(str(e)) return asset_id = PromptUtils.get_asset_id(wallet, arguments[1]) if not asset_id: print(f"Unknown asset id: {arguments[1]}") return try: index = int(arguments[2]) except ValueError: print(f"Invalid unspent index value: {arguments[2]}") return try: divisions = int(arguments[3]) except ValueError: print(f"Invalid divisions value: {arguments[3]}") return if divisions < 2: print("Divisions cannot be lower than 2") return if len(arguments) == 5: fee = Fixed8.TryParse(arguments[4], require_positive=True) if not fee: print(f"Invalid fee value: {arguments[4]}") return else: fee = Fixed8.Zero() return SplitUnspentCoin(wallet, asset_id, from_addr, index, divisions, fee)
def _validate_nep5_args(wallet, token_str, from_addr, to_addr, amount): """ A helper function to validate common arguments used in NEP-5 functions Args: wallet (Wallet): a UserWallet instance token_str (str): symbol name or script_hash from_addr (str): a wallet address to_addr (str): a wallet address amount (float): the number of tokens to send Raises: ValueError: for invalid arguments Returns: token (NEP5Token): instance """ try: token = PromptUtils.get_token(wallet, token_str) except ValueError: raise if not isValidPublicAddress(from_addr): raise ValueError("send_from is not a valid address") if not isValidPublicAddress(to_addr): raise ValueError("send_to is not a valid address") try: # internally this function uses the `Decimal` class which will parse the float amount to its required format. # the name is a bit misleading /shrug amount = amount_from_string(token, amount) except Exception: raise ValueError(f"{amount} is not a valid amount") return token
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) != 1: print("Please specify the required parameter") return False addr = arguments[0] if not isValidPublicAddress(addr): print("Invalid address specified") return False try: addr_script_hash = wallet.ToScriptHash(addr) wallet.AddWatchOnly(addr_script_hash) except ValueError as e: print(str(e)) return False print(f"Added address {addr} as watch-only") return True
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) < 2: print("Please specify the required parameters") return False arguments, priority_fee = PromptUtils.get_fee(arguments) token_str = arguments[0] try: token = PromptUtils.get_token(wallet, token_str) except ValueError as e: print(str(e)) return False register_addr = arguments[1:] addr_list = [] for addr in register_addr: if isValidPublicAddress(addr): addr_list.append(addr) else: print(f"{addr} is not a valid address") return False p_fee = Fixed8.Zero() if priority_fee is not None: p_fee = priority_fee if p_fee is False: logger.debug("invalid fee") return False tx, fee, results = token.CrowdsaleRegister(wallet, addr_list) if tx is not None and results is not None: if len(results) > 0 and results[0].GetBigInteger() > 0: print( "\n-----------------------------------------------------------" ) print("[%s] Will register addresses for crowdsale: %s " % (token.symbol, register_addr)) print("Invocation Fee: %s " % (fee.value / Fixed8.D)) print( "-------------------------------------------------------------\n" ) comb_fee = p_fee + fee if comb_fee != fee: print( f"Priority Fee ({p_fee.value / Fixed8.D}) + Invocation Fee ({fee.value / Fixed8.D}) = {comb_fee.value / Fixed8.D}\n" ) print("Enter your password to send to the network") try: passwd = prompt("[Password]> ", is_password=True) except KeyboardInterrupt: print("Registration cancelled") return False if not wallet.ValidatePassword(passwd): print("incorrect password") return False return InvokeContract(wallet, tx, comb_fee) print("Could not register address(es)") return False
def execute(self, arguments): wallet = PromptData.Wallet if len(arguments) < 2: print("Please specify the required parameters") return False if len(arguments) > 6: # the 3rd and 4th argument are for attaching neo/gas, 5th for attaching a fee, 6th for attaching attributes print("Too many parameters supplied. Please check your command") return False arguments, priority_fee = PromptUtils.get_fee(arguments) arguments, invoke_attrs = PromptUtils.get_tx_attr_from_args(arguments) token_str = arguments[0] try: token = PromptUtils.get_token(wallet, token_str) except ValueError as e: print(str(e)) return False to_addr = arguments[1] if not isValidPublicAddress(to_addr): print(f"{to_addr} is not a valid address") return False remaining_args = arguments[2:] asset_attachments = [] for optional in remaining_args: _, neo_to_attach, gas_to_attach = PromptUtils.get_asset_attachments( [optional]) if "attach-neo" in optional: if not neo_to_attach: print( f"Could not parse value from --attach-neo. Value must be an integer" ) return False else: asset_attachments.append(optional) if "attach-gas" in optional: if not gas_to_attach: print(f"Could not parse value from --attach-gas") return False else: asset_attachments.append(optional) fee = Fixed8.Zero() if priority_fee is not None: fee = priority_fee if fee is False: logger.debug("invalid fee") return False return token_mint(token, wallet, to_addr, asset_attachments=asset_attachments, fee=fee, invoke_attrs=invoke_attrs)