diff --git a/nimbus/nimbus.nim b/nimbus/nimbus.nim index 80f1a33cc..c2a3da651 100644 --- a/nimbus/nimbus.nim +++ b/nimbus/nimbus.nim @@ -14,7 +14,7 @@ import chronos, json_rpc/rpcserver, chronicles, eth/p2p/rlpx_protocols/[eth_protocol, les_protocol, whisper_protocol], eth/p2p/blockchain_sync, eth/net/nat, eth/p2p/peer_pool, - config, genesis, rpc/[common, p2p, debug, whisper], p2p/chain, + config, genesis, rpc/[common, p2p, debug, whisper, key_storage], p2p/chain, eth/trie/db, metrics, metrics/chronicles_support ## TODO: @@ -133,7 +133,7 @@ proc start() = if RpcFlags.Eth in conf.rpc.flags and ProtocolFlags.Eth in conf.net.protocols: setupEthRpc(nimbus.ethNode, chainDB, nimbus.rpcServer) if RpcFlags.Shh in conf.rpc.flags and ProtocolFlags.Shh in conf.net.protocols: - let keys = newWhisperKeys() + let keys = newKeyStorage() setupWhisperRPC(nimbus.ethNode, keys, nimbus.rpcServer) if RpcFlags.Debug in conf.rpc.flags: setupDebugRpc(chainDB, nimbus.rpcServer) diff --git a/nimbus/rpc/key_storage.nim b/nimbus/rpc/key_storage.nim new file mode 100644 index 000000000..9a341eec9 --- /dev/null +++ b/nimbus/rpc/key_storage.nim @@ -0,0 +1,22 @@ +# +# Nimbus +# (c) Copyright 2019 +# Status Research & Development GmbH +# +# Licensed under either of +# Apache License, version 2.0, (LICENSE-APACHEv2) +# MIT license (LICENSE-MIT) + +import tables, eth/keys, eth/p2p/rlpx_protocols/whisper/whisper_types + +type + KeyStorage* = ref object + asymKeys*: Table[string, KeyPair] + symKeys*: Table[string, SymKey] + + KeyGenerationError* = object of CatchableError + +proc newKeyStorage*(): KeyStorage = + new(result) + result.asymKeys = initTable[string, KeyPair]() + result.symKeys = initTable[string, SymKey]() diff --git a/nimbus/rpc/waku.nim b/nimbus/rpc/waku.nim index 6abc1ce19..fe78d5828 100644 --- a/nimbus/rpc/waku.nim +++ b/nimbus/rpc/waku.nim @@ -1,25 +1,14 @@ import - json_rpc/rpcserver, rpc_types, hexstrings, tables, options, sequtils, + json_rpc/rpcserver, tables, options, sequtils, eth/[common, rlp, keys, p2p], eth/p2p/rlpx_protocols/waku_protocol, - nimcrypto/[sysrand, hmac, sha2, pbkdf2] + nimcrypto/[sysrand, hmac, sha2, pbkdf2], + rpc_types, hexstrings, key_storage from stew/byteutils import hexToSeqByte, hexToByteArray # Blatant copy of Whisper RPC but for the Waku protocol -type - WhisperKeys* = ref object - asymKeys*: Table[string, KeyPair] - symKeys*: Table[string, SymKey] - - KeyGenerationError = object of CatchableError - -proc newWakuKeys*(): WhisperKeys = - new(result) - result.asymKeys = initTable[string, KeyPair]() - result.symKeys = initTable[string, SymKey]() - -proc setupWakuRPC*(node: EthereumNode, keys: WhisperKeys, rpcsrv: RpcServer) = +proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) = rpcsrv.rpc("shh_version") do() -> string: ## Returns string of the current whisper protocol version. diff --git a/nimbus/rpc/whisper.nim b/nimbus/rpc/whisper.nim index 106f35af3..78931b00e 100644 --- a/nimbus/rpc/whisper.nim +++ b/nimbus/rpc/whisper.nim @@ -1,26 +1,15 @@ import - json_rpc/rpcserver, rpc_types, hexstrings, tables, options, sequtils, + json_rpc/rpcserver, tables, options, sequtils, eth/[common, rlp, keys, p2p], eth/p2p/rlpx_protocols/whisper_protocol, - nimcrypto/[sysrand, hmac, sha2, pbkdf2] + nimcrypto/[sysrand, hmac, sha2, pbkdf2], + rpc_types, hexstrings, key_storage from stew/byteutils import hexToSeqByte, hexToByteArray # Whisper RPC implemented mostly as in # https://github.com/ethereum/go-ethereum/wiki/Whisper-v6-RPC-API -type - WhisperKeys* = ref object - asymKeys*: Table[string, KeyPair] - symKeys*: Table[string, SymKey] - - KeyGenerationError = object of CatchableError - -proc newWhisperKeys*(): WhisperKeys = - new(result) - result.asymKeys = initTable[string, KeyPair]() - result.symKeys = initTable[string, SymKey]() - -proc setupWhisperRPC*(node: EthereumNode, keys: WhisperKeys, rpcsrv: RpcServer) = +proc setupWhisperRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) = rpcsrv.rpc("shh_version") do() -> string: ## Returns string of the current whisper protocol version. diff --git a/tests/test_rpc_whisper.nim b/tests/test_rpc_whisper.nim index 6d6264712..0004d06a6 100644 --- a/tests/test_rpc_whisper.nim +++ b/tests/test_rpc_whisper.nim @@ -1,7 +1,9 @@ import unittest, strformat, options, stew/byteutils, json_rpc/[rpcserver, rpcclient], - eth/common as eth_common, eth/[rlp, keys], eth/p2p/rlpx_protocols/whisper_protocol, - ../nimbus/rpc/[common, hexstrings, rpc_types, whisper], ./test_helpers + eth/common as eth_common, eth/[rlp, keys], + eth/p2p/rlpx_protocols/whisper_protocol, + ../nimbus/rpc/[common, hexstrings, rpc_types, whisper, key_storage], + ./test_helpers from os import DirSep from strutils import rsplit @@ -20,7 +22,7 @@ proc doTests {.async.} = var rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT]) client = newRpcSocketClient() - let keys = newWhisperKeys() + let keys = newKeyStorage() setupWhisperRPC(ethNode, keys, rpcServer) # Begin tests diff --git a/waku/wakunode.nim b/waku/wakunode.nim index 27d4b9816..7428783c9 100644 --- a/waku/wakunode.nim +++ b/waku/wakunode.nim @@ -4,7 +4,7 @@ import eth/[keys, p2p, async_utils], eth/common/utils, eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes], eth/p2p/rlpx_protocols/[whisper_protocol, waku_protocol, waku_bridge], - ../nimbus/rpc/waku, ../nimbus/rpc/wakusim + ../nimbus/rpc/[waku, wakusim, key_storage] proc setBootNodes(nodes: openArray[string]): seq[ENode] = var bootnode: ENode @@ -69,7 +69,7 @@ proc run(config: WakuNodeConf) = let ta = initTAddress(config.rpcAddress, Port(config.rpcPort + config.portsShift)) var rpcServer = newRpcHttpServer([ta]) - let keys = newWakuKeys() + let keys = newKeyStorage() setupWakuRPC(node, keys, rpcServer) setupWakuSimRPC(node, rpcServer) rpcServer.start() diff --git a/wrappers/libnimbus.nim b/wrappers/libnimbus.nim index 247a915d9..67a4a711f 100644 --- a/wrappers/libnimbus.nim +++ b/wrappers/libnimbus.nim @@ -11,33 +11,17 @@ import chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash, sysrand], tables, stew/ranges/ptr_arith, eth/[keys, rlp, p2p, async_utils], eth/p2p/rlpx_protocols/whisper_protocol, - eth/p2p/[peer_pool, bootnodes, whispernodes] + eth/p2p/[peer_pool, bootnodes, whispernodes], ../nimbus/rpc/key_storage + +# 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. const idLen = 32 -# 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] - Identifier = array[idLen, byte] -proc newWhisperKeys(): WhisperKeys = - new(result) - result.asymKeys = initTable[string, KeyPair]() - result.symKeys = initTable[string, SymKey]() - -proc generateRandomID(): Identifier = - while true: # TODO: error instead of looping? - if randomBytes(result) == idLen: - break - -# 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). Perhaps some -# parts can be refactored in sharing some of the code. -type CReceivedMessage* = object decoded*: ptr byte decodedLen*: csize @@ -77,7 +61,12 @@ type var node: EthereumNode # You will only add more instead! -let whisperKeys = newWhisperKeys() +let whisperKeys = newKeyStorage() + +proc generateRandomID(): Identifier = + while true: # TODO: error instead of looping? + if randomBytes(result) == idLen: + break proc setBootNodes(nodes: openArray[string]): seq[ENode] = var bootnode: ENode