From 71e7cb836b092e6896d7bcfef1945df877232021 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 30 May 2018 19:02:12 +0300 Subject: [PATCH] BlockNumber, rlpHash, utils --- eth_common.nim | 4 ++-- eth_common.nimble | 3 ++- eth_common/eth_types.nim | 14 ++++++++------ eth_common/rlp_serialization.nim | 4 ++++ eth_common/utils.nim | 12 ++++++++++++ 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 eth_common/utils.nim diff --git a/eth_common.nim b/eth_common.nim index b1362c7..36ddfa1 100644 --- a/eth_common.nim +++ b/eth_common.nim @@ -1,2 +1,2 @@ -import eth_common / [eth_types, rlp_serialization] -export eth_types, rlp_serialization +import eth_common / [eth_types, rlp_serialization, utils] +export eth_types, rlp_serialization, utils diff --git a/eth_common.nimble b/eth_common.nimble index dc17f48..d5f0353 100644 --- a/eth_common.nimble +++ b/eth_common.nimble @@ -9,4 +9,5 @@ requires "nim > 0.18.0", "rlp", "nimcrypto", "ranges", - "stint" + "stint", + "https://github.com/status-im/nim-byteutils" diff --git a/eth_common/eth_types.nim b/eth_common/eth_types.nim index 14840e8..bf60d25 100644 --- a/eth_common/eth_types.nim +++ b/eth_common/eth_types.nim @@ -8,7 +8,7 @@ type Blob* = seq[byte] BloomFilter* = StUint[2048] - EthAddress* = MDigest[160] + EthAddress* = array[20, byte] GasInt* = int64 ## Type alias used for gas computation @@ -23,6 +23,8 @@ type payload*: Blob V*, R*, S*: UInt256 + BlockNumber* = UInt256 + BlockHeader* = object parentHash*: Hash256 ommersHash*: Hash256 @@ -32,10 +34,10 @@ type receiptRoot*: Hash256 bloom*: BloomFilter difficulty*: UInt256 - blockNumber*: uint + blockNumber*: BlockNumber gasLimit*: GasInt gasUsed*: GasInt - timestamp*: uint64 + timestamp*: EthTime extraData*: Blob mixDigest*: Hash256 nonce*: BlockNonce @@ -77,15 +79,15 @@ type coinbase*: EthAddress stateRoot*: Hash256 receiptRoot*: Hash256 - blockNumber*: uint + blockNumber*: BlockNumber HashOrNum* = object case isHash*: bool of true: hash*: Hash256 else: - number*: uint + number*: BlockNumber BlocksRequest* = object startBlock*: HashOrNum - maxResults*, skip*, reverse*: uint + maxResults*, skip*, reverse*: uint64 diff --git a/eth_common/rlp_serialization.nim b/eth_common/rlp_serialization.nim index ff699b2..1c31600 100644 --- a/eth_common/rlp_serialization.nim +++ b/eth_common/rlp_serialization.nim @@ -12,3 +12,7 @@ proc read*(rlp: var Rlp, T: typedesc[EthTime]): T {.inline.} = proc append*(rlpWriter: var RlpWriter, t: EthTime) {.inline.} = rlpWriter.append(t.toUnix()) + + +proc rlpHash*[T](v: T): Hash256 = + keccak256.digest(rlp.encode(v).toOpenArray) diff --git a/eth_common/utils.nim b/eth_common/utils.nim new file mode 100644 index 0000000..a7516ea --- /dev/null +++ b/eth_common/utils.nim @@ -0,0 +1,12 @@ +import nimcrypto, hashes, byteutils, eth_types + +proc hash*(d: MDigest): Hash {.inline.} = hash(d.data) + +proc toDigestAux(len: static[int], s: string): MDigest[len] = + hexToByteArray(s, result.data) + +proc toDigest*(hexString: static[string]): auto = + toDigestAux(hexString.len div 2 * 8, hexString) + +proc parseAddress*(hexString: string): EthAddress = + hexToByteArray(hexString, result)