Exemple #1
0
 def unauthorize_for_peer(self, account: Account, peer_publickeys: [],
                          pos_lists: [], payer: Account, gas_limit: int,
                          gas_price: int):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     if len(peer_publickeys) != len(pos_lists):
         raise Exception(
             "the length of peer_publickeys should equal the length of pos_lists"
         )
     param = {
         "address": account.get_address().to_bytes(),
         "publickeys_length": len(peer_publickeys)
     }
     for i in range(len(peer_publickeys)):
         param["publickey" + str(i)] = peer_publickeys[i]
     param["pos_lists_length"] = len(pos_lists)
     for i in range(len(pos_lists)):
         param["pos_lists" + str(i)] = pos_lists[i]
     invoke_code = build_native_invoke_code(contract_address, b'\x00',
                                            "unAuthorizeForPeer", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                      payer.get_address().to_bytes(), invoke_code,
                      bytearray(), [])
     tx.sign_transaction(account)
     if payer is not None and account.get_address_base58(
     ) is not payer.get_address_base58():
         tx.add_sign_transaction(payer)
     return self.__sdk.get_network().send_raw_transaction(tx)
 def withdraw(self, account: Account, peer_publickeys: list,
              withdraw_list: list, payer: Account, gas_limit: int,
              gas_price: int):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     if len(peer_publickeys) != len(withdraw_list):
         raise Exception(
             "the length of peer_publickeys should equal the length of pos_lists"
         )
     param = {
         "address": account.get_address().to_array(),
         "publickeys_length": len(peer_publickeys)
     }
     for i in range(len(peer_publickeys)):
         param["publickey" + str(i)] = peer_publickeys[i]
     param["pos_lists_length"] = len(withdraw_list)
     for i in range(len(withdraw_list)):
         param["pos_lists" + str(i)] = withdraw_list[i]
     invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                            "withdraw", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                      payer.get_address().to_array(), invoke_code,
                      bytearray(), [], bytearray())
     self.__sdk.sign_transaction(tx, account)
     if payer is not None and account.get_address_base58(
     ) is not payer.get_address_base58():
         self.__sdk.add_sign_transaction(tx, payer)
     self.__sdk.rpc.send_raw_transaction(tx)
     return tx.hash256_explorer()
    def query_allowance(self, asset: str, b58_from_address: str, b58_to_address: str) -> int:
        """

        :param asset: a string which is used to indicate which asset's allowance we want to get.
        :param b58_from_address: a base58 encode address which indicate where the allowance from.
        :param b58_to_address: a base58 encode address which indicate where the allowance to.
        :return: the amount of allowance in the from of int.
        """
        contract_address = self.get_asset_address(asset)
        raw_from = Address.b58decode(b58_from_address).to_bytes()
        raw_to = Address.b58decode(b58_to_address).to_bytes()
        args = {"from": raw_from, "to": raw_to}
        invoke_code = build_native_invoke_code(contract_address, b'\x00', "allowance", args)
        unix_time_now = int(time())
        version = 0
        tx_type = 0xd1
        gas_price = 0
        gas_limit = 0
        attributes = bytearray()
        signers = list()
        tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, None, invoke_code, attributes, signers)
        response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
        try:
            allowance = ContractDataParser.to_int(response['Result'])
            return allowance
        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.
        """
        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)
        unix_time_now = int(time())
        version = 0
        tx_type = 0xd1
        gas_price = 0
        gas_limit = 0
        attributes = bytearray()
        signers = list()
        tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit, None, invoke_code, attributes, signers)
        response = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
        try:
            balance = ContractDataParser.to_int(response['Result'])
            return balance
        except SDKException:
            return 0
    def new_withdraw_ong_transaction(self, b58_claimer_address: str, b58_recv_address: str, amount: int,
                                     b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction:
        """
        This interface is used to generate a Transaction object that
        allow one account to withdraw an amount of ong and transfer them to receive address.

        :param b58_claimer_address: a base58 encode address which is used to indicate who is the claimer.
        :param b58_recv_address: a base58 encode address which is used to indicate who receive the claimed ong.
        :param amount: the amount of asset that will be claimed.
        :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 can be used for withdraw ong.
        """
        if not isinstance(b58_claimer_address, str) or not isinstance(b58_recv_address, str) or not isinstance(
                b58_payer_address, str):
            raise SDKException(ErrorCode.param_err('the data type of base58 encode address should be the string.'))
        if len(b58_claimer_address) != 34 or len(b58_recv_address) != 34 or len(b58_payer_address) != 34:
            raise SDKException(ErrorCode.param_err('the length of base58 encode address should be 34 bytes.'))
        if amount <= 0:
            raise SDKException(ErrorCode.other_error('the amount should be greater than than zero.'))
        if gas_price < 0:
            raise SDKException(ErrorCode.other_error('the gas price should be equal or greater than zero.'))
        if gas_limit < 0:
            raise SDKException(ErrorCode.other_error('the gas limit should be equal or greater than zero.'))
        ont_contract_address = self.get_asset_address('ont')
        ong_contract_address = self.get_asset_address("ong")
        args = {"sender": Address.b58decode(b58_claimer_address).to_bytes(), "from": ont_contract_address,
                "to": Address.b58decode(b58_recv_address).to_bytes(), "value": amount}
        invoke_code = build_native_invoke_code(ong_contract_address, b'\x00', "transferFrom", args)
        unix_time_now = int(time())
        payer_array = Address.b58decode(b58_payer_address).to_bytes()
        return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer_array, invoke_code, bytearray(), list())
 def register_candidate(self, account: Account, peer_pubkey: str,
                        init_pos: int, identity: Identity, password: str,
                        key_no: int, payer: Account, gas_limit: int,
                        gas_price: int):
     contract_address = bytearray.fromhex(self.CONTRACT_ADDRESS)
     param = {
         "peer_pubkey": peer_pubkey,
         "address": account.get_address().to_array(),
         "init_pos": init_pos,
         "ontid": identity.ont_id.encode(),
         "key_no": key_no
     }
     invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                            "registerCandidate", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                      payer.get_address().to_array(), invoke_code,
                      bytearray(), [], bytearray())
     self.__sdk.sign_transaction(tx, account)
     ontid_acc = self.__sdk.wallet_manager.get_account(
         identity.ont_id, password)
     self.__sdk.add_sign_transaction(tx, ontid_acc)
     if account.get_address_base58() is not payer:
         self.__sdk.add_sign_transaction(tx, payer)
     res = self.__sdk.rpc.send_raw_transaction(tx)
     return tx.hash256_explorer()
Exemple #7
0
 def new_balance_of_tx(self, owner: Union[str,
                                          Address]) -> InvokeTransaction:
     owner = Address.b58decode(owner)
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'balanceOf',
                                            owner)
     return InvokeTransaction(payload=invoke_code)
    def new_approve_transaction(self, asset: str, b58_send_address: str, b58_recv_address: str, amount: int,
                                b58_payer_address: str, gas_limit: int, gas_price: int) -> Transaction:
        """
        This interface is used to generate a Transaction object for approve.

        :param asset: a string which is used to indicate which asset we want to approve.
        :param b58_send_address: a base58 encode address which indicate where the approve from.
        :param b58_recv_address: a base58 encode address which indicate where the approve to.
        :param amount: the amount of asset that will be approved.
        :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 can be used for approve.
        """
        if not isinstance(b58_send_address, str) or not isinstance(b58_recv_address, str):
            raise SDKException(ErrorCode.param_err('the data type of base58 encode address should be the string.'))
        if len(b58_send_address) != 34 or len(b58_recv_address) != 34:
            raise SDKException(ErrorCode.param_err('the length of base58 encode address should be 34 bytes.'))
        if amount <= 0:
            raise SDKException(ErrorCode.other_error('the amount should be greater than than zero.'))
        if gas_price < 0:
            raise SDKException(ErrorCode.other_error('the gas price should be equal or greater than zero.'))
        if gas_limit < 0:
            raise SDKException(ErrorCode.other_error('the gas limit should be equal or greater than zero.'))
        contract_address = self.get_asset_address(asset)
        raw_send = Address.b58decode(b58_send_address).to_bytes()
        raw_recv = Address.b58decode(b58_recv_address).to_bytes()
        raw_payer = Address.b58decode(b58_payer_address).to_bytes()
        args = {"from": raw_send, "to": raw_recv, "amount": amount}
        invoke_code = build_native_invoke_code(contract_address, b'\x00', 'approve', args)
        return Transaction(0, 0xd1, int(time()), gas_price, gas_limit, raw_payer, invoke_code, bytearray(), list())
    def query_allowance(self, asset: str, b58_from_address: str,
                        b58_to_address: str) -> int:
        """

        :param asset: a string which is used to indicate which asset's allowance we want to get.
        :param b58_from_address: a base58 encode address which indicate where the allowance from.
        :param b58_to_address: a base58 encode address which indicate where the allowance to.
        :return: the amount of allowance in the from of int.
        """
        contract_address = util.get_asset_address(asset)
        raw_from = Address.b58decode(b58_from_address).to_array()
        raw_to = Address.b58decode(b58_to_address).to_array()
        args = {"from": raw_from, "to": raw_to}
        invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                               "allowance", args)
        unix_time_now = int(time())
        payer = Address(ZERO_ADDRESS).to_array()
        version = 0
        tx_type = 0xd1
        gas_price = 0
        gas_limit = 0
        attributes = bytearray()
        signers = list()
        hash_value = bytearray()
        tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit,
                         payer, invoke_code, attributes, signers, hash_value)
        allowance = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
        array = bytearray(binascii.a2b_hex(allowance.encode('ascii')))
        array.reverse()
        try:
            allowance = int(binascii.b2a_hex(array).decode('ascii'), 16)
        except ValueError:
            allowance = 0
        return allowance
Exemple #10
0
 def make_transfer(self, contract_address: str, new_admin_ont_id: str, key_no: str, payer: str, gas_limit: str,gas_price: str):
     param = {"contract_address": a2b_hex(contract_address.encode()), 'new_admin_ont_id': new_admin_ont_id.encode('utf-8'), 'key_no': key_no}
     invoke_code = build_native_invoke_code(bytearray.fromhex(self.contract_address), bytes([0]), "transfer", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, payer.get_address().to_array(), invoke_code,
                      bytearray(), [], bytearray())
     return tx
Exemple #11
0
 def get_public_keys(self, ont_id: str):
     args = dict(ontid=ont_id.encode('utf-8'))
     invoke_code = build_vm.build_native_invoke_code(self._contract_address, self._version, 'getPublicKeys', args)
     tx = Transaction(0, TxType.InvokeNeoVm, 0, 0, b'', invoke_code)
     response = self._sdk.default_network.send_raw_transaction_pre_exec(tx)
     pub_keys = OntId.parse_pub_keys(ont_id, response['Result'])
     return pub_keys
Exemple #12
0
 def delegate(self, identity: Identity, password: str, key_no: int,
              contract_address: str, to_ont_id: str, role: str, period: int,
              level: int, payer: Account, gas_limit: int, gas_price: int):
     contract_address = bytearray.fromhex(contract_address)
     param = {
         "contract_address": contract_address,
         "ont_id": identity.ont_id.encode('utf-8'),
         "to_ont_id": to_ont_id.encode('utf-8'),
         "role": role.encode('utf-8'),
         "period": period,
         "level": level,
         "key_no": key_no
     }
     invoke_code = build_native_invoke_code(
         bytearray.fromhex(self.contract_address), bytes([0]), "delegate",
         param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                      payer.get_address().to_array(), invoke_code,
                      bytearray(), [], bytearray())
     account = self.__sdk.wallet_manager.get_account(
         identity.ont_id, password)
     self.__sdk.sign_transaction(tx, account)
     self.__sdk.add_sign_transaction(tx, payer)
     res = self.__sdk.rpc.send_raw_transaction(tx)
     return res
Exemple #13
0
 def assign_funcs_to_role(self, admin_identity: Identity, password: str,
                          key_no: int, contract_address: str, role: str,
                          function_name: list, payer: Account,
                          gas_limit: int, gas_price: int):
     contract_address = bytearray.fromhex(contract_address)
     param = {
         "contract_address": contract_address,
         "ontid": admin_identity.ont_id.encode('utf-8'),
         "role": role.encode('utf-8')
     }
     param['length'] = len(function_name)
     for i in range(len(function_name)):
         param['name' + str(i)] = function_name[i]
     param['key_no'] = key_no
     invoke_code = build_native_invoke_code(
         bytearray.fromhex(self.contract_address), bytes([0]),
         "assignFuncsToRole", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                      payer.get_address().to_array(), invoke_code,
                      bytearray(), [], bytearray())
     account = self.__sdk.wallet_manager.get_account(
         admin_identity.ont_id, password)
     self.__sdk.sign_transaction(tx, account)
     self.__sdk.add_sign_transaction(tx, payer)
     res = self.__sdk.rpc.send_raw_transaction(tx)
     return res
    def new_transfer_from_transaction(self, asset: str, b58_send_address: str, b58_from_address: str,
                                      b58_recv_address: str, amount: int, b58_payer_address: str, gas_limit: int,
                                      gas_price: int) -> Transaction:
        """
        This interface is used to generate a Transaction object that allow one account to transfer
        a amount of ONT or ONG Asset to another account, in the condition of the first account had been approved.

        :param asset: a string which is used to indicate which asset we want to transfer.
        :param b58_send_address: a base58 encode address which indicate where the asset from.
        :param b58_from_address: a base58 encode address which indicate where the asset from.
        :param b58_recv_address: a base58 encode address which indicate where the asset to.
        :param amount: the amount of asset that will be transferred.
        :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 allow one account to transfer a amount of asset to another account.
        """
        raw_sender = Address.b58decode(b58_send_address).to_bytes()
        raw_from = Address.b58decode(b58_from_address).to_bytes()
        raw_to = Address.b58decode(b58_recv_address).to_bytes()
        raw_payer = Address.b58decode(b58_payer_address).to_bytes()
        contract_address = self.get_asset_address(asset)
        args = {"sender": raw_sender, "from": raw_from, "to": raw_to, "amount": amount}
        invoke_code = build_native_invoke_code(contract_address, b'\x00', "transferFrom", args)
        unix_time_now = int(time())
        return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit, raw_payer, invoke_code, bytearray(), list())
Exemple #15
0
    def new_add_recovery_transaction(ont_id: str, hex_public_key: str,
                                     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 hex_public_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:
        """
        contract_address = ONTID_CONTRACT_ADDRESS
        bytes_recovery_address = Address.b58decode(
            b58_recovery_address).to_array()
        bytearray_public_key = bytearray.fromhex(hex_public_key)
        args = {
            "ontid": ont_id.encode(),
            "recovery": bytes_recovery_address,
            "pubkey": bytearray_public_key
        }
        invoke_code = build_vm.build_native_invoke_code(
            contract_address, bytes([0]), "addRecovery", args)
        unix_time_now = int(time())
        bytes_payer_address = Address.b58decode(b58_payer_address).to_array()
        tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                         bytes_payer_address, invoke_code, bytearray(), [],
                         bytearray())
        return tx
