feat: expose DNS Discovery and DiscV5

This commit is contained in:
Richard Ramos 2023-02-20 19:05:46 -04:00 committed by RichΛrd
parent dc6ab3cf27
commit 49ea163a59
10 changed files with 96 additions and 23 deletions

View File

@ -183,4 +183,8 @@ class ReactNativeModule(reactContext: ReactApplicationContext) : ReactContextBas
promise.resolve(Gowaku.filterUnsubscribe(filterJSON, ms.toLong()))
}
@ReactMethod
fun dnsDiscovery(url: String, nameserver: String = "", ms: Double, promise: Promise) {
promise.resolve(Gowaku.dnsDiscovery(url, nameserver, ms.toLong()))
}
}

View File

@ -30,11 +30,11 @@ mkdir -p tmp
cd tmp
rm -f ${SHA_FILE}
wget "https://github.com/status-im/go-waku/releases/download/v${VERSION}/${SHA_FILE}"
wget "https://github.com/waku-org/go-waku/releases/download/v${VERSION}/${SHA_FILE}"
if [ "$DOWNLOAD_ANDROID" = true ]; then
rm -f ${ANDROID_TAR}
wget "https://github.com/status-im/go-waku/releases/download/v${VERSION}/${ANDROID_TAR}"
curl -L "https://github.com/waku-org/go-waku/releases/download/v${VERSION}/${ANDROID_TAR}" --output $ANDROID_TAR
sha256sum --status --ignore-missing -c ${SHA_FILE}
if ! sha256sum --status --ignore-missing -c ${SHA_FILE}; then
echo "checksum failed - verify download"
@ -47,7 +47,7 @@ fi
if [ "$DOWNLOAD_IOS" = true ]; then
rm -f ${IOS_TAR}
wget "https://github.com/status-im/go-waku/releases/download/v${VERSION}/${IOS_TAR}"
curl -L "https://github.com/waku-org/go-waku/releases/download/v${VERSION}/${IOS_TAR}" --output $IOS_TAR
if ! sha256sum --status --ignore-missing -c ${SHA_FILE}; then
echo "checksum failed - verify download"

View File

