コード例 #1
0
    def test_wireup(self):
        """
    Verify that the command is wired up correctly.

    The API method indeed calls the appropiate command.
    """
        with patch('iota.commands.core.get_tips.GetTipsCommand.__call__',
                   MagicMock(return_value='You found me!')) as mocked_command:

            api = Iota(self.adapter)

            # Don't need to call with proper args here.
            response = api.get_tips()

            self.assertTrue(mocked_command.called)

            self.assertEqual(response, 'You found me!')
コード例 #2
0
class IotaCache(object):

    def __init__(self, uri=None, seed=None):
        if not uri:
            self.uri = "http://localhost:14700"
        else:
            self.uri = uri
        if not seed:
            self.seed = 'EBZYNR9YVFIOAZUPQOLRZXPPPIKRCJ9EJKVCXMYVLMNOCCOPYPJKCWUZNLJZZZZWTMVQUXZFYLVLZXJ9Q'
        else:
            self.seed = seed
        self.api = Iota(self.uri, self.seed, testnet=True)
        self.mwm = 1
        self.depth = 15

    def cache_txn_in_tangle_sdk(self, ipfs_addr, tag):
        api_response = self.api.get_new_addresses()
        addy = api_response['addresses'][0]
        address = binary_type(addy).decode('ascii')

        result = self.api.send_transfer(
            depth=self.depth,
            transfers=[
                ProposedTransaction(
                    address=Address(address),
                    value=0,
                    tag=Tag(tag),
                    message=TryteString.from_string(ipfs_addr),
                ),
            ],
            min_weight_magnitude=self.mwm,
        )
        return result

    def cache_txn_in_tangle_simple(self, data, tag):
        address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX"
        txns = self.api.get_transactions_to_approve(self.depth)
        tr = self.api.get_trytes([txns[u'branchTransaction']])
        txn = Transaction.from_tryte_string(tr[u'trytes'][0], txns[u'branchTransaction'])
        txn.trunk_transaction_hash = txns[u'trunkTransaction']
        txn.branch_transaction_hash = txns[u'branchTransaction']
        txn.tag = Tag(TryteString.from_string(tag))
        txn.signature_message_fragment = Fragment(TryteString.from_bytes(data))
        attach_trytes = attachToTangle(self.uri, txns[u'trunkTransaction'].__str__(), txns[u'branchTransaction'].__str__(), 1, txn.as_tryte_string().__str__())
        res = self.api.broadcast_and_store(attach_trytes[u'trytes'])
        return res

    def cache_txn_in_tangle_message(self, data, tag):
        #address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX"
        address = "14dD6ygPi5WXdwwBTt1FBZK3aD8uDem1FY"
        res = storeMessage(self.uri, address, data, tag)
        return res

    def get_balance(self, coin_type, account):
        address = "JVSVAFSXWHUIZPFDLORNDMASGNXWFGZFMXGLCJQGFWFEZWWOA9KYSPHCLZHFBCOHMNCCBAGNACPIGHVYX"
        res = getBalance(self.uri, address, coin_type, account)
        return res

    def get_approved_txns(self, tag):
        ret = []
        transactions = self.api.find_transactions(None, None, [tag], None)
        if len(transactions['hashes']) == 0:
            return ret
        tips = self.api.get_tips()
        states = self.api.get_inclusion_states(transactions['hashes'], tips['hashes'])
        i = 0
        for txn in transactions['hashes']:
            if states['states'][i] is True:
                ret.append(txn)
        return ret

    def get_non_consumed_txns(self, tag):
        ret = []
        txn_hashes_all = self.get_approved_txns(tag)
        if len(txn_hashes_all) == 0:
            return ret
        txn_trytes_all = self.api.get_trytes(txn_hashes_all)
        consumedSet = []
        txn_hashes_consumed = self.api.find_transactions(None, None, [tag+b"CONSUMED"], None)
        if len(txn_hashes_consumed['hashes']) != 0:
            txn_trytes_consumed = self.api.get_trytes(txn_hashes_consumed['hashes'])
            i=0
            for txnTrytes in txn_trytes_consumed['trytes']:
                txn = Transaction.from_tryte_string(txnTrytes, txn_hashes_consumed['hashes'][i])
                i+=1
                consumedSet.append(txn.signature_message_fragment)
        i=0
        for txnTrytes in txn_trytes_all['trytes']:
            txn = Transaction.from_tryte_string(txnTrytes, txn_hashes_all[i])
            i+=1
            if txn.signature_message_fragment not in consumedSet:
                msgTryte = txn.signature_message_fragment.decode()
                ret.append(msgTryte)

        return ret

    def set_txn_as_synced(self, ipfs_addr, tag):
        result = self.cache_txn_in_tangle_sdk(ipfs_addr, tag+b"CONSUMED")
        return result

    def add_neighbors(self, uris):
        res = addNeighbors(self.uri, uris)
        return res

    def get_block_content(self, hashes):
        res = getBlockContent(self.uri, hashes)
        return res

    def get_dag(self, dag_type):
        res = getDAG(self.uri, dag_type)
        return res

    def get_utxo(self, dag_type):
        res = getUTXO(self.uri, dag_type)
        return res

    def get_total_order(self):
        res = getTotalOrder(self.uri)
        return res
