2021-09-24 15:18:54 +00:00
|
|
|
# Nimbus
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-09-24 15:18:54 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2021-09-24 15:18:54 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
import ssz_serialization, eth/rlp, stew/[byteutils, results], nimcrypto/hash
|
2021-09-24 15:18:54 +00:00
|
|
|
|
2022-06-16 06:50:29 +00:00
|
|
|
export hash
|
|
|
|
|
2021-09-24 15:18:54 +00:00
|
|
|
type
|
|
|
|
ByteList* = List[byte, 2048]
|
|
|
|
Bytes2* = array[2, byte]
|
2021-11-17 16:11:17 +00:00
|
|
|
Bytes32* = array[32, byte]
|
2022-01-06 08:06:05 +00:00
|
|
|
|
2022-05-18 20:49:35 +00:00
|
|
|
ContentId* = UInt256
|
|
|
|
BlockHash* = MDigest[32 * 8] # Bytes32
|
2022-04-13 09:17:07 +00:00
|
|
|
|
|
|
|
func `$`*(x: ByteList): string =
|
|
|
|
x.asSeq.toHex()
|
2023-05-30 04:56:54 +00:00
|
|
|
|
|
|
|
func decodeRlp*(input: openArray[byte], T: type): Result[T, string] =
|
|
|
|
try:
|
|
|
|
ok(rlp.decode(input, T))
|
|
|
|
except RlpError as e:
|
|
|
|
err(e.msg)
|
|
|
|
|
|
|
|
func decodeSsz*(input: openArray[byte], T: type): Result[T, string] =
|
|
|
|
try:
|
|
|
|
ok(SSZ.decode(input, T))
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2023-05-30 04:56:54 +00:00
|
|
|
err(e.msg)
|
|
|
|
|
|
|
|
func decodeSszOrRaise*(input: openArray[byte], T: type): T =
|
|
|
|
try:
|
|
|
|
SSZ.decode(input, T)
|
2023-10-17 12:19:50 +00:00
|
|
|
except SerializationError as e:
|
2024-02-28 17:31:45 +00:00
|
|
|
raiseAssert(e.msg)
|