Skip to content

Commit

Permalink
Merge pull request #6 from lightninglabs/lnc-multiple-connections
Browse files Browse the repository at this point in the history
LNC-mobile: add support for multiple connections
  • Loading branch information
kaloudis authored Nov 17, 2022
2 parents 321fade + b2cb954 commit eec10dd
Show file tree
Hide file tree
Showing 99 changed files with 26,895 additions and 7,500 deletions.
Binary file modified android/libs/lnc-mobile-sources.jar
Binary file not shown.
Binary file modified android/libs/lnc-mobile.aar
Binary file not shown.
53 changes: 26 additions & 27 deletions android/src/main/java/com/lnc-rn/LncModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import java.security.cert.X509Certificate
import engineering.lightning.lnc.mobile.rn.AndroidCallback

import mobile.Mobile;
import mobile.NativeCallback;

class LncModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

Expand All @@ -23,105 +22,105 @@ class LncModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
}

@ReactMethod
fun registerLocalPrivCreateCallback(onLocalPrivCreate: Callback) {
fun registerLocalPrivCreateCallback(namespace: String, onLocalPrivCreate: Callback) {
val lpccb = AndroidCallback()
lpccb.setCallback(onLocalPrivCreate)

Mobile.registerLocalPrivCreateCallback(lpccb)
Mobile.registerLocalPrivCreateCallback(namespace, lpccb)
}

@ReactMethod
fun registerRemoteKeyReceiveCallback(onRemoteKeyReceive: Callback) {
fun registerRemoteKeyReceiveCallback(namespace: String, onRemoteKeyReceive: Callback) {
val rkrcb = AndroidCallback()
rkrcb.setCallback(onRemoteKeyReceive)

Mobile.registerRemoteKeyReceiveCallback(rkrcb)
Mobile.registerRemoteKeyReceiveCallback(namespace, rkrcb)
}

@ReactMethod
fun registerAuthDataCallback(onAuthData: Callback) {
fun registerAuthDataCallback(namespace: String, onAuthData: Callback) {
val oacb = AndroidCallback()
oacb.setCallback(onAuthData)

Mobile.registerAuthDataCallback(oacb)
Mobile.registerAuthDataCallback(namespace, oacb)
}

@ReactMethod
fun initLNC() {
Mobile.initLNC("info")
fun initLNC(namespace: String) {
Mobile.initLNC(namespace, "info")
}

@ReactMethod
fun isConnected(promise: Promise) {
fun isConnected(namespace: String, promise: Promise) {
try {
var response = Mobile.isConnected()
var response = Mobile.isConnected(namespace)
promise.resolve(response);
} catch(e: Throwable) {
promise.reject("request Error", e);
}
}

@ReactMethod
fun status(promise: Promise) {
fun status(namespace: String, promise: Promise) {
try {
var response = Mobile.status()
var response = Mobile.status(namespace)
promise.resolve(response);
} catch(e: Throwable) {
promise.reject("request Error", e);
}
}

@ReactMethod
fun expiry(promise: Promise) {
fun expiry(namespace: String, promise: Promise) {
try {
var response = Mobile.getExpiry()
var response = Mobile.getExpiry(namespace)
promise.resolve(response);
} catch(e: Throwable) {
promise.reject("request Error", e);
}
}

@ReactMethod
fun isReadOnly(promise: Promise) {
fun isReadOnly(namespace: String, promise: Promise) {
try {
var response = Mobile.isReadOnly()
var response = Mobile.isReadOnly(namespace)
promise.resolve(response);
} catch(e: Throwable) {
promise.reject("request Error", e);
}
}

@ReactMethod
fun hasPerms(permission: String, promise: Promise) {
fun hasPerms(namespace: String, permission: String, promise: Promise) {
try {
var response = Mobile.hasPermissions(permission)
var response = Mobile.hasPermissions(namespace, permission)
promise.resolve(response);
} catch(e: Throwable) {
promise.reject("request Error", e);
}
}

@ReactMethod
fun connectServer(mailboxServerAddr: String, isDevServer: Boolean = false, connectPhrase: String, localStatic: String, remoteStatic: String) {
fun connectServer(namespace: String, mailboxServerAddr: String, isDevServer: Boolean = false, connectPhrase: String, localStatic: String, remoteStatic: String) {
Log.d("connectMailbox", "called with connectPhrase: " + connectPhrase
+ " and mailboxServerAddr: " + mailboxServerAddr);

Mobile.connectServer(mailboxServerAddr, isDevServer, connectPhrase, localStatic ?: "", remoteStatic ?: "")
Mobile.connectServer(namespace, mailboxServerAddr, isDevServer, connectPhrase, localStatic ?: "", remoteStatic ?: "")
}

@ReactMethod
fun disconnect() {
Mobile.disconnect()
fun disconnect(namespace: String) {
Mobile.disconnect(namespace)
}

@ReactMethod
fun invokeRPC(route: String, requestData: String, rnCallback: Callback) {
fun invokeRPC(namespace: String, route: String, requestData: String, rnCallback: Callback) {
Log.d("request", "called with route: " + route
+ " and requestData: " + requestData);

val gocb = AndroidCallback()
gocb.setCallback(rnCallback)
Mobile.invokeRPC(route, requestData, gocb)
Mobile.invokeRPC(namespace, route, requestData, gocb)
}

private fun sendEvent(event: String, data: String) {
Expand All @@ -134,10 +133,10 @@ class LncModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaMod
}

@ReactMethod
fun initListener(eventName: String, request: String) {
fun initListener(namespace: String, eventName: String, request: String) {
val gocb = AndroidStreamingCallback()
gocb.setEventName(eventName)
gocb.setCallback(::sendEvent)
Mobile.invokeRPC(eventName, request, gocb)
Mobile.invokeRPC(namespace, eventName, request, gocb)
}
}
17 changes: 8 additions & 9 deletions demos/connect-demo/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface HomeProps {
}

interface HomeStore {
pnemonic: string;
mnemonic: string;
}

@inject('LNCStore')
Expand All @@ -24,22 +24,21 @@ export default class Home extends React.Component<HomeProps, HomeStore> {
constructor(props: any) {
super(props);
this.state = {
pnemonic:
'jump pave throw twenty noodle caution case dream song crucial'
mnemonic: ''
};
}

onChangePnemonic = (text) => this.setState({ pnemonic: text });
onChangeMnemonic = (text) => this.setState({ mnemonic: text });

attemptConnect = async () => {
await this.props.LNCStore.connect(this.state.pnemonic);
await this.props.LNCStore.connect(this.state.mnemonic);
this.props.LNCStore.getInfo();
};

disconnect = () => this.props.LNCStore.disconnect();

render() {
const { pnemonic } = this.state;
const { mnemonic } = this.state;
const { LNCStore } = this.props;
const { lnc, loading, connected, info } = LNCStore;

Expand All @@ -50,9 +49,9 @@ export default class Home extends React.Component<HomeProps, HomeStore> {
<>
<Text>lnc-mobile demo app</Text>
<TextInput
onChangeText={this.onChangePnemonic}
placeholder="Pnemonic"
value={pnemonic}
onChangeText={this.onChangeMnemonic}
placeholder="Enter pairing mnemonic here"
value={mnemonic}
/>
<Button title="Connect" onPress={this.attemptConnect} />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
// import com.swmansion.gesturehandler.RNGestureHandlerPackage;
import com.emeraldsanto.encryptedstorage.RNEncryptedStoragePackage;
import com.emeraldsanto.encryptedstorage.RNEncryptedStoragePackage;
import com.facebook.react.ReactInstanceManager;
Expand Down
4 changes: 1 addition & 3 deletions demos/connect-demo/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import org.apache.tools.ant.taskdefs.condition.Os

buildscript {
ext {
kotlinVersion = '1.6.0'
buildToolsVersion = findProperty('android.buildToolsVersion') ?: '31.0.0'
minSdkVersion = Integer.parseInt(findProperty('android.minSdkVersion') ?: '21')
compileSdkVersion = Integer.parseInt(findProperty('android.compileSdkVersion') ?: '31')
targetSdkVersion = Integer.parseInt(findProperty('android.targetSdkVersion') ?: '31')
if (findProperty('android.kotlinVersion')) {
kotlinVersion = findProperty('android.kotlinVersion')
}
frescoVersion = findProperty('expo.frescoVersion') ?: '2.5.0'

if (System.properties['os.arch'] == 'aarch64') {
Expand Down
2 changes: 2 additions & 0 deletions demos/connect-demo/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
rootProject.name = 'lnc-mobile-demo'
include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')
include ':react-native-encrypted-storage'
project(':react-native-encrypted-storage').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-encrypted-storage/android')
include ':react-native-encrypted-storage'
Expand Down
24 changes: 12 additions & 12 deletions demos/connect-demo/credentialStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ export default class LncCredentialStore implements CredentialStore {
private _localKey: string = '';
private _remoteKey: string = '';
private _pairingPhrase: string = '';
// namespace
public namespace: string = '';

/**
* Constructs a new `LncCredentialStore` instance
*/
constructor(pairingPhrase?: string) {
if (pairingPhrase) this.pairingPhrase = pairingPhrase;
constructor(namespace: string = 'default') {
this.namespace = namespace;

this.load(pairingPhrase);
this.load();
}

//
Expand All @@ -57,7 +59,7 @@ export default class LncCredentialStore implements CredentialStore {
set pairingPhrase(phrase: string) {
this._pairingPhrase = phrase;
this.persisted.pairingPhrase = phrase;
this.load(phrase);
this.load();
}

/** Stores the local private key which LNC uses to reestablish a connection */
Expand Down Expand Up @@ -94,7 +96,7 @@ export default class LncCredentialStore implements CredentialStore {

/** Clears any persisted data in the store */
clear() {
const key = `${STORAGE_KEY}:${this._pairingPhrase}`;
const key = `${STORAGE_KEY}:${this.namespace}`;
EncryptedStorage.removeItem(key);
this.persisted = {
serverHost: this.persisted.serverHost,
Expand All @@ -108,11 +110,9 @@ export default class LncCredentialStore implements CredentialStore {
}

/** Loads persisted data from EncryptedStorage */
async load(pairingPhrase?: string) {
// only load if pairingPhrase is set
if (!pairingPhrase) return;
async load() {
try {
const key = `${STORAGE_KEY}:${pairingPhrase}`;
const key = `${STORAGE_KEY}:${this.namespace}`;
const json = await EncryptedStorage.getItem(key);
if (!json) return this._save();
this.persisted = JSON.parse(json);
Expand All @@ -130,9 +130,9 @@ export default class LncCredentialStore implements CredentialStore {

/** Saves persisted data to EncryptedStorage */
private _save() {
// only save if pairingPhrase is set
if (!this._pairingPhrase) return;
const key = `${STORAGE_KEY}:${this._pairingPhrase}`;
// only save if namespace is set
if (!this.namespace) return;
const key = `${STORAGE_KEY}:${this.namespace}`;
EncryptedStorage.setItem(key, JSON.stringify(this.persisted));
}
}
Loading

0 comments on commit eec10dd

Please sign in to comment.