@ -24,8 +24,11 @@ import {
FilterSubscription,
ContentFilter,
filterSubscribe,
dnsDiscovery,
} from '@waku/react-native';
const myContentTopic = '/example/1/react-native-app/proto';
export default function App() {
const [result, setResult] = React.useState();
@ -39,14 +42,16 @@ export default function App() {
}
console.log('The node ID:', await peerID());
await relaySubscribe();
await relaySubscribe();
onMessage((event) => {
if (event.wakuMessage.contentTopic !== myContentTopic) return;
setResult(
'Message received: ' +
event.wakuMessage.timestamp +
' - payload:[' +
event.wakuMessage.payload +
JSON.stringify(event.wakuMessage.payload) +
']'
);
console.log('Message received: ', event);
@ -80,7 +85,7 @@ export default function App() {
console.log('Peers', await peers());
let msg = new WakuMessage();
msg.contentTopic = 'ABC';
msg.contentTopic = myContentTopic;
msg.payload = new Uint8Array([1, 2, 3, 4, 5]);
msg.timestamp = new Date();
msg.version = 0;
@ -92,13 +97,21 @@ export default function App() {
// TO RETRIEVE HISTORIC MESSAGES:
console.log('Retrieving messages from store node');
const query = new StoreQuery();
query.contentFilters.push(new ContentFilter('ABC'));
query.contentFilters.push(new ContentFilter(myContentTopic));
const queryResult = await storeQuery(
query,
'16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm'
);
console.log(queryResult);
// DNS Discovery
console.log('Retrieving Nodes using DNS Discovery');
const dnsDiscoveryResult = await dnsDiscovery(
'enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@test.waku.nodes.status.im',
'1.1.1.1'
);
console.log(dnsDiscoveryResult);
// USING FILTER INSTEAD OF RELAY:
// Instantiate the node passing these parameters:
// let config = new Config();

View File

@ -21,11 +21,11 @@
},
"..": {
"name": "@waku/react-native",
"version": "0.0.12",
"version": "0.2.0",
"license": "MIT",
"dependencies": {
"base-64": "^1.0.0",
"big-integer": "^1.6.51"
"big-integer": "^1.6.51",
"buffer": "^6.0.3"
},
"devDependencies": {
"@arkweid/lefthook": "^0.7.7",
@ -14055,8 +14055,8 @@
"@types/jest": "^28.1.2",
"@types/react": "~17.0.21",
"@types/react-native": "0.68.0",
"base-64": "^1.0.0",
"big-integer": "^1.6.51",
"buffer": "^6.0.3",
"commitlint": "^17.0.2",
"eslint": "^8.4.1",
"eslint-config-prettier": "^8.5.0",

View File

@ -1 +1 @@
0.3.1
0.5.1

View File

@ -146,5 +146,11 @@ RCT_EXTERN_METHOD(filterUnsubscribe:(NSString *)filterJSON
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
RCT_EXTERN_METHOD(dnsDiscovery:(NSString *)url
withNameserver:(NSString *)nameserver
withMs:(nonnull NSNumber *)ms
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
@end

View File

@ -177,4 +177,9 @@ class ReactNative: RCTEventEmitter {
resolve(GowakuFilterUnsubscribe(filterJSON, ms))
}
@objc(dnsDiscovery:withNameserver:withMs:withResolver:withRejecter:)
func dnsDiscovery(url: String, nameserver: String, ms: Int, resolve:RCTPromiseResolveBlock,reject:RCTPromiseRejectBlock) -> Void {
resolve(GowakuDnsDiscovery(url, nameserver, ms))
}
}

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@waku/react-native",
"version": "0.1.0",
"version": "0.2.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@waku/react-native",
"version": "0.1.0",
"version": "0.2.0",
"license": "MIT",
"dependencies": {
"big-integer": "^1.6.51",

View File

@ -1,6 +1,6 @@
{
"name": "@waku/react-native",
"version": "0.1.0",
"version": "0.2.0",
"description": "Waku React Native",
"author": "Status Research & Development GMBH",
"authors": [
@ -38,6 +38,7 @@
"prepare": "bob build",
"release": "release-it",
"example": "yarn --cwd example",
"pods": "npx pod-install",
"bootstrap": "yarn example && yarn && yarn example pods"
},
"keywords": [

View File

@ -56,20 +56,25 @@ export function onMessage(cb: (arg0: any) => void) {
: undefined;
signal.event.wakuMessage.version = msg.version || 0;
signal.event.wakuMessage.contentTopic = msg.contentTopic;
signal.event.wakuMessage.payload = Buffer.from(msg.payload, 'base64');
signal.event.wakuMessage.payload = Buffer.from(msg.payload || [], 'base64');
cb(signal.event);
});
}
export class Config {
host: String | null = null;
port: Number | null = null;
advertiseAddr: String | null = null;
nodeKey: String | null = null;
keepAliveInterval: Number | null = null;
relay: Boolean | null = null;
filter: Boolean | null = null;
host: String | null = null; // IP address. Default 0.0.0.0
port: Number | null = null; // TCP port to listen. Default 60000. Use 0 for random
advertiseAddr: String | null = null; // Advertise custom multiaddress
nodeKey: String | null = null; // secp256k1 private key. Default random
keepAliveInterval: Number | null = null; // interval in seconds to ping all peers
relay: Boolean | null = null; // enable waku relay
relayTopics: Array<String> = []; // array of pubsub topics that WakuRelay will automatically subscribe to when the node starts
minPeersToPublish: Number | null = null;
filter: Boolean | null = null; // enable waku filter
discv5: Boolean | null = null; // enable discv5
discV5BootstrapNodes: Array<String> = []; // array of bootstrap nodes ENR
discV5UDPPort: Number | null = null; // UDP port for DiscoveryV5
logLevel: String | null = null; // Set the log level. Default `INFO`. Allowed values "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL"
}
/**
@ -654,6 +659,45 @@ export function peers(): Promise<Array<Peer>> {
});
}
export class DiscoveredNode {
peerID: String = '';
addrs: Array<String> = Array();
enr: String = '';
constructor(
peerID: String,
addrs: Array<String>,
enr: String
) {
this.peerID = peerID;
this.addrs = addrs;
this.enr = enr;
}
}
/**
* Use DNS Discovery to retrieve a list of nodes from an enrtree:// URL
* @return List of Nodes
*/
export function dnsDiscovery(
url: String,
nameserver: String,
timeoutMs: Number = 0
): Promise<Array<DiscoveredNode>> {
return new Promise<Array<DiscoveredNode>>(async (resolve, reject) => {
let response = JSON.parse(await ReactNative.dnsDiscovery(url, nameserver, timeoutMs));
if (response.error) {
reject(response.error);
} else {
resolve(
response.result.map(
(x: any) => new DiscoveredNode(x.peerID, x.multiaddrs, x.enr)
)
);
}
});
}
export class Index {
digest: Uint8Array = new Uint8Array();
receiverTime: Number = 0;