2022-07-25 15:28:17 +00:00
|
|
|
// Set of functions that are exported when go-waku is built as a static/dynamic library
|
2022-03-21 23:15:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
|
2022-04-12 12:12:14 +00:00
|
|
|
mobile "github.com/status-im/go-waku/mobile"
|
2022-03-21 23:15:53 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {}
|
|
|
|
|
|
|
|
// Initialize a waku node. Receives a JSON string containing the configuration
|
|
|
|
// for the node. It can be NULL. Example configuration:
|
|
|
|
// ```
|
|
|
|
// {"host": "0.0.0.0", "port": 60000, "advertiseAddr": "1.2.3.4", "nodeKey": "0x123...567", "keepAliveInterval": 20, "relay": true}
|
|
|
|
// ```
|
|
|
|
// All keys are optional. If not specified a default value will be set:
|
|
|
|
// - host: IP address. Default 0.0.0.0
|
|
|
|
// - port: TCP port to listen. Default 60000. Use 0 for random
|
|
|
|
// - advertiseAddr: External IP
|
|
|
|
// - nodeKey: secp256k1 private key. Default random
|
|
|
|
// - keepAliveInterval: interval in seconds to ping all peers
|
|
|
|
// - relay: Enable WakuRelay. Default `true`
|
2022-05-06 18:13:10 +00:00
|
|
|
// - minPeersToPublish: The minimum number of peers required on a topic to allow broadcasting a message. Default `0`
|
2022-09-07 19:24:35 +00:00
|
|
|
// - filter: Enable Filter. Default `false`
|
2022-10-20 15:14:46 +00:00
|
|
|
// - logLevel: Set the log level. Default `INFO`. Allowed values "DEBUG", "INFO", "WARN", "ERROR", "DPANIC", "PANIC", "FATAL"
|
|
|
|
//
|
|
|
|
//export waku_new
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_new(configJSON *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.NewNode(C.GoString(configJSON))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Starts the waku node
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_start
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_start() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.Start()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stops a waku node
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_stop
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_stop() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.Stop()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 18:23:26 +00:00
|
|
|
// Determine is a node is started or not
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_is_started
|
2022-07-26 18:23:26 +00:00
|
|
|
func waku_is_started() *C.char {
|
|
|
|
response := mobile.IsStarted()
|
|
|
|
return C.CString(response)
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:15:53 +00:00
|
|
|
// Obtain the peer ID of the waku node
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_peerid
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_peerid() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.PeerID()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Obtain the multiaddresses the wakunode is listening to
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_listen_addresses
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_listen_addresses() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.ListenAddresses()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add node multiaddress and protocol to the wakunode peerstore
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_add_peer
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_add_peer(address *C.char, protocolID *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.AddPeer(C.GoString(address), C.GoString(protocolID))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
// Connect to peer at multiaddress. if ms > 0, cancel the function execution if it takes longer than N milliseconds
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_connect
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_connect(address *C.char, ms C.int) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.Connect(C.GoString(address), int(ms))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
// Connect to known peer by peerID. if ms > 0, cancel the function execution if it takes longer than N milliseconds
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_connect_peerid
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_connect_peerid(peerID *C.char, ms C.int) *C.char {
|
2022-10-10 01:14:28 +00:00
|
|
|
response := mobile.ConnectPeerID(C.GoString(peerID), int(ms))
|
2022-04-12 12:12:14 +00:00
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close connection to a known peer by peerID
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_disconnect
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_disconnect(peerID *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.Disconnect(C.GoString(peerID))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get number of connected peers
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_peer_cnt
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_peer_cnt() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.PeerCnt()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a content topic string according to RFC 23
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_content_topic
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_content_topic(applicationName *C.char, applicationVersion C.uint, contentTopicName *C.char, encoding *C.char) *C.char {
|
2022-03-22 16:30:14 +00:00
|
|
|
return C.CString(protocol.NewContentTopic(C.GoString(applicationName), uint(applicationVersion), C.GoString(contentTopicName), C.GoString(encoding)).String())
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a pubsub topic string according to RFC 23
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_pubsub_topic
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_pubsub_topic(name *C.char, encoding *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
return C.CString(mobile.PubsubTopic(C.GoString(name), C.GoString(encoding)))
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the default pubsub topic used in waku2: /waku/2/default-waku/proto
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_default_pubsub_topic
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_default_pubsub_topic() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
return C.CString(mobile.DefaultPubsubTopic())
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 18:13:10 +00:00
|
|
|
// Register callback to act as signal handler and receive application signals
|
|
|
|
// (in JSON) which are used to react to asynchronous events in waku. The function
|
2022-03-21 23:15:53 +00:00
|
|
|
// signature for the callback should be `void myCallback(char* signalJSON)`
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_set_event_callback
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_set_event_callback(cb unsafe.Pointer) {
|
2022-04-12 12:12:14 +00:00
|
|
|
mobile.SetEventCallback(cb)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 16:30:14 +00:00
|
|
|
// Retrieve the list of peers known by the waku node
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_peers
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_peers() *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.Peers()
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
// Decode a waku message using a 32 bytes symmetric key. The key must be a hex encoded string with "0x" prefix
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_decode_symmetric
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_decode_symmetric(messageJSON *C.char, symmetricKey *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.DecodeSymmetric(C.GoString(messageJSON), C.GoString(symmetricKey))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-03 00:22:42 +00:00
|
|
|
// Decode a waku message using a secp256k1 private key. The key must be a hex encoded string with "0x" prefix
|
2022-10-20 15:14:46 +00:00
|
|
|
//
|
|
|
|
//export waku_decode_asymmetric
|
2022-04-03 00:22:42 +00:00
|
|
|
func waku_decode_asymmetric(messageJSON *C.char, privateKey *C.char) *C.char {
|
2022-04-12 12:12:14 +00:00
|
|
|
response := mobile.DecodeAsymmetric(C.GoString(messageJSON), C.GoString(privateKey))
|
|
|
|
return C.CString(response)
|
2022-03-21 23:15:53 +00:00
|
|
|
}
|