Search in sources :

Example 6 with PrivKey

use of io.libp2p.core.crypto.PrivKey in project teku by ConsenSys.

the class LibP2PPrivateKeyLoaderTest method testPrivateKeyGeneration.

@Test
void testPrivateKeyGeneration() {
    final LibP2PPrivateKeyLoader loader = new LibP2PPrivateKeyLoader(store, Optional.empty());
    // check that new key is generated
    final PrivKey generatedPK = loader.get();
    assertThat(generatedPK).isNotNull();
    assertRoundTrip(generatedPK);
    // check the same key loaded next time
    PrivKey loadedPK = loader.get();
    assertThat(loadedPK).isEqualTo(generatedPK);
    // If store is cleared, we should generate a new key
    store.remove(LibP2PPrivateKeyLoader.GENERATED_NODE_KEY_KEY);
    // check that another key is generated after old key is deleted
    PrivKey generatedAnotherPK = loader.get();
    assertThat(generatedAnotherPK).isNotNull();
    assertRoundTrip(generatedAnotherPK);
    assertThat(generatedAnotherPK).isNotEqualTo(generatedPK);
}
Also used : PrivKey(io.libp2p.core.crypto.PrivKey) Test(org.junit.jupiter.api.Test)

Example 7 with PrivKey

use of io.libp2p.core.crypto.PrivKey in project teku by ConsenSys.

the class LibP2PPrivateKeyLoaderTest method testPrivateKeyLoaded.

@Test
void testPrivateKeyLoaded() {
    // check that user supplied private key file has precedence over generated file
    final PrivKey privKey = PrivateKeyGenerator.generate();
    final Bytes privKeyBytes = Bytes.wrap(privKey.bytes());
    PrivateKeySource privKeySource = () -> privKeyBytes;
    final LibP2PPrivateKeyLoader loader = new LibP2PPrivateKeyLoader(store, Optional.of(privKeySource));
    assertThat(loader.get()).isEqualTo(privKey);
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrivKey(io.libp2p.core.crypto.PrivKey) PrivateKeySource(tech.pegasys.teku.networking.p2p.network.config.NetworkConfig.PrivateKeySource) Test(org.junit.jupiter.api.Test)

Example 8 with PrivKey

use of io.libp2p.core.crypto.PrivKey in project teku by ConsenSys.

the class LibP2PNetworkBuilder method createHost.

protected Host createHost() {
    PrivKey privKey = privateKeyProvider.get();
    NodeId nodeId = new LibP2PNodeId(PeerId.fromPubKey(privKey.publicKey()));
    Multiaddr advertisedAddr = MultiaddrUtil.fromInetSocketAddress(new InetSocketAddress(config.getAdvertisedIp(), config.getAdvertisedPort()), nodeId);
    final Multiaddr listenAddr = MultiaddrUtil.fromInetSocketAddress(new InetSocketAddress(config.getNetworkInterface(), config.getListenPort()));
    return BuilderJKt.hostJ(hostBuilderDefaults, b -> {
        b.getIdentity().setFactory(() -> privKey);
        b.getTransports().add(TcpTransport::new);
        b.getSecureChannels().add(NoiseXXSecureChannel::new);
        b.getMuxers().add(StreamMuxerProtocol.getMplex());
        b.getNetwork().listen(listenAddr.toString());
        b.getProtocols().addAll(getDefaultProtocols(privKey.publicKey(), advertisedAddr));
        b.getProtocols().add(gossipNetwork.getGossip());
        b.getProtocols().addAll(rpcHandlers);
        if (config.getWireLogsConfig().isLogWireCipher()) {
            b.getDebug().getBeforeSecureHandler().addLogger(LogLevel.DEBUG, "wire.ciphered");
        }
        b.getDebug().getBeforeSecureHandler().addNettyHandler(firewall);
        if (config.getWireLogsConfig().isLogWirePlain()) {
            b.getDebug().getAfterSecureHandler().addLogger(LogLevel.DEBUG, "wire.plain");
        }
        if (config.getWireLogsConfig().isLogWireMuxFrames()) {
            b.getDebug().getMuxFramesHandler().addLogger(LogLevel.DEBUG, "wire.mux");
        }
        b.getConnectionHandlers().add(peerManager);
        b.getDebug().getMuxFramesHandler().addHandler(mplexFirewall);
    });
}
Also used : NoiseXXSecureChannel(io.libp2p.security.noise.NoiseXXSecureChannel) InetSocketAddress(java.net.InetSocketAddress) NodeId(tech.pegasys.teku.networking.p2p.peer.NodeId) PrivKey(io.libp2p.core.crypto.PrivKey) TcpTransport(io.libp2p.transport.tcp.TcpTransport) Multiaddr(io.libp2p.core.multiformats.Multiaddr)

Aggregations

PrivKey (io.libp2p.core.crypto.PrivKey)8 InetSocketAddress (java.net.InetSocketAddress)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Before (org.junit.Before)2 Test (org.junit.jupiter.api.Test)2 PeerId (io.libp2p.core.PeerId)1 PubKey (io.libp2p.core.crypto.PubKey)1 Multiaddr (io.libp2p.core.multiformats.Multiaddr)1 NoiseXXSecureChannel (io.libp2p.security.noise.NoiseXXSecureChannel)1 TcpTransport (io.libp2p.transport.tcp.TcpTransport)1 DiscoveryPeer (io.xdag.net.libp2p.discovery.DiscoveryPeer)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 InvalidConfigurationException (tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException)1 PrivateKeySource (tech.pegasys.teku.networking.p2p.network.config.NetworkConfig.PrivateKeySource)1 NodeId (tech.pegasys.teku.networking.p2p.peer.NodeId)1