2019-08-15 18:36:50 -07:00
|
|
|
from libp2p.crypto.keys import KeyPair
|
2019-08-13 18:18:12 -07:00
|
|
|
from libp2p.crypto.rsa import create_new_key_pair
|
2019-08-13 17:43:21 -07:00
|
|
|
from libp2p.host.basic_host import BasicHost
|
2019-10-15 19:02:03 +02:00
|
|
|
from libp2p.host.host_interface import IHost
|
2019-10-14 00:29:28 +02:00
|
|
|
from libp2p.host.routed_host import RoutedHost
|
2020-01-26 16:44:42 +08:00
|
|
|
from libp2p.network.network_interface import INetworkService
|
2019-08-13 17:43:21 -07:00
|
|
|
from libp2p.network.swarm import Swarm
|
|
|
|
from libp2p.peer.id import ID
|
|
|
|
from libp2p.peer.peerstore import PeerStore
|
2019-08-11 16:47:54 +08:00
|
|
|
from libp2p.peer.peerstore_interface import IPeerStore
|
|
|
|
from libp2p.routing.interfaces import IPeerRouting
|
2019-08-20 11:02:21 +08:00
|
|
|
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport
|
2019-08-24 00:00:11 +02:00
|
|
|
import libp2p.security.secio.transport as secio
|
2019-08-20 11:02:21 +08:00
|
|
|
from libp2p.stream_muxer.mplex.mplex import MPLEX_PROTOCOL_ID, Mplex
|
2019-08-13 17:43:21 -07:00
|
|
|
from libp2p.transport.tcp.tcp import TCP
|
2019-09-24 12:51:59 +08:00
|
|
|
from libp2p.transport.typing import TMuxerOptions, TSecurityOptions
|
2019-08-13 17:43:21 -07:00
|
|
|
from libp2p.transport.upgrader import TransportUpgrader
|
|
|
|
from libp2p.typing import TProtocol
|
2019-01-09 21:38:56 +03:00
|
|
|
|
|
|
|
|
2019-08-02 17:39:19 -07:00
|
|
|
def generate_new_rsa_identity() -> KeyPair:
|
|
|
|
return create_new_key_pair()
|
|
|
|
|
|
|
|
|
2019-09-03 13:26:58 -07:00
|
|
|
def generate_peer_id_from(key_pair: KeyPair) -> ID:
|
2019-08-02 17:39:19 -07:00
|
|
|
public_key = key_pair.public_key
|
|
|
|
return ID.from_pubkey(public_key)
|
2019-04-20 17:35:05 -04:00
|
|
|
|
2019-07-31 15:00:12 -07:00
|
|
|
|
2020-01-26 16:44:42 +08:00
|
|
|
def new_swarm(
|
2019-12-24 18:03:18 +08:00
|
|
|
key_pair: KeyPair = None,
|
2019-10-15 19:11:11 +02:00
|
|
|
muxer_opt: TMuxerOptions = None,
|
|
|
|
sec_opt: TSecurityOptions = None,
|
|
|
|
peerstore_opt: IPeerStore = None,
|
2019-12-24 18:03:18 +08:00
|
|
|
) -> INetworkService:
|
2019-10-24 20:10:45 +02:00
|
|
|
"""
|
2020-01-26 16:44:42 +08:00
|
|
|
Create a swarm instance based on the parameters.
|
2019-10-24 08:41:10 +02:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
:param key_pair: optional choice of the ``KeyPair``
|
2019-03-17 19:20:46 -04:00
|
|
|
:param muxer_opt: optional choice of stream muxer
|
|
|
|
:param sec_opt: optional choice of security upgrade
|
|
|
|
:param peerstore_opt: optional peerstore
|
|
|
|
:return: return a default swarm instance
|
|
|
|
"""
|
2019-04-20 17:35:05 -04:00
|
|
|
|
2020-01-26 16:44:42 +08:00
|
|
|
if key_pair is None:
|
2019-12-24 18:03:18 +08:00
|
|
|
key_pair = generate_new_rsa_identity()
|
2019-01-09 21:38:56 +03:00
|
|
|
|
2019-12-24 18:03:18 +08:00
|
|
|
id_opt = generate_peer_id_from(key_pair)
|
|
|
|
|
|
|
|
# TODO: Parse `listen_addrs` to determine transport
|
2019-04-24 22:36:09 -04:00
|
|
|
transport = TCP()
|
|
|
|
|
2019-08-20 11:02:21 +08:00
|
|
|
muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex}
|
2019-08-02 16:04:46 -07:00
|
|
|
security_transports_by_protocol = sec_opt or {
|
2019-08-24 00:00:11 +02:00
|
|
|
TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
|
2019-08-24 11:43:27 +02:00
|
|
|
TProtocol(secio.ID): secio.Transport(key_pair),
|
2019-08-02 16:04:46 -07:00
|
|
|
}
|
2019-08-16 14:12:10 +08:00
|
|
|
upgrader = TransportUpgrader(
|
|
|
|
security_transports_by_protocol, muxer_transports_by_protocol
|
|
|
|
)
|
2019-04-24 22:36:09 -04:00
|
|
|
|
2019-03-17 19:20:46 -04:00
|
|
|
peerstore = peerstore_opt or PeerStore()
|
2019-11-25 16:55:55 +08:00
|
|
|
# Store our key pair in peerstore
|
2019-11-26 11:52:31 +08:00
|
|
|
peerstore.add_key_pair(id_opt, key_pair)
|
2019-11-25 16:55:55 +08:00
|
|
|
|
2019-10-14 00:29:28 +02:00
|
|
|
return Swarm(id_opt, peerstore, upgrader, transport)
|
2019-03-17 19:20:46 -04:00
|
|
|
|
2019-02-27 22:49:51 +01:00
|
|
|
|
2020-01-26 16:44:42 +08:00
|
|
|
def new_host(
|
2019-12-24 18:03:18 +08:00
|
|
|
key_pair: KeyPair = None,
|
|
|
|
muxer_opt: TMuxerOptions = None,
|
|
|
|
sec_opt: TSecurityOptions = None,
|
|
|
|
peerstore_opt: IPeerStore = None,
|
|
|
|
disc_opt: IPeerRouting = None,
|
2020-01-26 16:44:42 +08:00
|
|
|
) -> IHost:
|
|
|
|
"""
|
|
|
|
Create a new libp2p host based on the given parameters.
|
|
|
|
|
|
|
|
:param key_pair: optional choice of the ``KeyPair``
|
|
|
|
:param muxer_opt: optional choice of stream muxer
|
|
|
|
:param sec_opt: optional choice of security upgrade
|
|
|
|
:param peerstore_opt: optional peerstore
|
|
|
|
:param disc_opt: optional discovery
|
|
|
|
:return: return a host instance
|
|
|
|
"""
|
|
|
|
swarm = new_swarm(
|
2019-12-24 18:03:18 +08:00
|
|
|
key_pair=key_pair,
|
|
|
|
muxer_opt=muxer_opt,
|
|
|
|
sec_opt=sec_opt,
|
|
|
|
peerstore_opt=peerstore_opt,
|
|
|
|
)
|
2020-01-26 16:44:42 +08:00
|
|
|
host: IHost
|
|
|
|
if disc_opt:
|
|
|
|
host = RoutedHost(swarm, disc_opt)
|
|
|
|
else:
|
|
|
|
host = BasicHost(swarm)
|
|
|
|
return host
|