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+
192199when 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
390402proc readValue * (r: var JsonReader [JrpcConv ], val: var TxOrHash )
391403 {.gcsafe , raises : [IOError , SerializationError ].} =
0 commit comments