fix: format

This commit is contained in:
Richard Ramos 2023-01-03 13:49:46 -04:00 committed by RichΛrd
parent 3ab9bb046b
commit 3c2371252d
1 changed files with 333 additions and 161 deletions

View File

@ -1,5 +1,5 @@
import { NativeModules, Platform, NativeEventEmitter} from 'react-native'; import { NativeModules, Platform, NativeEventEmitter } from 'react-native';
import {decode, encode} from 'base-64' import { decode, encode } from 'base-64';
const LINKING_ERROR = const LINKING_ERROR =
`The package '@waku/react-native' doesn't seem to be linked. Make sure: \n\n` + `The package '@waku/react-native' doesn't seem to be linked. Make sure: \n\n` +
@ -7,7 +7,9 @@ const LINKING_ERROR =
'- You rebuilt the app after installing the package\n' + '- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n'; '- You are not using Expo managed workflow\n';
const ReactNative = NativeModules.ReactNative ? NativeModules.ReactNative : new Proxy( const ReactNative = NativeModules.ReactNative
? NativeModules.ReactNative
: new Proxy(
{}, {},
{ {
get() { get() {
@ -23,21 +25,23 @@ export function multiply(a: number, b: number): Promise<number> {
const OneMillion = BigInt(1_000_000); const OneMillion = BigInt(1_000_000);
export class WakuMessage { export class WakuMessage {
payload: Uint8Array = new Uint8Array(); payload: Uint8Array = new Uint8Array();
contentTopic: String | null = ""; contentTopic: String | null = '';
version: Number | null = 0; version: Number | null = 0;
timestamp?: Date = undefined; timestamp?: Date = undefined;
toJSON(){ toJSON() {
const b64encoded = encode(String.fromCharCode(...this.payload)); const b64encoded = encode(String.fromCharCode(...this.payload));
return { return {
contentTopic: this.contentTopic, contentTopic: this.contentTopic,
version: this.version, version: this.version,
timestamp: this.timestamp ? (BigInt(this.timestamp.valueOf()) * OneMillion).toString(10) : 0, timestamp: this.timestamp
payload: b64encoded ? (BigInt(this.timestamp.valueOf()) * OneMillion).toString(10)
} : 0,
} payload: b64encoded,
} };
}
}
var eventEmitter = new NativeEventEmitter(NativeModules.ReactNative); var eventEmitter = new NativeEventEmitter(NativeModules.ReactNative);
@ -45,28 +49,32 @@ var eventEmitter = new NativeEventEmitter(NativeModules.ReactNative);
* Execute function each time a message is received * Execute function each time a message is received
* @param cb callback to be eecuted * @param cb callback to be eecuted
*/ */
export function onMessage(cb: (arg0:any) => void) { export function onMessage(cb: (arg0: any) => void) {
eventEmitter.addListener("message", event => { eventEmitter.addListener('message', (event) => {
let signal = JSON.parse(event.signal); let signal = JSON.parse(event.signal);
let msg = signal.event.wakuMessage; let msg = signal.event.wakuMessage;
signal.event.wakuMessage = new WakuMessage(); signal.event.wakuMessage = new WakuMessage();
signal.event.wakuMessage.timestamp = msg.timestamp; signal.event.wakuMessage.timestamp = msg.timestamp;
signal.event.wakuMessage.version = msg.version || 0; signal.event.wakuMessage.version = msg.version || 0;
signal.event.wakuMessage.contentTopic = msg.contentTopic; signal.event.wakuMessage.contentTopic = msg.contentTopic;
signal.event.wakuMessage.payload = new Uint8Array(decode(msg.payload ?? []).split("").map((c:any) => c.charCodeAt(0))); signal.event.wakuMessage.payload = new Uint8Array(
decode(msg.payload ?? [])
.split('')
.map((c: any) => c.charCodeAt(0))
);
cb(signal.event); cb(signal.event);
}) });
} }
export class Config { export class Config {
host: String | null = null host: String | null = null;
port: Number | null = null port: Number | null = null;
advertiseAddr: String | null = null advertiseAddr: String | null = null;
nodeKey: String | null = null nodeKey: String | null = null;
keepAliveInterval: Number | null = null keepAliveInterval: Number | null = null;
relay: Boolean | null = null relay: Boolean | null = null;
filter: Boolean | null = null filter: Boolean | null = null;
minPeersToPublish: Number | null = null minPeersToPublish: Number | null = null;
} }
/** /**
@ -74,9 +82,11 @@ export class Config {
* @param config options used to initialize a go-waku node * @param config options used to initialize a go-waku node
*/ */
export function newNode(config: Config | null): Promise<void> { export function newNode(config: Config | null): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.newNode(config ? JSON.stringify(config) : "")); let response = JSON.parse(
if(response.error){ await ReactNative.newNode(config ? JSON.stringify(config) : '')
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -90,7 +100,7 @@ export function newNode(config: Config | null): Promise<void> {
export function start(): Promise<void> { export function start(): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.start()); let response = JSON.parse(await ReactNative.start());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -104,7 +114,7 @@ export function start(): Promise<void> {
export function stop(): Promise<void> { export function stop(): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.stop()); let response = JSON.parse(await ReactNative.stop());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -119,7 +129,7 @@ export function stop(): Promise<void> {
export function isStarted(): Promise<Boolean> { export function isStarted(): Promise<Boolean> {
return new Promise<Boolean>(async (resolve, reject) => { return new Promise<Boolean>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.isStarted()); let response = JSON.parse(await ReactNative.isStarted());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -134,7 +144,7 @@ export function isStarted(): Promise<Boolean> {
export function peerID(): Promise<string> { export function peerID(): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.peerID()); let response = JSON.parse(await ReactNative.peerID());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -149,11 +159,17 @@ export function peerID(): Promise<string> {
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns string containing the message id * @returns string containing the message id
*/ */
export function relayPublish(msg: WakuMessage, pubsubTopic: String = "", timeoutMs: Number = 0): Promise<string> { export function relayPublish(
msg: WakuMessage,
pubsubTopic: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.relayPublish(messageJSON, pubsubTopic, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.relayPublish(messageJSON, pubsubTopic, timeoutMs)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -162,7 +178,7 @@ export function relayPublish(msg: WakuMessage, pubsubTopic: String = "", timeout
} }
/** /**
* Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay. * Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay.
* @param msg WakuMessage to publish. The message version is overwritten to `1` * @param msg WakuMessage to publish. The message version is overwritten to `1`
* @param publicKey hex encoded public key to be used for encryption. * @param publicKey hex encoded public key to be used for encryption.
* @param optionalSigningKey hex encoded private key to be used to sign the message. * @param optionalSigningKey hex encoded private key to be used to sign the message.
@ -170,11 +186,25 @@ export function relayPublish(msg: WakuMessage, pubsubTopic: String = "", timeout
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns string containing the message id * @returns string containing the message id
*/ */
export function relayPublishEncodeAsymmetric(msg: WakuMessage, publicKey: String, optionalSigningKey: String = "", pubsubTopic: String = "", timeoutMs: Number = 0): Promise<string> { export function relayPublishEncodeAsymmetric(
msg: WakuMessage,
publicKey: String,
optionalSigningKey: String = '',
pubsubTopic: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, pubsubTopic, publicKey, optionalSigningKey, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.relayPublishEncodeAsymmetric(
messageJSON,
pubsubTopic,
publicKey,
optionalSigningKey,
timeoutMs
)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -191,11 +221,25 @@ export function relayPublishEncodeAsymmetric(msg: WakuMessage, publicKey: String
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns string containing the message id * @returns string containing the message id
*/ */
export function relayPublishEncodeSymmetric(msg: WakuMessage, symmetricKey: String, optionalSigningKey: String = "", pubsubTopic: String = "", timeoutMs: Number = 0): Promise<string> { export function relayPublishEncodeSymmetric(
msg: WakuMessage,
symmetricKey: String,
optionalSigningKey: String = '',
pubsubTopic: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.relayPublishEncodeAsymmetric(messageJSON, pubsubTopic, symmetricKey, optionalSigningKey, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.relayPublishEncodeAsymmetric(
messageJSON,
pubsubTopic,
symmetricKey,
optionalSigningKey,
timeoutMs
)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -205,12 +249,12 @@ export function relayPublishEncodeSymmetric(msg: WakuMessage, symmetricKey: Stri
/** /**
* Subscribe to a Waku Relay pubsub topic to receive messages. * Subscribe to a Waku Relay pubsub topic to receive messages.
* @param topic Pubsub topic to subscribe to. * @param topic Pubsub topic to subscribe to.
*/ */
export function relaySubscribe(topic: String = ""): Promise<void> { export function relaySubscribe(topic: String = ''): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.relaySubscribe(topic)); let response = JSON.parse(await ReactNative.relaySubscribe(topic));
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -233,7 +277,7 @@ export function defaultPubsubTopic(): Promise<String> {
export function listenAddresses(): Promise<Array<String>> { export function listenAddresses(): Promise<Array<String>> {
return new Promise<Array<string>>(async (resolve, reject) => { return new Promise<Array<string>>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.listenAddresses()); let response = JSON.parse(await ReactNative.listenAddresses());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -247,10 +291,15 @@ export function listenAddresses(): Promise<Array<String>> {
* @param protocol protocol we expect the peer to support * @param protocol protocol we expect the peer to support
* @returns peer ID as a base58 `string` of the peer that was added * @returns peer ID as a base58 `string` of the peer that was added
*/ */
export function addPeer(multiAddress: String, protocol: String): Promise<String> { export function addPeer(
multiAddress: String,
protocol: String
): Promise<String> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.addPeer(multiAddress, protocol)); let response = JSON.parse(
if(response.error){ await ReactNative.addPeer(multiAddress, protocol)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -263,10 +312,15 @@ export function addPeer(multiAddress: String, protocol: String): Promise<String>
* @param multiAddress multiaddress to reach the peer being dialed * @param multiAddress multiaddress to reach the peer being dialed
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
*/ */
export function connect(multiAddress: String, timeoutMs: Number = 0): Promise<void> { export function connect(
multiAddress: String,
timeoutMs: Number = 0
): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.connect(multiAddress, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.connect(multiAddress, timeoutMs)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -279,10 +333,15 @@ export function connect(multiAddress: String, timeoutMs: Number = 0): Promise<vo
* @param peerID Peer ID to dial. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect` * @param peerID Peer ID to dial. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect`
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
*/ */
export function connectPeerID(peerID: String, timeoutMs: Number = 0): Promise<void> { export function connectPeerID(
peerID: String,
timeoutMs: Number = 0
): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.connectPeerID(peerID, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.connectPeerID(peerID, timeoutMs)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -297,7 +356,7 @@ export function connectPeerID(peerID: String, timeoutMs: Number = 0): Promise<vo
export function disconnect(peerID: String): Promise<void> { export function disconnect(peerID: String): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.disconnect(peerID)); let response = JSON.parse(await ReactNative.disconnect(peerID));
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -312,7 +371,7 @@ export function disconnect(peerID: String): Promise<void> {
export function peerCnt(): Promise<Number> { export function peerCnt(): Promise<Number> {
return new Promise<Number>(async (resolve, reject) => { return new Promise<Number>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.peerCnt()); let response = JSON.parse(await ReactNative.peerCnt());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -323,10 +382,10 @@ export function peerCnt(): Promise<Number> {
export class DecodedPayload { export class DecodedPayload {
payload: Uint8Array = new Uint8Array(); payload: Uint8Array = new Uint8Array();
padding: Uint8Array = new Uint8Array(); padding: Uint8Array = new Uint8Array();
pubkey: String | null = ""; pubkey: String | null = '';
signature: String | null = ""; signature: String | null = '';
toJSON(){ toJSON() {
const b64payload = encode(String.fromCharCode(...this.payload)); const b64payload = encode(String.fromCharCode(...this.payload));
const b64padding = encode(String.fromCharCode(...this.padding)); const b64padding = encode(String.fromCharCode(...this.padding));
return { return {
@ -334,7 +393,7 @@ export class DecodedPayload {
padding: b64padding, padding: b64padding,
pubkey: this.pubkey, pubkey: this.pubkey,
signature: this.signature, signature: this.signature,
} };
} }
} }
@ -344,16 +403,29 @@ export class DecodedPayload {
* @param symmetricKey 32 byte symmetric key hex encoded * @param symmetricKey 32 byte symmetric key hex encoded
* @returns DecodedPayload * @returns DecodedPayload
*/ */
export function decodeSymmetric(msg: WakuMessage, symmetricKey: String): Promise<DecodedPayload> { export function decodeSymmetric(
msg: WakuMessage,
symmetricKey: String
): Promise<DecodedPayload> {
return new Promise<DecodedPayload>(async (resolve, reject) => { return new Promise<DecodedPayload>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg); let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.decodeSymmetric(messageJSON, symmetricKey)); let response = JSON.parse(
if(response.error){ await ReactNative.decodeSymmetric(messageJSON, symmetricKey)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
let decodedPayload = new DecodedPayload(); let decodedPayload = new DecodedPayload();
decodedPayload.payload = new Uint8Array(atob(response.result.payload).split("").map(c => c.charCodeAt(0))); decodedPayload.payload = new Uint8Array(
decodedPayload.padding = new Uint8Array(atob(response.result.padding).split("").map(c => c.charCodeAt(0))); atob(response.result.payload)
.split('')
.map((c) => c.charCodeAt(0))
);
decodedPayload.padding = new Uint8Array(
atob(response.result.padding)
.split('')
.map((c) => c.charCodeAt(0))
);
decodedPayload.pubkey = response.result.pubkey; decodedPayload.pubkey = response.result.pubkey;
decodedPayload.signature = response.result.signature; decodedPayload.signature = response.result.signature;
resolve(decodedPayload); resolve(decodedPayload);
@ -362,21 +434,34 @@ export function decodeSymmetric(msg: WakuMessage, symmetricKey: String): Promise
} }
/** /**
* Decrypt a message using a secp256k1 private key * Decrypt a message using a secp256k1 private key
* @param msg WakuMessage to decode. The message version is expected to be 1 * @param msg WakuMessage to decode. The message version is expected to be 1
* @param privateKey secp256k1 private key hex encoded * @param privateKey secp256k1 private key hex encoded
* @returns DecodedPayload * @returns DecodedPayload
*/ */
export function decodeAsymmetric(msg: WakuMessage, privateKey: String): Promise<DecodedPayload> { export function decodeAsymmetric(
msg: WakuMessage,
privateKey: String
): Promise<DecodedPayload> {
return new Promise<DecodedPayload>(async (resolve, reject) => { return new Promise<DecodedPayload>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg); let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.decodeSymmetric(messageJSON, privateKey)); let response = JSON.parse(
if(response.error){ await ReactNative.decodeSymmetric(messageJSON, privateKey)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
let decodedPayload = new DecodedPayload(); let decodedPayload = new DecodedPayload();
decodedPayload.payload = new Uint8Array(atob(response.result.payload).split("").map(c => c.charCodeAt(0))); decodedPayload.payload = new Uint8Array(
decodedPayload.padding = new Uint8Array(atob(response.result.padding).split("").map(c => c.charCodeAt(0))); atob(response.result.payload)
.split('')
.map((c) => c.charCodeAt(0))
);
decodedPayload.padding = new Uint8Array(
atob(response.result.padding)
.split('')
.map((c) => c.charCodeAt(0))
);
decodedPayload.pubkey = response.result.pubkey; decodedPayload.pubkey = response.result.pubkey;
decodedPayload.signature = response.result.signature; decodedPayload.signature = response.result.signature;
resolve(decodedPayload); resolve(decodedPayload);
@ -389,10 +474,10 @@ export function decodeAsymmetric(msg: WakuMessage, privateKey: String): Promise<
* @param pubsubTopic Pubsub topic to verify. If not specified, it will verify the default pubsub topic * @param pubsubTopic Pubsub topic to verify. If not specified, it will verify the default pubsub topic
* @returns boolean indicates whether there are enough peers * @returns boolean indicates whether there are enough peers
*/ */
export function relayEnoughPeers(pubsubTopic: String = ""): Promise<Boolean> { export function relayEnoughPeers(pubsubTopic: String = ''): Promise<Boolean> {
return new Promise<Boolean>(async (resolve, reject) => { return new Promise<Boolean>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.relayEnoughPeers(pubsubTopic)); let response = JSON.parse(await ReactNative.relayEnoughPeers(pubsubTopic));
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -402,12 +487,12 @@ export function relayEnoughPeers(pubsubTopic: String = ""): Promise<Boolean> {
/** /**
* Closes the pubsub subscription to a pubsub topic. No more messages will be received from this pubsub topic. * Closes the pubsub subscription to a pubsub topic. No more messages will be received from this pubsub topic.
* @param pubsubTopic * @param pubsubTopic
*/ */
export function relayUnsubscribe(pubsubTopic: String = ""): Promise<void> { export function relayUnsubscribe(pubsubTopic: String = ''): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.relayUnsubscribe(pubsubTopic)); let response = JSON.parse(await ReactNative.relayUnsubscribe(pubsubTopic));
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -418,16 +503,28 @@ export function relayUnsubscribe(pubsubTopic: String = ""): Promise<void> {
/** /**
* Publish a message using Waku Lightpush. * Publish a message using Waku Lightpush.
* @param msg WakuMessage to publish. The message version is overwritten to `0` * @param msg WakuMessage to publish. The message version is overwritten to `0`
* @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic. * @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic.
* @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect` * @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect`
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns the message ID * @returns the message ID
*/ */
export function lightpushPublish(msg: WakuMessage, pubsubTopic: String = "", peerID: String = "", timeoutMs: Number = 0): Promise<string> { export function lightpushPublish(
msg: WakuMessage,
pubsubTopic: String = '',
peerID: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.lightpushPublish(messageJSON, pubsubTopic, peerID, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.lightpushPublish(
messageJSON,
pubsubTopic,
peerID,
timeoutMs
)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -440,16 +537,32 @@ export function lightpushPublish(msg: WakuMessage, pubsubTopic: String = "", pee
* @param msg WakuMessage to publish. The message version is overwritten to `1` * @param msg WakuMessage to publish. The message version is overwritten to `1`
* @param publicKey hex encoded public key to be used for encryption * @param publicKey hex encoded public key to be used for encryption
* @param optionalSigningKey hex encoded private key to be used to sign the message * @param optionalSigningKey hex encoded private key to be used to sign the message
* @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic. * @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic.
* @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect` * @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect`
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns the message ID * @returns the message ID
*/ */
export function lightpushPublishEncAsymmetric(msg: WakuMessage, publicKey: String, optionalSigningKey: String = "", pubsubTopic: String = "", peerID: String = "", timeoutMs: Number = 0): Promise<string> { export function lightpushPublishEncAsymmetric(
msg: WakuMessage,
publicKey: String,
optionalSigningKey: String = '',
pubsubTopic: String = '',
peerID: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.lightpushPublishEncodeAsymmetric(messageJSON, pubsubTopic, peerID, publicKey, optionalSigningKey, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.lightpushPublishEncodeAsymmetric(
messageJSON,
pubsubTopic,
peerID,
publicKey,
optionalSigningKey,
timeoutMs
)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -462,16 +575,32 @@ export function lightpushPublishEncAsymmetric(msg: WakuMessage, publicKey: Strin
* @param msg WakuMessage to publish. The message version is overwritten to `1` * @param msg WakuMessage to publish. The message version is overwritten to `1`
* @param symmetricKey hex encoded secret key to be used for encryption. * @param symmetricKey hex encoded secret key to be used for encryption.
* @param optionalSigningKey hex encoded private key to be used to sign the message. * @param optionalSigningKey hex encoded private key to be used to sign the message.
* @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic. * @param pubsubTopic pubsub topic on which to publish the message. If not specified, it uses the default pubsub topic.
* @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect` * @param peerID Peer ID supporting the lightpush protocol. The peer must be already known. It must have been added before with `addPeer` or previously dialed with `connect`
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns the message ID * @returns the message ID
*/ */
export function lightpushPublishEncSymmetric(msg: WakuMessage, symmetricKey: String, optionalSigningKey: String = "", pubsubTopic: String = "", peerID: String = "", timeoutMs: Number = 0): Promise<string> { export function lightpushPublishEncSymmetric(
msg: WakuMessage,
symmetricKey: String,
optionalSigningKey: String = '',
pubsubTopic: String = '',
peerID: String = '',
timeoutMs: Number = 0
): Promise<string> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let messageJSON = JSON.stringify(msg) let messageJSON = JSON.stringify(msg);
let response = JSON.parse(await ReactNative.lightpushPublishEncodeAsymmetric(messageJSON, pubsubTopic, peerID, symmetricKey, optionalSigningKey, timeoutMs)); let response = JSON.parse(
if(response.error){ await ReactNative.lightpushPublishEncodeAsymmetric(
messageJSON,
pubsubTopic,
peerID,
symmetricKey,
optionalSigningKey,
timeoutMs
)
);
if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -480,12 +609,17 @@ export function lightpushPublishEncSymmetric(msg: WakuMessage, symmetricKey: Str
} }
export class Peer { export class Peer {
addrs: Array<String> = Array() addrs: Array<String> = Array();
connected: Boolean = false connected: Boolean = false;
peerID: String = "" peerID: String = '';
protocols: Array<String> = Array() protocols: Array<String> = Array();
constructor(addrs: Array<String>, connected: Boolean, peerID: String, protocols: Array<String>){ constructor(
addrs: Array<String>,
connected: Boolean,
peerID: String,
protocols: Array<String>
) {
this.addrs = addrs; this.addrs = addrs;
this.connected = connected; this.connected = connected;
this.peerID = peerID; this.peerID = peerID;
@ -500,75 +634,99 @@ export class Peer {
export function peers(): Promise<Array<Peer>> { export function peers(): Promise<Array<Peer>> {
return new Promise<Array<Peer>>(async (resolve, reject) => { return new Promise<Array<Peer>>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.peers()); let response = JSON.parse(await ReactNative.peers());
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result.map((x:any) => new Peer(x.addrs, x.connected, x.peerID, x.protocols))); resolve(
response.result.map(
(x: any) => new Peer(x.addrs, x.connected, x.peerID, x.protocols)
)
);
} }
}) });
} }
export class Index { export class Index {
digest: Uint8Array = new Uint8Array(); digest: Uint8Array = new Uint8Array();
receiverTime: Number = 0 receiverTime: Number = 0;
senderTime: Number = 0 senderTime: Number = 0;
pubsubTopic: String = "" pubsubTopic: String = '';
} }
export class PagingOptions { export class PagingOptions {
pageSize: Number = 0 pageSize: Number = 0;
cursor: Index | null = null cursor: Index | null = null;
forward: Boolean = false forward: Boolean = false;
constructor(pageSize: Number = 0, forward: Boolean = false, cursor: Index | null = null){ constructor(
this.pageSize = pageSize pageSize: Number = 0,
this.forward = forward forward: Boolean = false,
this.cursor = cursor cursor: Index | null = null
) {
this.pageSize = pageSize;
this.forward = forward;
this.cursor = cursor;
} }
} }
export class ContentFilter { export class ContentFilter {
contentTopic: String = "" contentTopic: String = '';
constructor(contentTopic: String = "") { constructor(contentTopic: String = '') {
this.contentTopic = contentTopic this.contentTopic = contentTopic;
} }
} }
export class StoreQuery { export class StoreQuery {
pubsubTopic: String | null = null pubsubTopic: String | null = null;
contentFilters: Array<ContentFilter> = Array() contentFilters: Array<ContentFilter> = Array();
startTime?: Date = undefined startTime?: Date = undefined;
endTime?: Date = undefined endTime?: Date = undefined;
pagingOptions: PagingOptions | null = null pagingOptions: PagingOptions | null = null;
constructor(pubsubTopic: String | null = null, contentFilters: Array<ContentFilter> = Array(), startTime: Date | undefined = undefined, endTime: Date | undefined = undefined, pagingOptions: PagingOptions | null = null) { constructor(
this.pubsubTopic = pubsubTopic pubsubTopic: String | null = null,
this.contentFilters = contentFilters contentFilters: Array<ContentFilter> = Array(),
this.startTime = startTime startTime: Date | undefined = undefined,
this.endTime = endTime endTime: Date | undefined = undefined,
this.pagingOptions = pagingOptions pagingOptions: PagingOptions | null = null
) {
this.pubsubTopic = pubsubTopic;
this.contentFilters = contentFilters;
this.startTime = startTime;
this.endTime = endTime;
this.pagingOptions = pagingOptions;
} }
} }
/** /**
* *
* @param query * @param query
* @param peerID * @param peerID
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
* @returns * @returns
*/ */
export function storeQuery(query: StoreQuery, peerID: String = "", timeoutMs: Number = 0): Promise<any> { export function storeQuery(
query: StoreQuery,
peerID: String = '',
timeoutMs: Number = 0
): Promise<any> {
return new Promise<string>(async (resolve, reject) => { return new Promise<string>(async (resolve, reject) => {
let queryJSON = JSON.stringify({ let queryJSON = JSON.stringify({
pubsubTopic: query.pubsubTopic, pubsubTopic: query.pubsubTopic,
contentFilters: query.contentFilters, contentFilters: query.contentFilters,
startTime: query.startTime ? (BigInt(query.startTime.valueOf()) * OneMillion).toString(10) : 0, startTime: query.startTime
endTime: query.endTime ? (BigInt(query.endTime.valueOf()) * OneMillion).toString(10) : 0, ? (BigInt(query.startTime.valueOf()) * OneMillion).toString(10)
pagingOptions: query.pagingOptions : 0,
}) endTime: query.endTime
let response = JSON.parse(await ReactNative.storeQuery(queryJSON, peerID, timeoutMs)); ? (BigInt(query.endTime.valueOf()) * OneMillion).toString(10)
: 0,
pagingOptions: query.pagingOptions,
});
let response = JSON.parse(
await ReactNative.storeQuery(queryJSON, peerID, timeoutMs)
);
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(response.result); resolve(response.result);
@ -577,27 +735,36 @@ export function storeQuery(query: StoreQuery, peerID: String = "", timeoutMs: Nu
} }
export class FilterSubscription { export class FilterSubscription {
pubsubTopic: String | null = null pubsubTopic: String | null = null;
contentFilters: Array<ContentFilter> = Array() contentFilters: Array<ContentFilter> = Array();
constructor(pubsubTopic: String | null = null, contentFilters: Array<ContentFilter> = Array()) { constructor(
this.pubsubTopic = pubsubTopic pubsubTopic: String | null = null,
this.contentFilters = contentFilters contentFilters: Array<ContentFilter> = Array()
) {
this.pubsubTopic = pubsubTopic;
this.contentFilters = contentFilters;
} }
} }
/** /**
* *
* @param filter * @param filter
* @param peerID * @param peerID
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
*/ */
export function filterSubscribe(filter: FilterSubscription, peerID: String = "", timeoutMs: Number = 0): Promise<void> { export function filterSubscribe(
filter: FilterSubscription,
peerID: String = '',
timeoutMs: Number = 0
): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let filterJSON = JSON.stringify(filter) let filterJSON = JSON.stringify(filter);
let response = JSON.parse(await ReactNative.filterSubscribe(filterJSON, peerID, timeoutMs)); let response = JSON.parse(
await ReactNative.filterSubscribe(filterJSON, peerID, timeoutMs)
);
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();
@ -606,16 +773,21 @@ export function filterSubscribe(filter: FilterSubscription, peerID: String = "",
} }
/** /**
* *
* @param filter * @param filter
* @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned * @param timeoutMs Timeout value in milliseconds to execute the call. If the function takes longer than this value, the execution will be canceled and an error returned
*/ */
export function filterUnsubscribe(filter: FilterSubscription, timeoutMs: Number = 0): Promise<void> { export function filterUnsubscribe(
filter: FilterSubscription,
timeoutMs: Number = 0
): Promise<void> {
return new Promise<void>(async (resolve, reject) => { return new Promise<void>(async (resolve, reject) => {
let filterJSON = JSON.stringify(filter) let filterJSON = JSON.stringify(filter);
let response = JSON.parse(await ReactNative.filterUnsubscribe(filterJSON, timeoutMs)); let response = JSON.parse(
await ReactNative.filterUnsubscribe(filterJSON, timeoutMs)
);
if(response.error){ if (response.error) {
reject(response.error); reject(response.error);
} else { } else {
resolve(); resolve();