@Test void createHandshake() { KeyPair keyPair = KeyPair.random(); KeyPair peerKeyPair = KeyPair.random(); byte[] nonce = new byte[32]; new SecureRandom().nextBytes(nonce); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes payload = RLPxConnectionFactory.init(keyPair, peerKeyPair.publicKey(), ephemeralKeyPair, Bytes32.wrap(nonce)); AtomicReference<Bytes> ref = new AtomicReference<>(); RLPxConnection conn = RLPxConnectionFactory.respondToHandshake(payload, peerKeyPair, ref::set); HandshakeMessage responder = RLPxConnectionFactory.readResponse(ref.get(), keyPair.secretKey()); assertNotNull(conn); assertNotNull(responder); }
public static AsyncResult<RLPxConnection> createHandshake( KeyPair keyPair, PublicKey remotePublicKey, Function<Bytes, AsyncResult<Bytes>> initAndResponse) { Bytes32 nonce = generateRandomBytes32(); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes initHandshakeMessage = init(keyPair, remotePublicKey, ephemeralKeyPair, nonce); AsyncResult<Bytes> response = initAndResponse.apply(initHandshakeMessage); return response.thenApply(responseBytes -> { HandshakeMessage responseMessage = readResponse(responseBytes, keyPair.secretKey()); return createConnection( true, initHandshakeMessage, responseBytes, ephemeralKeyPair.secretKey(), responseMessage.ephemeralPublicKey(), nonce, responseMessage.nonce(), keyPair.publicKey(), remotePublicKey); }); }
RLPxConnectionFactory { public static AsyncResult<RLPxConnection> createHandshake( KeyPair keyPair, PublicKey remotePublicKey, Function<Bytes, AsyncResult<Bytes>> initAndResponse) { Bytes32 nonce = generateRandomBytes32(); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes initHandshakeMessage = init(keyPair, remotePublicKey, ephemeralKeyPair, nonce); AsyncResult<Bytes> response = initAndResponse.apply(initHandshakeMessage); return response.thenApply(responseBytes -> { HandshakeMessage responseMessage = readResponse(responseBytes, keyPair.secretKey()); return createConnection( true, initHandshakeMessage, responseBytes, ephemeralKeyPair.secretKey(), responseMessage.ephemeralPublicKey(), nonce, responseMessage.nonce(), keyPair.publicKey(), remotePublicKey); }); } }
RLPxConnectionFactory { public static AsyncResult<RLPxConnection> createHandshake( KeyPair keyPair, PublicKey remotePublicKey, Function<Bytes, AsyncResult<Bytes>> initAndResponse) { Bytes32 nonce = generateRandomBytes32(); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes initHandshakeMessage = init(keyPair, remotePublicKey, ephemeralKeyPair, nonce); AsyncResult<Bytes> response = initAndResponse.apply(initHandshakeMessage); return response.thenApply(responseBytes -> { HandshakeMessage responseMessage = readResponse(responseBytes, keyPair.secretKey()); return createConnection( true, initHandshakeMessage, responseBytes, ephemeralKeyPair.secretKey(), responseMessage.ephemeralPublicKey(), nonce, responseMessage.nonce(), keyPair.publicKey(), remotePublicKey); }); }  }
RLPxConnectionFactory { public static AsyncResult<RLPxConnection> createHandshake( KeyPair keyPair, PublicKey remotePublicKey, Function<Bytes, AsyncResult<Bytes>> initAndResponse) { Bytes32 nonce = generateRandomBytes32(); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes initHandshakeMessage = init(keyPair, remotePublicKey, ephemeralKeyPair, nonce); AsyncResult<Bytes> response = initAndResponse.apply(initHandshakeMessage); return response.thenApply(responseBytes -> { HandshakeMessage responseMessage = readResponse(responseBytes, keyPair.secretKey()); return createConnection( true, initHandshakeMessage, responseBytes, ephemeralKeyPair.secretKey(), responseMessage.ephemeralPublicKey(), nonce, responseMessage.nonce(), keyPair.publicKey(), remotePublicKey); }); }  static AsyncResult<RLPxConnection> createHandshake(
      KeyPair keyPair,
      PublicKey remotePublicKey,
      Function<Bytes, AsyncResult<Bytes>> initAndResponse); static RLPxConnection respondToHandshake(
      Bytes initiatorMessageBytes,
      KeyPair keyPair,
      Consumer<Bytes> responseHandler); static Bytes init(
      KeyPair keyPair,
      PublicKey remotePublicKey,
      KeyPair ephemeralKeyPair,
      Bytes32 initiatorNonce); static HandshakeMessage readResponse(Bytes response, SecretKey privateKey); static Bytes32 generateRandomBytes32(); static RLPxConnection createConnection(
      boolean initiator,
      Bytes initiatorMessage,
      Bytes responderMessage,
      SecretKey ourEphemeralPrivateKey,
      PublicKey peerEphemeralPublicKey,
      Bytes32 initiatorNonce,
      Bytes32 responderNonce,
      PublicKey ourPublicKey,
      PublicKey peerPublicKey); static int messageSize(Bytes msgBytes); }
RLPxConnectionFactory { public static AsyncResult<RLPxConnection> createHandshake( KeyPair keyPair, PublicKey remotePublicKey, Function<Bytes, AsyncResult<Bytes>> initAndResponse) { Bytes32 nonce = generateRandomBytes32(); KeyPair ephemeralKeyPair = KeyPair.random(); Bytes initHandshakeMessage = init(keyPair, remotePublicKey, ephemeralKeyPair, nonce); AsyncResult<Bytes> response = initAndResponse.apply(initHandshakeMessage); return response.thenApply(responseBytes -> { HandshakeMessage responseMessage = readResponse(responseBytes, keyPair.secretKey()); return createConnection( true, initHandshakeMessage, responseBytes, ephemeralKeyPair.secretKey(), responseMessage.ephemeralPublicKey(), nonce, responseMessage.nonce(), keyPair.publicKey(), remotePublicKey); }); }  static AsyncResult<RLPxConnection> createHandshake(
      KeyPair keyPair,
      PublicKey remotePublicKey,
      Function<Bytes, AsyncResult<Bytes>> initAndResponse); static RLPxConnection respondToHandshake(
      Bytes initiatorMessageBytes,
      KeyPair keyPair,
      Consumer<Bytes> responseHandler); static Bytes init(
      KeyPair keyPair,
      PublicKey remotePublicKey,
      KeyPair ephemeralKeyPair,
      Bytes32 initiatorNonce); static HandshakeMessage readResponse(Bytes response, SecretKey privateKey); static Bytes32 generateRandomBytes32(); static RLPxConnection createConnection(
      boolean initiator,
      Bytes initiatorMessage,
      Bytes responderMessage,
      SecretKey ourEphemeralPrivateKey,
      PublicKey peerEphemeralPublicKey,
      Bytes32 initiatorNonce,
      Bytes32 responderNonce,
      PublicKey ourPublicKey,
      PublicKey peerPublicKey); static int messageSize(Bytes msgBytes);  }