Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: auto handshake on connect #17

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,21 @@ public static String getHandshake() {

@JSBody(params = {"announceExport", "protocolId"}, script =
"window.fruzhin.libp.handle(protocolId, async ({connection, stream}) => {" +
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 69) {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" announceExport.blockAnnounce(Ed25519.b2h(subarr.slice(2)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
" });")
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 69) {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" announceExport.blockAnnounce(Ed25519.b2h(subarr.slice(2)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
"});" +
"fruzhin.libp.addEventListener('peer:connect', async (evt) => {" +
" let handshake = announceExport.getHandshake();" +
" (await ItPbStream.pbStream(await window.fruzhin.libp.dialProtocol(evt.detail, protocolId))).writeLP(Ed25519.h2b(handshake));" +
"});")
public static native void registerHandler(JSObject announceExport, String protocolId);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,26 @@ public static String getNeighbourMessage() {

@JSBody(params = {"grandpaExport", "protocolId"}, script =
"window.fruzhin.libp.handle(protocolId, async ({connection, stream}) => {" +
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" + " let subarr = msg.subarray();" +
" if(subarr.length === 1) {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" if(subarr.slice(1)[0] === 2) {" +
" let niehgbourMessage = grandpaExport.getNeighbourMessage();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(niehgbourMessage));" +
" }" +
" grandpaExport.handleMessage(Ed25519.b2h(subarr.slice(1)), connection.remotePeer.toString());" +
" }" + " }" + " });" + " });")
" ItPipe.pipe(stream, async function (source) {" +
" for await (const msg of source) {" +
" let subarr = msg.subarray();" +
" if(subarr.length === 1) {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(handshake));" +
" } else if (subarr.length > 1) {" +
" if(subarr.slice(1)[0] === 2) {" +
" let niehgbourMessage = grandpaExport.getNeighbourMessage();" +
" (await ItPbStream.pbStream(stream)).writeLP(Ed25519.h2b(niehgbourMessage));" +
" }" +
" grandpaExport.handleMessage(Ed25519.b2h(subarr.slice(1)), connection.remotePeer.toString());" +
" }" +
" }" +
" });" +
"});" +
"fruzhin.libp.addEventListener('peer:connect', async (evt) => {" +
" let handshake = grandpaExport.getHandshake();" +
" (await ItPbStream.pbStream(await window.fruzhin.libp.dialProtocol(evt.detail, protocolId))).writeLP(Ed25519.h2b(handshake));" +
"});")
public static native void registerHandler(JSObject grandpaExport, String protocolId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,6 @@ public GrandpaService(String protocolId) {
this.protocolId = protocolId;
}

// /**
// * Sends a neighbour message to a peer. If there is no initiator stream opened with the peer,
// * sends a handshake instead.
// *
// * @param us our host object
// * @param peerId message receiver
// */
// public void sendNeighbourMessage(Host us, PeerId peerId) {
// Optional.ofNullable(connectionManager.getPeerInfo(peerId))
// .map(p -> p.getGrandpaStreams().getInitiator())
// .ifPresentOrElse(
// this::sendNeighbourMessage,
// () -> sendHandshake(us, peerId)
// );
// }
//
// private void sendNeighbourMessage(Stream stream) {
// GrandpaController controller = new GrandpaController(stream);
// controller.sendNeighbourMessage();
// }

private boolean isRegistered = false;

public void sendHandshake() {
Expand Down
26 changes: 13 additions & 13 deletions src/main/webapp/js/fruzhin-lib.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import 'https://unpkg.com/@chainsafe/libp2p-yamux/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-noise/dist/index.min.js';
import 'https://unpkg.com/@libp2p/websockets/dist/index.min.js';
import 'https://unpkg.com/@libp2p/kad-dht/dist/index.min.js';
import 'https://unpkg.com/@libp2p/identify/dist/index.min.js';
import 'https://unpkg.com/@libp2p/bootstrap/dist/index.min.js';
import 'https://unpkg.com/@libp2p/ping/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-gossipsub/dist/index.min.js';
import 'https://unpkg.com/libp2p/dist/index.min.js';
import 'https://unpkg.com/it-pipe/dist/index.min.js';
import 'https://unpkg.com/it-pb-stream/dist/index.min.js';
import 'https://unpkg.com/@muradsenteca/ed25519/dist/index.min.js'
import 'https://unpkg.com/@muradsenteca/blake2b/dist/index.min.js'
import 'https://unpkg.com/@chainsafe/libp2p-yamux@6.0.2/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-noise@15.1.2/dist/index.min.js';
import 'https://unpkg.com/@libp2p/websockets@8.2.0/dist/index.min.js';
import 'https://unpkg.com/@libp2p/kad-dht@12.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/identify@2.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/bootstrap@10.1.5/dist/index.min.js';
import 'https://unpkg.com/@libp2p/ping@1.1.6/dist/index.min.js';
import 'https://unpkg.com/@chainsafe/libp2p-gossipsub@13.2.0/dist/index.min.js';
import 'https://unpkg.com/libp2p@1.9.4/dist/index.min.js';
import 'https://unpkg.com/it-pipe@3.0.1/dist/index.min.js';
import 'https://unpkg.com/it-pb-stream@4.0.2/dist/index.min.js';
import 'https://unpkg.com/@muradsenteca/ed25519@1.0.0/dist/index.min.js'
import 'https://unpkg.com/@muradsenteca/blake2b@1.0.1/dist/index.min.js'

import * as HTTP from './http.js';
import * as Fruzhin from './fruzhin.js'
Expand Down
Loading