Skip to content

Commit e1de14a

Browse files
authored
Add hash support when parsing BlockTag (#242)
* Add hash support when parsing BlockTag * Fix test * Fix test
1 parent 044625e commit e1de14a

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

tests/test_json_marshalling.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# nim-web3
2-
# Copyright (c) 2018-2025 Status Research & Development GmbH
2+
# Copyright (c) 2018-2026 Status Research & Development GmbH
33
# Licensed under either of
44
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
55
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@@ -249,6 +249,11 @@ suite "JSON-RPC Quantity":
249249
let d = JrpcConv.decode("10", RtBlockIdentifier)
250250
discard d
251251

252+
const w = hash32"0x0012c7b99594801d513ae92396379e5ffcf60e23127cbcabb166db28586f01aa"
253+
let e = JrpcConv.decode("\"" & $w & "\"", RtBlockIdentifier)
254+
check e.kind == bidHash
255+
check e.hash == w
256+
252257
test "check address or list":
253258
let a = AddressOrList(kind: slkNull)
254259
let x = JrpcConv.encode(a)
@@ -264,7 +269,7 @@ suite "JSON-RPC Quantity":
264269
check b == "\"0x16345785d8a0000\""
265270

266271
let x = JrpcConv.decode("\"0xFFFF_FFFF_FFFF_FFFF\"", typeName)
267-
check x.uint64 == 0xFFFF_FFFF_FFFF_FFFF_FFFF'u64
272+
check x.uint64 == 0xFFFF_FFFF_FFFF_FFFF'u64
268273
let y = JrpcConv.encode(x)
269274
check y == "\"0xffffffffffffffff\""
270275

web3/conversions.nim

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# nim-web3
2-
# Copyright (c) 2019-2025 Status Research & Development GmbH
2+
# Copyright (c) 2019-2026 Status Research & Development GmbH
33
# Licensed under either of
44
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
55
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
@@ -189,6 +189,13 @@ func valid(hex: string): bool =
189189
if x notin HexDigits: return false
190190
true
191191

192+
func validHash(hex: string): bool =
193+
# assume `hex` has been checked before by `valid`
194+
const hexHashLen = 32*2
195+
if hex[0] == '0' and hex[1] in {'x', 'X'}:
196+
return hex.len == hexHashLen + 2
197+
hex.len == hexHashLen
198+
192199
when not declared(json_serialization.streamElement): # json_serialization < 0.3.0
193200
template streamElement(w: var JsonWriter, s, body: untyped) =
194201
template s: untyped = w.stream
@@ -376,8 +383,12 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var RtBlockIdentifier)
376383
let hexStr = r.parseString()
377384
wrapValueError:
378385
if valid(hexStr):
379-
val = RtBlockIdentifier(
380-
kind: bidNumber, number: Quantity fromHex[uint64](hexStr))
386+
if validHash(hexStr):
387+
val = RtBlockIdentifier(
388+
kind: bidHash, hash: fromHex(Hash32, hexStr))
389+
else:
390+
val = RtBlockIdentifier(
391+
kind: bidNumber, number: Quantity fromHex[uint64](hexStr))
381392
else:
382393
val = RtBlockIdentifier(kind: bidAlias, alias: hexStr)
383394

@@ -386,6 +397,7 @@ proc writeValue*(w: var JsonWriter[JrpcConv], v: RtBlockIdentifier)
386397
case v.kind
387398
of bidNumber: w.writeValue(v.number)
388399
of bidAlias: w.writeValue(v.alias)
400+
of bidHash: w.writeValue(v.hash)
389401

390402
proc readValue*(r: var JsonReader[JrpcConv], val: var TxOrHash)
391403
{.gcsafe, raises: [IOError, SerializationError].} =

web3/eth_api_types.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,16 @@ type
244244
BlockIdentifierKind* = enum
245245
bidNumber
246246
bidAlias
247+
bidHash
247248

248249
RtBlockIdentifier* = object
249250
case kind*: BlockIdentifierKind
250251
of bidNumber:
251252
number*: Quantity
252253
of bidAlias:
253254
alias*: string
255+
of bidHash:
256+
hash*: Hash32
254257

255258
FeeHistoryReward* = seq[UInt256]
256259

0 commit comments

Comments
 (0)