From 845671bf0a3e9aeac40026ecd45a64ae54846a34 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 20 Jul 2020 13:50:05 +0700 Subject: [PATCH] fix compilation error following breaking changes in nim-eth --- nimbus/config.nim | 4 ++-- nimbus/random_keys.nim | 17 +++++++++++++++++ nimbus/rpc/whisper.nim | 7 +++++-- nimbus/transaction.nim | 2 +- nimbus/vm/precompiles.nim | 2 +- nimbus/vm_state.nim | 2 +- tests/test_helpers.nim | 5 +++-- 7 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 nimbus/random_keys.nim diff --git a/nimbus/config.nim b/nimbus/config.nim index 0d3299dd4..589dd8efb 100644 --- a/nimbus/config.nim +++ b/nimbus/config.nim @@ -11,7 +11,7 @@ import parseopt, strutils, macros, os, times, json, stew/[byteutils], chronos, eth/[keys, common, p2p, net/nat], chronicles, nimcrypto/hash, eth/p2p/bootnodes, eth/p2p/rlpx_protocols/whisper_protocol, - ./db/select_backend, + ./db/select_backend, ./random_keys, ./vm/interpreter/vm_forks const @@ -847,7 +847,7 @@ proc initConfiguration(): NimbusConfiguration = result.net.ident = NimbusIdent result.net.nat = NatAny result.net.protocols = defaultProtocols - result.net.nodekey = PrivateKey.random().tryGet() + result.net.nodekey = randomPrivateKey() const dataDir = getDefaultDataDir() diff --git a/nimbus/random_keys.nim b/nimbus/random_keys.nim new file mode 100644 index 000000000..b5ca5b066 --- /dev/null +++ b/nimbus/random_keys.nim @@ -0,0 +1,17 @@ +import eth/keys as ethkeys + +# You should only create one instance of the RNG per application / library +# Ref is used so that it can be shared between components + +var theRNG {.threadvar.}: ref BrHmacDrbgContext + +proc getRng*(): ref BrHmacDrbgContext {.gcsafe.} = + if theRNG.isNil: + theRNG = newRng() + theRNG + +proc randomPrivateKey*(): PrivateKey = + random(PrivateKey, theRNG[]) + +proc randomKeyPair*(): KeyPair = + random(KeyPair, theRNG[]) diff --git a/nimbus/rpc/whisper.nim b/nimbus/rpc/whisper.nim index c0ea03f35..58b97f6f4 100644 --- a/nimbus/rpc/whisper.nim +++ b/nimbus/rpc/whisper.nim @@ -2,10 +2,13 @@ import json_rpc/rpcserver, tables, options, eth/[common, rlp, keys, p2p], eth/p2p/rlpx_protocols/whisper_protocol, nimcrypto/[sysrand, hmac, sha2, pbkdf2], - rpc_types, hexstrings, key_storage + rpc_types, hexstrings, key_storage, ../random_keys from stew/byteutils import hexToSeqByte, hexToByteArray +template generateRandomID*(): string = + generateRandomID(getRNG()[]) + # Whisper RPC implemented mostly as in # https://github.com/ethereum/go-ethereum/wiki/Whisper-v6-RPC-API @@ -71,7 +74,7 @@ proc setupWhisperRPC*(node: EthereumNode, keys: KeyStorage, rpcsrv: RpcServer) = ## ## Returns key identifier on success and an error on failure. result = generateRandomID().Identifier - keys.asymKeys.add(result.string, KeyPair.random().tryGet()) + keys.asymKeys.add(result.string, randomKeyPair()) rpcsrv.rpc("shh_addPrivateKey") do(key: PrivateKey) -> Identifier: ## Stores the key pair, and returns its ID. diff --git a/nimbus/transaction.nim b/nimbus/transaction.nim index 28b3e9604..e6e417ba4 100644 --- a/nimbus/transaction.nim +++ b/nimbus/transaction.nim @@ -59,7 +59,7 @@ proc getSender*(transaction: Transaction, output: var EthAddress): bool = var sig: Signature if transaction.getSignature(sig): var txHash = transaction.txHashNoSignature - let pubkey = recover(sig, txHash) + let pubkey = recover(sig, SkMessage(txHash.data)) if pubkey.isOk: output = pubkey[].toCanonicalAddress() result = true diff --git a/nimbus/vm/precompiles.nim b/nimbus/vm/precompiles.nim index b3b50b75c..36f71a174 100644 --- a/nimbus/vm/precompiles.nim +++ b/nimbus/vm/precompiles.nim @@ -101,7 +101,7 @@ proc ecRecover*(computation: Computation) = var (msgHash, sig) = computation.getSignature() - var pubkey = recover(sig, SkMessage(data: msgHash)) + var pubkey = recover(sig, SkMessage(msgHash)) if pubkey.isErr: raise newException(ValidationError, "Could not derive public key from computation") diff --git a/nimbus/vm_state.nim b/nimbus/vm_state.nim index 312b9d02f..ea3338316 100644 --- a/nimbus/vm_state.nim +++ b/nimbus/vm_state.nim @@ -86,7 +86,7 @@ proc calcMinerAddress(sigRaw: openArray[byte], vmState: BaseVMState, output: var var sig: Signature if sigRaw.getSignature(sig): let headerHash = headerHashOriExtraData(vmState) - let pubkey = recover(sig, headerHash) + let pubkey = recover(sig, SKMessage(headerHash.data)) if pubkey.isOk: output = pubkey[].toCanonicalAddress() result = true diff --git a/tests/test_helpers.nim b/tests/test_helpers.nim index 4a05e9872..40eb1c577 100644 --- a/tests/test_helpers.nim +++ b/tests/test_helpers.nim @@ -11,7 +11,8 @@ import testutils/markdown_reports, ../nimbus/[config, transaction, utils, errors], ../nimbus/vm/interpreter/vm_forks, - ../nimbus/db/accounts_cache + ../nimbus/db/accounts_cache, + ../nimbus/random_keys func revmap(x: Table[Fork, string]): Table[string, Fork] = result = initTable[string, Fork]() @@ -211,7 +212,7 @@ proc hashLogEntries*(logs: seq[Log]): string = proc setupEthNode*(capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode = var conf = getConfiguration() - conf.net.nodekey = PrivateKey.random().tryGet() + conf.net.nodekey = randomPrivateKey() let keypair = conf.net.nodekey.toKeyPair() var srvAddress: Address