コード例 #3
0
class TangleConnector:
    def __init__(self,
                 url='https://perma.iota.partners:443',
                 seed="TESTSEED9"):
        self.iri_url = url
        self.iota_api = Iota(url, seed)

    def get_node(self) -> Dict:
        """
        Get IRI node info
        """
        try:
            res = self.iota_api.get_node_info()
        except BadApiResponse as e:
            logger.warning("Failed to IRI node info for " + self.iri_url, e)
            res = None
        return res

    def get_tips(self) -> Dict:
        """
        Get all unreferenced transactions
        """
        try:
            res = self.iota_api.get_tips()
        except BadApiResponse as e:
            logger.warning("Failed to get tips", e)
            res = None
        return res

    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 (BadApiResponse, KeyError) as e:
            logger.warning(
                "Failed to get all tx from address " + address.__str__(), e)
            hashes = None
        return hashes

    def get_trytes_from_hashes(
            self, hashes: List[TransactionHash]) -> List[TryteString]:
        """
        Get tryte signature fragments from hashes
        """
        try:
            response = self.iota_api.get_trytes(hashes)
            trytes = [tryte[0:2187] for tryte in response['trytes']]
        except (BadApiResponse, KeyError) as e:
            logger.warning("Failed to get all signature fragments", e)
            trytes = None
        return trytes

    def get_all_trytes_from_address(
            self, address: Address) -> Dict[TransactionHash, TryteString]:
        """
        Get all trytes from address
        """
        hashes = self.get_hashes_from_addr(address)
        trytes = self.get_trytes_from_hashes(hashes)
        if hashes and trytes:
            result = dict(zip(hashes, trytes))
        else:
            result = None
        return result

    @staticmethod
    def get_json_from_tryte(tryte: TryteString) -> Dict:
        try:
            str_from_tryte = tryte.as_string()
            dict_from_tryte = json.loads(str_from_tryte)
        except ValueError as e:
            logger.error("Failed to convet trytes to JSON", e)
            dict_from_tryte = None
        return dict_from_tryte

    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=2,
                transfers=[
                    ProposedTransaction(address=address,
                                        value=0,
                                        tag=Tag(tag),
                                        message=TryteString.from_string(msg))
                ])
        except BadApiResponse as e:
            logger.warning(
                "Message '" + msg + "' has failed to be stored in " +
                address.__str__(), e)
            response = None
        return response

    def get_bundles_from_addr(self, address: Address) -> List[Dict]:
        '''
        Retrive all bundles sent to this address from IRI
        # requres IRI 1.6+ (or it seems so)
        '''
        hashes = self.get_hashes_from_addr(address)
        bundles = []
        if hashes:
            for tx in hashes:
                try:
                    # have to loop through all txs, if tx is not 'tail' of a bundle then get_bundles() throws error
                    bundle = self.iota_api.get_bundles(tx)
                except BadApiResponse:
                    bundle = None

                if bundle:
                    bundles.append(bundle)
        return bundles

    def get_messages_from_bundles(self, bundles: List) -> Dict:
        """
        Loop through list of bundles and get string per bundle
        # requres IRI 1.6+ (or it seems so)
        """
        output_msgs = {}
        for bundle in bundles:
            tx_list = bundle['bundles'][0].as_json_compatible()
            msg = ''
            for tx in tx_list:
                # if its a bundle with iotas, just get text from first tx in bundle, the rest are signatures
                if tx['current_index'] == 0 and tx['value'] > 0:
                    msg = tx['signature_message_fragment'].as_string()
                    break
                # if its a bundle without iota value, then consequently get all message fields and join strings
                if tx['value'] == 0:
                    msg = msg + tx['signature_message_fragment'].as_string()
            bundle_hash = tx_list[0]['bundle_hash'].__str__()
            addr = tx_list[0]['address'].__str__()
            timestamp = tx_list[0]['timestamp']
            value = tx_list[0]['value']
            output_msgs[bundle_hash] = {
                'msg': msg,
                'address': addr,
                'timestamp': timestamp,
                'value': value
            }
        return output_msgs

    def get_all_msg_from_addr_by_bundle(self, address: Address) -> Dict:
        '''
        Get dict of all msg with timestamps from address by bundle hash as key
        # requres IRI 1.6+ (or it seems so)
        '''
        bundles = self.get_bundles_from_addr(address)
        hashes_and_msgs = self.get_messages_from_bundles(bundles)
        return hashes_and_msgs
