def _get_MAM_from_address(self, address: Address): # TODO need refactoring and to be able to handle bundles and validate JSONs for MAML protocol hashes_and_trytes = self.tangle_con.get_all_trytes_from_address(address) if hashes_and_trytes: trytes = hashes_and_trytes[list(hashes_and_trytes.keys())[0]] tx_json = self.tangle_con.get_json_from_tryte(trytes) else: return None try: if not self.trusted_pubkeys: is_trusted = True elif tx_json['pubkey'] in self.trusted_pubkeys: is_trusted = Ed25519Cipher.verify_signature((tx_json['data_payload'] + address.__str__()).encode(), tx_json['signature'].encode(), Ed25519Cipher.key_from_string(tx_json['pubkey'])) else: is_trusted = False if self.channel_pwd: tx_json['data_payload'] = AESCipher(self.channel_pwd).decrypt(tx_json['data_payload']) msg = Message(tx_json['data_payload'], tx_json['pubkey'], tx_json['signature'],tx_json['timestamp']) response = Response(address.__str__(), self.tryte_hash(address.__str__() + self.channel_pwd), msg, True, is_trusted) except: # empty response response = Response(address.__str__(), self.tryte_hash(address.__str__()+ self.channel_pwd), Message(), False, False) return response
def __init__(self, root_address: Address, channel_password = ''): self.tangle_con = TangleConnector() self.root_address = root_address.__str__() self.channel_pwd = channel_password self.current_write_addr = root_address.__str__() self.current_read_addr = root_address.__str__() self.trusted_pubkeys = {}
def __init__(self, root_address: Address, write_pwd: str = '', read_pwd: str = ''): self.auth_comm = AuthCommunicator() self.root_address = root_address.__str__() self.write_pwd = write_pwd self.read_pwd = read_pwd self.current_write_addr = root_address.__str__() self.current_read_addr = root_address.__str__() self.trusted_pubkeys = {} self.set_stream_type(write_pwd, read_pwd)
def _get_MAM_from_address(self, address: Address) -> Response or None: ''' Discover whether the address contains messages that belong to the stream ''' try: auth_txs = self.auth_comm.get_auth_txs_from_address( address, list(self.trusted_pubkeys.keys())) if auth_txs: auth_msgs = [tx.auth_msg for tx in auth_txs] auth_msgs = self._decrypt_msgs(auth_msgs) response = Response( address.__str__(), hash_tryte(address.__str__() + self.write_pwd), auth_msgs, True, True) else: response = None except: response = None return response
def get_hashes_from_addr(self, address: Address) -> List[TransactionHash]: """ Get all tx from address """ try: response = self.iota_api.find_transactions(addresses=[address]) hashes = response['hashes'] except Exception as e: logging.warning( "Failed to get all tx from address " + address.__str__(), e) hashes = None return hashes
def send_msg_to_addr(self, address: Address, msg: str, tag: str) -> dict: """ Sends msg on Tangle to address with a tag """ try: response = self.iota_api.send_transfer( depth=5, transfers=[ ProposedTransaction(address=address, value=0, tag=Tag(tag), message=TryteString.from_string(msg)) ]) except Exception as e: logging.warning( "Message '" + msg + "' has failed to be stored in " + address.__str__(), e) response = None return response
def send_msg(self, auth_msg: AuthMessage, address: Address, tag: str = 'AUTH9MSG') -> AuthTransaction: ''' Send AuthMessage to address :param auth_msg: AuthMessage :param address: Address :param tag: str :return: AuthTransaction ''' auth_tx = None if auth_msg.validate(): res = self.tangle_con.send_msg_to_addr(address, auth_msg.to_json(), tag) if res: bundle = res['bundle'].as_json_compatible()[0] auth_tx = AuthTransaction( bundle['bundle_hash'].__str__(), address.__str__(), bundle['timestamp'], 0, # value auth_msg) return auth_tx