Exemple #16
0
    def new_add_attribute_transaction(ont_id: str, hex_public_key: str,
                                      attribute_list: list,
                                      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 hex_public_key: the hexadecimal public key in the form of string.
        :param attribute_list: 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.
        """
        contract_address = ONTID_CONTRACT_ADDRESS
        args = {"ontid": ont_id.encode(), "length": len(attribute_list)}
        for i in range(len(attribute_list)):
            args["key" + str(i)] = bytes(attribute_list[i]["key"].encode())
            args["type" + str(i)] = bytes(attribute_list[i]["type"].encode())
            args["value" + str(i)] = bytes(attribute_list[i]["value"].encode())
        args["pubkey"] = bytearray.fromhex(hex_public_key)
        invoke_code = build_vm.build_native_invoke_code(
            contract_address, bytes([0]), "addAttributes", args)
        unix_time_now = int(time())
        array_payer_address = Address.b58decode(b58_payer_address).to_array()
        tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                         array_payer_address, invoke_code, bytearray(), [],
                         bytearray())
        return tx
Exemple #17
0
    def new_add_public_key_transaction(ont_id: str,
                                       hex_public_key_or_recovery: str,
                                       hex_new_public_key: str, payer: str,
                                       gas_limit: int, gas_price: int):
        """
        This interface is used to send a Transaction object which is used to add public key.

        :param ont_id: ontid.
        :param hex_public_key_or_recovery: the old hexadecimal public key in the form of string.
        :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.
        :return: a Transaction object which is used to add public key.
        """
        contract_address = ONTID_CONTRACT_ADDRESS
        args = {
            "ontid": ont_id.encode(),
            "pubkey": bytearray.fromhex(hex_new_public_key),
            "pubkey_or_recovery": bytearray.fromhex(hex_public_key_or_recovery)
        }
        invoke_code = build_vm.build_native_invoke_code(
            contract_address, bytes([0]), "addKey", args)
        unix_time_now = int(time())
        tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                         Address.b58decode(payer).to_array(), invoke_code,
                         bytearray(), [], bytearray())
        return tx
Exemple #18
0
    def new_remove_attribute_transaction(ont_id: str, hex_public_key: str,
                                         path: 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 hex_public_key: the hexadecimal public key in the form of string.
        :param path: 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.
        """
        contract_address = ONTID_CONTRACT_ADDRESS
        args = {
            "ontid": ont_id.encode(),
            "key": bytes(path.encode()),
            "pubkey": bytearray.fromhex(hex_public_key)
        }
        invoke_code = build_vm.build_native_invoke_code(
            contract_address, bytes([0]), "removeAttribute", args)
        unix_time_now = int(time())
        tx = Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                         Address.b58decode(b58_payer_address).to_array(),
                         invoke_code, bytearray(), [], bytearray())
        return tx
