Skip to content

Commit 76c78fa

Browse files
authored
Merge pull request #50 from functionland/relay
Added relay option to fula
2 parents f4d0aca + ae697a4 commit 76c78fa

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ const peerId //returns peerId as string
2222
storePath: string, // leave empty to use the default temp one
2323
bloxAddr: string, //leave empty for testing without a backend node
2424
exchange: 'noop'|'', //add noop for testing without a backend
25-
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
25+
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
26+
useRelay: boolean //default to true. If true it forces the connection through relay
2627
)
2728
```
2829

@@ -39,7 +40,8 @@ await fula.init(
3940
storePath: string, // leave empty to use the default temp one
4041
bloxAddr: string, //leave empty for testing without a backend node
4142
exchange: 'noop'|'', //add noop for testing without a backend
42-
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
43+
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
44+
useRelay: boolean //default to true. If true it forces the connection through relay
4345
);
4446
```
4547

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ repositories {
6262
dependencies {
6363
//noinspection GradleDynamicVersion
6464
implementation "com.facebook.react:react-native:+" // From node_modules
65-
implementation 'com.github.functionland:fula-build-aar:v0.8.3' // From jitpack.io
65+
implementation 'com.github.functionland:fula-build-aar:v0.8.4' // From jitpack.io
6666
implementation 'com.github.functionland:wnfs-build-aar:v1.4.1' // From jitpack.io
6767
implementation 'commons-io:commons-io:20030203.000550'
6868
// implementation files('mobile.aar')

android/src/main/java/land/fx/fula/FulaModule.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ public void checkConnection(Promise promise) {
169169
}
170170

171171
@ReactMethod
172-
public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, Promise promise) {
172+
public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, Promise promise) {
173173
Log.d("ReactNative", "newClient started");
174174
ThreadUtils.runOnExecutor(() -> {
175175
try {
176176
Log.d("ReactNative", "newClient storePath= " + storePath);
177177
byte[] identity = toByte(identityString);
178178
Log.d("ReactNative", "newClient identity= " + identityString);
179-
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush);
179+
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
180180
//String objString = Arrays.toString(obj);
181181
String peerId = this.fula.id();
182182
promise.resolve(peerId);
@@ -211,15 +211,15 @@ public void isReady(boolean filesystemCheck, Promise promise) {
211211
}
212212

213213
@ReactMethod
214-
public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, Promise promise) {
214+
public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, boolean useRelay, Promise promise) {
215215
Log.d("ReactNative", "init started");
216216
ThreadUtils.runOnExecutor(() -> {
217217
try {
218218
WritableMap resultData = new WritableNativeMap();
219219
Log.d("ReactNative", "init storePath= " + storePath);
220220
byte[] identity = toByte(identityString);
221221
Log.d("ReactNative", "init identity= " + identityString);
222-
String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig);
222+
String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay);
223223
Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
224224
resultData.putString("peerId", obj[0]);
225225
resultData.putString("rootCid", obj[1]);
@@ -469,7 +469,7 @@ private boolean logoutInternal(byte[] identity, String storePath) throws Excepti
469469
}
470470

471471
@NonNull
472-
private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush) throws Exception {
472+
private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay) throws Exception {
473473
try {
474474
Config config_ext = new Config();
475475
if (storePath == null || storePath.trim().isEmpty()) {
@@ -486,6 +486,10 @@ private byte[] newClientInternal(byte[] identity, String storePath, String bloxA
486486
Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
487487
config_ext.setExchange(exchange);
488488
config_ext.setSyncWrites(autoFlush);
489+
if (useRelay) {
490+
config_ext.setAllowTransientConnection(true);
491+
config_ext.setForceReachabilityPrivate(true);
492+
}
489493
if (this.fula == null) {
490494
Log.d("ReactNative", "Creating a new Fula instance");
491495
this.fula = Fulamobile.newClient(config_ext);
@@ -501,10 +505,10 @@ private byte[] newClientInternal(byte[] identity, String storePath, String bloxA
501505
}
502506

503507
@NonNull
504-
private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid) throws Exception {
508+
private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid, boolean useRelay) throws Exception {
505509
try {
506510
if (this.fula == null) {
507-
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush);
511+
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
508512
}
509513
if(this.client == null) {
510514
this.client = new Client(this.fula);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.0.9",
3+
"version": "1.1.0",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/interfaces/fulaNativeModule.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ interface FulaNativeModule {
77
bloxAddr: string, //Blox multiadddr needs to be manually entered now
88
exchange: string, //set to 'noope' for testing
99
autoFlush: boolean, //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write
10-
rootCid: string | null //if you have the latest rootCid you can send it and it generates the private_ref for filesystem
10+
rootCid: string | null, //if you have the latest rootCid you can send it and it generates the private_ref for filesystem
11+
useRelay: boolean | null // if true it forces the use of relay
1112
) => Promise<{ peerId: string; rootCid: string; private_ref: string }>;
1213
newClient: (
1314
identity: string, //Private key of did identity
1415
storePath: string, //You can leave empty
1516
bloxAddr: string, //Blox multiadddr needs to be manually entered now
1617
exchange: string, //set to 'noope' for testing
17-
autoFlush: boolean //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write
18-
) => Promise<string>;
18+
autoFlush: boolean, //set to false always unless you know what you are doing. This is to write actions to disk explicitly after each write
19+
useRelay: boolean | null // if true it forces the use of relay
20+
) => Promise<string>;
1921
isReady: (filesystemCheck: boolean) => Promise<boolean>;
2022
logout: (identity: string, storePath: string) => Promise<boolean>;
2123
checkFailedActions: (retry: boolean) => Promise<boolean>;

src/protocols/fula.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,19 @@ export const init = (
1313
bloxAddr: string,
1414
exchange: string,
1515
autoFlush: boolean = false,
16-
rootCid: string | null = null
16+
rootCid: string | null = null,
17+
useRelay: boolean = true
1718
): Promise<{ peerId: string; rootCid: string; private_ref: string }> => {
1819
console.log(
1920
'init in react-native started',
2021
identity,
2122
storePath,
2223
bloxAddr,
2324
exchange,
24-
autoFlush
25+
autoFlush,
26+
useRelay
2527
);
26-
return Fula.init(identity, storePath, bloxAddr, exchange, autoFlush, rootCid);
28+
return Fula.init(identity, storePath, bloxAddr, exchange, autoFlush, rootCid, useRelay);
2729
};
2830

2931
/**
@@ -38,17 +40,19 @@ export const newClient = (
3840
storePath: string,
3941
bloxAddr: string,
4042
exchange: string,
41-
autoFlush: boolean = false
43+
autoFlush: boolean = false,
44+
useRelay: boolean = true
4245
): Promise<string> => {
4346
console.log(
4447
'newClient in react-native started',
4548
identity,
4649
storePath,
4750
bloxAddr,
4851
exchange,
49-
autoFlush
52+
autoFlush,
53+
useRelay
5054
);
51-
return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush);
55+
return Fula.newClient(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
5256
};
5357

5458
/**

0 commit comments

Comments
 (0)