コード例 #4
0
ファイル: tangle_connector.py プロジェクト: AleBuser/MAMCR
class TangleConnector:
    def __init__(self,
                 url='https://nutzdoch.einfachiota.de',
                 seed="TESTSEED9"):
        self.iri_url = url
        self.iota_api = Iota(url, seed)

    def get_node(self) -> dict:
        """
        Get IRI node info
        """
        try:
            res = self.iota_api.get_node_info()
        except Exception as e:
            logging.warning("Failed to IRI node info for " + self.iri_url, e)
            res = None
        return res

    def get_tips(self) -> dict:
        """
        Get all unreferenced transactions
        """
        try:
            res = self.iota_api.get_tips()
        except Exception as e:
            logging.warning("Failed to get tips", e)
            res = None
        return res

    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 get_trytes_from_hashes(
            self, hashes: List[TransactionHash]) -> List[TryteString]:
        """
        Get tryte signature fragments from hashes
        """
        try:
            response = self.iota_api.get_trytes(hashes)
            if response['trytes']:
                trytes = [tryte[0:2187] for tryte in response['trytes']]
        except Exception:
            #logging.warning("Failed to get all signature fragments")
            trytes = None
        return trytes

    def get_all_trytes_from_address(
            self, address: Address) -> Dict[TransactionHash, TryteString]:
        """
        Get all trytes from address
        """
        hashes = self.get_hashes_from_addr(address)
        trytes = self.get_trytes_from_hashes(hashes)
        if hashes and trytes:
            result = dict(zip(hashes, trytes))
        else:
            result = None
        return result

    @staticmethod
    def get_json_from_tryte(tryte: TryteString) -> dict:
        try:
            str_from_tryte = tryte.as_string()
            dict_from_tryte = json.loads(str_from_tryte)
        except Exception as e:
            logging.error("Failed to convet trytes to JSON", e)
            dict_from_tryte = None
        return dict_from_tryte

    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
コード例 #5
0
ファイル: iota_cache.py プロジェクト: btellstrom/iri
class IotaCache(object):
    def __init__(self, uri=None, seed=None):
        if not uri:
            self.uri = "http://localhost:14700"
        else:
            self.uri = uri
        if not seed:
            self.seed = 'EBZYNR9YVFIOAZUPQOLRZXPPPIKRCJ9EJKVCXMYVLMNOCCOPYPJKCWUZNLJZZZZWTMVQUXZFYLVLZXJ9Q'
        else:
            self.seed = seed
        self.api = Iota(self.uri, self.seed)

    def cache_txn_in_tangle(self, ipfs_addr, tag):
        api_response = self.api.get_new_addresses()
        addy = api_response['addresses'][0]
        address = binary_type(addy).decode('ascii')

        result = self.api.send_transfer(
            depth=3,
            transfers=[
                ProposedTransaction(
                    address=Address(address),
                    value=0,
                    tag=Tag(tag),
                    message=TryteString.from_string(ipfs_addr),
                ),
            ],
        )
        return result

    def get_approved_txns(self, tag):
        ret = []
        transactions = self.api.find_transactions(None, None, [tag], None)
        if len(transactions['hashes']) == 0:
            return ret
        tips = self.api.get_tips()
        states = self.api.get_inclusion_states(transactions['hashes'],
                                               tips['hashes'])
        i = 0
        for txn in transactions['hashes']:
            if states['states'][i] is True:
                ret.append(txn)
        return ret

    def get_non_consumed_txns(self, tag):
        txn_hashes_all = self.get_approved_txns(tag)
        txn_trytes_all = self.api.get_trytes(txn_hashes_all)
        txn_hashes_consumed = self.api.find_transactions(
            None, None, [tag + b"CONSUMED"], None)
        txn_trytes_consumed = self.api.get_trytes(
            txn_hashes_consumed['hashes'])
        consumedSet = []
        ret = []
        for txnTrytes in txn_trytes_consumed['trytes']:
            txn = Transaction.from_tryte_string(txnTrytes)
            consumedSet.append(txn.signature_message_fragment)
        for txnTrytes in txn_trytes_all['trytes']:
            txn = Transaction.from_tryte_string(txnTrytes)
            if txn.signature_message_fragment not in consumedSet:
                msgTryte = txn.signature_message_fragment.decode()
                ret.append(msgTryte)
        return ret

    def set_txn_as_synced(self, ipfs_addr, tag):
        result = self.cache_txn_in_tangle(ipfs_addr, tag + b"CONSUMED")
        return result