2018-11-11 22:56:44 +08:00
|
|
|
from ..peer.peerstore import PeerStore
|
2018-11-11 23:43:03 +08:00
|
|
|
from ..network.swarm import Swarm
|
|
|
|
from ..host.basic_host import BasicHost
|
|
|
|
from ..transport.upgrader import TransportUpgrader
|
2018-11-12 02:21:47 +08:00
|
|
|
from Crypto.PublicKey import RSA
|
2018-11-11 22:56:44 +08:00
|
|
|
|
|
|
|
class Libp2p(object):
|
|
|
|
|
|
|
|
def __init__(self, idOpt, \
|
2018-11-12 01:36:15 +08:00
|
|
|
transportOpt = ["/ip4/127.0.0.1/tcp/10000"], \
|
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:
|
|
|
|
# TODO generate RSA public key pair
|
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
|
|
|
|
|
|
|
|
def new_node(self):
|
|
|
|
|
2018-11-12 02:21:47 +08:00
|
|
|
swarm = Swarm(self.idOpt, self.peerstore)
|
2018-11-11 23:43:03 +08:00
|
|
|
host = BasicHost(swarm)
|
|
|
|
upgrader = TransportUpgrader(self.secOpt, self.transportOpt)
|
|
|
|
|
|
|
|
# TODO transport upgrade
|
|
|
|
|
|
|
|
# TODO listen on addrs
|
|
|
|
|
2018-11-12 01:36:15 +08:00
|
|
|
# TODO swarm add transports
|
|
|
|
|
|
|
|
# TODO: return host
|