Skip to content

Commit

Permalink
Added Centrifuge type registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjan Zijderveld committed Feb 10, 2020
1 parent 4fe452c commit 8c5c2f0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
38 changes: 38 additions & 0 deletions scalecodec/type_registry/centrifuge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"runtime_id": 198,
"types": {
"BlockNumber": "u32",
"Index": "u64",
"Keys": {
"type": "struct",
"type_mapping": [
["grandpa", "AccountId"],
["babe", "AccountId"],
["im_online", "AccountId"],
["authority_discovery", "AccountId"]
]
},
"Fee<Hash, Balance>": {
"type": "struct",
"type_mapping": [
["key", "Hash"],
["price", "Balance"]
]
},
"PreCommitData<Hash, AccountId, BlockNumber>": {
"type": "struct",
"type_mapping": [
["signing_root", "Hash"],
["identity", "AccountId"],
["expiration_block", "BlockNumber"]
]
},
"Proof": {
"type": "struct",
"type_mapping": [
["leaf_hash", "H256"],
["sorted_hashes", "Vec<H256>"]
]
}
}
}
39 changes: 31 additions & 8 deletions scalecodec/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ def process_encode(self, value):
return ScaleBytes(value)


class VecU8Length20(ScaleType):
type_string = '[u8; 20]'

def process(self):
return '0x{}'.format(self.get_next_bytes(20).hex())

def process_encode(self, value):
if value[0:2] != '0x' and len(value) == 42:
raise ValueError('Value should start with "0x" and should be 20 bytes long')
return ScaleBytes(value)


class VecU8Length16(ScaleType):
type_string = '[u8; 16]'

Expand Down Expand Up @@ -489,6 +501,25 @@ def process_encode(self, value):
return ScaleBytes(value)


class VecH256Length3(ScaleType):
type_string = '[H256; 3]'

def process(self):
return [self.process_type('H256').value, self.process_type('H256').value, self.process_type('H256').value]

def process_encode(self, value):
if type(value) is not list:
raise ValueError("Provided value is not a list")

data = None

for element in value:
element_obj = self.get_decoder_class('H256', metadata=self.metadata)
data += element_obj.encode(element)

return data


class Struct(ScaleType):

def __init__(self, data, type_mapping=None, **kwargs):
Expand Down Expand Up @@ -782,18 +813,10 @@ class BalanceOf(Balance):
pass


class BlockNumber(U64):
pass


class NewAccountOutcome(CompactU32):
type_string = 'NewAccountOutcome'


class Index(U64):
pass


class Vec(ScaleType):

def __init__(self, data=None, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# For a discussion on single-sourcing the version across setup.py and the
# project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.9.8', # Required
version='0.9.9', # Required

# This is a one-line description or tagline of what your project does. This
# corresponds to the "Summary" metadata field:
Expand Down
5 changes: 5 additions & 0 deletions test/test_scale_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_compact_u32_remaining_bytes(self):
obj = ScaleDecoder.get_decoder_class('Compact<u32>', ScaleBytes("0x02093d0001"))
self.assertRaises(RemainingScaleBytesNotEmptyException, obj.decode)

def test_compact_u32_invalid(self):
obj = ScaleDecoder.get_decoder_class('Compact<u32>', ScaleBytes("0x"))
self.assertRaises(InvalidScaleTypeValueException, obj.decode)


def test_u16(self):
obj = ScaleDecoder.get_decoder_class('u16', ScaleBytes("0x2efb"))
obj.decode()
Expand Down

0 comments on commit 8c5c2f0

Please sign in to comment.