def setup_test(window=1, packet_size=1000, packets=10000): """ Create two nodes who will be sending packets to each other. :param window: the window size for packets (1 is synchronous) :param packet_size: the size of each packet :param packets: the number of packets to send in the experiment :return: the deferred that fires once the experiment is complete """ configuration = get_default_configuration() configuration['overlays'] = [] configuration['keys'] = [] master_peer = Peer(ECCrypto().generate_key(u"low")) peer_ipv8 = IPv8(configuration) peer_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))} peer_ipv8.overlays = [ LoadOverlay(master_peer, peer_ipv8.keys['my_peer'], peer_ipv8.endpoint, peer_ipv8.network, window, packet_size, packets) ] counterparty_ipv8 = IPv8(configuration) counterparty_ipv8.keys = {'my_peer': Peer(ECCrypto().generate_key(u"low"))} counterparty_ipv8.overlays = [ LoadOverlay(master_peer, counterparty_ipv8.keys['my_peer'], counterparty_ipv8.endpoint, counterparty_ipv8.network, 0, packet_size, packets) ] peer_ipv8.overlays[0].send(counterparty_ipv8.endpoint.get_address()) return DeferredList( [peer_ipv8.overlays[0].done, counterparty_ipv8.overlays[0].done])
def connect_peer(self, mid): matched_node = MockObject() matched_node.mid = mid matched_node.public_key = ECCrypto().generate_key("low").pub() nearby_node = MockObject() nearby_node.mid = unhexlify('b' * 20) nearby_node.public_key = ECCrypto().generate_key("low").pub() return succeed([matched_node, nearby_node])
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def started(self): async def check_peers(): global INSTANCES, LOW_EDGE, LOW_EDGE_PEER, START_TIME if self.get_peers(): if LOW_EDGE and self.my_peer != LOW_EDGE_PEER: print("%.4f,%.4f" % (LOW_EDGE, time.time() - START_TIME)) async def shutdown(): for instance in INSTANCES: await instance.stop(False) get_event_loop().stop() ensure_future(shutdown()) else: LOW_EDGE = time.time() - START_TIME LOW_EDGE_PEER = self.my_peer self.register_task("check_peers", check_peers, interval=0.1, delay=0) def create_introduction_response(self, lan_socket_address, socket_address, identifier, introduction=None): return super(MyCommunity, self).create_introduction_response( lan_socket_address, socket_address, identifier, introduction, b'1') def create_introduction_request(self, socket_address): return super(MyCommunity, self).create_introduction_request(socket_address, b'2')
def test_peer_inequality_key(self): """ Check if peers with a different key and same address are not equal. """ other = Peer(ECCrypto().generate_key(u"very-low"), self.peer.address) self.assertNotEqual(self.peer, other)
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def started(self): def check_peers(): global INSTANCES, LOW_EDGE, LOW_EDGE_PEER, START_TIME if self.get_peers(): if LOW_EDGE and self.my_peer != LOW_EDGE_PEER: for instance in INSTANCES: instance.stop(False) reactor.callFromThread(reactor.stop) print("%.4f,%.4f" % (LOW_EDGE, time.time() - START_TIME)) else: LOW_EDGE = time.time() - START_TIME LOW_EDGE_PEER = self.my_peer self.register_task("check_peers", LoopingCall(check_peers)).start(0.1, True) def create_introduction_response(self, lan_socket_address, socket_address, identifier, introduction=None): return super(MyCommunity, self).create_introduction_response( lan_socket_address, socket_address, identifier, introduction, b'1') def create_introduction_request(self, socket_address): return super(MyCommunity, self).create_introduction_request(socket_address, b'2')
def __init__(self, crypto_curve, overlay_class, *args, **kwargs): self.endpoint = AutoMockEndpoint() self.endpoint.open() self.network = Network() self.my_peer = Peer(ECCrypto().generate_key(crypto_curve), self.endpoint.wan_address) self.overlay = overlay_class(self.my_peer, self.endpoint, self.network, *args, **kwargs) self.discovery = MockWalk(self.overlay) self.overlay.my_estimated_wan = self.endpoint.wan_address self.overlay.my_estimated_lan = self.endpoint.lan_address
def bootstrap_new_identity(self, amount): """ One-way payment channel. Create a new temporary identity, and transfer funds to the new identity. A different party can then take the result and do a transfer from the temporary identity to itself """ # Create new identity for the temporary identity crypto = ECCrypto() tmp_peer = Peer(crypto.generate_key(u"curve25519")) # Create the transaction specification transaction = {'up': 0, 'down': amount, 'type': b'tribler_bandwidth'} # Create the two half blocks that form the transaction local_half_block = TriblerBandwidthBlock.create( b'tribler_bandwidth', transaction, self.trustchain.persistence, self.trustchain.my_peer.public_key.key_to_bin(), link_pk=tmp_peer.public_key.key_to_bin()) local_half_block.sign(self.trustchain.my_peer.key) tmp_half_block = TriblerBandwidthBlock.create( b'tribler_bandwidth', transaction, self.trustchain.persistence, tmp_peer.public_key.key_to_bin(), link=local_half_block, link_pk=self.trustchain.my_peer.public_key.key_to_bin()) tmp_half_block.sign(tmp_peer.key) self.trustchain.persistence.add_block(local_half_block) self.trustchain.persistence.add_block(tmp_half_block) # Create the bootstrapped identity format block = { 'block_hash': b64encode(tmp_half_block.hash), 'sequence_number': tmp_half_block.sequence_number } result = { 'private_key': b64encode(tmp_peer.key.key_to_bin()), 'transaction': { 'up': amount, 'down': 0 }, 'block': block } return result
async def load_tunnel_community_in_session(self, session, exitnode=False, start_lt=False): """ Load the tunnel community in a given session. We are using our own tunnel community here instead of the one used in Tribler. """ await self.sanitize_network(session) keypair = ECCrypto().generate_key(u"curve25519") tunnel_peer = Peer(keypair) session.config.set_tunnel_community_exitnode_enabled(exitnode) overlay = self.test_class(tunnel_peer, session.ipv8.endpoint, session.ipv8.network, tribler_session=session, settings={"max_circuits": 1}) if exitnode: overlay.settings.peer_flags.add(PEER_FLAG_EXIT_ANY) overlay._use_main_thread = False overlay.dht_provider = MockDHTProvider( Peer(overlay.my_peer.key, overlay.my_estimated_wan)) overlay.settings.remove_tunnel_delay = 0 session.ipv8.overlays.append(overlay) await overlay.wait_for_socks_servers() if start_lt: # If libtorrent tries to connect to the socks5 servers before they are loaded, # it will never recover (on Mac/Linux with Libtorrent >=1.2.0). Therefore, we start # libtorrent afterwards. tunnel_community_ports = session.config.get_tunnel_community_socks5_listen_ports( ) session.config.set_anon_proxy_settings( 2, ("127.0.0.1", tunnel_community_ports)) session.dlmgr = DownloadManager(session) session.dlmgr.initialize() session.dlmgr.is_shutdown_ready = lambda: True return overlay
async def start_communities(): # Override the Community master peers so we don't interfere with the live network # Also hook in our custom logic for introduction responses for community_cls in _COMMUNITIES.values(): community_cls.master_peer = Peer(ECCrypto().generate_key(u"medium")) community_cls.introduction_response_callback = custom_intro_response_cb # Create two peers with separate working directories previous_workdir = getcwd() for i in [1, 2]: configuration = get_default_configuration() configuration['port'] = 12000 + randint(0, 10000) configuration['logger']['level'] = "CRITICAL" for overlay in configuration['overlays']: overlay['walkers'] = [walker for walker in overlay['walkers'] if walker['strategy'] in _WALKERS] workdir = path.abspath(path.join(path.dirname(__file__), str(i))) if not path.exists(workdir): mkdir(workdir) chdir(workdir) await IPv8(configuration).start() chdir(previous_workdir)
async def setUp(self): """ Setup various variables and load the tunnel community in the main downloader session. """ await TestAsServer.setUp(self) self.seed_tdef = None self.sessions = [] self.session2 = None self.bypass_dht = False self.seed_config = None self.tunnel_community_seeder = None self.eccrypto = ECCrypto() ec = self.eccrypto.generate_key(u"curve25519") self.test_class = TriblerTunnelCommunity self.test_class.master_peer = Peer(ec) self.tunnel_community = await self.load_tunnel_community_in_session( self.session, exitnode=True, start_lt=True) self.session.tunnel_community = self.tunnel_community # Magic! self.tunnel_communities = []
class TestPeer(unittest.TestCase): test_key = ECCrypto().generate_key(u"very-low") def setUp(self): super(TestPeer, self).setUp() self.peer = Peer(TestPeer.test_key, ("1.2.3.4", 5)) def test_default_timestamp(self): """ Check if the default Lamport timestamp of a Peer is 0. """ self.assertEqual(self.peer.get_lamport_timestamp(), 0) def test_increment_timestamp(self): """ Check if the Lamport timestamp of a Peer can be incremented. """ self.peer.update_clock(1) self.assertEqual(self.peer.get_lamport_timestamp(), 1) def test_increase_timestamp(self): """ Check if the Lamport timestamp of a Peer can be increased arbitrarily. """ self.peer.update_clock(42) self.assertEqual(self.peer.get_lamport_timestamp(), 42) def test_decrease_timestamp(self): """ Check if the Lamport timestamp of a Peer cannot be decreased. """ self.peer.update_clock(-1) self.assertEqual(self.peer.get_lamport_timestamp(), 0) def test_peer_equality(self): """ Check if peers with the same key and address are equal. """ other = Peer(self.peer.key, self.peer.address) self.assertEqual(self.peer, other) def test_peer_inequality_key(self): """ Check if peers with a different key and same address are not equal. """ other = Peer(ECCrypto().generate_key(u"very-low"), self.peer.address) self.assertNotEqual(self.peer, other) def test_peer_inequality_address(self): """ Check if peers with the same key and a different address are not equal. """ other = Peer(self.peer.key) self.assertNotEqual(self.peer, other) def test_to_string(self): """ Check if the __str__ method functions properly. """ self.assertEqual( str(self.peer), "Peer<1.2.3.4:5, %s>" % self.peer.mid.encode('base64')[:-1])
def setUp(self): self.ec = ECCrypto() self.key = self.ec.generate_key(u"very-low") self.key_nacl = self.ec.generate_key(u"curve25519")
def __init__(self): endpoint = AutoMockEndpoint() endpoint.open() network = Network() peer = Peer(ECCrypto().generate_key(u"very-low"), endpoint.wan_address) super(MockCommunity, self).__init__(peer, endpoint, network)
# If it takes longer than 30 seconds to find anything, abort the experiment and set the intro time to -1.0 def on_timeout(): for definition in get_default_configuration()['overlays']: if definition['class'] not in RESULTS: RESULTS[definition['class']] = -1.0 print(definition['class'], "found no peers at all!", file=sys.stderr) reactor.callFromThread(reactor.stop) # Override the Community master peers so we don't interfere with the live network # Also hook in our custom logic for introduction responses for community_cls in _COMMUNITIES.values(): community_cls.master_peer = Peer(ECCrypto().generate_key(u"medium")) community_cls.introduction_response_callback = custom_intro_response_cb # Create two peers with separate working directories previous_workdir = getcwd() for i in [1, 2]: configuration = get_default_configuration() configuration['port'] = 12000 + randint(0, 10000) configuration['logger']['level'] = "CRITICAL" for overlay in configuration['overlays']: overlay['walkers'] = [ walker for walker in overlay['walkers'] if walker['strategy'] in _WALKERS ] workdir = path.abspath(path.join(path.dirname(__file__), "%d" % i)) if not path.exists(workdir):
class TestECCrypto(unittest.TestCase): m2crypto_key = ECCrypto().generate_key(u"very-low") libnacl_key = ECCrypto().generate_key(u"curve25519") def setUp(self): self.ecc = ECCrypto() def test_available(self): """ Check if the required curves are available. """ available = self.ecc.security_levels self.assertIn(u"very-low", available) self.assertIn(u"low", available) self.assertIn(u"medium", available) self.assertIn(u"high", available) self.assertIn(u"curve25519", available) def test_generate_m2crypto(self): """ Check if M2Crypto backend keys can be generated correctly. """ self.assertIsInstance(TestECCrypto.m2crypto_key, Key) self.assertIsInstance(TestECCrypto.m2crypto_key, PrivateKey) self.assertIsInstance(TestECCrypto.m2crypto_key, PublicKey) self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoSK) self.assertIsInstance(TestECCrypto.m2crypto_key, M2CryptoPK) def test_generate_nacl(self): """ Check if libnacl backend keys can be generated correctly. """ self.assertIsInstance(TestECCrypto.libnacl_key, Key) self.assertIsInstance(TestECCrypto.libnacl_key, PrivateKey) self.assertIsInstance(TestECCrypto.libnacl_key, PublicKey) self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLSK) self.assertIsInstance(TestECCrypto.libnacl_key, LibNaCLPK) def test_generate_bogus(self): """ Check if a bogus curve produces a RuntimeError """ self.assertRaises(RuntimeError, self.ecc.generate_key, u"idontexist") def test_key_to_bin_m2crypto(self): """ Check if ECCrypto correctly detects an M2Crypto key for bin. """ key_bin = self.ecc.key_to_bin(TestECCrypto.m2crypto_key) self.assertEqual(key_bin, TestECCrypto.m2crypto_key.key_to_bin()) def test_key_to_bin_nacl(self): """ Check if ECCrypto correctly detects an libnacl key for bin. """ key_bin = self.ecc.key_to_bin(TestECCrypto.libnacl_key) self.assertEqual(key_bin, TestECCrypto.libnacl_key.key_to_bin()) def test_key_to_hash_m2crypto(self): """ Check if ECCrypto correctly detects an M2Crypto key for hash. """ key_hash = self.ecc.key_to_hash(TestECCrypto.m2crypto_key) self.assertEqual(key_hash, TestECCrypto.m2crypto_key.key_to_hash()) def test_key_to_hash_nacl(self): """ Check if ECCrypto correctly detects an libnacl key for hash. """ key_hash = self.ecc.key_to_hash(TestECCrypto.libnacl_key) self.assertEqual(key_hash, TestECCrypto.libnacl_key.key_to_hash()) def test_is_valid_private_bin_m2crypto(self): """ Check if ECCrypto can detect a valid M2Crypto private key. """ self.assertTrue( self.ecc.is_valid_private_bin( TestECCrypto.m2crypto_key.key_to_bin())) def test_is_valid_private_bin_m2crypto_public(self): """ Check if ECCrypto doesn't detect a valid public M2Crypto key as a private key. """ self.assertFalse( self.ecc.is_valid_private_bin( TestECCrypto.m2crypto_key.pub().key_to_bin())) def test_is_valid_private_bin_nacl(self): """ Check if ECCrypto can detect a valid libnacl private key. """ self.assertTrue( self.ecc.is_valid_private_bin( TestECCrypto.libnacl_key.key_to_bin())) def test_is_valid_private_bin_nacl_public(self): """ Check if ECCrypto doesn't detect a valid public libnacl key as a private key. """ self.assertFalse( self.ecc.is_valid_private_bin( TestECCrypto.libnacl_key.pub().key_to_bin())) def test_is_valid_public_bin_m2crypto(self): """ Check if ECCrypto doesn't detect a valid M2Crypto private key as a public key. """ self.assertFalse( self.ecc.is_valid_public_bin( TestECCrypto.m2crypto_key.key_to_bin())) def test_is_valid_public_bin_m2crypto_public(self): """ Check if ECCrypto detects a valid public M2Crypto key as a public key. """ self.assertTrue( self.ecc.is_valid_public_bin( TestECCrypto.m2crypto_key.pub().key_to_bin())) def test_is_valid_public_bin_nacl(self): """ Check if ECCrypto doesn't detect a valid libnacl private key as a public key. """ self.assertFalse( self.ecc.is_valid_public_bin( TestECCrypto.libnacl_key.key_to_bin())) def test_is_valid_public_bin_nacl_public(self): """ Check if ECCrypto detects a valid public libnacl key as a public key. """ self.assertTrue( self.ecc.is_valid_public_bin( TestECCrypto.libnacl_key.pub().key_to_bin()))
def __init__(self, configuration, endpoint_override=None, enable_statistics=False): if endpoint_override: self.endpoint = endpoint_override else: self.endpoint = UDPEndpoint(port=configuration['port'], ip=configuration['address']) self.endpoint.open() if enable_statistics: self.endpoint = StatisticsEndpoint(self, self.endpoint) self.network = Network() # Load/generate keys self.keys = {} for key_block in configuration['keys']: if key_block['file'] and isfile(key_block['file']): with open(key_block['file'], 'r') as f: content = f.read() try: # IPv8 Standardized bin format self.keys[key_block['alias']] = Peer( ECCrypto().key_from_private_bin(content)) except ValueError: try: # Try old Tribler M2Crypto PEM format content = content[31:-30].replace( '\n', '').decode("BASE64") peer = Peer(M2CryptoSK(keystring=content)) peer.mid # This will error out if the keystring is not M2Crypto self.keys[key_block['alias']] = peer except: # Try old LibNacl format content = "LibNaCLSK:" + content self.keys[key_block['alias']] = Peer( ECCrypto().key_from_private_bin(content)) else: self.keys[key_block['alias']] = Peer( ECCrypto().generate_key(key_block['generation'])) if key_block['file']: with open(key_block['file'], 'w') as f: f.write( self.keys[key_block['alias']].key.key_to_bin()) # Setup logging logging.basicConfig(**configuration['logger']) self.overlay_lock = RLock() self.strategies = [] self.overlays = [] for overlay in configuration['overlays']: overlay_class = _COMMUNITIES[overlay['class']] my_peer = self.keys[overlay['key']] overlay_instance = overlay_class(my_peer, self.endpoint, self.network, **overlay['initialize']) self.overlays.append(overlay_instance) for walker in overlay['walkers']: strategy_class = _WALKERS[walker['strategy']] args = walker['init'] target_peers = walker['peers'] self.strategies.append( (strategy_class(overlay_instance, **args), target_peers)) for config in overlay['on_start']: reactor.callWhenRunning( getattr(overlay_instance, config[0]), *config[1:]) self.state_machine_lc = LoopingCall(self.on_tick) self.state_machine_lc.start(configuration['walker_interval'], False)
class MyCommunity(Community): master_peer = Peer(ECCrypto().generate_key(u"medium")) def __init__(self, *args, **kwargs): super(MyCommunity, self).__init__(*args, **kwargs) self.request_cache = RequestCache() def unload(self): self.request_cache.shutdown() super(MyCommunity, self).unload() def finish_ping(self, cache, include=True): global RESULTS print(cache.hostname, cache.address, time.time() - cache.starttime) if include: if (cache.hostname, cache.address) in RESULTS: RESULTS[(cache.hostname, cache.address)].append(time.time() - cache.starttime) else: RESULTS[(cache.hostname, cache.address)] = [time.time() - cache.starttime] elif (cache.hostname, cache.address) not in RESULTS: RESULTS[(cache.hostname, cache.address)] = [] self.next_ping() def next_ping(self): global CHECK_QUEUE if CHECK_QUEUE: hostname, address = CHECK_QUEUE.pop() packet = self.create_introduction_request(address) self.request_cache.add( PingCache(self, hostname, address, time.time())) self.endpoint.send(address, packet) else: reactor.callFromThread(reactor.stop) def introduction_response_callback(self, peer, dist, payload): if self.request_cache.has(u"introping", payload.identifier): cache = self.request_cache.pop(u"introping", payload.identifier) self.finish_ping(cache) def started(self): global CHECK_QUEUE dnsmap = {} for (address, port) in _DNS_ADDRESSES: try: ip = gethostbyname(address) dnsmap[(ip, port)] = address except: pass UNKNOWN_NAME = '*' for (ip, port) in _DEFAULT_ADDRESSES: hostname = dnsmap.get((ip, port), None) if not hostname: hostname = UNKNOWN_NAME UNKNOWN_NAME = UNKNOWN_NAME + '*' CHECK_QUEUE.append((hostname, (ip, port))) CHECK_QUEUE = CHECK_QUEUE * CONST_REQUESTS self.next_ping()
def __init__(self, endpoint): my_peer = Peer(ECCrypto().generate_key(u"very-low")) super(EndpointServer, self).__init__(my_peer, endpoint, Network()) self.churn_lc = self.register_task("churn", LoopingCall(self.do_churn)).start(5.0, now=False) self.churn_strategy = RandomChurn(self) self.crypto = ECCrypto()
from twisted.web import server from ipv8.REST.rest_manager import RESTRequest from ipv8.REST.root_endpoint import RootEndpoint from ipv8.attestation.identity.community import IdentityCommunity from ipv8.attestation.wallet.community import AttestationCommunity from ipv8.configuration import get_default_configuration from ipv8.keyvault.crypto import ECCrypto from ipv8.peer import Peer from ipv8.taskmanager import TaskManager from ipv8.test.REST.rtest.peer_communication import GetStyleRequests, PostStyleRequests from ipv8.test.REST.rtest.rest_peer_communication import HTTPGetRequester, HTTPPostRequester from ipv8_service import IPv8 COMMUNITY_TO_MASTER_PEER_KEY = { 'AttestationCommunity': ECCrypto().generate_key(u'high'), 'DiscoveryCommunity': ECCrypto().generate_key(u'high'), 'HiddenTunnelCommunity': ECCrypto().generate_key(u'high'), 'IdentityCommunity': ECCrypto().generate_key(u'high'), 'TrustChainCommunity': ECCrypto().generate_key(u'high'), 'TunnelCommunity': ECCrypto().generate_key(u'high') } class TestPeer(object): """ Class for the purpose of testing the REST API """ def __init__(self, path,
def _generate_peer(): key = ECCrypto().generate_key(u'very-low') address = (".".join([str(random.randint(0, 255)) for _ in range(4)]), random.randint(0, 65535)) return Peer(key, address)
from __future__ import absolute_import from __future__ import print_function from binascii import hexlify import sys from ipv8.keyvault.crypto import ECCrypto # This script generates a curve25519 key and prints it in hex format if '--public' in sys.argv: print(hexlify(ECCrypto().generate_key(u"curve25519").pub().key_to_bin())) else: print(hexlify(ECCrypto().generate_key(u"curve25519").key_to_bin()))
def setUp(self): self.ecc = ECCrypto()
def setUp(self): self.ec = ECCrypto() self.data = "".join([chr(i) for i in range(256)])