Skip to content

Commit

Permalink
Added relay option to fula
Browse files Browse the repository at this point in the history
Now fula can connect over a relay

Co-Authored-By: siroossha <[email protected]>
Co-Authored-By: negar-kln <[email protected]>
  • Loading branch information
3 people committed Jan 4, 2023
1 parent f4d0aca commit ae697a4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
```

Expand All @@ -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
);
```

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
18 changes: 11 additions & 7 deletions android/src/main/java/land/fx/fula/FulaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -211,15 +211,15 @@ 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 {
WritableMap resultData = new WritableNativeMap();
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]);
Expand Down Expand Up @@ -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()) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/interfaces/fulaNativeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
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<string>;
isReady: (filesystemCheck: boolean) => Promise<boolean>;
logout: (identity: string, storePath: string) => Promise<boolean>;
checkFailedActions: (retry: boolean) => Promise<boolean>;
Expand Down
16 changes: 10 additions & 6 deletions src/protocols/fula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ 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',
identity,
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);
};

/**
Expand All @@ -38,17 +40,19 @@ export const newClient = (
storePath: string,
bloxAddr: string,
exchange: string,
autoFlush: boolean = false
autoFlush: boolean = false,
useRelay: boolean = true
): Promise<string> => {
console.log(
'newClient in react-native started',
identity,
storePath,
bloxAddr,
exchange,
autoFlush
autoFlush,
useRelay
);
return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush);
return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
};

/**
Expand Down

0 comments on commit ae697a4

Please sign in to comment.