2019-07-31 08:05:22 +00:00
|
|
|
#
|
2019-09-12 16:32:07 +00:00
|
|
|
# Nimbus
|
2019-07-31 08:05:22 +00:00
|
|
|
# (c) Copyright 2018
|
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
import
|
2019-09-12 16:32:07 +00:00
|
|
|
chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash], tables,
|
2019-11-04 12:03:28 +00:00
|
|
|
stew/ranges/ptr_arith, eth/[keys, rlp, p2p, async_utils],
|
|
|
|
eth/p2p/rlpx_protocols/whisper_protocol,
|
|
|
|
eth/p2p/[enode, peer_pool, bootnodes, whispernodes]
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
# TODO: If we really want/need this type of API for the keys, put it somewhere
|
|
|
|
# seperate as it is the same code for Whisper RPC
|
|
|
|
type
|
|
|
|
WhisperKeys* = ref object
|
|
|
|
asymKeys*: Table[string, KeyPair]
|
|
|
|
symKeys*: Table[string, SymKey]
|
|
|
|
|
|
|
|
proc newWhisperKeys*(): WhisperKeys =
|
|
|
|
new(result)
|
|
|
|
result.asymKeys = initTable[string, KeyPair]()
|
|
|
|
result.symKeys = initTable[string, SymKey]()
|
|
|
|
|
|
|
|
# TODO: again, lots of overlap with Nimbus whisper RPC here, however not all
|
|
|
|
# the same due to type conversion (no use of Option and such)
|
2019-07-31 08:05:22 +00:00
|
|
|
type
|
|
|
|
CReceivedMessage* = object
|
|
|
|
decoded*: ptr byte
|
|
|
|
decodedLen*: csize
|
2019-09-12 16:32:07 +00:00
|
|
|
source*: PublicKey
|
2019-10-25 15:31:42 +00:00
|
|
|
recipientPublicKey*: PublicKey
|
2019-07-31 08:05:22 +00:00
|
|
|
timestamp*: uint32
|
|
|
|
ttl*: uint32
|
|
|
|
topic*: Topic
|
|
|
|
pow*: float64
|
|
|
|
hash*: Hash
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
CFilterOptions* = object
|
|
|
|
symKeyID*: cstring
|
|
|
|
privateKeyID*: cstring
|
2019-10-25 15:31:42 +00:00
|
|
|
source*: ptr PublicKey
|
2019-09-12 16:32:07 +00:00
|
|
|
minPow*: float64
|
|
|
|
topic*: Topic # lets go with one topic for now
|
2019-10-25 15:31:42 +00:00
|
|
|
allowP2P*: bool
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
CPostMessage* = object
|
|
|
|
symKeyID*: cstring
|
2019-10-25 15:31:42 +00:00
|
|
|
pubKey*: ptr PublicKey
|
|
|
|
sourceID*: cstring
|
2019-09-12 16:32:07 +00:00
|
|
|
ttl*: uint32
|
|
|
|
topic*: Topic
|
2019-11-04 12:03:28 +00:00
|
|
|
payload*: ptr byte
|
|
|
|
payloadLen*: csize
|
|
|
|
padding*: ptr byte
|
|
|
|
paddingLen*: csize
|
2019-09-12 16:32:07 +00:00
|
|
|
powTime*: float64
|
|
|
|
powTarget*: float64
|
|
|
|
|
|
|
|
CTopic* = object
|
|
|
|
topic*: Topic
|
|
|
|
|
2019-07-31 08:05:22 +00:00
|
|
|
proc `$`*(digest: SymKey): string =
|
|
|
|
for c in digest: result &= hexChar(c.byte)
|
|
|
|
|
|
|
|
# Don't do this at home, you'll never get rid of ugly globals like this!
|
|
|
|
var
|
|
|
|
node: EthereumNode
|
2019-09-12 16:32:07 +00:00
|
|
|
# You will only add more instead!
|
|
|
|
let whisperKeys = newWhisperKeys()
|
2019-07-31 08:05:22 +00:00
|
|
|
|
|
|
|
# TODO: Return filter ID if we ever want to unsubscribe
|
|
|
|
proc subscribeChannel(
|
|
|
|
channel: string, handler: proc (msg: ReceivedMessage) {.gcsafe.}) =
|
|
|
|
var ctx: HMAC[sha256]
|
|
|
|
var symKey: SymKey
|
|
|
|
discard ctx.pbkdf2(channel, "", 65356, symKey)
|
|
|
|
|
|
|
|
let channelHash = digest(keccak256, channel)
|
|
|
|
var topic: array[4, byte]
|
|
|
|
for i in 0..<4:
|
|
|
|
topic[i] = channelHash.data[i]
|
|
|
|
|
|
|
|
info "Subscribing to channel", channel, topic, symKey
|
|
|
|
|
|
|
|
discard node.subscribeFilter(newFilter(symKey = some(symKey),
|
2019-09-12 17:27:43 +00:00
|
|
|
topics = @[topic]),
|
|
|
|
handler)
|
2019-07-31 08:05:22 +00:00
|
|
|
|
|
|
|
# proc handler(msg: ReceivedMessage) {.gcsafe.} =
|
|
|
|
# try:
|
|
|
|
# # ["~#c4",["dcasdc","text/plain","~:public-group-user-message",
|
|
|
|
# # 154604971756901,1546049717568,[
|
|
|
|
# # "^ ","~:chat-id","nimbus-test","~:text","dcasdc"]]]
|
|
|
|
# let
|
|
|
|
# src =
|
|
|
|
# if msg.decoded.src.isSome(): $msg.decoded.src.get()
|
|
|
|
# else: ""
|
|
|
|
# payload = cast[string](msg.decoded.payload)
|
|
|
|
# data = parseJson(cast[string](msg.decoded.payload))
|
|
|
|
# channel = data.elems[1].elems[5].elems[2].str
|
|
|
|
# time = $fromUnix(data.elems[1].elems[4].num div 1000)
|
|
|
|
# message = data.elems[1].elems[0].str
|
|
|
|
|
|
|
|
# info "adding", full=(cast[string](msg.decoded.payload))
|
|
|
|
# except:
|
|
|
|
# notice "no luck parsing", message=getCurrentExceptionMsg()
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
proc nimbus_start(port: uint16, startListening: bool, enableDiscovery: bool,
|
2019-11-04 15:34:02 +00:00
|
|
|
minPow: float64, privateKey: ptr byte): bool {.exportc.} =
|
2019-07-31 08:05:22 +00:00
|
|
|
let address = Address(
|
|
|
|
udpPort: port.Port, tcpPort: port.Port, ip: parseIpAddress("0.0.0.0"))
|
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
var keypair: KeyPair
|
|
|
|
if privateKey.isNil:
|
|
|
|
keypair = newKeyPair()
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
let privKey = initPrivateKey(makeOpenArray(privateKey, 32))
|
|
|
|
keypair = KeyPair(seckey: privKey, pubkey: privKey.getPublicKey())
|
|
|
|
except EthKeysException:
|
|
|
|
error "Passed an invalid privateKey"
|
|
|
|
return false
|
|
|
|
|
|
|
|
node = newEthereumNode(keypair, address, 1, nil, addAllCapabilities = false)
|
2019-07-31 08:05:22 +00:00
|
|
|
node.addCapability Whisper
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
node.protocolState(Whisper).config.powRequirement = minPow
|
2019-10-28 11:56:39 +00:00
|
|
|
# TODO: should we start the node with an empty bloomfilter?
|
|
|
|
# var bloom: Bloom
|
|
|
|
# node.protocolState(Whisper).config.bloom = bloom
|
2019-07-31 08:05:22 +00:00
|
|
|
|
|
|
|
var bootnodes: seq[ENode] = @[]
|
|
|
|
for nodeId in MainnetBootnodes:
|
|
|
|
var bootnode: ENode
|
|
|
|
discard initENode(nodeId, bootnode)
|
|
|
|
bootnodes.add(bootnode)
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
traceAsyncErrors node.connectToNetwork(bootnodes, startListening, enableDiscovery)
|
2019-07-31 08:05:22 +00:00
|
|
|
# main network has mostly non SHH nodes, so we connect directly to SHH nodes
|
|
|
|
for nodeId in WhisperNodes:
|
|
|
|
var whisperENode: ENode
|
|
|
|
discard initENode(nodeId, whisperENode)
|
|
|
|
var whisperNode = newNode(whisperENode)
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
traceAsyncErrors node.peerPool.connectToNode(whisperNode)
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
result = true
|
|
|
|
|
2019-07-31 08:05:22 +00:00
|
|
|
proc nimbus_poll() {.exportc.} =
|
|
|
|
poll()
|
|
|
|
|
2019-09-12 17:27:43 +00:00
|
|
|
proc nimbus_join_public_chat(channel: cstring,
|
|
|
|
handler: proc (msg: ptr CReceivedMessage)
|
|
|
|
{.gcsafe, cdecl.}) {.exportc.} =
|
2019-07-31 08:05:22 +00:00
|
|
|
if handler.isNil:
|
|
|
|
subscribeChannel($channel, nil)
|
|
|
|
else:
|
|
|
|
proc c_handler(msg: ReceivedMessage) =
|
|
|
|
var cmsg = CReceivedMessage(
|
|
|
|
decoded: unsafeAddr msg.decoded.payload[0],
|
|
|
|
decodedLen: csize msg.decoded.payload.len(),
|
|
|
|
timestamp: msg.timestamp,
|
|
|
|
ttl: msg.ttl,
|
|
|
|
topic: msg.topic,
|
|
|
|
pow: msg.pow,
|
|
|
|
hash: msg.hash
|
|
|
|
)
|
|
|
|
|
|
|
|
handler(addr cmsg)
|
|
|
|
|
|
|
|
subscribeChannel($channel, c_handler)
|
|
|
|
|
|
|
|
# TODO: Add signing key as parameter
|
|
|
|
# TODO: How would we do key management? In nimbus (like in rpc) or in status go?
|
2019-09-12 17:27:43 +00:00
|
|
|
proc nimbus_post_public(channel: cstring, payload: cstring) {.exportc.} =
|
2019-07-31 08:05:22 +00:00
|
|
|
let encPrivateKey = initPrivateKey("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3")
|
|
|
|
|
|
|
|
var ctx: HMAC[sha256]
|
|
|
|
var symKey: SymKey
|
|
|
|
var npayload = cast[Bytes]($payload)
|
|
|
|
discard ctx.pbkdf2($channel, "", 65356, symKey)
|
|
|
|
|
|
|
|
let channelHash = digest(keccak256, $channel)
|
|
|
|
var topic: array[4, byte]
|
|
|
|
for i in 0..<4:
|
|
|
|
topic[i] = channelHash.data[i]
|
|
|
|
|
|
|
|
# TODO: Handle error case
|
|
|
|
discard node.postMessage(symKey = some(symKey),
|
|
|
|
src = some(encPrivateKey),
|
|
|
|
ttl = 20,
|
|
|
|
topic = topic,
|
|
|
|
payload = npayload,
|
|
|
|
powTarget = 0.002)
|
|
|
|
|
|
|
|
proc nimbus_add_peer(nodeId: cstring) {.exportc.} =
|
|
|
|
var whisperENode: ENode
|
|
|
|
discard initENode($nodeId, whisperENode)
|
|
|
|
var whisperNode = newNode(whisperENode)
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
traceAsyncErrors node.peerPool.connectToNode(whisperNode)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
# Whisper API (Similar to Whisper RPC API)
|
|
|
|
# Mostly an example for now, lots of things to fix if continued like this.
|
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
proc nimbus_channel_to_topic(channel: cstring): CTopic {.exportc.} =
|
|
|
|
let hash = digest(keccak256, $channel)
|
2019-09-12 16:32:07 +00:00
|
|
|
for i in 0..<4:
|
|
|
|
result.topic[i] = hash.data[i]
|
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
# Asymmetric Keys API
|
2019-09-12 17:27:43 +00:00
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_new_keypair(): cstring {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
## It is important that the caller makes a copy of the returned cstring before
|
2019-10-31 14:38:27 +00:00
|
|
|
## doing any other API calls. This might not hold for all types of GC.
|
2019-09-12 16:32:07 +00:00
|
|
|
result = generateRandomID()
|
|
|
|
whisperKeys.asymKeys.add($result, newKeyPair())
|
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
proc nimbus_add_keypair(privateKey: ptr byte):
|
2019-10-30 13:23:30 +00:00
|
|
|
cstring {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
## It is important that the caller makes a copy of the returned cstring before
|
2019-10-31 14:38:27 +00:00
|
|
|
## doing any other API calls. This might not hold for all types of GC.
|
2019-11-04 15:34:02 +00:00
|
|
|
doAssert(not privateKey.isNil, "Passed a null pointer as privateKey")
|
2019-09-12 17:27:43 +00:00
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
var privKey: PrivateKey
|
|
|
|
try:
|
|
|
|
privKey = initPrivateKey(makeOpenArray(privateKey, 32))
|
|
|
|
except EthKeysException:
|
|
|
|
error "Passed an invalid privateKey"
|
|
|
|
return ""
|
|
|
|
|
|
|
|
result = generateRandomID()
|
2019-10-25 12:50:14 +00:00
|
|
|
# Creating a KeyPair here does a copy of the key and so does the add
|
2019-11-04 15:34:02 +00:00
|
|
|
whisperKeys.asymKeys.add($result, KeyPair(seckey: privKey,
|
|
|
|
pubkey: privKey.getPublicKey()))
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_delete_keypair(id: cstring): bool {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
var unneeded: KeyPair
|
|
|
|
result = whisperKeys.asymKeys.take($id, unneeded)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
proc nimbus_get_private_key(id: cstring, privateKey: ptr PrivateKey):
|
2019-10-30 13:23:30 +00:00
|
|
|
bool {.exportc.} =
|
2019-11-04 15:34:02 +00:00
|
|
|
doAssert(not privateKey.isNil, "Passed a null pointer as privateKey")
|
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
try:
|
|
|
|
privateKey[] = whisperKeys.asymkeys[$id].seckey
|
|
|
|
result = true
|
|
|
|
except KeyError:
|
2019-11-04 15:34:02 +00:00
|
|
|
error "Private key not found"
|
2019-10-25 12:50:14 +00:00
|
|
|
result = false
|
|
|
|
|
|
|
|
# Symmetric Keys API
|
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
proc nimbus_add_symkey(symKey: ptr SymKey): cstring {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
## It is important that the caller makes a copy of the returned cstring before
|
2019-10-31 14:38:27 +00:00
|
|
|
## doing any other API calls. This might not hold for all types of GC.
|
2019-11-04 15:34:02 +00:00
|
|
|
doAssert(not symKey.isNil, "Passed a null pointer as symKey")
|
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
result = generateRandomID().cstring
|
|
|
|
|
|
|
|
# Copy of key happens at add
|
2019-11-04 15:34:02 +00:00
|
|
|
whisperKeys.symKeys.add($result, symKey[])
|
2019-10-25 12:50:14 +00:00
|
|
|
|
|
|
|
proc nimbus_add_symkey_from_password(password: cstring):
|
2019-10-30 13:23:30 +00:00
|
|
|
cstring {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
## It is important that the caller makes a copy of the returned cstring before
|
2019-10-31 14:38:27 +00:00
|
|
|
## doing any other API calls. This might not hold for all types of GC.
|
2019-09-12 16:32:07 +00:00
|
|
|
var ctx: HMAC[sha256]
|
|
|
|
var symKey: SymKey
|
|
|
|
if pbkdf2(ctx, $password, "", 65356, symKey) != sizeof(SymKey):
|
2019-10-25 12:50:14 +00:00
|
|
|
return nil # TODO: Something else than nil? And, can this practically occur?
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
result = generateRandomID()
|
|
|
|
|
|
|
|
whisperKeys.symKeys.add($result, symKey)
|
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_delete_symkey(id: cstring): bool {.exportc.} =
|
2019-10-25 12:50:14 +00:00
|
|
|
var unneeded: SymKey
|
|
|
|
result = whisperKeys.symKeys.take($id, unneeded)
|
|
|
|
|
|
|
|
proc nimbus_get_symkey(id: cstring, symKey: ptr SymKey):
|
2019-10-30 13:23:30 +00:00
|
|
|
bool {.exportc.} =
|
2019-11-04 15:34:02 +00:00
|
|
|
doAssert(not symKey.isNil, "Passed a null pointer as symKey")
|
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
try:
|
|
|
|
symKey[] = whisperKeys.symkeys[$id]
|
|
|
|
result = true
|
|
|
|
except KeyError:
|
|
|
|
result = false
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
# Whisper message posting and receiving API
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_post(message: ptr CPostMessage): bool {.exportc.} =
|
2019-11-04 12:03:28 +00:00
|
|
|
## Encryption is mandatory.
|
2019-11-04 15:34:02 +00:00
|
|
|
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
2019-11-04 12:03:28 +00:00
|
|
|
## Providing a payload is mandatory, it cannot be nil, but can be of length 0.
|
2019-09-12 16:32:07 +00:00
|
|
|
var
|
|
|
|
sigPrivKey: Option[PrivateKey]
|
|
|
|
asymKey: Option[PublicKey]
|
|
|
|
symKey: Option[SymKey]
|
|
|
|
padding: Option[Bytes]
|
|
|
|
payload: Bytes
|
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
if not message.pubKey.isNil() and not message.symKeyID.isNil():
|
|
|
|
warn "Both symmetric and asymmetric keys are provided, choose one."
|
|
|
|
return false
|
|
|
|
|
|
|
|
if message.pubKey.isNil() and message.symKeyID.isNil():
|
|
|
|
warn "Both symmetric and asymmetric keys are nil, provide one."
|
|
|
|
return false
|
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
if not message.pubKey.isNil():
|
|
|
|
asymKey = some(message.pubKey[])
|
|
|
|
|
|
|
|
try:
|
|
|
|
if not message.symKeyID.isNil():
|
|
|
|
symKey = some(whisperKeys.symKeys[$message.symKeyID])
|
|
|
|
if not message.sourceID.isNil():
|
|
|
|
sigPrivKey = some(whisperKeys.asymKeys[$message.sourceID].seckey)
|
|
|
|
except KeyError:
|
2019-11-04 12:03:28 +00:00
|
|
|
warn "No key found with provided key ID."
|
2019-10-25 15:31:42 +00:00
|
|
|
return false
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
if not message.payload.isNil():
|
2019-11-04 12:03:28 +00:00
|
|
|
# This will make a copy
|
|
|
|
payload = @(makeOpenArray(message.payload, message.payloadLen))
|
2019-10-25 15:31:42 +00:00
|
|
|
else:
|
2019-11-04 12:03:28 +00:00
|
|
|
warn "Message payload was nil, post aborted."
|
2019-10-25 15:31:42 +00:00
|
|
|
return false
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
if not message.padding.isNil():
|
2019-11-04 12:03:28 +00:00
|
|
|
# This will make a copy
|
|
|
|
padding = some(@(makeOpenArray(message.padding, message.paddingLen)))
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
result = node.postMessage(asymKey,
|
|
|
|
symKey,
|
|
|
|
sigPrivKey,
|
|
|
|
ttl = message.ttl,
|
|
|
|
topic = message.topic,
|
|
|
|
payload = payload,
|
|
|
|
padding = padding,
|
|
|
|
powTime = message.powTime,
|
|
|
|
powTarget = message.powTarget)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
2019-10-30 11:18:11 +00:00
|
|
|
handler: proc (msg: ptr CReceivedMessage, udata: pointer) {.gcsafe, cdecl.},
|
2019-10-30 13:23:30 +00:00
|
|
|
udata: pointer = nil): cstring {.exportc.} =
|
2019-11-04 15:34:02 +00:00
|
|
|
## Encryption is mandatory.
|
|
|
|
## A symmetric key or an asymmetric key must be provided. Both is not allowed.
|
2019-10-25 15:31:42 +00:00
|
|
|
## In case of a passed handler, the received msg needs to be copied before the
|
|
|
|
## handler ends.
|
2019-10-30 12:20:31 +00:00
|
|
|
var
|
|
|
|
src: Option[PublicKey]
|
|
|
|
symKey: Option[SymKey]
|
|
|
|
privateKey: Option[PrivateKey]
|
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
if not options.privateKeyID.isNil() and not options.symKeyID.isNil():
|
|
|
|
warn "Both symmetric and asymmetric keys are provided, choose one."
|
|
|
|
return ""
|
|
|
|
|
|
|
|
if options.privateKeyID.isNil() and options.symKeyID.isNil():
|
|
|
|
warn "Both symmetric and asymmetric keys are nil, provide one."
|
|
|
|
return ""
|
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
if not options.source.isNil():
|
2019-10-30 12:20:31 +00:00
|
|
|
src = some(options.source[])
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
try:
|
|
|
|
if not options.symKeyID.isNil():
|
2019-10-30 12:20:31 +00:00
|
|
|
symKey = some(whisperKeys.symKeys[$options.symKeyID])
|
2019-10-25 15:31:42 +00:00
|
|
|
if not options.privateKeyID.isNil():
|
2019-10-30 12:20:31 +00:00
|
|
|
privateKey = some(whisperKeys.asymKeys[$options.privateKeyID].seckey)
|
2019-10-25 15:31:42 +00:00
|
|
|
except KeyError:
|
2019-11-04 15:34:02 +00:00
|
|
|
return ""
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-30 12:20:31 +00:00
|
|
|
let filter = newFilter(src, privateKey, symKey, @[options.topic],
|
|
|
|
options.minPow, options.allowP2P)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
if handler.isNil:
|
2019-10-25 15:31:42 +00:00
|
|
|
result = node.subscribeFilter(filter, nil)
|
|
|
|
else:
|
|
|
|
proc c_handler(msg: ReceivedMessage) {.gcsafe.} =
|
|
|
|
var cmsg = CReceivedMessage(
|
|
|
|
decoded: unsafeAddr msg.decoded.payload[0],
|
|
|
|
decodedLen: csize msg.decoded.payload.len(),
|
|
|
|
timestamp: msg.timestamp,
|
|
|
|
ttl: msg.ttl,
|
|
|
|
topic: msg.topic,
|
|
|
|
pow: msg.pow,
|
|
|
|
hash: msg.hash
|
|
|
|
)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
# TODO: change this to ptr, so that C/go code can check on nil?
|
|
|
|
if msg.decoded.src.isSome():
|
|
|
|
cmsg.source = msg.decoded.src.get()
|
|
|
|
if msg.dst.isSome():
|
2019-11-04 12:03:28 +00:00
|
|
|
cmsg.recipientPublicKey = msg.dst.get()
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-30 11:18:11 +00:00
|
|
|
handler(addr cmsg, udata)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
result = node.subscribeFilter(filter, c_handler)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-28 11:56:39 +00:00
|
|
|
# Bloom filter has to follow only the subscribed topics
|
|
|
|
# TODO: better to have an "adding" proc here
|
|
|
|
traceAsyncErrors node.setBloomFilter(node.filtersToBloom())
|
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_unsubscribe_filter(id: cstring): bool {.exportc.} =
|
2019-10-25 15:31:42 +00:00
|
|
|
result = node.unsubscribeFilter($id)
|
2019-10-28 11:54:31 +00:00
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_get_min_pow(): float64 {.exportc.} =
|
2019-10-28 11:54:31 +00:00
|
|
|
result = node.protocolState(Whisper).config.powRequirement
|
|
|
|
|
2019-10-30 13:23:30 +00:00
|
|
|
proc nimbus_get_bloom_filter(bloom: ptr Bloom) {.exportc.} =
|
2019-10-28 11:54:31 +00:00
|
|
|
bloom[] = node.protocolState(Whisper).config.bloom
|