add json encoding for bitlist and vector

This commit is contained in:
Danny Ryan 2019-06-30 22:04:47 -06:00
parent de95f54c4f
commit 17b0bc35d8
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A

View File

@ -1,6 +1,7 @@
from eth2spec.utils.ssz.ssz_impl import hash_tree_root
from eth2spec.utils.ssz.ssz_impl import hash_tree_root, serialize
from eth2spec.utils.ssz.ssz_typing import (
uint, Container, boolean
uint, boolean,
Bitlist, Bitvector, Container
)
@ -12,6 +13,8 @@ def encode(value, include_hash_tree_roots=False):
return int(value)
elif isinstance(value, boolean):
return value == 1
elif isinstance(value, (Bitlist, Bitvector)):
return '0x' + serialize(value).hex()
elif isinstance(value, list): # normal python lists, ssz-List, Vector
return [encode(element, include_hash_tree_roots) for element in value]
elif isinstance(value, bytes): # both bytes and BytesN