code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
func = InvokeFunction('approve') if not isinstance(amount, int): raise SDKException(ErrorCode.param_err('the data type of amount should be int.')) if amount < 0: raise SDKException(ErrorCode.param_err('the amount should be equal or great than 0.')) owner_address = owner_acct.get_address().to_bytes() Oep4.__b58_address_check(b58_spender_address) spender_address = Address.b58decode(b58_spender_address).to_bytes() func.set_params_value(owner_address, spender_address, amount) tx_hash = self.__sdk.get_network().send_neo_vm_transaction(self.__hex_contract_address, owner_acct, payer_acct, gas_limit, gas_price, func) return tx_hash
def approve(self, owner_acct: Account, b58_spender_address: str, amount: int, payer_acct: Account, gas_limit: int, gas_price: int)
This interface is used to call the Approve method in ope4 that allows spender to withdraw a certain amount of oep4 token from owner account multiple times. If this function is called again, it will overwrite the current allowance with new value. :param owner_acct: an Account class that indicate the owner. :param b58_spender_address: a base58 encode address that be allowed to spend the oep4 token in owner's account. :param amount: an int value that indicate the amount oep4 token that will be transferred in this transaction. :param payer_acct: an Account class that used to pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: the hexadecimal transaction hash value.
3.936793
3.987792
0.987211
func = InvokeFunction('allowance') Oep4.__b58_address_check(b58_owner_address) owner = Address.b58decode(b58_owner_address).to_bytes() Oep4.__b58_address_check(b58_spender_address) spender = Address.b58decode(b58_spender_address).to_bytes() func.set_params_value(owner, spender) result = self.__sdk.get_network().send_neo_vm_transaction_pre_exec(self.__hex_contract_address, None, func) try: allowance = ContractDataParser.to_int(result['Result']) except SDKException: allowance = 0 return allowance
def allowance(self, b58_owner_address: str, b58_spender_address: str)
This interface is used to call the Allowance method in ope4 that query the amount of spender still allowed to withdraw from owner account. :param b58_owner_address: a base58 encode address that represent owner's account. :param b58_spender_address: a base58 encode address that represent spender's account. :return: the amount of oep4 token that owner allow spender to transfer from the owner account.
4.267992
4.103526
1.040079
func = InvokeFunction('transferFrom') Oep4.__b58_address_check(b58_from_address) Oep4.__b58_address_check(b58_to_address) if not isinstance(spender_acct, Account): raise SDKException(ErrorCode.param_err('the data type of spender_acct should be Account.')) spender_address_array = spender_acct.get_address().to_bytes() from_address_array = Address.b58decode(b58_from_address).to_bytes() to_address_array = Address.b58decode(b58_to_address).to_bytes() if not isinstance(value, int): raise SDKException(ErrorCode.param_err('the data type of value should be int.')) func.set_params_value(spender_address_array, from_address_array, to_address_array, value) params = func.create_invoke_code() unix_time_now = int(time.time()) params.append(0x67) bytearray_contract_address = bytearray.fromhex(self.__hex_contract_address) bytearray_contract_address.reverse() for i in bytearray_contract_address: params.append(i) if payer_acct is None: raise SDKException(ErrorCode.param_err('payer account is None.')) payer_address_array = payer_acct.get_address().to_bytes() tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_address_array, params, bytearray(), []) tx.sign_transaction(spender_acct) if spender_acct.get_address_base58() != payer_acct.get_address_base58(): tx.add_sign_transaction(payer_acct) tx_hash = self.__sdk.get_network().send_raw_transaction(tx) return tx_hash
def transfer_from(self, spender_acct: Account, b58_from_address: str, b58_to_address: str, value: int, payer_acct: Account, gas_limit: int, gas_price: int)
This interface is used to call the Allowance method in ope4 that allow spender to withdraw amount of oep4 token from from-account to to-account. :param spender_acct: an Account class that actually spend oep4 token. :param b58_from_address: an base58 encode address that actually pay oep4 token for the spender's spending. :param b58_to_address: a base58 encode address that receive the oep4 token. :param value: the amount of ope4 token in this transaction. :param payer_acct: an Account class that used to pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: the hexadecimal transaction hash value.
2.7779
2.806735
0.989726
scrypt_n = Scrypt().n pri_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, b58_address, salt, scrypt_n, self.scheme) info = self.__create_identity(label, pwd, salt, pri_key) for identity in self.wallet_in_mem.identities: if identity.ont_id == info.ont_id: return identity raise SDKException(ErrorCode.other_error('Import identity failed.'))
def import_identity(self, label: str, encrypted_pri_key: str, pwd: str, salt: str, b58_address: str) -> Identity
This interface is used to import identity by providing encrypted private key, password, salt and base58 encode address which should be correspond to the encrypted private key provided. :param label: a label for identity. :param encrypted_pri_key: an encrypted private key in base64 encoding from. :param pwd: a password which is used to encrypt and decrypt the private key. :param salt: a salt value which will be used in the process of encrypt private key. :param b58_address: a base58 encode address which correspond with the encrypted private key provided. :return: if succeed, an Identity object will be returned.
6.526369
7.454556
0.875487
salt = get_random_hex_str(16) identity = self.__create_identity(label, pwd, salt, private_key) return identity
def create_identity_from_private_key(self, label: str, pwd: str, private_key: str) -> Identity
This interface is used to create identity based on given label, password and private key. :param label: a label for identity. :param pwd: a password which will be used to encrypt and decrypt the private key. :param private_key: a private key in the form of string. :return: if succeed, an Identity object will be returned.
4.800086
5.762206
0.833029
pri_key = get_random_hex_str(64) salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] acct = self.__create_account(label, pwd, salt, pri_key, True) return self.get_account_by_b58_address(acct.get_address_base58(), pwd)
def create_account(self, pwd: str, label: str = '') -> Account
This interface is used to create account based on given password and label. :param label: a label for account. :param pwd: a password which will be used to encrypt and decrypt the private key :return: if succeed, return an data structure which contain the information of a wallet account.
4.012677
4.08475
0.982356
salt = base64.b64decode(b64_salt.encode('ascii')).decode('latin-1') private_key = Account.get_gcm_decoded_private_key(encrypted_pri_key, pwd, b58_address, salt, n, self.scheme) acct_info = self.create_account_info(label, pwd, salt, private_key) for acct in self.wallet_in_mem.accounts: if not isinstance(acct, AccountData): raise SDKException(ErrorCode.other_error('Invalid account data in memory.')) if acct_info.address_base58 == acct.b58_address: return acct raise SDKException(ErrorCode.other_error('Import account failed.'))
def import_account(self, label: str, encrypted_pri_key: str, pwd: str, b58_address: str, b64_salt: str, n: int = 16384) -> AccountData
This interface is used to import account by providing account data. :param label: str, wallet label :param encrypted_pri_key: str, an encrypted private key in base64 encoding from :param pwd: str, a password which is used to encrypt and decrypt the private key :param b58_address: str, a base58 encode wallet address value :param b64_salt: str, a base64 encode salt value which is used in the encryption of private key :param n: int, CPU/Memory cost parameter. It must be a power of 2 and less than :math:`2^{32}` :return: if succeed, return an data structure which contain the information of a wallet account. if failed, return a None object.
4.215127
4.618728
0.912616
salt = get_random_hex_str(16) if len(label) == 0 or label is None: label = uuid.uuid4().hex[0:8] info = self.create_account_info(label, password, salt, private_key) for acct in self.wallet_in_mem.accounts: if info.address_base58 == acct.b58_address: return acct raise SDKException(ErrorCode.other_error(f'Create account from key {private_key} failed.'))
def create_account_from_private_key(self, password: str, private_key: str, label: str = '') -> AccountData
This interface is used to create account by providing an encrypted private key and it's decrypt password. :param label: a label for account. :param password: a password which is used to decrypt the encrypted private key. :param private_key: a private key in the form of string. :return: if succeed, return an AccountData object. if failed, return a None object.
5.19369
5.487151
0.946518
WalletManager.__check_ont_id(ont_id) for identity in self.wallet_in_mem.identities: if identity.ont_id == ont_id: addr = identity.ont_id.replace(DID_ONT, "") key = identity.controls[0].key salt = base64.b64decode(identity.controls[0].salt) n = self.wallet_in_mem.scrypt.n private_key = Account.get_gcm_decoded_private_key(key, password, addr, salt, n, self.scheme) return Account(private_key, self.scheme) raise SDKException(ErrorCode.other_error(f'Get account {ont_id} failed.'))
def get_account_by_ont_id(self, ont_id: str, password: str) -> Account
:param ont_id: OntId. :param password: a password which is used to decrypt the encrypted private key. :return:
5.433712
5.603566
0.969688
acct = self.get_account_data_by_b58_address(b58_address) n = self.wallet_in_mem.scrypt.n salt = base64.b64decode(acct.salt) private_key = Account.get_gcm_decoded_private_key(acct.key, password, b58_address, salt, n, self.scheme) return Account(private_key, self.scheme)
def get_account_by_b58_address(self, b58_address: str, password: str) -> Account
:param b58_address: a base58 encode address. :param password: a password which is used to decrypt the encrypted private key. :return:
4.838737
5.057518
0.956742
for acct in self.wallet_in_mem.accounts: if not isinstance(acct, AccountData): raise SDKException(ErrorCode.other_error('Invalid account data in memory.')) if acct.is_default: return acct raise SDKException(ErrorCode.get_default_account_err)
def get_default_account_data(self) -> AccountData
This interface is used to get the default account in WalletManager. :return: an AccountData object that contain all the information of a default account.
6.948794
6.311472
1.100978
for func in self.functions: if func['name'] == name: return AbiFunction(func['name'], func['parameters'], func.get('returntype', '')) return None
def get_function(self, name: str) -> AbiFunction or None
This interface is used to get an AbiFunction object from AbiInfo object by given function name. :param name: the function name in abi file :return: if succeed, an AbiFunction will constructed based on given function name
3.981077
4.705536
0.846041
is_even = public_key.startswith(b'\x02') x = string_to_number(public_key[1:]) curve = NIST256p.curve order = NIST256p.order p = curve.p() alpha = (pow(x, 3, p) + (curve.a() * x) + curve.b()) % p beta = square_root_mod_prime(alpha, p) if is_even == bool(beta & 1): y = p - beta else: y = beta point = Point(curve, x, y, order) return b''.join([number_to_string(point.x(), order), number_to_string(point.y(), order)])
def __uncompress_public_key(public_key: bytes) -> bytes
Uncompress the compressed public key. :param public_key: compressed public key :return: uncompressed public key
2.782565
2.768293
1.005156
payload = self.generate_json_rpc_payload(RpcMethod.GET_VERSION) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_version(self, is_full: bool = False) -> dict or str
This interface is used to get the version information of the connected node in current network. Return: the version information of the connected node.
6.050577
5.644289
1.071982
payload = self.generate_json_rpc_payload(RpcMethod.GET_NODE_COUNT) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_connection_count(self, is_full: bool = False) -> int
This interface is used to get the current number of connections for the node in current network. Return: the number of connections.
6.707229
6.245479
1.073934
payload = self.generate_json_rpc_payload(RpcMethod.GET_GAS_PRICE) response = self.__post(self.__url, payload) if is_full: return response return response['result']['gasprice']
def get_gas_price(self, is_full: bool = False) -> int or dict
This interface is used to get the gas price in current network. Return: the value of gas price.
5.030321
4.663428
1.078674
payload = self.generate_json_rpc_payload(RpcMethod.GET_NETWORK_ID) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_network_id(self, is_full: bool = False) -> int
This interface is used to get the network id of current network. Return: the network id of current network.
5.431417
5.209512
1.042596
payload = self.generate_json_rpc_payload(RpcMethod.GET_BLOCK, [block_hash, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_block_by_hash(self, block_hash: str, is_full: bool = False) -> dict
This interface is used to get the hexadecimal hash value of specified block height in current network. :param block_hash: a hexadecimal value of block hash. :param is_full: :return: the block information of the specified block hash.
4.999509
4.952527
1.009487
payload = self.generate_json_rpc_payload(RpcMethod.GET_BLOCK, [height, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_block_by_height(self, height: int, is_full: bool = False) -> dict
This interface is used to get the block information by block height in current network. Return: the decimal total number of blocks in current network.
5.335999
5.146399
1.036841
payload = self.generate_json_rpc_payload(RpcMethod.GET_BLOCK_COUNT) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_block_count(self, is_full: bool = False) -> int or dict
This interface is used to get the decimal block number in current network. Return: the decimal total number of blocks in current network.
5.26478
4.866443
1.081854
response = self.get_block_count(is_full=True) response['result'] -= 1 if is_full: return response return response['result']
def get_block_height(self, is_full: bool = False) -> int or dict
This interface is used to get the decimal block height in current network. Return: the decimal total height of blocks in current network.
5.16446
4.45611
1.158962
payload = self.generate_json_rpc_payload(RpcMethod.GET_CURRENT_BLOCK_HASH) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_current_block_hash(self, is_full: bool = False) -> str
This interface is used to get the hexadecimal hash value of the highest block in current network. Return: the hexadecimal hash value of the highest block in current network.
4.477922
4.192423
1.068099
payload = self.generate_json_rpc_payload(RpcMethod.GET_BLOCK_HASH, [height, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_block_hash_by_height(self, height: int, is_full: bool = False) -> str
This interface is used to get the hexadecimal hash value of specified block height in current network. :param height: a decimal block height value. :param is_full: :return: the hexadecimal hash value of the specified block height.
5.241284
4.920771
1.065135
payload = self.generate_json_rpc_payload(RpcMethod.GET_BALANCE, [b58_address, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_balance(self, b58_address: str, is_full: bool = False) -> dict
This interface is used to get the account balance of specified base58 encoded address in current network. :param b58_address: a base58 encoded account address. :param is_full: :return: the value of account balance in dictionary form.
4.528988
4.388
1.03213
payload = self.generate_json_rpc_payload(RpcMethod.GET_ALLOWANCE, [asset_name, from_address, to_address]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_allowance(self, asset_name: str, from_address: str, to_address: str, is_full: bool = False) -> str
This interface is used to get the the allowance from transfer-from account to transfer-to account in current network. :param asset_name: :param from_address: a base58 encoded account address. :param to_address: a base58 encoded account address. :param is_full: :return: the information of allowance in dictionary form.
3.601197
3.641256
0.988999
payload = self.generate_json_rpc_payload(RpcMethod.GET_STORAGE, [hex_contract_address, hex_key, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_storage(self, hex_contract_address: str, hex_key: str, is_full: bool = False) -> str
This interface is used to get the corresponding stored value based on hexadecimal contract address and stored key. :param hex_contract_address: hexadecimal contract address. :param hex_key: a hexadecimal stored key. :param is_full: :return: the information of contract storage.
4.219928
4.282126
0.985475
payload = self.generate_json_rpc_payload(RpcMethod.GET_SMART_CONTRACT_EVENT, [tx_hash, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_smart_contract_event_by_tx_hash(self, tx_hash: str, is_full: bool = False) -> dict
This interface is used to get the corresponding smart contract event based on the height of block. :param tx_hash: a hexadecimal hash value. :param is_full: :return: the information of smart contract event in dictionary form.
4.345767
4.388142
0.990343
payload = self.generate_json_rpc_payload(RpcMethod.GET_SMART_CONTRACT_EVENT, [height, 1]) response = self.__post(self.__url, payload) if is_full: return response event_list = response['result'] if event_list is None: event_list = list() return event_list
def get_smart_contract_event_by_height(self, height: int, is_full: bool = False) -> List[dict]
This interface is used to get the corresponding smart contract event based on the height of block. :param height: a decimal height value. :param is_full: :return: the information of smart contract event in dictionary form.
3.449544
3.737883
0.92286
payload = self.generate_json_rpc_payload(RpcMethod.GET_TRANSACTION, [tx_hash, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_transaction_by_tx_hash(self, tx_hash: str, is_full: bool = False) -> dict
This interface is used to get the corresponding transaction information based on the specified hash value. :param tx_hash: str, a hexadecimal hash value. :param is_full: :return: dict
4.805814
5.138239
0.935304
if not isinstance(hex_contract_address, str): raise SDKException(ErrorCode.param_err('a hexadecimal contract address is required.')) if len(hex_contract_address) != 40: raise SDKException(ErrorCode.param_err('the length of the contract address should be 40 bytes.')) payload = self.generate_json_rpc_payload(RpcMethod.GET_SMART_CONTRACT, [hex_contract_address, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_smart_contract(self, hex_contract_address: str, is_full: bool = False) -> dict
This interface is used to get the information of smart contract based on the specified hexadecimal hash value. :param hex_contract_address: str, a hexadecimal hash value. :param is_full: :return: the information of smart contract in dictionary form.
3.795565
3.962092
0.95797
payload = self.generate_json_rpc_payload(RpcMethod.GET_MERKLE_PROOF, [tx_hash, 1]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def get_merkle_proof(self, tx_hash: str, is_full: bool = False) -> dict
This interface is used to get the corresponding merkle proof based on the specified hexadecimal hash value. :param tx_hash: an hexadecimal transaction hash value. :param is_full: :return: the merkle proof in dictionary form.
4.383381
4.273447
1.025725
tx_data = tx.serialize(is_hex=True) payload = self.generate_json_rpc_payload(RpcMethod.SEND_TRANSACTION, [tx_data]) response = self.__post(self.__url, payload) if is_full: return response return response['result']
def send_raw_transaction(self, tx: Transaction, is_full: bool = False) -> str
This interface is used to send the transaction into the network. :param tx: Transaction object in ontology Python SDK. :param is_full: :return: a hexadecimal transaction hash value.
4.339866
4.251025
1.020899
if len(serialized_ddo) == 0: return dict() if isinstance(serialized_ddo, str): stream = StreamManager.get_stream(bytearray.fromhex(serialized_ddo)) elif isinstance(serialized_ddo, bytes): stream = StreamManager.get_stream(serialized_ddo) else: raise SDKException(ErrorCode.params_type_error('bytes or str parameter is required.')) reader = BinaryReader(stream) try: public_key_bytes = reader.read_var_bytes() except SDKException: public_key_bytes = b'' try: attribute_bytes = reader.read_var_bytes() except SDKException: attribute_bytes = b'' try: recovery_bytes = reader.read_var_bytes() except SDKException: recovery_bytes = b'' if len(recovery_bytes) != 0: b58_recovery = Address(recovery_bytes).b58encode() else: b58_recovery = '' pub_keys = OntId.parse_pub_keys(ont_id, public_key_bytes) attribute_list = OntId.parse_attributes(attribute_bytes) ddo = dict(Owners=pub_keys, Attributes=attribute_list, Recovery=b58_recovery, OntId=ont_id) return ddo
def parse_ddo(ont_id: str, serialized_ddo: str or bytes) -> dict
This interface is used to deserialize a hexadecimal string into a DDO object in the from of dict. :param ont_id: the unique ID for identity. :param serialized_ddo: an serialized description object of ONT ID in form of str or bytes. :return: a description object of ONT ID in the from of dict.
2.729907
2.629544
1.038167
args = dict(ontid=ont_id.encode('utf-8')) invoke_code = build_vm.build_native_invoke_code(self.__contract_address, self.__version, 'getDDO', args) unix_time_now = int(time()) tx = Transaction(0, 0xd1, unix_time_now, 0, 0, None, invoke_code, bytearray(), []) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) ddo = OntId.parse_ddo(ont_id, response['Result']) return ddo
def get_ddo(self, ont_id: str) -> dict
This interface is used to get a DDO object in the from of dict. :param ont_id: the unique ID for identity. :return: a description object of ONT ID in the from of dict.
6.776091
6.646665
1.019472
if not isinstance(ctrl_acct, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) b58_payer_address = payer.get_address_base58() bytes_ctrl_pub_key = ctrl_acct.get_public_key_bytes() tx = self.new_registry_ont_id_transaction(ont_id, bytes_ctrl_pub_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def registry_ont_id(self, ont_id: str, ctrl_acct: Account, payer: Account, gas_limit: int, gas_price: int)
This interface is used to send a Transaction object which is used to registry ontid. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value.
3.68643
3.630004
1.015544
if not isinstance(operator, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) if is_recovery: bytes_operator = operator.get_address_bytes() else: bytes_operator = operator.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_add_public_key_transaction(ont_id, bytes_operator, hex_new_public_key, b58_payer_address, gas_limit, gas_price, is_recovery) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def add_public_key(self, ont_id: str, operator: Account, hex_new_public_key: str, payer: Account, gas_limit: int, gas_price: int, is_recovery: bool = False)
This interface is used to send a Transaction object which is used to add public key. :param ont_id: OntId. :param operator: an Account object which indicate who will sign for the transaction. :param hex_new_public_key: the new hexadecimal public key in the form of string. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :param is_recovery: indicate whether ctrl account is a recovery account. :return: a hexadecimal transaction hash value.
3.060509
3.015708
1.014856
if not isinstance(ctrl_acct, Account) or not isinstance(payer, Account): raise SDKException(ErrorCode.require_acct_params) pub_key = ctrl_acct.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_add_attribute_transaction(ont_id, pub_key, attributes, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = self.__sdk.get_network().send_raw_transaction(tx) return tx_hash
def add_attribute(self, ont_id: str, ctrl_acct: Account, attributes: Attribute, payer: Account, gas_limit: int, gas_price: int) -> str
This interface is used to send a Transaction object which is used to add attribute. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param attributes: a list of attributes we want to add. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value.
3.504768
3.410864
1.027531
pub_key = operator.get_public_key_bytes() b58_payer_address = payer.get_address_base58() tx = self.new_remove_attribute_transaction(ont_id, pub_key, attrib_key, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(operator) tx.add_sign_transaction(payer) return self.__sdk.get_network().send_raw_transaction(tx)
def remove_attribute(self, ont_id: str, operator: Account, attrib_key: str, payer: Account, gas_limit: int, gas_price: int)
This interface is used to send a Transaction object which is used to remove attribute. :param ont_id: OntId. :param operator: an Account object which indicate who will sign for the transaction. :param attrib_key: a string which is used to indicate which attribute we want to remove. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a hexadecimal transaction hash value.
3.433561
3.408474
1.00736
b58_payer_address = payer.get_address_base58() pub_key = ctrl_acct.get_public_key_bytes() tx = self.new_add_recovery_transaction(ont_id, pub_key, b58_recovery_address, b58_payer_address, gas_limit, gas_price) tx.sign_transaction(ctrl_acct) tx.add_sign_transaction(payer) tx_hash = self.__sdk.get_network().send_raw_transaction(tx) return tx_hash
def add_recovery(self, ont_id: str, ctrl_acct: Account, b58_recovery_address: str, payer: Account, gas_limit: int, gas_price: int)
This interface is used to send a Transaction object which is used to add the recovery. :param ont_id: OntId. :param ctrl_acct: an Account object which indicate who will sign for the transaction. :param b58_recovery_address: a base58 encode address which indicate who is the recovery. :param payer: an Account object which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which is used to add the recovery.
2.964621
2.913533
1.017535
if isinstance(pub_key, str): bytes_ctrl_pub_key = bytes.fromhex(pub_key) elif isinstance(pub_key, bytes): bytes_ctrl_pub_key = pub_key else: raise SDKException(ErrorCode.param_err('a bytes or str type of public key is required.')) args = dict(ontid=ont_id.encode('utf-8'), ctrl_pk=bytes_ctrl_pub_key) tx = self.__generate_transaction('regIDWithPublicKey', args, b58_payer_address, gas_limit, gas_price) return tx
def new_registry_ont_id_transaction(self, ont_id: str, pub_key: str or bytes, b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction
This interface is used to generate a Transaction object which is used to register ONT ID. :param ont_id: OntId. :param pub_key: the hexadecimal public key in the form of string. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which is used to register ONT ID.
3.724566
3.597019
1.035459
if isinstance(new_pub_key, str): bytes_new_pub_key = bytes.fromhex(new_pub_key) elif isinstance(new_pub_key, bytes): bytes_new_pub_key = new_pub_key else: raise SDKException(ErrorCode.params_type_error('a bytes or str type of public key is required.')) if is_recovery: args = dict(ontid=ont_id, pk=bytes_new_pub_key, operator=bytes_operator) else: args = dict(ontid=ont_id, pk=bytes_new_pub_key, operator=bytes_operator) tx = self.__generate_transaction('addKey', args, b58_payer_address, gas_limit, gas_price) return tx
def new_add_public_key_transaction(self, ont_id: str, bytes_operator: bytes, new_pub_key: str or bytes, b58_payer_address: str, gas_limit: int, gas_price: int, is_recovery: bool = False)
This interface is used to send a Transaction object which is used to add public key. :param ont_id: OntId. :param bytes_operator: operator args in from of bytes. :param new_pub_key: the new hexadecimal public key in the form of string. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :param is_recovery: indicate whether ctrl account is a recovery account. :return: a Transaction object which is used to add public key.
2.429384
2.38598
1.018191
if isinstance(revoked_pub_key, str): bytes_revoked_pub_key = bytes.fromhex(revoked_pub_key) elif isinstance(revoked_pub_key, bytes): bytes_revoked_pub_key = revoked_pub_key else: raise SDKException(ErrorCode.params_type_error('a bytes or str type of public key is required.')) bytes_ont_id = ont_id.encode('utf-8') args = dict(ontid=bytes_ont_id, pk=bytes_revoked_pub_key, operator=bytes_operator) tx = self.__generate_transaction('removeKey', args, b58_payer_address, gas_limit, gas_price) return tx
def new_revoke_public_key_transaction(self, ont_id: str, bytes_operator: bytes, revoked_pub_key: str or bytes, b58_payer_address: str, gas_limit: int, gas_price: int)
This interface is used to generate a Transaction object which is used to remove public key. :param ont_id: OntId. :param bytes_operator: operator args in from of bytes. :param revoked_pub_key: a public key string which will be removed. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which is used to remove public key.
2.66416
2.54966
1.044908
if isinstance(pub_key, str): bytes_pub_key = bytes.fromhex(pub_key) elif isinstance(pub_key, bytes): bytes_pub_key = pub_key else: raise SDKException(ErrorCode.params_type_error('a bytes or str type of public key is required.')) bytes_ont_id = ont_id.encode('utf-8') args = dict(ontid=bytes_ont_id) attrib_dict = attributes.to_dict() args = dict(**args, **attrib_dict) args['pubkey'] = bytes_pub_key tx = self.__generate_transaction('addAttributes', args, b58_payer_address, gas_limit, gas_price) return tx
def new_add_attribute_transaction(self, ont_id: str, pub_key: str or bytes, attributes: Attribute, b58_payer_address: str, gas_limit: int, gas_price: int)
This interface is used to generate a Transaction object which is used to add attribute. :param ont_id: OntId. :param pub_key: the hexadecimal public key in the form of string. :param attributes: a list of attributes we want to add. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which is used to add attribute.
3.044329
3.063622
0.993702
if isinstance(pub_key, str): bytes_pub_key = bytes.fromhex(pub_key) elif isinstance(pub_key, bytes): bytes_pub_key = pub_key else: raise SDKException(ErrorCode.params_type_error('a bytes or str type of public key is required.')) args = dict(ontid=ont_id.encode('utf-8'), attrib_key=attrib_key.encode('utf-8'), pk=bytes_pub_key) tx = self.__generate_transaction('removeAttribute', args, b58_payer_address, gas_limit, gas_price) return tx
def new_remove_attribute_transaction(self, ont_id: str, pub_key: str or bytes, attrib_key: str, b58_payer_address: str, gas_limit: int, gas_price: int)
This interface is used to generate a Transaction object which is used to remove attribute. :param ont_id: OntId. :param pub_key: the hexadecimal public key in the form of string. :param attrib_key: a string which is used to indicate which attribute we want to remove. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return: a Transaction object which is used to remove attribute.
3.157874
3.021532
1.045123
if isinstance(pub_key, str): bytes_pub_key = bytes.fromhex(pub_key) elif isinstance(pub_key, bytes): bytes_pub_key = pub_key else: raise SDKException(ErrorCode.params_type_error('a bytes or str type of public key is required.')) bytes_recovery_address = Address.b58decode(b58_recovery_address).to_bytes() args = dict(ontid=ont_id.encode('utf-8'), recovery=bytes_recovery_address, pk=bytes_pub_key) tx = self.__generate_transaction('addRecovery', args, b58_payer_address, gas_limit, gas_price) return tx
def new_add_recovery_transaction(self, ont_id: str, pub_key: str or bytes, b58_recovery_address: str, b58_payer_address: str, gas_limit: int, gas_price: int)
This interface is used to generate a Transaction object which is used to add the recovery. :param ont_id: OntId. :param pub_key: the hexadecimal public key in the form of string. :param b58_recovery_address: a base58 encode address which indicate who is the recovery. :param b58_payer_address: a base58 encode address which indicate who will pay for the transaction. :param gas_limit: an int value that indicate the gas limit. :param gas_price: an int value that indicate the gas price. :return:
3.173992
3.122347
1.016541
tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = [Sig([signer.get_public_key_bytes()], 1, [sig_data])] self.sig_list = sig
def sign_transaction(self, signer: Account)
This interface is used to sign the transaction. :param signer: an Account object which will sign the transaction. :return: a Transaction object which has been signed.
7.562856
7.035426
1.074968
if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException(ErrorCode.param_err('the number of transaction signatures should not be over 16')) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) sig = Sig([signer.get_public_key_bytes()], 1, [sig_data]) self.sig_list.append(sig)
def add_sign_transaction(self, signer: Account)
This interface is used to add signature into the transaction. :param signer: an Account object which will sign the transaction. :return: a Transaction object which has been signed.
4.318303
4.23117
1.020593
for index, pk in enumerate(pub_keys): if isinstance(pk, str): pub_keys[index] = pk.encode('ascii') pub_keys = ProgramBuilder.sort_public_keys(pub_keys) tx_hash = self.hash256() sig_data = signer.generate_signature(tx_hash) if self.sig_list is None or len(self.sig_list) == 0: self.sig_list = [] elif len(self.sig_list) >= TX_MAX_SIG_SIZE: raise SDKException(ErrorCode.param_err('the number of transaction signatures should not be over 16')) else: for i in range(len(self.sig_list)): if self.sig_list[i].public_keys == pub_keys: if len(self.sig_list[i].sig_data) + 1 > len(pub_keys): raise SDKException(ErrorCode.param_err('too more sigData')) if self.sig_list[i].m != m: raise SDKException(ErrorCode.param_err('M error')) self.sig_list[i].sig_data.append(sig_data) return sig = Sig(pub_keys, m, [sig_data]) self.sig_list.append(sig)
def add_multi_sign_transaction(self, m: int, pub_keys: List[bytes] or List[str], signer: Account)
This interface is used to generate an Transaction object which has multi signature. :param tx: a Transaction object which will be signed. :param m: the amount of signer. :param pub_keys: a list of public keys. :param signer: an Account object which will sign the transaction. :return: a Transaction object which has been signed.
3.107991
3.058454
1.016197
if len(__mstreams_available__) == 0: if data: mstream = MemoryStream(data) mstream.seek(0) else: mstream = MemoryStream() __mstreams__.append(mstream) return mstream mstream = __mstreams_available__.pop() if data is not None and len(data): mstream.clean_up() mstream.write(data) mstream.seek(0) return mstream
def get_stream(data=None)
Get a MemoryStream instance. Args: data (bytes, bytearray, BytesIO): (Optional) data to create the stream from. Returns: MemoryStream: instance.
3.60827
3.512259
1.027336
script_hash = Address.to_script_hash(bytearray.fromhex(code))[::-1] return Address(script_hash)
def address_from_vm_code(code: str)
generate contract address from avm bytecode. :param code: str :return: Address
6.233196
5.284722
1.179475
r = 8 p = 8 dk_len = 64 scrypt = Scrypt(n, r, p, dk_len) derived_key = scrypt.generate_kd(password, salt) iv = derived_key[0:12] key = derived_key[32:64] hdr = self.__address.b58encode().encode() mac_tag, cipher_text = AESHandler.aes_gcm_encrypt_with_iv(self.__private_key, hdr, key, iv) encrypted_key = bytes.hex(cipher_text) + bytes.hex(mac_tag) encrypted_key_str = base64.b64encode(bytes.fromhex(encrypted_key)) return encrypted_key_str.decode('utf-8')
def export_gcm_encrypted_private_key(self, password: str, salt: str, n: int = 16384) -> str
This interface is used to export an AES algorithm encrypted private key with the mode of GCM. :param password: the secret pass phrase to generate the keys from. :param salt: A string to use for better protection from dictionary attacks. This value does not need to be kept secret, but it should be randomly chosen for each derivation. It is recommended to be at least 8 bytes long. :param n: CPU/memory cost parameter. It must be a power of 2 and less than 2**32 :return: an gcm encrypted private key in the form of string.
3.956731
3.90173
1.014097
r = 8 p = 8 dk_len = 64 scrypt = Scrypt(n, r, p, dk_len) derived_key = scrypt.generate_kd(password, salt) iv = derived_key[0:12] key = derived_key[32:64] encrypted_key = base64.b64decode(encrypted_key_str).hex() mac_tag = bytes.fromhex(encrypted_key[64:96]) cipher_text = bytes.fromhex(encrypted_key[0:64]) private_key = AESHandler.aes_gcm_decrypt_with_iv(cipher_text, b58_address.encode(), mac_tag, key, iv) if len(private_key) == 0: raise SDKException(ErrorCode.decrypt_encrypted_private_key_error) acct = Account(private_key, scheme) if acct.get_address().b58encode() != b58_address: raise SDKException(ErrorCode.other_error('Address error.')) return private_key.hex()
def get_gcm_decoded_private_key(encrypted_key_str: str, password: str, b58_address: str, salt: str, n: int, scheme: SignatureScheme) -> str
This interface is used to decrypt an private key which has been encrypted. :param encrypted_key_str: an gcm encrypted private key in the form of string. :param password: the secret pass phrase to generate the keys from. :param b58_address: a base58 encode address which should be correspond with the private key. :param salt: a string to use for better protection from dictionary attacks. :param n: CPU/memory cost parameter. :param scheme: the signature scheme. :return: a private key in the form of string.
3.391899
3.407654
0.995377
data = b''.join([b'\x80', self.__private_key, b'\01']) checksum = Digest.hash256(data[0:34]) wif = base58.b58encode(b''.join([data, checksum[0:4]])) return wif.decode('ascii')
def export_wif(self) -> str
This interface is used to get export ECDSA private key in the form of WIF which is a way to encoding an ECDSA private key and make it easier to copy. :return: a WIF encode private key.
4.615894
4.739816
0.973855
if wif is None or wif is "": raise Exception("none wif") data = base58.b58decode(wif) if len(data) != 38 or data[0] != 0x80 or data[33] != 0x01: raise Exception("wif wrong") checksum = Digest.hash256(data[0:34]) for i in range(4): if data[len(data) - 4 + i] != checksum[i]: raise Exception("wif wrong") return data[1:33]
def get_private_key_from_wif(wif: str) -> bytes
This interface is used to decode a WIF encode ECDSA private key. :param wif: a WIF encode private key. :return: a ECDSA private key in the form of bytes.
2.644589
2.757316
0.959117
key = b'' index = 1 bytes_seed = str_to_bytes(seed) while len(key) < dk_len: key += Digest.sha256(b''.join([bytes_seed, index.to_bytes(4, 'big', signed=True)])) index += 1 return key[:dk_len]
def pbkdf2(seed: str or bytes, dk_len: int) -> bytes
Derive one key from a seed. :param seed: the secret pass phrase to generate the keys from. :param dk_len: the length in bytes of every derived key. :return:
2.979756
3.013608
0.988767
for index, key in enumerate(pub_keys): if isinstance(key, str): pub_keys[index] = bytes.fromhex(key) return sorted(pub_keys, key=ProgramBuilder.compare_pubkey)
def sort_public_keys(pub_keys: List[bytes] or List[str])
:param pub_keys: a list of public keys in format of bytes. :return: sorted public keys.
4.076623
3.850861
1.058626
if len(params) != len(self.parameters): raise Exception("parameter error") temp = self.parameters self.parameters = [] for i in range(len(params)): self.parameters.append(Parameter(temp[i]['name'], temp[i]['type'])) self.parameters[i].set_value(params[i])
def set_params_value(self, *params)
This interface is used to set parameter value for an function in abi file.
2.694736
2.50484
1.075812
for p in self.parameters: if p.name == param_name: return p raise SDKException(ErrorCode.param_err('get parameter failed.'))
def get_parameter(self, param_name: str) -> Parameter
This interface is used to get a Parameter object from an AbiFunction object which contain given function parameter's name, type and value. :param param_name: a string used to indicate which parameter we want to get from AbiFunction. :return: a Parameter object which contain given function parameter's name, type and value.
8.821698
10.330135
0.853977
try: info = struct.unpack(fmt, self.stream.read(length))[0] except struct.error as e: raise SDKException(ErrorCode.unpack_error(e.args[0])) return info
def unpack(self, fmt, length=1)
Unpack the stream contents according to the specified format in `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. length (int): amount of bytes to read. Returns: variable: the result according to the specified format.
4.603021
4.979599
0.924376
try: if do_ord: return ord(self.stream.read(1)) else: return self.stream.read(1) except Exception as e: raise SDKException(ErrorCode.read_byte_error(e.args[0]))
def read_byte(self, do_ord=True) -> int
Read a single byte. Args: do_ord (bool): (default True) convert the byte to an ordinal first. Returns: bytes: a single byte if successful. 0 (int) if an exception occurred.
3.88896
3.792374
1.025468
value = self.stream.read(length) return value
def read_bytes(self, length) -> bytes
Read the specified number of bytes from the stream. Args: length (int): number of bytes to read. Returns: bytes: `length` number of bytes.
12.038841
8.366091
1.439004
if little_endian: endian = "<" else: endian = ">" return self.unpack("%sf" % endian, 4)
def read_float(self, little_endian=True)
Read 4 bytes as a float value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: float:
3.762681
6.122593
0.614557
if little_endian: endian = "<" else: endian = ">" return self.unpack("%sd" % endian, 8)
def read_double(self, little_endian=True)
Read 8 bytes as a double value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: float:
3.838469
6.945325
0.552669
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sb' % endian)
def read_int8(self, little_endian=True)
Read 1 byte as a signed integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.323504
7.610515
0.568096
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sB' % endian)
def read_uint8(self, little_endian=True)
Read 1 byte as an unsigned integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.056419
6.626929
0.612111
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sh' % endian, 2)
def read_int16(self, little_endian=True)
Read 2 byte as a signed integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.084551
7.478234
0.546192
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sH' % endian, 2)
def read_uint16(self, little_endian=True)
Read 2 byte as an unsigned integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
3.923824
6.366415
0.616332
if little_endian: endian = "<" else: endian = ">" return self.unpack('%si' % endian, 4)
def read_int32(self, little_endian=True)
Read 4 bytes as a signed integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.070528
6.847332
0.594469
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sI' % endian, 4)
def read_uint32(self, little_endian=True)
Read 4 bytes as an unsigned integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.080524
6.504466
0.627342
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sq' % endian, 8)
def read_int64(self, little_endian=True)
Read 8 bytes as a signed integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.725439
8.340466
0.566568
if little_endian: endian = "<" else: endian = ">" return self.unpack('%sQ' % endian, 8)
def read_uint64(self, little_endian=True)
Read 8 bytes as an unsigned integer value from the stream. Args: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int:
4.136957
6.601496
0.62667
fb = self.read_byte() if fb is 0: return fb if hex(fb) == '0xfd': value = self.read_uint16() elif hex(fb) == '0xfe': value = self.read_uint32() elif hex(fb) == '0xff': value = self.read_uint64() else: value = fb if value > max_size: raise SDKException(ErrorCode.param_err('Invalid format')) return int(value)
def read_var_int(self, max_size=sys.maxsize)
Read a variable length integer from the stream. The NEO network protocol supports encoded storage for space saving. See: http://docs.neo.org/en-us/node/network-protocol.html#convention Args: max_size (int): (Optional) maximum number of bytes to read. Returns: int:
3.167715
3.443863
0.919815
length = self.read_var_int(max_size) return self.read_bytes(length)
def read_var_bytes(self, max_size=sys.maxsize) -> bytes
Read a variable length of bytes from the stream. Args: max_size (int): (Optional) maximum number of bytes to read. Returns: bytes:
4.082213
4.783928
0.853318
length = self.read_uint8() return self.unpack(str(length) + 's', length)
def read_str(self)
Read a string from the stream. Returns: str:
6.202775
9.078097
0.683268
length = self.read_var_int(max_size) return self.unpack(str(length) + 's', length)
def read_var_str(self, max_size=sys.maxsize)
Similar to `ReadString` but expects a variable length indicator instead of the fixed 1 byte indicator. Args: max_size (int): (Optional) maximum number of bytes to read. Returns: bytes:
4.453102
6.294591
0.707449
module = '.'.join(class_name.split('.')[:-1]) class_name = class_name.split('.')[-1] class_attr = getattr(importlib.import_module(module), class_name) length = self.read_var_int(max_size=max_size) items = [] try: for _ in range(0, length): item = class_attr() item.Deserialize(self) items.append(item) except Exception as e: raise SDKException(ErrorCode.param_err("Couldn't deserialize %s" % e)) return items
def read_serializable_array(self, class_name, max_size=sys.maxsize)
Deserialize a stream into the object specific by `class_name`. Args: class_name (str): a full path to the class to be deserialized into. e.g. 'neo.Core.Block.Block' max_size (int): (Optional) maximum number of bytes to read. Returns: list: list of `class_name` objects deserialized from the stream.
2.81267
2.857581
0.984284
items = [] for _ in range(0, 2000): data = self.read_bytes(64) ba = bytearray(binascii.unhexlify(data)) ba.reverse() items.append(ba.hex().encode('utf-8')) return items
def read_2000256_list(self)
Read 2000 times a 64 byte value from the stream. Returns: list: a list containing 2000 64 byte values in reversed form.
3.862134
3.137856
1.230819
var_len = self.read_var_int() items = [] for _ in range(0, var_len): ba = bytearray(self.read_bytes(32)) ba.reverse() items.append(ba.hex()) return items
def read_hashes(self)
Read Hash values from the stream. Returns: list: a list of hash values. Each value is of the bytearray type.
3.768389
3.241497
1.162546
if isinstance(value, bytes): self.stream.write(value) elif isinstance(value, str): self.stream.write(value.encode('utf-8')) elif isinstance(value, int): self.stream.write(bytes([value]))
def write_byte(self, value)
Write a single byte to the stream. Args: value (bytes, str or int): value to write to the stream.
1.944905
1.768698
1.099625
return self.write_bytes(struct.pack(fmt, data))
def pack(self, fmt, data)
Write bytes by packing them according to the provided format `fmt`. For more information about the `fmt` format see: https://docs.python.org/3/library/struct.html Args: fmt (str): format string. data (object): the data to write to the raw stream. Returns: int: the number of bytes written.
6.397187
10.627769
0.601931
if little_endian: endian = "<" else: endian = ">" return self.pack('%sf' % endian, value)
def write_float(self, value, little_endian=True)
Pack the value as a float and write 4 bytes to the stream. Args: value (number): the value to write to the stream. little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.913267
5.104572
0.76662
if little_endian: endian = "<" else: endian = ">" return self.pack('%sd' % endian, value)
def write_double(self, value, little_endian=True)
Pack the value as a double and write 8 bytes to the stream. Args: value (number): the value to write to the stream. little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
4.168755
5.554808
0.750477
if little_endian: endian = "<" else: endian = ">" return self.pack('%sb' % endian, value)
def write_int8(self, value, little_endian=True)
Pack the value as a signed byte and write 1 byte to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
4.056092
5.273913
0.769086
if little_endian: endian = "<" else: endian = ">" return self.pack('%sB' % endian, value)
def write_uint8(self, value, little_endian=True)
Pack the value as an unsigned byte and write 1 byte to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.78546
4.840343
0.782064
if little_endian: endian = "<" else: endian = ">" return self.pack('%sh' % endian, value)
def write_int16(self, value, little_endian=True)
Pack the value as a signed integer and write 2 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.932136
5.479931
0.717552
if little_endian: endian = "<" else: endian = ">" return self.pack('%sH' % endian, value)
def write_uint16(self, value, little_endian=True)
Pack the value as an unsigned integer and write 2 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.702572
4.805724
0.770451
if little_endian: endian = "<" else: endian = ">" return self.pack('%si' % endian, value)
def write_int32(self, value, little_endian=True)
Pack the value as a signed integer and write 4 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.907969
5.132948
0.76135
if little_endian: endian = "<" else: endian = ">" return self.pack('%sI' % endian, value)
def write_uint32(self, value, little_endian=True)
Pack the value as an unsigned integer and write 4 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.694022
4.75027
0.777645
if little_endian: endian = "<" else: endian = ">" return self.pack('%sq' % endian, value)
def write_int64(self, value, little_endian=True)
Pack the value as a signed integer and write 8 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
4.641118
6.277514
0.739324
if little_endian: endian = "<" else: endian = ">" return self.pack('%sQ' % endian, value)
def write_uint64(self, value, little_endian=True)
Pack the value as an unsigned integer and write 8 bytes to the stream. Args: value: little_endian (bool): specify the endianness. (Default) Little endian. Returns: int: the number of bytes written.
3.731094
4.863722
0.767127
if not isinstance(value, int): raise SDKException(ErrorCode.param_err('%s not int type.' % value)) if value < 0: raise SDKException(ErrorCode.param_err('%d too small.' % value)) elif value < 0xfd: return self.write_byte(value) elif value <= 0xffff: self.write_byte(0xfd) return self.write_uint16(value, little_endian) elif value <= 0xFFFFFFFF: self.write_byte(0xfe) return self.write_uint32(value, little_endian) else: self.write_byte(0xff) return self.write_uint64(value, little_endian)
def write_var_int(self, value, little_endian=True)
Write an integer value in a space saving way to the stream. Args: value (int): little_endian (bool): specify the endianness. (Default) Little endian. Raises: SDKException: if `value` is not of type int. SDKException: if `value` is < 0. Returns: int: the number of bytes written.
2.163179
2.17557
0.994304
length = len(value) self.write_var_int(length, little_endian) return self.write_bytes(value, to_bytes=False)
def write_var_bytes(self, value, little_endian: bool = True)
Write an integer value in a space saving way to the stream. :param value: :param little_endian: specify the endianness. (Default) Little endian. :return: int: the number of bytes written.
4.315948
4.42445
0.975477
if isinstance(value, str): value = value.encode(encoding) self.write_var_int(len(value)) self.write_bytes(value)
def write_var_str(self, value, encoding: str = 'utf-8')
Write a string value to the stream. :param value: value to write to the stream. :param encoding: string encoding format.
2.713763
3.058284
0.887348
towrite = value.encode('utf-8') slen = len(towrite) if slen > length: raise SDKException(ErrorCode.param_err('string longer than fixed length: %s' % length)) self.write_bytes(towrite) diff = length - slen while diff > 0: self.write_byte(0) diff -= 1
def write_fixed_str(self, value, length)
Write a string value to the stream. Args: value (str): value to write to the stream. length (int): length of the string to write.
4.194358
4.665187
0.899076
if array is None: self.write_byte(0) else: self.write_var_int(len(array)) for item in array: item.Serialize(self)
def write_serializable_array(self, array)
Write an array of serializable objects to the stream. Args: array(list): a list of serializable objects. i.e. extending neo.IO.Mixins.SerializableMixin
2.897038
3.143003
0.921742
length = len(arr) self.write_var_int(length) for item in arr: ba = bytearray(binascii.unhexlify(item)) ba.reverse() self.write_bytes(ba)
def write_hashes(self, arr)
Write an array of hashes to the stream. Args: arr (list): a list of 32 byte hashes.
3.099222
3.248788
0.953963
if asset.upper() == 'ONT': return self.__ont_contract elif asset.upper() == 'ONG': return self.__ong_contract else: raise SDKException(ErrorCode.other_error('asset is not equal to ONT or ONG.'))
def get_asset_address(self, asset: str) -> bytes
This interface is used to get the smart contract address of ONT otr ONG. :param asset: a string which is used to indicate which asset's contract address we want to get. :return: the contract address of asset in the form of bytearray.
6.819227
4.593126
1.484659
raw_address = Address.b58decode(b58_address).to_bytes() contract_address = self.get_asset_address(asset) invoke_code = build_native_invoke_code(contract_address, b'\x00', "balanceOf", raw_address) tx = Transaction(0, 0xd1, int(time()), 0, 0, None, invoke_code, bytearray(), list()) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) try: balance = ContractDataParser.to_int(response['Result']) return balance except SDKException: return 0
def query_balance(self, asset: str, b58_address: str) -> int
This interface is used to query the account's ONT or ONG balance. :param asset: a string which is used to indicate which asset we want to check the balance. :param b58_address: a base58 encode account address. :return: account balance.
5.696562
5.693024
1.000621
contract_address = self.get_asset_address('ont') unbound_ong = self.__sdk.rpc.get_allowance("ong", Address(contract_address).b58encode(), base58_address) return int(unbound_ong)
def query_unbound_ong(self, base58_address: str) -> int
This interface is used to query the amount of account's unbound ong. :param base58_address: a base58 encode address which indicate which account's unbound ong we want to query. :return: the amount of unbound ong in the form of int.
7.787943
7.175485
1.085354
contract_address = self.get_asset_address(asset) method = 'symbol' invoke_code = build_native_invoke_code(contract_address, b'\x00', method, bytearray()) tx = Transaction(0, 0xd1, int(time()), 0, 0, None, invoke_code, bytearray(), list()) response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx) symbol = ContractDataParser.to_utf8_str(response['Result']) return symbol
def query_symbol(self, asset: str) -> str
This interface is used to query the asset's symbol of ONT or ONG. :param asset: a string which is used to indicate which asset's symbol we want to get. :return: asset's symbol in the form of string.
7.295254
7.143451
1.021251