Skip to content

Commit

Permalink
Exported all needed methods and variables through fruzhin object whic…
Browse files Browse the repository at this point in the history
…h is bind to window
  • Loading branch information
Georgi Grigorov committed Sep 10, 2024
1 parent 702f0ba commit 3d5b1d7
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 85 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.teavm.gradle.api.JSModuleType

plugins {
id("java")
id("war")
Expand Down Expand Up @@ -28,6 +30,7 @@ teavm.js {
addedToWebApp = true
mainClass = "com.limechain.Main"
targetFileName = "fruzhin.js"
moduleType = JSModuleType.ES2015
}

//TODO: Debug only. Remove when doing release build
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/limechain/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public static void main(String[] args) {
}

@JSBody(params = {"f", "apiName"}, script = "window[apiName] = f;" +
"isRpcExported = true;")
"window.fruzhin.HTTP.changeRpcExported(true);")
private static native void exportAPI(Function f, JSString apiName);
}
14 changes: 7 additions & 7 deletions src/main/java/com/limechain/network/kad/KademliaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,28 @@ public void updateSuccessfulBootNodes() {
successfulBootNodes = getPeerStoreSize();
}

@JSBody(params = {"bootNodes"}, script = "start(bootNodes)")
@JSBody(params = {"bootNodes"}, script = "window.fruzhin.startLibp2p(bootNodes)")
public static native void startNetwork(String[] bootNodes);

@JSBody(script = "return getPeerId()")
@JSBody(script = "return window.fruzhin.libp?.peerId")
public static native Object getPeerId();

@JSBody(script = "return libp.peerId.privateKey")
@JSBody(script = "return window.fruzhin.libp.peerId.privateKey")
public static native byte[] getPeerPrivateKey();

@JSBody(script = "return libp.peerId.publicKey")
@JSBody(script = "return window.fruzhin.libp.peerId.publicKey")
public static native byte[] getPeerPublicKey();

@JSBody(script = "return libp.getConnections().length")
@JSBody(script = "return window.fruzhin.libp.getConnections().length")
public static native int getPeerStoreSize();

/**
* Populates Kademlia dht with peers closest in distance to a random id then makes connections with our node
*/
@JSBody(script = "libp.peerStore.forEach( async (p) => {" +
@JSBody(script = "window.fruzhin.libp.peerStore.forEach( async (p) => {" +
" for await (const foundPeer of dht.peerRouting.getClosestPeers(p.id.toBytes())){" +
" if(foundPeer.peer?.multiaddrs?.length > 0){" +
" try{libp.dial(foundPeer.peer)}finally{}" +
" try{window.fruzhin.libp.dial(foundPeer.peer)}finally{}" +
" }" +
" }" +
"});")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public WarpSyncResponse send(WarpSyncRequest req, String protocolId) {
}

@JSBody(params = {"blockHash", "protocolId"}, script = "return (async () => {" +
" let peer = libp.getConnections()[0].remotePeer;" +
" let stream = await ItPbStream.pbStream(await libp.dialProtocol(peer, protocolId));" +
" let peer = window.fruzhin.libp.getConnections()[0].remotePeer;" +
" let stream = await ItPbStream.pbStream(await window.fruzhin.libp.dialProtocol(peer, protocolId));" +
" stream.writeLP(new Uint8Array([...blockHash.matchAll(/../g)].map(m => parseInt(m[0], 16))));" +
" let bytes = (await stream.readLP()).subarray();" +
" return [...bytes].map(n => n.toString(16).padStart(2, '0')).join('');" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static byte[] getDataToVerify(Precommit precommit, BigInteger authoritie
}

@JSBody(params = {"publicKeyHex", "signatureHex",
"messageHex"}, script = "return verifyAsync(signatureHex, messageHex, publicKeyHex);")
"messageHex"}, script = "return window.fruzhin.ED25519.verifyAsync(signatureHex, messageHex, publicKeyHex);")
public static native JSPromise<JSBoolean> verifyAsync(String publicKeyHex, String signatureHex,
String messageHex);
}
4 changes: 2 additions & 2 deletions src/main/java/com/limechain/teavm/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ private static void asyncHttpRequest(String method, String url, String body, Asy
});
}

@JSBody(params = {"method", "url", "body", "callback"}, script = "return asyncHttpRequest(method, url, body, callback);")
@JSBody(params = {"method", "url", "body", "callback"}, script = "return window.fruzhin.HTTP.asyncHttpRequest(method, url, body, callback);")
public static native void createAsyncHttpRequest(String method, String url, String body, TeaVMCallback callback);

@JSBody(params = {"method", "url", "body"}, script = "return httpRequestSync(method, url, body);")
@JSBody(params = {"method", "url", "body"}, script = "return window.fruzhin.HTTP.httpRequestSync(method, url, body);")
public static native String createHttpRequest(String method, String url, String body);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/limechain/utils/HashUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.limechain.utils;

import com.limechain.polkaj.Hash256;
import lombok.experimental.UtilityClass;
import org.teavm.jso.JSBody;
import org.teavm.jso.core.JSString;
Expand All @@ -10,6 +9,6 @@ public class HashUtils {

@JSBody(params = {"inputHex"}, script = "{" +
"let bytes = new Uint8Array([...inputHex.matchAll(/../g)].map(m => parseInt(m[0], 16)));" +
"return Blake2b.hash(bytes,undefined,32);" + "}")
"return window.fruzhin.Blake2b.hash(bytes,undefined,32);" + "}")
public static native JSString hashWithBlake2b(String inputHex);
}
53 changes: 3 additions & 50 deletions src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,11 @@
<head>
<title>Fruzhin</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="module" src="js/fruzhin.js"></script>
<script type="module" src="js/fruzhin-lib.js"></script>
</head>
<body>
<script>
var libp = undefined;

