Move WhisperKeys to KeyStorage

This commit is contained in:
kdeme 2020-01-20 17:45:49 +01:00 committed by zah
parent 776f924e39
commit d56655d278
7 changed files with 50 additions and 59 deletions

View File

@ -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)

View File

@ -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]()

View File

@ -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.

View File

@ -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.

View File

@ -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

View File

@ -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()

View File

@ -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