@@ -189,6 +189,12 @@ func valid(hex: string): bool =
189189 if x notin HexDigits : return false
190190 true
191191
192+ # https://business.1inch.com/portal/documentation/apis/web3/ethereum/methods/debug_getRawBlock
193+ func isBlockTag (tag: string ): bool =
194+ case tag.toLowerAscii
195+ of " earliest" , " latest" , " pending" , " safe" , " finalized" : true
196+ else : false
197+
192198func validHash (hex: string ): bool =
193199 # assume `hex` has been checked before by `valid`
194200 const hexHashLen = 32 * 2
@@ -263,8 +269,11 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var Address)
263269
264270proc readValue * (r: var JsonReader [JrpcConv ], val: var Hash32 )
265271 {.gcsafe , raises : [IOError , JsonReaderError ].} =
266- wrapValueError:
267- val = fromHex (Hash32 , r.parseString ())
272+ let hexStr = r.parseString ()
273+ if not valid (hexStr):
274+ r.raiseUnexpectedValue (" hex string without 0x prefix" )
275+ wrapValueError:
276+ val = fromHex (Hash32 , hexStr)
268277
269278proc writeValue * (w: var JsonWriter [JrpcConv ], v: Number )
270279 {.gcsafe , raises : [IOError ].} =
@@ -378,19 +387,20 @@ proc readValue*(r: var JsonReader[JrpcConv], val: var seq[byte])
378387 # skip empty hex
379388 val = hexToSeqByte (hexStr)
380389
381- proc readValue * (r: var JsonReader [JrpcConv ], val: var RtBlockIdentifier )
390+ proc readValue * ( r: var JsonReader [JrpcConv ], val: var RtBlockIdentifier )
382391 {.gcsafe , raises : [IOError , JsonReaderError ].} =
383- let hexStr = r.parseString ()
384- wrapValueError:
385- if valid (hexStr):
386- if validHash (hexStr):
387- val = RtBlockIdentifier (
388- kind: bidHash, hash: fromHex (Hash32 , hexStr))
392+ let blockId = r.parseString ()
393+ wrapValueError:
394+ if valid (blockId) and validHash (blockId):
395+ val = RtBlockIdentifier (kind: bidHash, hash: fromHex (Hash32 , blockId))
396+ elif valid (blockId):
397+ val = RtBlockIdentifier (kind: bidNumber, number: Quantity fromHex [uint64 ](blockId))
398+ elif isBlockTag (blockId):
399+ val = RtBlockIdentifier (kind: bidAlias, alias: blockId)
400+ elif blockId.len > 0 and blockId.allCharsInSet (HexDigits ):
401+ r.raiseUnexpectedValue (" hex string without 0x prefix" )
389402 else :
390- val = RtBlockIdentifier (
391- kind: bidNumber, number: Quantity fromHex [uint64 ](hexStr))
392- else :
393- val = RtBlockIdentifier (kind: bidAlias, alias: hexStr)
403+ r.raiseUnexpectedValue (" invalid block tag" )
394404
395405proc writeValue * (w: var JsonWriter [JrpcConv ], v: RtBlockIdentifier )
396406 {.gcsafe , raises : [IOError ].} =
0 commit comments