From 82fdfee61fbe82acd346b3ebe68bea80b01f11f1 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Mon, 20 Feb 2023 19:05:46 -0400 Subject: [PATCH] feat: expose DNS Discovery and DiscV5 --- .../com/wakureactnative/ReactNativeModule.kt | 4 ++ download-gowaku.sh | 6 +- example/App.js | 21 +++++-- example/package-lock.json | 8 +-- go-waku.VERSION | 2 +- ios/ReactNative.m | 6 ++ ios/ReactNative.swift | 5 ++ package-lock.json | 4 +- package.json | 3 +- src/index.tsx | 60 ++++++++++++++++--- 10 files changed, 96 insertions(+), 23 deletions(-) diff --git a/android/src/main/java/com/wakureactnative/ReactNativeModule.kt b/android/src/main/java/com/wakureactnative/ReactNativeModule.kt index 3741708..7d295bc 100644 --- a/android/src/main/java/com/wakureactnative/ReactNativeModule.kt +++ b/android/src/main/java/com/wakureactnative/ReactNativeModule.kt @@ -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())) + } } diff --git a/download-gowaku.sh b/download-gowaku.sh index dc16e76..ec8b782 100755 --- a/download-gowaku.sh +++ b/download-gowaku.sh @@ -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" diff --git a/example/App.js b/example/App.js index d438bba..5605a69 100644 --- a/example/App.js +++ b/example/App.js @@ -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(); diff --git a/example/package-lock.json b/example/package-lock.json index 69f0dbc..a8c3d0a 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -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", diff --git a/go-waku.VERSION b/go-waku.VERSION index a2268e2..4b9fcbe 100644 --- a/go-waku.VERSION +++ b/go-waku.VERSION @@ -1 +1 @@ -0.3.1 \ No newline at end of file +0.5.1 diff --git a/ios/ReactNative.m b/ios/ReactNative.m index ff674a4..d9f9c6f 100644 --- a/ios/ReactNative.m +++ b/ios/ReactNative.m @@ -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 diff --git a/ios/ReactNative.swift b/ios/ReactNative.swift index 0127332..fd0381e 100644 --- a/ios/ReactNative.swift +++ b/ios/ReactNative.swift @@ -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)) + } + } diff --git a/package-lock.json b/package-lock.json index 4fdddc2..d998aa7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 0b0d45b..22941f6 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/index.tsx b/src/index.tsx index 1814483..262c450 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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 = []; // 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 = []; // 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> { }); } +export class DiscoveredNode { + peerID: String = ''; + addrs: Array = Array(); + enr: String = ''; + + constructor( + peerID: String, + addrs: Array, + 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> { + return new Promise>(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;