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-12 02:21:47 +08:00
|
|
|
from Crypto.PublicKey import RSA
|
2018-11-11 22:56:44 +08:00
|
|
|
|
|
|
|
class Libp2p(object):
|
|
|
|
|
2018-11-12 08:46:00 +08:00
|
|
|
def __init__(self, idOpt = None, \
|
2018-11-13 00:00:43 +08:00
|
|
|
transportOpt = ["/ip4/127.0.0.1/tcp/8001"], \
|
2018-11-11 22:56:44 +08:00
|
|
|
muxerOpt = ["mplex/6.7.0"], \
|
|
|
|
secOpt = ["secio"], \
|
2018-11-11 23:43:03 +08:00
|
|
|
peerstore = PeerStore()):
|
2018-11-11 22:56:44 +08:00
|
|
|
|
|
|
|
if idOpt:
|
|
|
|
self.idOpt = idOpt
|
|
|
|
else:
|
2018-11-12 02:21:47 +08:00
|
|
|
new_key = RSA.generate(2048, e=65537)
|
|
|
|
self.idOpt = new_key.publickey().exportKey("PEM")
|
|
|
|
self.private_key = new_key.exportKey("PEM")
|
2018-11-12 02:23:34 +08:00
|
|
|
|
2018-11-11 23:43:03 +08:00
|
|
|
self.transportOpt = transportOpt
|
2018-11-12 02:22:31 +08:00
|
|
|
self.muxerOpt = muxerOpt
|
2018-11-11 23:43:03 +08:00
|
|
|
self.secOpt = secOpt
|
|
|
|
self.peerstore = peerstore
|
|
|
|
|
2018-11-13 00:00:43 +08:00
|
|
|
async def new_node(self):
|
2018-11-11 23:43:03 +08:00
|
|
|
|
|
|
|
upgrader = TransportUpgrader(self.secOpt, self.transportOpt)
|
2018-11-12 09:41:21 +08:00
|
|
|
swarm = Swarm(self.idOpt, self.peerstore, upgrader)
|
|
|
|
tcp = TCP()
|
|
|
|
swarm.add_transport(tcp)
|
2018-11-13 00:00:43 +08:00
|
|
|
await swarm.listen(self.transportOpt[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
|