feat: adding gowaku functions to swift impl

This commit is contained in:
Richard Ramos 2022-05-12 19:41:31 -04:00
parent 47f8e9780c
commit 1ad33ba4c8
4 changed files with 268 additions and 43 deletions

View File

@ -1,10 +1,7 @@
import * as React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { defaultPubsubTopic, listenAddresses, newNode, onMessage, peerID, relayEnoughPeers, relayPublish, relaySubscribe, relayUnsubscribe, start, stop, WakuMessage, connect, peerCnt, peers } from '@waku/react-native';
import { defaultPubsubTopic, newNode, start, stop, peerID, relayEnoughPeers, listenAddresses, connect, peerCnt, peers, relayPublish, relayUnsubscribe, relaySubscribe, WakuMessage/*, onMessage*/ } from '@waku/react-native';
export default function App() {
const [result, setResult] = React.useState<string | undefined>();
@ -12,17 +9,19 @@ export default function App() {
React.useEffect(() => {
(async () => {
await newNode(); // TODO: This must be called only once
await newNode(null); // TODO: This must be called only once
await start(); // // TODO: This must be called only once
console.log("The node ID:", await peerID())
await relaySubscribe()
/*
// TODO: this is only implemented in android for now
onMessage(event => {
console.log("Message Received: ", event)
})
*/
console.log("enoughPeers?", await relayEnoughPeers())
console.log("addresses", await listenAddresses())

View File

@ -2,13 +2,124 @@
@interface RCT_EXTERN_MODULE(ReactNative, NSObject)
RCT_EXTERN_METHOD(multiply:(float)a withB:(float)b
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(defaultPubsubTopic:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(newNode:(NSString *)config
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(start:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(stop:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(peerID:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relayEnoughPeers:(NSString *)topic
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(listenAddresses:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(connect:(NSString *)address
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(peerCnt:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(peers:
(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relaySubscribe:(NSString *)topic
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relayUnsubscribe:(NSString *)topic
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relayPublish:(NSString *)msg
withTopic:(NSString *)topic
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relayPublishEncodeAsymmetric:(NSString *)msg
withTopic:(NSString *)topic
withPublicKey:(NSString *)publicKey
withOptionalSigningKey:(NSString *)optionalSigningKey
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(relayPublishEncodeSymmetric:(NSString *)msg
withTopic:(NSString *)topic
withSymmetricKey:(NSString *)symmetricKey
withOptionalSigningKey:(NSString *)optionalSigningKey
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(addPeer:(NSString *)addr
withProtocol:(NSString *)proto
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(connectPeerID:(NSString *)peerID
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(disconnect:(NSString *)peerID
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(lightpushPublish:(NSString *)msg
withTopic:(NSString *)topic
withPeerID:(NSString *)peerID
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(lightpushPublishEncodeAsymmetric:(NSString *)msg
withTopic:(NSString *)topic
withPeerID:(NSString *)peerID
withPublicKey:(NSString *)publicKey
withOptionalSigningKey:(NSString *)optionalSigningKey
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(lightpushPublishEncodeSymmetric:(NSString *)msg
withTopic:(NSString *)topic
withPeerID:(NSString *)peerID
withSymmetricKey:(NSString *)symmetricKey
withOptionalSigningKey:(NSString *)optionalSigningKey
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(decodeSymmetric:(NSString *)msg
withSymmetricKey:(NSString *)symmetricKey
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(decodeAsymmetric:(NSString *)msg
withPrivateKey:(NSString *)privateKey
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
@end

View File

@ -3,13 +3,119 @@ import Gowaku
@objc(ReactNative)
class ReactNative: NSObject {
@objc(multiply:withB:withResolver:withRejecter:)
func multiply(a: Float, b: Float, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(a*b)
}
@objc(defaultPubsubTopic:withRejecter:)
func defaultPubsubTopic(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDefaultPubsubTopic())
}
@objc(newNode:withResolver:withRejecter:)
func newNode(config: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuNewNode(config))
}
@objc(start:withRejecter:)
func start(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuStart())
}
@objc(stop:withRejecter:)
func stop(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuStop())
}
@objc(peerID:withRejecter:)
func peerID(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuPeerID())
}
@objc(relayEnoughPeers:withResolver:withRejecter:)
func relayEnoughPeers(topic: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelayEnoughPeers(topic))
}
@objc(listenAddresses:withRejecter:)
func listenAddresses(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuListenAddresses())
}
@objc(connect:withMs:withResolver:withRejecter:)
func connect(address: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuConnect(address, ms))
}
@objc(addPeer:withProtocol:withResolver:withRejecter:)
func addPeer(addr: String, proto: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuAddPeer(addr, proto))
}
@objc(connectPeerID:withMs:withResolver:withRejecter:)
func connectPeerID(peerID: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuConnect(peerID, ms))
}
@objc(disconnect:withResolver:withRejecter:)
func disconnect(peerID: String,resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDisconnect(peerID))
}
@objc(peerCnt:withRejecter:)
func peerCnt(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuPeerCnt())
}
@objc(peers:withRejecter:)
func peers(_ resolve:RCTPromiseResolveBlock, withRejecter reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuPeers())
}
@objc(relaySubscribe:withResolver:withRejecter:)
func relaySubscribe(topic: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelaySubscribe(topic))
}
@objc(relayUnsubscribe:withResolver:withRejecter:)
func relayUnsubscribe(topic: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelayUnsubscribe(topic))
}
@objc(relayPublish:withTopic:withMs:withResolver:withRejecter:)
func relayPublish(msg: String, topic: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelayPublish(msg, topic, ms))
}
@objc(relayPublishEncodeAsymmetric:withTopic:withPublicKey:withOptionalSigningKey:withMs:withResolver:withRejecter:)
func relayPublishEncodeAsymmetric(msg: String, topic: String, publicKey: String, optionalSigningKey: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelayPublishEncodeAsymmetric(msg, topic, publicKey, optionalSigningKey, ms))
}
@objc(relayPublishEncodeSymmetric:withTopic:withSymmetricKey:withOptionalSigningKey:withMs:withResolver:withRejecter:)
func relayPublishEncodeSymmetric(msg: String, topic: String, symmetricKey: String, optionalSigningKey: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuRelayPublishEncodeSymmetric(msg, topic, symmetricKey, optionalSigningKey, ms))
}
@objc(lightpushPublish:withTopic:withPeerID:withMs:withResolver:withRejecter:)
func lightpushPublish(msg: String, topic: String, peerID: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuLightpushPublish(msg, topic, peerID, ms))
}
@objc(lightpushPublishEncodeAsymmetric:withTopic:withPeerID:withPublicKey:withOptionalSigningKey:withMs:withResolver:withRejecter:)
func lightpushPublishEncodeAsymmetric(msg: String, topic: String, peerID: String, publicKey: String, optionalSigningKey: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuLightpushPublishEncodeAsymmetric(msg, topic, peerID, publicKey, optionalSigningKey, ms))
}
@objc(lightpushPublishEncodeSymmetric:withTopic:withPeerID:withSymmetricKey:withOptionalSigningKey:withMs:withResolver:withRejecter:)
func lightpushPublishEncodeSymmetric(msg: String, topic: String, peerID: String, symmetricKey: String, optionalSigningKey: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuLightpushPublishEncodeSymmetric(msg, topic, peerID, symmetricKey, optionalSigningKey, ms))
}
@objc(decodeSymmetric:withSymmetricKey:withResolver:withRejecter:)
func decodeSymmetric(msg: String, symmetricKey: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDecodeSymmetric(msg, symmetricKey))
}
@objc(decodeAsymmetric:withPrivateKey:withResolver:withRejecter:)
func decodeAsymmetric(msg: String, privateKey: String, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDecodeAsymmetric(msg, privateKey))
}
}

View File

@ -1,4 +1,4 @@
import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
import { NativeModules, Platform /*, NativeEventEmitter */} from 'react-native';
import {decode, encode} from 'base-64'
const LINKING_ERROR =
@ -18,8 +18,6 @@ const ReactNative = NativeModules.ReactNative
}
);
var eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
export class WakuMessage {
payload: Uint8Array = new Uint8Array();
contentTopic: String | null = "";
@ -35,7 +33,12 @@ export class WakuMessage {
payload: b64encoded
}
}
}
}
/*
// TODO: this has been only implemented in android for now
var eventEmitter = new NativeEventEmitter(NativeModules.ToastExample);
export function onMessage(cb: (arg0:any) => void) {
eventEmitter.addListener("message", event => {
@ -49,6 +52,7 @@ export function onMessage(cb: (arg0:any) => void) {
cb(signal);
})
}
*/
export class Config {
host: String | null = null
@ -116,10 +120,10 @@ export function relayPublish(msg: WakuMessage, topic: String = "", ms: Number =
});
}
export function relayPublishEncodeAsymmetric(msg: WakuMessage, publicKey: String, topic: String = "", ms: Number = 0): Promise<string> {
export function relayPublishEncodeAsymmetric(msg: WakuMessage, publicKey: String, optionalSigningKey: String = "", topic: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, publicKey, ms));
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, publicKey, optionalSigningKey, ms));
if(response.error){
reject(response.error);
} else {
@ -128,10 +132,10 @@ export function relayPublishEncodeAsymmetric(msg: WakuMessage, publicKey: String
});
}
export function relayPublishEncodeSymmetric(msg: WakuMessage, symmetricKey: String, topic: String = "", ms: Number = 0): Promise<string> {
export function relayPublishEncodeSymmetric(msg: WakuMessage, symmetricKey: String, optionalSigningKey: String = "", topic: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, symmetricKey, ms));
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, symmetricKey, optionalSigningKey, ms));
if(response.error){
reject(response.error);
} else {
@ -295,10 +299,15 @@ export function relayUnsubscribe(topic: String = ""): Promise<void> {
});
}
export function lightpushPublish(msg: WakuMessage, topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
let response = JSON.parse(await ReactNative.relayPublish(messageJSON, topic, peerID, ms));
let response = JSON.parse(await ReactNative.lightpushPublish(messageJSON, topic, peerID, ms));
if(response.error){
reject(response.error);
} else {
@ -307,10 +316,10 @@ export function lightpushPublish(msg: WakuMessage, topic: String = "", peerID: S
});
}
export function lightpushPublishEncAsymmetric(msg: WakuMessage, publicKey: String, topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
export function lightpushPublishEncAsymmetric(msg: WakuMessage, publicKey: String, optionalSigningKey: String = "", topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, peerID, publicKey, ms));
let response = JSON.parse(await ReactNative.lightpushPublishEncodeAsymmetric(messageJSON, topic, peerID, publicKey, optionalSigningKey, ms));
if(response.error){
reject(response.error);
} else {
@ -319,10 +328,10 @@ export function lightpushPublishEncAsymmetric(msg: WakuMessage, publicKey: Strin
});
}
export function lightpushPublishEncSymmetric(msg: WakuMessage, symmetricKey: String, topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
export function lightpushPublishEncSymmetric(msg: WakuMessage, symmetricKey: String, optionalSigningKey: String = "", topic: String = "", peerID: String = "", ms: Number = 0): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg)
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, topic, peerID, symmetricKey, ms));
let response = JSON.parse(await ReactNative.lightpushPublishEncodeAsymmetric(messageJSON, topic, peerID, symmetricKey, optionalSigningKey, ms));
if(response.error){
reject(response.error);
} else {