Exemple #19
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.
        """
        raw_address = Address.b58decode(b58_address).to_array()
        contract_address = util.get_asset_address(asset)
        invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                               "balanceOf", raw_address)
        unix_time_now = int(time())
        payer = Address(ZERO_ADDRESS).to_array()
        version = 0
        tx_type = 0xd1
        gas_price = 0
        gas_limit = 0
        attributes = bytearray()
        signers = list()
        hash_value = bytearray()
        tx = Transaction(version, tx_type, unix_time_now, gas_price, gas_limit,
                         payer, invoke_code, attributes, signers, hash_value)
        balance = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
        array = bytearray(binascii.a2b_hex(balance.encode('ascii')))
        array.reverse()
        try:
            balance = int(binascii.b2a_hex(array).decode('ascii'), 16)
        except ValueError:
            balance = 0
        return balance
Exemple #20
0
async def get_ddo(ont_id):
    contract_address = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
                       b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03'
    version = b'\x00'
    remote = 'http://dappnode1.ont.io:20336/'

    sdk = Ontology(rpc_address=remote)
    args = dict(ontid=ont_id.encode('utf-8'))
    invoke_code = build_vm.build_native_invoke_code(contract_address, version,
                                                    'getDDO', args)
    tx = Transaction(0, 0xd1, 0, 0, b'', invoke_code)
    response = await sdk.aio_rpc.send_raw_transaction_pre_exec(tx)
    serialized_ddo = response['Result']

    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('unexpected data type {}'.format(
                type(serialized_ddo))))
    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 = ''
    try:
        controller = reader.read_var_bytes().decode('utf-8')
    except SDKException:
        controller = ''
    try:
        recovery = reader.read_var_bytes().decode('utf-8')
    except SDKException:
        recovery = ''

    pub_keys = ontid.OntId.parse_pub_keys(ont_id, public_key_bytes)
    attribute_list = ontid.OntId.parse_attributes(attribute_bytes)
    return dict(
        id=ont_id,
        keys=pub_keys,
        ctrl=controller,
        attributes=attribute_list,
        recovery=recovery if recovery else b58_recovery,
    )
Exemple #21
0
 def get_public_keys(self, ont_id: str):
     args = dict(ontid=ont_id.encode('utf-8'))
     invoke_code = build_vm.build_native_invoke_code(self.__contract_address, self.__version, 'getPublicKeys', args)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, 0, 0, None, invoke_code, bytearray(), [])
     response = self.__sdk.get_network().send_raw_transaction_pre_exec(tx)
     pub_keys = OntId.parse_pub_keys(ont_id, response['Result'])
     return pub_keys
Exemple #22
0
 def new_allowance_tx(self, from_address: Union[str, Address],
                      to_address: Union[str, Address]) -> InvokeTransaction:
     args = dict(from_address=Address.b58decode(from_address),
                 to_address=Address.b58decode(to_address))
     invoke_code = build_native_invoke_code(self._invoke_address,
                                            self._version, 'allowance',
                                            args)
     return InvokeTransaction(payload=invoke_code)
Exemple #23
0
 def query_decimals(self, asset: str):
     contract_address = util.get_asset_address(asset)
     invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                            "decimals", bytearray())
     unix_timenow = int(time())
     payer = Address(ZERO_ADDRESS).to_array()
     tx = Transaction(0, 0xd1, unix_timenow, 0, 0, payer, invoke_code,
                      bytearray(), [], bytearray())
     res = self.__sdk.rpc.send_raw_transaction_preexec(tx)
     return res
Exemple #24
0
 def verify_token(self, identity: Identity, password: str, key_no: int, contract_address: str, function_name: str):
     contract_address = bytearray.fromhex(contract_address)
     param = {"contract_address": contract_address, "ontid": identity.ont_id.encode('utf-8'), "function_name": function_name.encode('utf-8'), "key_no": key_no}
     invoke_code = build_native_invoke_code(bytearray.fromhex(self.contract_address), bytes([0]), "verifyToken", param)
     unix_time_now = int(time())
     tx = Transaction(0, 0xd1, unix_time_now, 0, 0, Address(ZERO_ADDRESS).to_array(), invoke_code, bytearray(), [], bytearray())
     account = self.__sdk.wallet_manager.get_account(identity.ont_id, password)
     self.__sdk.sign_transaction(tx, account)
     res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
     return res
Exemple #25
0
 def query_symbol(self, asset: str) -> str:
     contract_address = util.get_asset_address(asset)
     invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                            "symbol", bytearray())
     unix_time_now = int(time())
     payer = Address(ZERO_ADDRESS).to_array()
     tx = Transaction(0, 0xd1, unix_time_now, 0, 0, payer, invoke_code,
                      bytearray(), [], bytearray())
     res = self.__sdk.rpc.send_raw_transaction_pre_exec(tx)
     return bytes.fromhex(res).decode()
Exemple #26
0
 def new_registry_ontid_transaction(ont_id: str, pubkey: str, payer: str,
                                    gas_limit: int, gas_price: int):
     contract_address = ONTID_CONTRACT_ADDRESS
     args = {"ontid": ont_id.encode(), "pubkey": bytearray.fromhex(pubkey)}
     invoke_code = build_vm.build_native_invoke_code(
         contract_address, bytes([0]), "regIDWithPublicKey", args)
     unix_time_now = int(time())
     return Transaction(0, 0xd1, unix_time_now, gas_price, gas_limit,
                        Address.b58decode(payer).to_array(), invoke_code,
                        bytearray(), [], bytearray())
Exemple #27
0
    def new_transfer_transaction(asset: str, b58_from_address: str,
                                 b58_to_address: str, amount: int,
                                 b58_payer_address: str, gas_limit: int,
                                 gas_price: int) -> Transaction:
        """
        This interface is used to generate a Transaction object for transfer.

        :param asset: a string which is used to indicate which asset we want to transfer.
        :param b58_from_address: a base58 encode address which indicate where the asset from.
        :param b58_to_address: a base58 encode address which indicate where the asset to.
        :param amount: the amount of asset that will be transferred.
        :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 can be used for transfer.
        """
        if not isinstance(b58_from_address, str) or not isinstance(
                b58_to_address, str) or not isinstance(b58_payer_address, str):
            raise SDKException(
                ErrorCode.param_err(
                    'the data type of base58 encode address should be the string.'
                ))
        if len(b58_from_address) != 34 or len(b58_to_address) != 34 or len(
                b58_payer_address) != 34:
            raise SDKException(
                ErrorCode.param_err(
                    'the length of base58 encode address should be 34 bytes.'))
        if amount <= 0:
            raise SDKException(
                ErrorCode.other_error(
                    'the amount should be greater than than zero.'))
        if gas_price < 0:
            raise SDKException(
                ErrorCode.other_error(
                    'the gas price should be equal or greater than zero.'))
        if gas_limit < 0:
            raise SDKException(
                ErrorCode.other_error(
                    'the gas limit should be equal or greater than zero.'))
        contract_address = util.get_asset_address(asset)
        raw_from = Address.b58decode(b58_from_address).to_array()
        raw_to = Address.b58decode(b58_to_address).to_array()
        raw_payer = Address.b58decode(b58_payer_address).to_array()
        state = [{"from": raw_from, "to": raw_to, "amount": amount}]
        invoke_code = build_native_invoke_code(contract_address, bytes([0]),
                                               "transfer", state)
        unix_time_now = int(time())
        version = 0
        tx_type = 0xd1
        attributes = bytearray()
        signers = list()
        hash_value = bytearray()
        return Transaction(version, tx_type, unix_time_now, gas_price,
                           gas_limit, raw_payer, invoke_code, attributes,
                           signers, hash_value)
Exemple #28
0
 def new_get_ddo_transaction(ont_id: str):
     contract_address = ONTID_CONTRACT_ADDRESS
     args = {"ontid": ont_id.encode()}
     invoke_code = build_vm.build_native_invoke_code(
         contract_address, bytes([0]), "getDDO", args)
     unix_time_now = int(time())
     payer = Address(
         a2b_hex("0000000000000000000000000000000000000000".encode())
     ).to_array()
     return Transaction(0, 0xd1, unix_time_now, 0, 0, payer, invoke_code,
                        bytearray(), [], bytearray())
Exemple #29
0
 def query_balance(self, asset: str, addr: str):
     contract_address = util.get_asset_address(asset)
     invoke_code = build_native_invoke_code(
         contract_address, bytes([0]), "balanceOf",
         Address.decodeBase58(addr).to_array())
     unix_timenow = int(time())
     payer = Address(ZERO_ADDRESS).to_array()
     tx = Transaction(0, 0xd1, unix_timenow, 0, 0, payer, invoke_code,
                      bytearray(), [], bytearray())
     res = self.__sdk.rpc.send_raw_transaction_preexec(tx)
     return int(res, 16)
Exemple #30
0
 def __generate_transaction(self, method: str, args: dict, b58_payer_address: str or None, gas_limit: int,
                            gas_price: int):
     if isinstance(b58_payer_address, str):
         payer = Address.b58decode(b58_payer_address).to_bytes()
     elif b58_payer_address is None:
         payer = None
     else:
         raise SDKException(ErrorCode.require_str_params)
     invoke_code = build_vm.build_native_invoke_code(self.__contract_address, self.__version, method, args)
     tx = Transaction(0, 0xd1, int(time()), gas_price, gas_limit, payer, invoke_code, bytearray(), [])
     return tx