Search in sources :

Example 1 with PrivKey

use of io.libp2p.core.crypto.PrivKey in project xdagj by XDagger.

the class DiscoveryPeerTest method startup.

@Before
public void startup() throws UnknownHostException {
    String priString = "0x0802122074ca7d1380b2c407be6878669ebb5c7a2ee751bb18198f1a0f214bcb93b894b5";
    Bytes privkeybytes = Bytes.fromHexString(priString);
    PrivKey privKey = KeyKt.unmarshalPrivateKey(privkeybytes.toArrayUnsafe());
    // PrivKey privKey1 = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
    // String s = Hex.toHexString(privKey1.bytes());
    // System.out.println(Arrays.toString(Hex.decode(s)));
    discoveryPeer = new DiscoveryPeer(Bytes.wrap(privKey.publicKey().raw()), new InetSocketAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 10001));
    // System.out.println(discoveryPeer.getNodeAddress().toString());
    List<String> boot = new ArrayList<>();
    Bytes bytes = Bytes.wrap(privKey.raw());
    discV5Service1 = DiscV5Service.create((bytes), "127.0.0.1", 10001, Collections.emptyList());
    if (discV5Service1.getEnr().isPresent()) {
        boot.add(discV5Service1.getEnr().get());
    }
    discV5Service2 = DiscV5Service.create(Bytes.wrap(KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1().raw()), "127.0.0.1", 11111, boot);
    discV5Service1.start();
    discV5Service1.searchForPeers();
    discV5Service2.start();
    discV5Service2.searchForPeers();
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) PrivKey(io.libp2p.core.crypto.PrivKey) Before(org.junit.Before)

Example 2 with PrivKey

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

the class LibP2PPrivateKeyLoaderTest method assertRoundTrip.

private void assertRoundTrip(final PrivKey generatedPK) {
    final PrivKey reparsed = KeyKt.unmarshalPrivateKey(generatedPK.bytes());
    assertThat(reparsed).isEqualTo(generatedPK);
}
Also used : PrivKey(io.libp2p.core.crypto.PrivKey)

Example 3 with PrivKey

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

the class LibP2PPrivateKeyLoader method generateNewPrivateKey.

private Bytes generateNewPrivateKey() {
    final Bytes privateKey;
    final Optional<Bytes> generatedKeyBytes = keyValueStore.get(GENERATED_NODE_KEY_KEY);
    if (generatedKeyBytes.isEmpty()) {
        final PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
        privateKey = Bytes.wrap(KeyKt.marshalPrivateKey(privKey));
        keyValueStore.put(GENERATED_NODE_KEY_KEY, privateKey);
        STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, true);
    } else {
        privateKey = generatedKeyBytes.get();
        STATUS_LOG.usingGeneratedP2pPrivateKey(GENERATED_NODE_KEY_KEY, false);
    }
    return privateKey;
}
Also used : Bytes(org.apache.tuweni.bytes.Bytes) PrivKey(io.libp2p.core.crypto.PrivKey)

Example 4 with PrivKey

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

the class PeerCommand method validateParamsAndGenerate.

void validateParamsAndGenerate(String outputFile, int number) throws IOException {
    try {
        File f = new File(outputFile);
        if (f.exists()) {
            throw new InvalidConfigurationException(String.format("Not overwriting existing file %s \nDelete file or use --output-file to point to a file that does not currently exist.", outputFile));
        }
        FileWriter fileWriter = new FileWriter(outputFile, Charset.defaultCharset());
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.println("Private Key(Hex)\tPublic Key(Hex)\tPeerId(Base58)");
        for (int i = 0; i < number; i++) {
            PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
            PubKey pubKey = privKey.publicKey();
            PeerId peerId = PeerId.fromPubKey(pubKey);
            printWriter.println(Bytes.wrap(privKey.bytes()).toHexString() + "\t" + Bytes.wrap(pubKey.bytes()).toHexString() + "\t" + peerId.toBase58());
        }
        printWriter.close();
    } catch (final FileNotFoundException ex) {
        throw new InvalidConfigurationException("use --output-file to point to a file in an existing directory " + ex.getMessage());
    }
}
Also used : PubKey(io.libp2p.core.crypto.PubKey) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) PrivKey(io.libp2p.core.crypto.PrivKey) File(java.io.File) InvalidConfigurationException(tech.pegasys.teku.infrastructure.exceptions.InvalidConfigurationException) PrintWriter(java.io.PrintWriter) PeerId(io.libp2p.core.PeerId)

Example 5 with PrivKey

use of io.libp2p.core.crypto.PrivKey in project xdagj by XDagger.

the class DiscoveryPeerTest method start.

@Before
public void start() throws UnknownHostException {
    PrivKey privKey = KeyKt.generateKeyPair(KEY_TYPE.SECP256K1).component1();
    peer = new DiscoveryPeer(Bytes.wrap(privKey.publicKey().raw()), new InetSocketAddress(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 10000));
    expectStirng = "/ip4/127.0.0.1/tcp/10000/ipfs/" + new LibP2PNodeId(PeerId.fromPubKey(privKey.publicKey())).toString();
}
Also used : DiscoveryPeer(io.xdag.net.libp2p.discovery.DiscoveryPeer) InetSocketAddress(java.net.InetSocketAddress) PrivKey(io.libp2p.core.crypto.PrivKey) Before(org.junit.Before)

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