packages/hyprspace: make pnet optional

This commit is contained in:
Max Headroom 2022-11-12 21:51:21 +01:00
parent b45ed60e8b
commit 411143c600

View file

@ -69,27 +69,28 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
return return
} }
var swarmKey *os.File maybePrivateNet := libp2p.ChainOptions()
swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY") swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY")
if ok { if ok {
fmt.Println("[+] Using swarm key " + swarmKeyFile) fmt.Println("[+] Using swarm key " + swarmKeyFile)
var swarmKey *os.File
swarmKey, err = os.Open(swarmKeyFile) swarmKey, err = os.Open(swarmKeyFile)
if err != nil { if err != nil {
return return
} }
defer swarmKey.Close() defer swarmKey.Close()
key, _ := pnet.DecodeV1PSK(swarmKey)
maybePrivateNet = libp2p.PrivateNetwork(key)
} }
ip6tcp := fmt.Sprintf("/ip6/::/tcp/%d", port) ip6tcp := fmt.Sprintf("/ip6/::/tcp/%d", port)
ip4tcp := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port) ip4tcp := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)
key, _ := pnet.DecodeV1PSK(swarmKey)
peerChan := make(chan peer.AddrInfo) peerChan := make(chan peer.AddrInfo)
// Create libp2p node // Create libp2p node
node, err = libp2p.New( node, err = libp2p.New(
libp2p.PrivateNetwork(key), maybePrivateNet,
libp2p.ListenAddrStrings(ip6tcp, ip4tcp), libp2p.ListenAddrStrings(ip6tcp, ip4tcp),
libp2p.Identity(privateKey), libp2p.Identity(privateKey),
libp2p.DefaultSecurity, libp2p.DefaultSecurity,