2019-07-31 08:05:22 +00:00
|
|
|
#
|
2019-09-12 16:32:07 +00:00
|
|
|
# Nimbus
|
2019-11-07 11:09:30 +00:00
|
|
|
# (c) Copyright 2019
|
2019-07-31 08:05:22 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
|
|
|
|
|
|
|
import
|
2019-11-13 10:41:27 +00:00
|
|
|
chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash, sysrand], 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,
|
2020-07-20 10:03:20 +00:00
|
|
|
eth/p2p/[peer_pool, bootnodes, whispernodes], ../nimbus/rpc/key_storage,
|
|
|
|
../nimbus/random_keys
|
2020-01-20 16:45:49 +00:00
|
|
|
|
|
|
|
# TODO: lots of overlap with Nimbus Whisper RPC here, however not all
|
|
|
|
# the same due to type conversion (no use of Option and such). Perhaps some
|
|
|
|
# parts can be refactored in sharing some of the code.
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
const idLen = 32
|
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
type
|
2019-11-13 10:41:27 +00:00
|
|
|
Identifier = array[idLen, byte]
|
|
|
|
|
2019-07-31 08:05:22 +00:00
|
|
|
CReceivedMessage* = object
|
|
|
|
decoded*: ptr byte
|
2020-07-20 10:03:20 +00:00
|
|
|
decodedLen*: int # csize_t
|
2019-11-06 12:57:59 +00:00
|
|
|
source*: ptr byte
|
|
|
|
recipientPublicKey*: ptr byte
|
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
|
2019-11-13 10:41:27 +00:00
|
|
|
symKeyID*: ptr byte
|
|
|
|
privateKeyID*: ptr byte
|
2019-11-04 16:51:59 +00:00
|
|
|
source*: ptr byte
|
2019-09-12 16:32:07 +00:00
|
|
|
minPow*: float64
|
2019-11-07 11:09:30 +00:00
|
|
|
topic*: Topic # lets go with one topic for now unless more are required
|
2019-10-25 15:31:42 +00:00
|
|
|
allowP2P*: bool
|
2019-09-12 16:32:07 +00:00
|
|
|
|
|
|
|
CPostMessage* = object
|
2019-11-13 10:41:27 +00:00
|
|
|
symKeyID*: ptr byte
|
2019-11-04 16:51:59 +00:00
|
|
|
pubKey*: ptr byte
|
2019-11-13 10:41:27 +00:00
|
|
|
sourceID*: ptr byte
|
2019-09-12 16:32:07 +00:00
|
|
|
ttl*: uint32
|
|
|
|
topic*: Topic
|
2019-11-04 12:03:28 +00:00
|
|
|
payload*: ptr byte
|
2020-07-20 10:03:20 +00:00
|
|
|
payloadLen*: int # csize_t
|
2019-11-04 12:03:28 +00:00
|
|
|
padding*: ptr byte
|
2020-07-20 10:03:20 +00:00
|
|
|
paddingLen*: int # csize_t
|
2019-09-12 16:32:07 +00:00
|
|
|
powTime*: float64
|
|
|
|
powTarget*: float64
|
|
|
|
|
|
|
|
CTopic* = object
|
|
|
|
topic*: Topic
|
|
|
|
|
2019-07-31 08:05:22 +00:00
|
|
|
# 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!
|
2020-01-20 16:45:49 +00:00
|
|
|
let whisperKeys = newKeyStorage()
|
|
|
|
|
|
|
|
proc generateRandomID(): Identifier =
|
|
|
|
while true: # TODO: error instead of looping?
|
|
|
|
if randomBytes(result) == idLen:
|
|
|
|
break
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-05 12:47:47 +00:00
|
|
|
proc setBootNodes(nodes: openArray[string]): seq[ENode] =
|
|
|
|
result = newSeqOfCap[ENode](nodes.len)
|
|
|
|
for nodeId in nodes:
|
|
|
|
# For now we can just do assert as we only pass our own const arrays.
|
2020-04-07 09:53:50 +00:00
|
|
|
let enode = ENode.fromString(nodeId).expect("correct enode")
|
|
|
|
result.add(enode)
|
2019-11-05 12:47:47 +00:00
|
|
|
|
|
|
|
proc connectToNodes(nodes: openArray[string]) =
|
|
|
|
for nodeId in nodes:
|
|
|
|
# For now we can just do assert as we only pass our own const arrays.
|
2020-04-07 09:53:50 +00:00
|
|
|
let enode = ENode.fromString(nodeId).expect("correct enode")
|
2019-11-05 12:47:47 +00:00
|
|
|
|
2020-04-07 09:53:50 +00:00
|
|
|
traceAsyncErrors node.peerPool.connectToNode(newNode(enode))
|
2019-11-05 12:47:47 +00:00
|
|
|
|
2019-11-07 11:09:30 +00:00
|
|
|
# Setting up the node
|
|
|
|
|
2019-11-04 12:56:24 +00:00
|
|
|
proc nimbus_start(port: uint16, startListening: bool, enableDiscovery: bool,
|
2019-11-29 10:08:45 +00:00
|
|
|
minPow: float64, privateKey: ptr byte, staging: bool): bool
|
|
|
|
{.exportc, dynlib.} =
|
2019-11-05 09:20:19 +00:00
|
|
|
# TODO: any async calls can still create `Exception`, why?
|
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:
|
2020-07-20 10:03:20 +00:00
|
|
|
#var kp = KeyPair.random()
|
|
|
|
#if kp.isErr:
|
|
|
|
#error "Can't generate keypair", err = kp.error
|
|
|
|
#return false
|
|
|
|
#keypair = kp[]
|
|
|
|
keypair = randomKeyPair()
|
2019-11-04 15:34:02 +00:00
|
|
|
else:
|
2020-04-05 13:12:48 +00:00
|
|
|
let
|
|
|
|
privKey = PrivateKey.fromRaw(makeOpenArray(privateKey, 32))
|
2020-06-24 13:32:49 +00:00
|
|
|
if privKey.isErr:
|
2019-11-07 11:09:30 +00:00
|
|
|
error "Passed an invalid private key."
|
2019-11-04 15:34:02 +00:00
|
|
|
return false
|
|
|
|
|
2020-06-24 13:32:49 +00:00
|
|
|
keypair = privKey[].toKeyPair()
|
2020-04-05 13:12:48 +00:00
|
|
|
|
2021-02-13 09:32:48 +00:00
|
|
|
node = newEthereumNode(keypair, address, 1.NetworkId, 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
|
|
|
|
2019-11-05 12:47:47 +00:00
|
|
|
let bootnodes = if staging: setBootNodes(StatusBootNodesStaging)
|
|
|
|
else: setBootNodes(StatusBootNodes)
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-05 12:47:47 +00:00
|
|
|
traceAsyncErrors node.connectToNetwork(bootnodes, startListening,
|
|
|
|
enableDiscovery)
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-05 12:47:47 +00:00
|
|
|
# Connect to known Status Whisper fleet directly
|
|
|
|
if staging: connectToNodes(WhisperNodesStaging)
|
|
|
|
else: connectToNodes(WhisperNodes)
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-04 15:34:02 +00:00
|
|
|
result = true
|
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_poll() {.exportc, dynlib.} =
|
2019-07-31 08:05:22 +00:00
|
|
|
poll()
|
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_add_peer(nodeId: cstring): bool {.exportc, dynlib.} =
|
2019-11-05 09:20:19 +00:00
|
|
|
var
|
|
|
|
whisperNode: Node
|
2020-04-07 09:53:50 +00:00
|
|
|
let enode = ENode.fromString($nodeId)
|
|
|
|
if enode.isErr:
|
|
|
|
return false
|
2019-11-05 09:20:19 +00:00
|
|
|
try:
|
2020-04-07 09:53:50 +00:00
|
|
|
whisperNode = newNode(enode[])
|
2020-04-05 13:12:48 +00:00
|
|
|
except CatchableError:
|
2019-11-05 09:20:19 +00:00
|
|
|
return false
|
2019-07-31 08:05:22 +00:00
|
|
|
|
2019-11-05 09:20:19 +00:00
|
|
|
# TODO: call can create `Exception`, why?
|
2019-11-04 12:56:24 +00:00
|
|
|
traceAsyncErrors node.peerPool.connectToNode(whisperNode)
|
2019-11-05 09:20:19 +00:00
|
|
|
result = true
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-07 11:09:30 +00:00
|
|
|
# Whisper API (Similar to Whisper JSON-RPC API)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_channel_to_topic(channel: cstring): CTopic
|
2020-04-05 13:12:48 +00:00
|
|
|
{.exportc, dynlib, raises: [Defect].} =
|
2019-11-07 11:09:30 +00:00
|
|
|
# Only used for the example, to conveniently convert channel to topic.
|
|
|
|
doAssert(not channel.isNil, "Channel cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-11-04 12:03:28 +00:00
|
|
|
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-11-07 11:09:30 +00:00
|
|
|
# Asymmetric Keys
|
2019-09-12 17:27:43 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_new_keypair(id: var Identifier): bool
|
2020-04-05 13:12:48 +00:00
|
|
|
{.exportc, dynlib, raises: [Defect].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
## Caller needs to provide as id a pointer to 32 bytes allocation.
|
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
|
|
|
|
|
|
|
id = generateRandomID()
|
2019-11-05 09:20:19 +00:00
|
|
|
try:
|
2020-07-20 10:03:20 +00:00
|
|
|
whisperKeys.asymKeys.add(id.toHex(), randomKeyPair())
|
2019-11-13 10:41:27 +00:00
|
|
|
result = true
|
2020-04-05 13:12:48 +00:00
|
|
|
except CatchableError:
|
2019-11-05 09:20:19 +00:00
|
|
|
# Don't think this can actually happen, comes from the `getPublicKey` part
|
|
|
|
# in `newKeyPair`
|
2019-11-13 10:41:27 +00:00
|
|
|
discard
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-13 17:34:05 +00:00
|
|
|
proc nimbus_add_keypair(privateKey: ptr byte, id: var Identifier):
|
2020-06-24 13:32:49 +00:00
|
|
|
bool {.exportc, dynlib, raises: [Defect, Exception].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
## Caller needs to provide as id a pointer to 32 bytes allocation.
|
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-07 11:09:30 +00:00
|
|
|
doAssert(not privateKey.isNil, "Private key cannot be nil.")
|
2019-09-12 17:27:43 +00:00
|
|
|
|
2019-11-05 09:20:19 +00:00
|
|
|
var keypair: KeyPair
|
2020-04-05 13:12:48 +00:00
|
|
|
if privateKey.isNil:
|
2020-07-20 10:03:20 +00:00
|
|
|
#var kp = KeyPair.random()
|
|
|
|
#if kp.isErr:
|
|
|
|
#error "Can't generate keypair", err = kp.error
|
|
|
|
#return false
|
|
|
|
#keypair = kp[]
|
|
|
|
keypair = randomKeyPair()
|
2020-04-05 13:12:48 +00:00
|
|
|
else:
|
|
|
|
let
|
|
|
|
privKey = PrivateKey.fromRaw(makeOpenArray(privateKey, 32))
|
2020-06-24 13:32:49 +00:00
|
|
|
if privKey.isErr:
|
2020-04-05 13:12:48 +00:00
|
|
|
error "Passed an invalid private key."
|
|
|
|
return false
|
|
|
|
|
2020-06-24 13:32:49 +00:00
|
|
|
keypair = privKey[].toKeyPair()
|
2019-11-04 15:34:02 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
result = true
|
|
|
|
id = generateRandomID()
|
|
|
|
whisperKeys.asymKeys.add(id.toHex(), keypair)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_delete_keypair(id: Identifier): bool
|
|
|
|
{.exportc, dynlib, raises: [].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
var unneeded: KeyPair
|
2019-11-13 10:41:27 +00:00
|
|
|
result = whisperKeys.asymKeys.take(id.toHex(), unneeded)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-13 10:49:35 +00:00
|
|
|
proc nimbus_get_private_key(id: Identifier, privateKey: var PrivateKey):
|
2020-06-24 13:32:49 +00:00
|
|
|
bool {.exportc, dynlib, raises: [OSError, IOError, ValueError, Exception].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-13 10:49:35 +00:00
|
|
|
doAssert(not (unsafeAddr privateKey).isNil, "Private key cannot be nil.")
|
2019-11-04 15:34:02 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
try:
|
2019-11-13 10:49:35 +00:00
|
|
|
privateKey = whisperKeys.asymkeys[id.toHex()].seckey
|
2019-10-25 12:50:14 +00:00
|
|
|
result = true
|
|
|
|
except KeyError:
|
2019-11-07 11:09:30 +00:00
|
|
|
error "Private key not found."
|
2019-10-25 12:50:14 +00:00
|
|
|
|
2019-11-07 11:09:30 +00:00
|
|
|
# Symmetric Keys
|
2019-10-25 12:50:14 +00:00
|
|
|
|
2019-11-13 17:34:05 +00:00
|
|
|
proc nimbus_add_symkey(symKey: ptr SymKey, id: var Identifier): bool
|
2020-04-21 12:17:08 +00:00
|
|
|
{.exportc, dynlib, raises: [Defect].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
## Caller needs to provide as id a pointer to 32 bytes allocation.
|
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-07 11:09:30 +00:00
|
|
|
doAssert(not symKey.isNil, "Symmetric key cannot be nil.")
|
2019-11-04 15:34:02 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
id = generateRandomID()
|
|
|
|
result = true
|
2019-10-25 12:50:14 +00:00
|
|
|
|
|
|
|
# Copy of key happens at add
|
2019-11-13 10:41:27 +00:00
|
|
|
whisperKeys.symKeys.add(id.toHex, symKey[])
|
2019-10-25 12:50:14 +00:00
|
|
|
|
2019-11-13 17:34:05 +00:00
|
|
|
proc nimbus_add_symkey_from_password(password: cstring, id: var Identifier):
|
2020-04-07 09:53:50 +00:00
|
|
|
bool {.exportc, dynlib, raises: [Defect].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
## Caller needs to provide as id a pointer to 32 bytes allocation.
|
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-07 11:09:30 +00:00
|
|
|
doAssert(not password.isNil, "Password cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
var ctx: HMAC[sha256]
|
|
|
|
var symKey: SymKey
|
|
|
|
if pbkdf2(ctx, $password, "", 65356, symKey) != sizeof(SymKey):
|
2019-11-13 10:41:27 +00:00
|
|
|
return false
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
id = generateRandomID()
|
|
|
|
result = true
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
whisperKeys.symKeys.add(id.toHex(), symKey)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_delete_symkey(id: Identifier): bool
|
2020-04-21 12:17:08 +00:00
|
|
|
{.exportc, dynlib, raises: [Defect].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
var unneeded: SymKey
|
2019-11-13 10:41:27 +00:00
|
|
|
result = whisperKeys.symKeys.take(id.toHex(), unneeded)
|
2019-10-25 12:50:14 +00:00
|
|
|
|
2019-11-13 10:49:35 +00:00
|
|
|
proc nimbus_get_symkey(id: Identifier, symKey: var SymKey):
|
2020-06-24 13:32:49 +00:00
|
|
|
bool {.exportc, dynlib, raises: [Defect, Exception].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-13 10:49:35 +00:00
|
|
|
doAssert(not (unsafeAddr symKey).isNil, "Symmetric key cannot be nil.")
|
2019-11-04 15:34:02 +00:00
|
|
|
|
2019-10-25 12:50:14 +00:00
|
|
|
try:
|
2019-11-13 10:49:35 +00:00
|
|
|
symKey = whisperKeys.symkeys[id.toHex()]
|
2019-10-25 12:50:14 +00:00
|
|
|
result = true
|
|
|
|
except KeyError:
|
2019-11-13 10:41:27 +00:00
|
|
|
error "Symmetric key not found."
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-07 11:09:30 +00:00
|
|
|
# Whisper message posting and receiving
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_post(message: ptr CPostMessage): bool {.exportc, dynlib.} =
|
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-11-07 11:09:30 +00:00
|
|
|
doAssert(not message.isNil, "Message pointer cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-09-12 16:32:07 +00:00
|
|
|
var
|
|
|
|
sigPrivKey: Option[PrivateKey]
|
|
|
|
asymKey: Option[PublicKey]
|
|
|
|
symKey: Option[SymKey]
|
2020-04-20 18:12:44 +00:00
|
|
|
padding: Option[seq[byte]]
|
|
|
|
payload: seq[byte]
|
2019-09-12 16:32:07 +00:00
|
|
|
|
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():
|
2020-04-05 13:12:48 +00:00
|
|
|
let pubkey = PublicKey.fromRaw(makeOpenArray(message.pubKey, 64))
|
|
|
|
if pubkey.isErr:
|
2019-11-07 11:09:30 +00:00
|
|
|
error "Passed an invalid public key for encryption."
|
2019-11-04 16:51:59 +00:00
|
|
|
return false
|
2020-04-05 13:12:48 +00:00
|
|
|
asymKey = some(pubkey[])
|
2019-10-25 15:31:42 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
if not message.symKeyID.isNil():
|
2019-11-13 10:41:27 +00:00
|
|
|
let symKeyId = makeOpenArray(message.symKeyID, idLen).toHex()
|
|
|
|
symKey = some(whisperKeys.symKeys[symKeyId])
|
2019-10-25 15:31:42 +00:00
|
|
|
if not message.sourceID.isNil():
|
2019-11-13 10:41:27 +00:00
|
|
|
let sourceId = makeOpenArray(message.sourceID, idLen).toHex()
|
|
|
|
sigPrivKey = some(whisperKeys.asymKeys[sourceId].seckey)
|
2019-10-25 15:31:42 +00:00
|
|
|
except KeyError:
|
2019-11-07 11:09:30 +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-11-05 09:20:19 +00:00
|
|
|
# TODO: call can create `Exception`, why?
|
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-11-13 17:34:05 +00:00
|
|
|
proc nimbus_subscribe_filter(options: ptr CFilterOptions,
|
2021-04-02 16:45:09 +00:00
|
|
|
handler: proc (msg: ptr CReceivedMessage, udata: pointer)
|
|
|
|
{.gcsafe, cdecl, raises: [Defect].},
|
2019-11-29 10:08:45 +00:00
|
|
|
udata: pointer = nil, id: var Identifier): bool {.exportc, dynlib.} =
|
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-11-18 21:07:08 +00:00
|
|
|
## The received message needs to be copied before the passed handler ends.
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not (unsafeAddr id).isNil, "Key id cannot be nil.")
|
2019-11-07 11:09:30 +00:00
|
|
|
doAssert(not options.isNil, "Filter options pointer cannot be nil.")
|
2019-11-18 21:07:08 +00:00
|
|
|
doAssert(not handler.isNil, "Filter handler cannot be nil." )
|
2019-11-04 16:51:59 +00:00
|
|
|
|
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."
|
2019-11-13 10:41:27 +00:00
|
|
|
return false
|
2019-11-04 15:34:02 +00:00
|
|
|
|
|
|
|
if options.privateKeyID.isNil() and options.symKeyID.isNil():
|
|
|
|
warn "Both symmetric and asymmetric keys are nil, provide one."
|
2019-11-13 10:41:27 +00:00
|
|
|
return false
|
2019-11-04 15:34:02 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
if not options.source.isNil():
|
2020-04-05 13:12:48 +00:00
|
|
|
let pubkey = PublicKey.fromRaw(makeOpenArray(options.source, 64))
|
|
|
|
if pubkey.isErr:
|
2019-11-07 11:09:30 +00:00
|
|
|
error "Passed an invalid public key as source."
|
2019-11-13 10:41:27 +00:00
|
|
|
return false
|
2020-04-05 13:12:48 +00:00
|
|
|
src = some(pubkey[])
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2019-10-25 15:31:42 +00:00
|
|
|
try:
|
|
|
|
if not options.symKeyID.isNil():
|
2019-11-13 10:41:27 +00:00
|
|
|
let symKeyId = makeOpenArray(options.symKeyID, idLen).toHex()
|
|
|
|
symKey = some(whisperKeys.symKeys[symKeyId])
|
2019-10-25 15:31:42 +00:00
|
|
|
if not options.privateKeyID.isNil():
|
2019-11-13 10:41:27 +00:00
|
|
|
let privKeyId = makeOpenArray(options.privateKeyID, idLen).toHex()
|
|
|
|
privateKey = some(whisperKeys.asymKeys[privKeyId].seckey)
|
2019-10-25 15:31:42 +00:00
|
|
|
except KeyError:
|
2019-11-13 10:41:27 +00:00
|
|
|
return false
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2020-01-14 22:12:53 +00:00
|
|
|
let filter = initFilter(src, privateKey, symKey, @[options.topic],
|
2019-10-30 12:20:31 +00:00
|
|
|
options.minPow, options.allowP2P)
|
2019-09-12 16:32:07 +00:00
|
|
|
|
2021-04-02 16:45:09 +00:00
|
|
|
proc c_handler(msg: ReceivedMessage) {.gcsafe, raises: [Defect].} =
|
2019-11-18 21:07:08 +00:00
|
|
|
var cmsg = CReceivedMessage(
|
|
|
|
decoded: unsafeAddr msg.decoded.payload[0],
|
2020-07-20 10:03:20 +00:00
|
|
|
decodedLen: msg.decoded.payload.len(),
|
2019-11-18 21:07:08 +00:00
|
|
|
timestamp: msg.timestamp,
|
|
|
|
ttl: msg.ttl,
|
|
|
|
topic: msg.topic,
|
|
|
|
pow: msg.pow,
|
|
|
|
hash: msg.hash
|
|
|
|
)
|
|
|
|
|
|
|
|
# Could also allocate here, but this should stay in scope until handler
|
|
|
|
# finishes so it should be fine.
|
|
|
|
var
|
|
|
|
source: array[RawPublicKeySize, byte]
|
|
|
|
recipientPublicKey: array[RawPublicKeySize, byte]
|
|
|
|
if msg.decoded.src.isSome():
|
|
|
|
# Need to pass the serialized form
|
2020-04-05 13:12:48 +00:00
|
|
|
source = msg.decoded.src.get().toRaw()
|
2019-11-18 21:07:08 +00:00
|
|
|
cmsg.source = addr source[0]
|
|
|
|
if msg.dst.isSome():
|
|
|
|
# Need to pass the serialized form
|
2020-04-05 13:12:48 +00:00
|
|
|
recipientPublicKey = msg.decoded.src.get().toRaw()
|
2019-11-18 21:07:08 +00:00
|
|
|
cmsg.recipientPublicKey = addr recipientPublicKey[0]
|
|
|
|
|
|
|
|
handler(addr cmsg, udata)
|
|
|
|
|
|
|
|
# TODO: call can create `Exception`, why?
|
|
|
|
# TODO: if we decide to internally also work with other IDs, we don't need
|
|
|
|
# to do this hex conversion back and forth.
|
|
|
|
hexToBytes(node.subscribeFilter(filter, c_handler), id)
|
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
|
2019-11-05 09:20:19 +00:00
|
|
|
# TODO: call can create `Exception`, why?
|
2019-10-28 11:56:39 +00:00
|
|
|
traceAsyncErrors node.setBloomFilter(node.filtersToBloom())
|
2019-11-13 10:41:27 +00:00
|
|
|
result = true
|
2019-10-28 11:56:39 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_unsubscribe_filter(id: Identifier): bool
|
|
|
|
{.exportc, dynlib, raises: [].} =
|
2019-11-13 10:41:27 +00:00
|
|
|
doAssert(not(unsafeAddr id).isNil, "Filter id cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-11-13 10:41:27 +00:00
|
|
|
result = node.unsubscribeFilter(id.toHex())
|
2019-10-28 11:54:31 +00:00
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_get_min_pow(): float64 {.exportc, dynlib, raises: [].} =
|
2019-10-28 11:54:31 +00:00
|
|
|
result = node.protocolState(Whisper).config.powRequirement
|
|
|
|
|
2019-11-29 10:08:45 +00:00
|
|
|
proc nimbus_get_bloom_filter(bloom: var Bloom) {.exportc, dynlib, raises: [].} =
|
2019-11-13 10:49:35 +00:00
|
|
|
doAssert(not (unsafeAddr bloom).isNil, "Bloom pointer cannot be nil.")
|
2019-11-04 16:51:59 +00:00
|
|
|
|
2019-11-13 10:49:35 +00:00
|
|
|
bloom = node.protocolState(Whisper).config.bloom
|
2019-11-07 11:09:30 +00:00
|
|
|
|
|
|
|
# Nimbus limited Status chat API
|
|
|
|
|
|
|
|
# TODO: Return filter ID if we ever want to unsubscribe
|
|
|
|
proc subscribeChannel(
|
2021-04-02 16:45:09 +00:00
|
|
|
channel: string, handler: proc (msg: ReceivedMessage)
|
|
|
|
{.gcsafe, raises: [Defect].}) =
|
2019-11-07 11:09:30 +00:00
|
|
|
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
|
|
|
|
|
2020-01-14 22:12:53 +00:00
|
|
|
discard node.subscribeFilter(initFilter(symKey = some(symKey),
|
2019-11-07 11:09:30 +00:00
|
|
|
topics = @[topic]),
|
|
|
|
handler)
|
|
|
|
|
|
|
|
proc nimbus_join_public_chat(channel: cstring,
|
2021-04-02 16:45:09 +00:00
|
|
|
handler: proc (msg: ptr CReceivedMessage)
|
|
|
|
{.gcsafe, cdecl, raises: [Defect].}) {.exportc, dynlib.} =
|
2019-11-07 11:09:30 +00:00
|
|
|
if handler.isNil:
|
|
|
|
subscribeChannel($channel, nil)
|
|
|
|
else:
|
2021-04-02 16:45:09 +00:00
|
|
|
proc c_handler(msg: ReceivedMessage) {.raises: [Defect].} =
|
2019-11-07 11:09:30 +00:00
|
|
|
var cmsg = CReceivedMessage(
|
|
|
|
decoded: unsafeAddr msg.decoded.payload[0],
|
2020-07-20 10:03:20 +00:00
|
|
|
decodedLen: msg.decoded.payload.len(),
|
2019-11-07 11:09:30 +00:00
|
|
|
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-11-29 10:08:45 +00:00
|
|
|
proc nimbus_post_public(channel: cstring, payload: cstring)
|
|
|
|
{.exportc, dynlib.} =
|
2020-04-05 13:12:48 +00:00
|
|
|
let encPrivateKey = PrivateKey.fromHex("5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3")[]
|
2019-11-07 11:09:30 +00:00
|
|
|
|
|
|
|
var ctx: HMAC[sha256]
|
|
|
|
var symKey: SymKey
|
2020-04-20 18:12:44 +00:00
|
|
|
var npayload = cast[seq[byte]]($payload)
|
2019-11-07 11:09:30 +00:00
|
|
|
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)
|