2018-11-13 02:02:49 +08:00
|
|
|
from Crypto.PublicKey import RSA
|
2018-11-12 09:29:17 +08:00
|
|
|
from peer.peerstore import PeerStore
|
|
|
|
from network.swarm import Swarm
|
|
|
|
from host.basic_host import BasicHost
|
|
|
|
from transport.upgrader import TransportUpgrader
|
2018-11-12 09:46:32 +08:00
|
|
|
from transport.tcp.tcp import TCP
|
2018-11-11 22:56:44 +08:00
|
|
|
|
|
|
|
|
2018-11-13 02:02:49 +08:00
|
|
|
class Libp2p():
|
|
|
|
|
|
|
|
def __init__(self, id_opt=None, transport_opt=["/ip4/127.0.0.1/tcp/8001"], \
|
|
|
|
muxer_opt=["mplex/6.7.0"], sec_opt=["secio"], peerstore=PeerStore()):
|
|
|
|
|
|
|
|
if id_opt:
|
|
|
|
self.id_opt = id_opt
|
2018-11-11 22:56:44 +08:00
|
|
|
else:
|
2018-11-12 02:21:47 +08:00
|
|
|
new_key = RSA.generate(2048, e=65537)
|
2018-11-13 02:02:49 +08:00
|
|
|
self.id_opt = new_key.publickey().exportKey("PEM")
|
2018-11-12 02:21:47 +08:00
|
|
|
self.private_key = new_key.exportKey("PEM")
|
2018-11-13 02:02:49 +08:00
|
|
|
|
|
|
|
self.transport_opt = transport_opt
|
|
|
|
self.muxer_opt = muxer_opt
|
|
|
|
self.sec_opt = sec_opt
|
2018-11-11 23:43:03 +08:00
|
|
|
self.peerstore = peerstore
|
|
|
|
|
2018-11-13 00:00:43 +08:00
|
|
|
async def new_node(self):
|
2018-11-11 23:43:03 +08:00
|
|
|
|
2018-11-13 02:02:49 +08:00
|
|
|
upgrader = TransportUpgrader(self.sec_opt, self.transport_opt)
|
|
|
|
swarm = Swarm(self.id_opt, self.peerstore, upgrader)
|
2018-11-12 09:41:21 +08:00
|
|
|
tcp = TCP()
|
|
|
|
swarm.add_transport(tcp)
|
2018-11-13 02:02:49 +08:00
|
|
|
await swarm.listen(self.transport_opt[0])
|
2018-11-11 23:43:03 +08:00
|
|
|
host = BasicHost(swarm)
|
|
|
|
|
2018-11-13 00:00:43 +08:00
|
|
|
# TODO MuxedConnection currently contains all muxing logic (move to a Muxer)
|
2018-11-12 09:41:21 +08:00
|
|
|
# TODO routing unimplemented
|
|
|
|
return host
|