From ae697a4e299dd1b32679293d2780c7f3d417954f Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Wed, 4 Jan 2023 18:42:28 -0500 Subject: [PATCH] Added relay option to fula Now fula can connect over a relay Co-Authored-By: siroossha <113217130+siroossha@users.noreply.github.com> Co-Authored-By: negar-kln <113377124+negar-kln@users.noreply.github.com> --- README.md | 6 ++++-- android/build.gradle | 2 +- .../src/main/java/land/fx/fula/FulaModule.java | 18 +++++++++++------- package.json | 2 +- src/interfaces/fulaNativeModule.ts | 8 +++++--- src/protocols/fula.ts | 16 ++++++++++------ 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 7a1460b..50f9d08 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,8 @@ const peerId //returns peerId as string storePath: string, // leave empty to use the default temp one bloxAddr: string, //leave empty for testing without a backend node exchange: 'noop'|'', //add noop for testing without a backend - autoFlush: boolean //Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true + autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true + useRelay: boolean //default to true. If true it forces the connection through relay ) ``` @@ -39,7 +40,8 @@ await fula.init( storePath: string, // leave empty to use the default temp one bloxAddr: string, //leave empty for testing without a backend node exchange: 'noop'|'', //add noop for testing without a backend - autoFlush: boolean //Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true + autoFlush: boolean, //Default to false. Always set to false unless you know what you are doing. explicitly write data to disk after each operation if set to true + useRelay: boolean //default to true. If true it forces the connection through relay ); ``` diff --git a/android/build.gradle b/android/build.gradle index e4897c2..1bda750 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -62,7 +62,7 @@ repositories { dependencies { //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules - implementation 'com.github.functionland:fula-build-aar:v0.8.3' // From jitpack.io + implementation 'com.github.functionland:fula-build-aar:v0.8.4' // From jitpack.io implementation 'com.github.functionland:wnfs-build-aar:v1.4.1' // From jitpack.io implementation 'commons-io:commons-io:20030203.000550' // implementation files('mobile.aar') diff --git a/android/src/main/java/land/fx/fula/FulaModule.java b/android/src/main/java/land/fx/fula/FulaModule.java index ba52be6..62b13f0 100755 --- a/android/src/main/java/land/fx/fula/FulaModule.java +++ b/android/src/main/java/land/fx/fula/FulaModule.java @@ -169,14 +169,14 @@ public void checkConnection(Promise promise) { } @ReactMethod - public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, Promise promise) { + public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, Promise promise) { Log.d("ReactNative", "newClient started"); ThreadUtils.runOnExecutor(() -> { try { Log.d("ReactNative", "newClient storePath= " + storePath); byte[] identity = toByte(identityString); Log.d("ReactNative", "newClient identity= " + identityString); - this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush); + this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay); //String objString = Arrays.toString(obj); String peerId = this.fula.id(); promise.resolve(peerId); @@ -211,7 +211,7 @@ public void isReady(boolean filesystemCheck, Promise promise) { } @ReactMethod - public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, Promise promise) { + public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, boolean useRelay, Promise promise) { Log.d("ReactNative", "init started"); ThreadUtils.runOnExecutor(() -> { try { @@ -219,7 +219,7 @@ public void init(String identityString, String storePath, String bloxAddr, Strin Log.d("ReactNative", "init storePath= " + storePath); byte[] identity = toByte(identityString); Log.d("ReactNative", "init identity= " + identityString); - String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig); + String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay); Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]"); resultData.putString("peerId", obj[0]); resultData.putString("rootCid", obj[1]); @@ -469,7 +469,7 @@ private boolean logoutInternal(byte[] identity, String storePath) throws Excepti } @NonNull - private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush) throws Exception { + private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay) throws Exception { try { Config config_ext = new Config(); if (storePath == null || storePath.trim().isEmpty()) { @@ -486,6 +486,10 @@ private byte[] newClientInternal(byte[] identity, String storePath, String bloxA Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr()); config_ext.setExchange(exchange); config_ext.setSyncWrites(autoFlush); + if (useRelay) { + config_ext.setAllowTransientConnection(true); + config_ext.setForceReachabilityPrivate(true); + } if (this.fula == null) { Log.d("ReactNative", "Creating a new Fula instance"); this.fula = Fulamobile.newClient(config_ext); @@ -501,10 +505,10 @@ private byte[] newClientInternal(byte[] identity, String storePath, String bloxA } @NonNull - private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid) throws Exception { + private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid, boolean useRelay) throws Exception { try { if (this.fula == null) { - this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush); + this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay); } if(this.client == null) { this.client = new Client(this.fula); diff --git a/package.json b/package.json index c9038d2..e205817 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@functionland/react-native-fula", - "version": "1.0.9", + "version": "1.1.0", "description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/interfaces/fulaNativeModule.ts b/src/interfaces/fulaNativeModule.ts index bddf4b0..f29e294 100644 --- a/src/interfaces/fulaNativeModule.ts +++ b/src/interfaces/fulaNativeModule.ts @@ -7,15 +7,17 @@ interface FulaNativeModule { bloxAddr: string, //Blox multiadddr needs to be manually entered now exchange: string, //set to 'noope' for testing autoFlush: boolean, //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write - rootCid: string | null //if you have the latest rootCid you can send it and it generates the private_ref for filesystem + rootCid: string | null, //if you have the latest rootCid you can send it and it generates the private_ref for filesystem + useRelay: boolean | null // if true it forces the use of relay ) => Promise<{ peerId: string; rootCid: string; private_ref: string }>; newClient: ( identity: string, //Private key of did identity storePath: string, //You can leave empty bloxAddr: string, //Blox multiadddr needs to be manually entered now exchange: string, //set to 'noope' for testing - autoFlush: boolean //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write - ) => Promise; + autoFlush: boolean, //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write + useRelay: boolean | null // if true it forces the use of relay + ) => Promise; isReady: (filesystemCheck: boolean) => Promise; logout: (identity: string, storePath: string) => Promise; checkFailedActions: (retry: boolean) => Promise; diff --git a/src/protocols/fula.ts b/src/protocols/fula.ts index 1e81537..d21601b 100644 --- a/src/protocols/fula.ts +++ b/src/protocols/fula.ts @@ -13,7 +13,8 @@ export const init = ( bloxAddr: string, exchange: string, autoFlush: boolean = false, - rootCid: string | null = null + rootCid: string | null = null, + useRelay: boolean = true ): Promise<{ peerId: string; rootCid: string; private_ref: string }> => { console.log( 'init in react-native started', @@ -21,9 +22,10 @@ export const init = ( storePath, bloxAddr, exchange, - autoFlush + autoFlush, + useRelay ); - return Fula.init(identity, storePath, bloxAddr, exchange, autoFlush, rootCid); + return Fula.init(identity, storePath, bloxAddr, exchange, autoFlush, rootCid, useRelay); }; /** @@ -38,7 +40,8 @@ export const newClient = ( storePath: string, bloxAddr: string, exchange: string, - autoFlush: boolean = false + autoFlush: boolean = false, + useRelay: boolean = true ): Promise => { console.log( 'newClient in react-native started', @@ -46,9 +49,10 @@ export const newClient = ( storePath, bloxAddr, exchange, - autoFlush + autoFlush, + useRelay ); - return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush); + return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush, useRelay); }; /**