def get_account(self, public_key):
        try:
            logging.info("Public Key {}  from get_account".format(public_key))
            address = addresser.create_account_address(account_id=public_key,
                                                       index=0)
            logging.info(
                "Account address in get_account is {}".format(address))
        except Exception as e:
            logging.error(e)
            raise InvalidTransaction("get_account error =={}".format(e))

        ##entries will be a weird list of the form
        ##[address: "318c9fa5d39e9ccd2769115795e384b8e83b3267172ae518136ac49ddc5adf71d87814"
        ##data: "\nB02dbf0f4a3defef38df754122ef7c10fee6a4bb363312367524f86d230e205d459\022$b6de5d5b-7870-49df-971e-0885986bfa96\032
        ##\006seller\"\021978-0-9956537-6-4*\0161-191-790-04532\r1-932866-82-5:\001\000"]

        entries = self._context.get_state(addresses=[address],
                                          timeout=self._timeout)
        try:
            entry = entries[0]
        except Exception as e:
            logging.info("No account with address {} and publickey {} can be \
                        found".format(address, public_key))
            return False

        account = create_empty_account()
        account.ParseFromString(entry.data)
        logging.info("This is the account at {} stored on blockchain \
                                {}".format(address, account))
        return account
    def set_account(self, public_key, account_payload):
        if "" in [
                account_payload.role, account_payload.adhaar,
                account_payload.phone_number, account_payload.pancard,
                account_payload.organization_name
        ]:
            raise InvalidTransaction('shouldnt be left empty')

        logging.info("Entered into set_Account with parent pub %s" %
                     account_payload.parent_pub)
        address = addresser.create_account_address(account_id=public_key,
                                                   index=0)

        logging.info("THis is the accoutn address {}".format(address))
        self.claim_float_account(account_payload.parent_pub,
                                 account_payload.parent_idx, public_key,
                                 account_payload.indian_time)

        logging.info("Float account has been claimed ")

        #container = _get_account_container(self._state_entries, address)

        account = create_empty_account()
        account.public = public_key
        account.parent_zero_pub = account_payload.parent_zero_pub
        account.user_id = account_payload.user_id
        account.adhaar = account_payload.adhaar
        account.phone_number = account_payload.phone_number
        account.pancard = account_payload.pancard
        account.first_name = account_payload.first_name
        account.last_name = account_payload.last_name
        account.organization_name = account_payload.organization_name
        account.email = account_payload.email
        account.time = account_payload.time
        account.indian_time = account_payload.indian_time
        account.deactivate = account_payload.deactivate
        account.deactivate_on = account_payload.deactivate_on
        account.last_active = account_payload.last_active

        account.role = account_payload.role
        account.parent_role = account_payload.parent_role

        if account_payload.create_asset_idxs:
            account.create_asset_idxs.extend(account_payload.create_asset_idxs)

        logging.info(account)
        logging.info("Account after serialization %s",
                     account.SerializeToString())
        return self._context.set_state({address: account.SerializeToString()},
                                       self._timeout)
    def set_float_account_idxs(self, public_key, key_index):
        account = self.get_account(public_key)

        address = addresser.create_account_address(account_id=public_key,
                                                   index=0)

        if key_index in account.float_account_idxs:
            raise InvalidTransaction(
                "Key index is already present in float_account_idxs")

        account.float_account_idxs.append(key_index)

        self._context.set_state({address: account.SerializeToString()},
                                self._timeout)
        return
    def set_asset(self, payload, public, account, account_type):
        """
        payload will have the CreateAsset in the payload
        public: hex public key with whose private key transaction was signed
        account: could be a float account or account
        account_type: could be FLOAT_ACCOUNT or CREATE_ACCOUNT
        """
        logging.info("Account  in set_asset <<{}>>".format(account))
        if account_type == "FLOAT_ACCOUNT":
            account_address = addresser.float_account_address(
                account_id=payload.flt_account_parent_pub,
                index=payload.flt_account_parent_idx)
        else:
            account_address = addresser.create_account_address(
                account_id=payload.zero_pub, index=0)

        self.update_asset_index(account_address, account, payload.idx)

        address = addresser.create_asset_address(asset_id=public,
                                                 index=payload.idx)

        asset = create_empty_asset()

        asset.key = payload.key
        asset.url = payload.url
        asset.time = payload.time
        asset.indiantime = payload.indiantime
        asset.file_name = payload.file_name
        asset.file_hash = payload.file_hash
        asset.idx = payload.idx
        asset.master_key = payload.master_key
        asset.master_url = payload.master_url
        asset.role = payload.role
        asset.public = public

        if payload.scope:
            asset.scope.cert_type = payload.scope.cert_type
            asset.scope.product_type = payload.scope.product_type
            asset.scope.product_name = payload.scope.product_name

        logging.info(asset)
        logging.info("Account after serialization %s",
                     asset.SerializeToString())
        return self._context.set_state({address: asset.SerializeToString()},
                                       self._timeout)
    def transfer_assets(self, payload, public):

        issuer_account_address = addresser.create_account_address(
            account_id=payload.issuer_zero_pub, index=0)

        issuer_asset = self.asset_at_address(payload.issuer_address)
        issuer_asset.ownership_transfer = payload.receiver_address
        issuer_asset.transferred_on = payload.indiantime

        receiver_asset = self.asset_at_address(payload.receiver_address)
        receiver_asset.key = payload.key
        receiver_asset.url = payload.url
        receiver_asset.file_name = payload.file_name
        receiver_asset.file_hash = payload.file_hash
        receiver_asset.master_key = payload.master_key
        receiver_asset.master_url = payload.master_url

        if payload.scope:
            receiver_asset.scope.cert_type = payload.scope.cert_type
            receiver_asset.scope.product_type = payload.scope.product_type
            receiver_asset.scope.product_name = payload.scope.product_name
        """
        signature_list = []
        if payload.signatures:
            sig = create_empty_sig()
            for signature in payload.signatures:
                sig.ParseFromString(signature.encode())
            signature_list.append(sig)

        receiver_asset.authenticity_signatures = signature_list
        """
        receiver_asset.ownership_received = payload.issuer_address
        receiver_asset.received_on = payload.indiantime
        receiver_asset.parent_address = issuer_account_address

        self._context.set_state(
            {payload.issuer_address: issuer_asset.SerializeToString()},
            self._timeout)

        self._context.set_state(
            {payload.receiver_address: receiver_asset.SerializeToString()},
            self._timeout)
        return