Skip to content

Commit 7cdddec

Browse files
author
Arjan Zijderveld
committed
Multi-sig AccountId support
1 parent 03f93e1 commit 7cdddec

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

scalecodec/block.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,7 @@ def __init__(self, data=None, sub_type=None, metadata: MetadataDecoder = None, a
5959

6060
def generate_hash(self):
6161
if self.contains_transaction:
62-
63-
if self.extrinsic_length:
64-
extrinsic_data = self.data.data
65-
else:
66-
# Fallback for legacy version, prefix additional Compact<u32> with length
67-
extrinsic_length_type = CompactU32(ScaleBytes(bytearray()))
68-
extrinsic_length_type.encode(self.data.length)
69-
extrinsic_data = extrinsic_length_type.data.data + self.data.data
70-
71-
return blake2b(extrinsic_data, digest_size=32).digest().hex()
62+
return blake2b(self.data.data, digest_size=32).digest().hex()
7263
else:
7364
return None
7465

scalecodec/types.py

+32
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# along with Polkascan. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from datetime import datetime
20+
from hashlib import blake2b
21+
2022
from scalecodec.base import ScaleType, ScaleBytes
2123
from scalecodec.exceptions import InvalidScaleTypeValueException
2224

@@ -796,9 +798,14 @@ class Linkage(Struct):
796798

797799
class AccountId(H256):
798800

801+
def __init__(self, data=None, sub_type=None, metadata=None):
802+
self.ss58_address = None
803+
super().__init__(data, sub_type, metadata)
804+
799805
def process_encode(self, value):
800806
if value[0:2] != '0x' and len(value) == 47:
801807
from scalecodec.utils.ss58 import ss58_decode
808+
self.ss58_address = value
802809
value = '0x{}'.format(ss58_decode(value))
803810
return super().process_encode(value)
804811

@@ -1949,3 +1956,28 @@ def process_encode(self, value):
19491956

19501957
return data
19511958

1959+
1960+
class MultiAccountId(AccountId):
1961+
1962+
@classmethod
1963+
def create_from_account_list(cls, accounts, threshold):
1964+
from scalecodec.utils.ss58 import ss58_decode
1965+
1966+
account_ids = []
1967+
for account in accounts:
1968+
if account[0:2] != '0x':
1969+
account = '0x{}'.format(ss58_decode(account))
1970+
account_ids.append(account)
1971+
1972+
account_list_cls = cls.get_decoder_class('Vec<AccountId>')
1973+
account_list_data = account_list_cls.encode(sorted(account_ids))
1974+
threshold_data = cls.get_decoder_class("u16").encode(threshold)
1975+
1976+
multi_account_id = "0x{}".format(blake2b(
1977+
b"modlpy/utilisuba" + bytes(account_list_data.data) + bytes(threshold_data.data), digest_size=32
1978+
).digest().hex())
1979+
1980+
multi_account_obj = cls()
1981+
multi_account_obj.encode(multi_account_id)
1982+
1983+
return multi_account_obj

test/test_scale_types.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from scalecodec.base import ScaleDecoder, ScaleBytes, RemainingScaleBytesNotEmptyException, \
2525
InvalidScaleTypeValueException, RuntimeConfiguration
2626
from scalecodec.type_registry import load_type_registry_preset
27+
from scalecodec.utils.ss58 import ss58_encode
2728

2829

2930
class TestScaleTypes(unittest.TestCase):
@@ -228,5 +229,15 @@ def test_parse_subtype(self):
228229

229230
self.assertEqual(obj.sub_type, "UncleEntryItem<BlockNumber, Hash, AccountId>".lower())
230231

232+
def test_create_multi_sig_address(self):
233+
MultiAccountId = RuntimeConfiguration().get_decoder_class("MultiAccountId")
234+
235+
multi_sig_account = MultiAccountId.create_from_account_list(
236+
["CdVuGwX71W4oRbXHsLuLQxNPns23rnSSiZwZPN4etWf6XYo",
237+
"J9aQobenjZjwWtU2MsnYdGomvcYbgauCnBeb8xGrcqznvJc",
238+
"HvqnQxDQbi3LL2URh7WQfcmi8b2ZWfBhu7TEDmyyn5VK8e2"], 2)
239+
240+
multi_sig_address = ss58_encode(multi_sig_account.value.replace('0x', ''), 2)
241+
242+
self.assertEqual(multi_sig_address, "HFXXfXavDuKhLLBhFQTat2aaRQ5CMMw9mwswHzWi76m6iLt")
231243

232-
# TODO make type_index in Metadatadecoder and add tests if all types are supported

0 commit comments

Comments
 (0)