Move WhisperKeys to KeyStorage
This commit is contained in:
parent
776f924e39
commit
d56655d278
|
@ -14,7 +14,7 @@ import
|
||||||
chronos, json_rpc/rpcserver, chronicles,
|
chronos, json_rpc/rpcserver, chronicles,
|
||||||
eth/p2p/rlpx_protocols/[eth_protocol, les_protocol, whisper_protocol],
|
eth/p2p/rlpx_protocols/[eth_protocol, les_protocol, whisper_protocol],
|
||||||
eth/p2p/blockchain_sync, eth/net/nat, eth/p2p/peer_pool,
|
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
|
eth/trie/db, metrics, metrics/chronicles_support
|
||||||
|
|
||||||
## TODO:
|
## TODO:
|
||||||
|
@ -133,7 +133,7 @@ proc start() =
|
||||||
if RpcFlags.Eth in conf.rpc.flags and ProtocolFlags.Eth in conf.net.protocols:
|
if RpcFlags.Eth in conf.rpc.flags and ProtocolFlags.Eth in conf.net.protocols:
|
||||||
setupEthRpc(nimbus.ethNode, chainDB, nimbus.rpcServer)
|
setupEthRpc(nimbus.ethNode, chainDB, nimbus.rpcServer)
|
||||||
if RpcFlags.Shh in conf.rpc.flags and ProtocolFlags.Shh in conf.net.protocols:
|
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)
|
setupWhisperRPC(nimbus.ethNode, keys, nimbus.rpcServer)
|
||||||
if RpcFlags.Debug in conf.rpc.flags:
|
if RpcFlags.Debug in conf.rpc.flags:
|
||||||
setupDebugRpc(chainDB, nimbus.rpcServer)
|
setupDebugRpc(chainDB, nimbus.rpcServer)
|
||||||
|
|
|
@ -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]()
|
|
@ -1,25 +1,14 @@
|
||||||
import
|
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,
|
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
|
from stew/byteutils import hexToSeqByte, hexToByteArray
|
||||||
|
|
||||||
# Blatant copy of Whisper RPC but for the Waku protocol
|
# Blatant copy of Whisper RPC but for the Waku protocol
|
||||||
|
|
||||||
type
|
proc setupWakuRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
|
||||||
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) =
|
|
||||||
|
|
||||||
rpcsrv.rpc("shh_version") do() -> string:
|
rpcsrv.rpc("shh_version") do() -> string:
|
||||||
## Returns string of the current whisper protocol version.
|
## Returns string of the current whisper protocol version.
|
||||||
|
|
|
@ -1,26 +1,15 @@
|
||||||
import
|
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,
|
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
|
from stew/byteutils import hexToSeqByte, hexToByteArray
|
||||||
|
|
||||||
# Whisper RPC implemented mostly as in
|
# Whisper RPC implemented mostly as in
|
||||||
# https://github.com/ethereum/go-ethereum/wiki/Whisper-v6-RPC-API
|
# https://github.com/ethereum/go-ethereum/wiki/Whisper-v6-RPC-API
|
||||||
|
|
||||||
type
|
proc setupWhisperRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) =
|
||||||
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) =
|
|
||||||
|
|
||||||
rpcsrv.rpc("shh_version") do() -> string:
|
rpcsrv.rpc("shh_version") do() -> string:
|
||||||
## Returns string of the current whisper protocol version.
|
## Returns string of the current whisper protocol version.
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import
|
import
|
||||||
unittest, strformat, options, stew/byteutils, json_rpc/[rpcserver, rpcclient],
|
unittest, strformat, options, stew/byteutils, json_rpc/[rpcserver, rpcclient],
|
||||||
eth/common as eth_common, eth/[rlp, keys], eth/p2p/rlpx_protocols/whisper_protocol,
|
eth/common as eth_common, eth/[rlp, keys],
|
||||||
../nimbus/rpc/[common, hexstrings, rpc_types, whisper], ./test_helpers
|
eth/p2p/rlpx_protocols/whisper_protocol,
|
||||||
|
../nimbus/rpc/[common, hexstrings, rpc_types, whisper, key_storage],
|
||||||
|
./test_helpers
|
||||||
|
|
||||||
from os import DirSep
|
from os import DirSep
|
||||||
from strutils import rsplit
|
from strutils import rsplit
|
||||||
|
@ -20,7 +22,7 @@ proc doTests {.async.} =
|
||||||
var
|
var
|
||||||
rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT])
|
rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT])
|
||||||
client = newRpcSocketClient()
|
client = newRpcSocketClient()
|
||||||
let keys = newWhisperKeys()
|
let keys = newKeyStorage()
|
||||||
setupWhisperRPC(ethNode, keys, rpcServer)
|
setupWhisperRPC(ethNode, keys, rpcServer)
|
||||||
|
|
||||||
# Begin tests
|
# Begin tests
|
||||||
|
|
|
@ -4,7 +4,7 @@ import
|
||||||
eth/[keys, p2p, async_utils], eth/common/utils,
|
eth/[keys, p2p, async_utils], eth/common/utils,
|
||||||
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes],
|
eth/p2p/[discovery, enode, peer_pool, bootnodes, whispernodes],
|
||||||
eth/p2p/rlpx_protocols/[whisper_protocol, waku_protocol, waku_bridge],
|
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] =
|
proc setBootNodes(nodes: openArray[string]): seq[ENode] =
|
||||||
var bootnode: ENode
|
var bootnode: ENode
|
||||||
|
@ -69,7 +69,7 @@ proc run(config: WakuNodeConf) =
|
||||||
let ta = initTAddress(config.rpcAddress,
|
let ta = initTAddress(config.rpcAddress,
|
||||||
Port(config.rpcPort + config.portsShift))
|
Port(config.rpcPort + config.portsShift))
|
||||||
var rpcServer = newRpcHttpServer([ta])
|
var rpcServer = newRpcHttpServer([ta])
|
||||||
let keys = newWakuKeys()
|
let keys = newKeyStorage()
|
||||||
setupWakuRPC(node, keys, rpcServer)
|
setupWakuRPC(node, keys, rpcServer)
|
||||||
setupWakuSimRPC(node, rpcServer)
|
setupWakuSimRPC(node, rpcServer)
|
||||||
rpcServer.start()
|
rpcServer.start()
|
||||||
|
|
|
@ -11,33 +11,17 @@ import
|
||||||
chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash, sysrand], tables,
|
chronos, chronicles, nimcrypto/[utils, hmac, pbkdf2, hash, sysrand], tables,
|
||||||
stew/ranges/ptr_arith, eth/[keys, rlp, p2p, async_utils],
|
stew/ranges/ptr_arith, eth/[keys, rlp, p2p, async_utils],
|
||||||
eth/p2p/rlpx_protocols/whisper_protocol,
|
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
|
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
|
type
|
||||||
WhisperKeys* = ref object
|
|
||||||
asymKeys*: Table[string, KeyPair]
|
|
||||||
symKeys*: Table[string, SymKey]
|
|
||||||
|
|
||||||
Identifier = array[idLen, byte]
|
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
|
CReceivedMessage* = object
|
||||||
decoded*: ptr byte
|
decoded*: ptr byte
|
||||||
decodedLen*: csize
|
decodedLen*: csize
|
||||||
|
@ -77,7 +61,12 @@ type
|
||||||
var
|
var
|
||||||
node: EthereumNode
|
node: EthereumNode
|
||||||
# You will only add more instead!
|
# 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] =
|
proc setBootNodes(nodes: openArray[string]): seq[ENode] =
|
||||||
var bootnode: ENode
|
var bootnode: ENode
|
||||||
|
|
Loading…
Reference in New Issue