var getPeerId = () => {
return libp?.peerId;
}

var start = async (bootnodes) => {
console.log('Starting libp2p node');
console.log(bootnodes);
let test = {
addresses: {
listen: [],
},
transports: [
Libp2PWebsockets.webSockets()
],
streamMuxers: [ChainsafeLibp2PYamux.yamux()],
connectionEncryption: [ChainsafeLibp2PNoise.noise()],
peerDiscovery: [
Libp2PBootstrap.bootstrap({
list: bootnodes
})
],
services: {
identify: Libp2PIdentify.identify(),
ping: Libp2PPing.ping(),
dht: Libp2PKadDht.kadDHT({protocol: "/dot/kad"}),
pubsub: ChainsafeLibp2PGossipsub.gossipsub(),
}
};

libp = await Libp2P.createLibp2p(test);
libp.start();

// libp.addEventListener('peer:discovery', (evt) => console.log('Discovered:', evt.detail.id.toString()))
// libp.addEventListener('peer:connect', (evt) => console.log('Connected:', evt.detail.toString()))
// libp.addEventListener('peer:disconnect', (evt) => console.log('Disconnected:', evt.detail.toString()))

// let pbStream = ItProtobufStream.pbStream;
//
// libp.handle('/dot/sync/warp', ({stream}) => {
//
// console.log('Received a warp sync request');
// stream.write('Hello from the other side');
// stream.end();
// });
}
main();
<script type="module" >
fruzhin.main();
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/main/webapp/js/blake2b.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Blake2b = {
export var Blake2b = {
v: new Uint32Array(32),
m: new Uint32Array(32),
BLAKE2B_IV32: new Uint32Array([4089235720, 1779033703, 2227873595, 3144134277, 4271175723, 1013904242, 1595750129, 2773480762, 2917565137, 1359893119, 725511199, 2600822924, 4215389547, 528734635, 327033209, 1541459225]),
Expand Down
5 changes: 3 additions & 2 deletions src/main/webapp/js/ed25519.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ const _verify = (sig, msg, pub, opts = dvo) => {
};
return { hashable, finish };
};
const verifyAsync = async (s, m, p, opts = dvo) => hashFinish(true, _verify(s, m, p, opts));
const cr = () => // We support: 1) browsers 2) node.js 19+
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
const etc = {
Expand Down Expand Up @@ -316,4 +315,6 @@ const wNAF = (n) => {
}
}
return { p, f }; // return both real and fake points for JIT
};
};

export const verifyAsync = async (s, m, p, opts = dvo) => hashFinish(true, _verify(s, m, p, opts));
66 changes: 66 additions & 0 deletions src/main/webapp/js/fruzhin-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 * as Blake2b from './blake2b.js';
import * as ED25519 from './ed25519.js';
import * as HTTP from './http.js';
import * as Fruzhin from './fruzhin.js'

var startLibp2p = async (bootnodes) => {
console.log('Starting libp2p node');
console.log(bootnodes);
let test = {
addresses: {
listen: [],
},
transports: [
Libp2PWebsockets.webSockets()
],
streamMuxers: [ChainsafeLibp2PYamux.yamux()],
connectionEncryption: [ChainsafeLibp2PNoise.noise()],
peerDiscovery: [
Libp2PBootstrap.bootstrap({
list: bootnodes
})
],
services: {
identify: Libp2PIdentify.identify(),
ping: Libp2PPing.ping(),
dht: Libp2PKadDht.kadDHT({protocol: "/dot/kad"}),
pubsub: ChainsafeLibp2PGossipsub.gossipsub(),
}
};

window.fruzhin.libp = await Libp2P.createLibp2p(test);
window.fruzhin.libp.start();

// libp.addEventListener('peer:discovery', (evt) => console.log('Discovered:', evt.detail.id.toString()))
// libp.addEventListener('peer:connect', (evt) => console.log('Connected:', evt.detail.toString()))
// libp.addEventListener('peer:disconnect', (evt) => console.log('Disconnected:', evt.detail.toString()))

// let pbStream = ItProtobufStream.pbStream;
//
// libp.handle('/dot/sync/warp', ({stream}) => {
//
// console.log('Received a warp sync request');
// stream.write('Hello from the other side');
// stream.end();
// });
}

window.fruzhin = {
startLibp2p,
ED25519,
HTTP,
...Blake2b,
...Fruzhin,
}
5 changes: 0 additions & 5 deletions src/main/webapp/js/fruzhin.js

This file was deleted.

18 changes: 17 additions & 1 deletion src/main/webapp/js/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var isRpcExported = false;

function sendRpcRequest(method, params) {
return new Promise((resolve, reject) => {
if (isRpcExported === false) {
if (window.fruzhin.HTTP.getRpcExported() === false) {
window.setTimeout(async () => {
try {
const result = await sendRpcRequest(method, params);
Expand All @@ -60,3 +60,19 @@ function sendRpcRequest(method, params) {
}
});
}

function changeRpcExported(newVal){
isRpcExported = newVal;
}

function getRpcExported(){
return isRpcExported;
}

export {
sendRpcRequest,
asyncHttpRequest,
httpRequestSync,
changeRpcExported,
getRpcExported
}
11 changes: 0 additions & 11 deletions src/main/webapp/js/lib/external.js

This file was deleted.

0 comments on commit 3d5b1d7

Please sign in to comment.