From d2caed0ec9e0da3155310fb163a64977c0c6e8c9 Mon Sep 17 00:00:00 2001 From: modship Date: Thu, 14 Dec 2023 17:10:02 +0100 Subject: [PATCH 1/3] add chain id to operations --- massa_test_framework/massa_py/operations.py | 27 +++++++++++++-------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/massa_test_framework/massa_py/operations.py b/massa_test_framework/massa_py/operations.py index c9481bf..7335e3e 100644 --- a/massa_test_framework/massa_py/operations.py +++ b/massa_test_framework/massa_py/operations.py @@ -6,6 +6,7 @@ import base58 import varint from blake3 import blake3 +import struct class Serializable(ABC): @@ -66,10 +67,11 @@ def serialize(self) -> bytes: enc_op = self.op.serialize() return bytes(enc_fee + enc_expire_period + enc_type_id + enc_op) - def sign(self, creator_public_key: str, sender_private_key: str) -> bytes: + def sign(self, creator_public_key: str, sender_private_key: str, chainID: int) -> bytes: enc_data = self.serialize() - enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key) - enc_data = enc_sender_pub_key + enc_data + enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key) + enc_data = struct.pack('>Q', chainID) + enc_sender_pub_key + enc_data + # Hash enc_data = blake3(enc_data).digest() @@ -83,10 +85,10 @@ def sign(self, creator_public_key: str, sender_private_key: str) -> bytes: class OperationInput: def __init__( - self, creator_public_key: str, content: Operation, sender_private_key: str + self, creator_public_key: str, content: Operation, sender_private_key: str, chainID: int ): self.creator_public_key = creator_public_key - self.signature = content.sign(creator_public_key, sender_private_key).decode( + self.signature = content.sign(creator_public_key, sender_private_key, chainID).decode( "utf-8" ) self.serialized_content = list(content.serialize()) @@ -187,9 +189,10 @@ def create_roll_buy( fee: int, expire_period: int, roll_count: int, + chainID: int, ): op = Operation(fee, expire_period, RollBuy(roll_count)) - op_in = OperationInput(creator_public_key, op, sender_private_key) + op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) return op_in.__dict__ @@ -199,9 +202,10 @@ def create_roll_sell( fee: int, expire_period: int, roll_count: int, + chainID: int, ): op = Operation(fee, expire_period, RollSell(roll_count)) - op_in = OperationInput(creator_public_key, op, sender_private_key) + op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) return op_in.__dict__ @@ -212,9 +216,10 @@ def create_transaction( expire_period: int, recipient_address: str, amount: int, + chainID: int, ): op = Operation(fee, expire_period, Transaction(recipient_address, amount)) - op_in = OperationInput(creator_public_key, op, sender_private_key) + op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) return op_in.__dict__ @@ -228,11 +233,12 @@ def create_call_sc( param: bytes, max_gas: int, coins: int, + chainID: int, ): op = Operation( fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins) ) - op_in = OperationInput(creator_public_key, op, sender_private_key) + op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) return op_in.__dict__ @@ -245,7 +251,8 @@ def create_execute_sc( max_gas: int, max_coins: int, datastore: Datastore, + chainID: int, ): op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore)) - op_in = OperationInput(creator_public_key, op, sender_private_key) + op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) return op_in.__dict__ From 5cd30194a122815aeea422d453b35148870ef890 Mon Sep 17 00:00:00 2001 From: modship Date: Fri, 15 Dec 2023 10:51:44 +0100 Subject: [PATCH 2/3] comment pr --- massa_test_framework/massa_py/operations.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/massa_test_framework/massa_py/operations.py b/massa_test_framework/massa_py/operations.py index 7335e3e..1f74a4a 100644 --- a/massa_test_framework/massa_py/operations.py +++ b/massa_test_framework/massa_py/operations.py @@ -67,9 +67,13 @@ def serialize(self) -> bytes: enc_op = self.op.serialize() return bytes(enc_fee + enc_expire_period + enc_type_id + enc_op) + # massa doc https://docs.massa.net/docs/learn/operation-format-execution def sign(self, creator_public_key: str, sender_private_key: str, chainID: int) -> bytes: enc_data = self.serialize() enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key) + # doc https://docs.massa.net/docs/learn/operation-format-execution + # > -> big-endian + # Q -> unsigned long long enc_data = struct.pack('>Q', chainID) + enc_sender_pub_key + enc_data # Hash From 5d249fe353d39ef6e53a8bafbe972c1b63cf666e Mon Sep 17 00:00:00 2001 From: modship Date: Fri, 15 Dec 2023 15:31:23 +0100 Subject: [PATCH 3/3] move chainID to init op --- massa_test_framework/massa_py/operations.py | 31 +++++++++++---------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/massa_test_framework/massa_py/operations.py b/massa_test_framework/massa_py/operations.py index 1f74a4a..46b49df 100644 --- a/massa_test_framework/massa_py/operations.py +++ b/massa_test_framework/massa_py/operations.py @@ -55,10 +55,11 @@ def serialize(self) -> bytes: class Operation(Serializable): - def __init__(self, fee: int, expire_period: int, op: InnerOp): + def __init__(self, fee: int, expire_period: int, op: InnerOp, chainID: int): self.fee = fee self.expire_period = expire_period self.op = op + self.chainID = chainID def serialize(self) -> bytes: enc_fee = varint.encode(int(self.fee)) @@ -68,13 +69,13 @@ def serialize(self) -> bytes: return bytes(enc_fee + enc_expire_period + enc_type_id + enc_op) # massa doc https://docs.massa.net/docs/learn/operation-format-execution - def sign(self, creator_public_key: str, sender_private_key: str, chainID: int) -> bytes: + def sign(self, creator_public_key: str, sender_private_key: str) -> bytes: enc_data = self.serialize() enc_sender_pub_key = decode_pubkey_to_bytes(creator_public_key) # doc https://docs.massa.net/docs/learn/operation-format-execution # > -> big-endian # Q -> unsigned long long - enc_data = struct.pack('>Q', chainID) + enc_sender_pub_key + enc_data + enc_data = struct.pack('>Q', self.chainID) + enc_sender_pub_key + enc_data # Hash enc_data = blake3(enc_data).digest() @@ -89,10 +90,10 @@ def sign(self, creator_public_key: str, sender_private_key: str, chainID: int) - class OperationInput: def __init__( - self, creator_public_key: str, content: Operation, sender_private_key: str, chainID: int + self, creator_public_key: str, content: Operation, sender_private_key: str ): self.creator_public_key = creator_public_key - self.signature = content.sign(creator_public_key, sender_private_key, chainID).decode( + self.signature = content.sign(creator_public_key, sender_private_key).decode( "utf-8" ) self.serialized_content = list(content.serialize()) @@ -195,8 +196,8 @@ def create_roll_buy( roll_count: int, chainID: int, ): - op = Operation(fee, expire_period, RollBuy(roll_count)) - op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) + op = Operation(fee, expire_period, RollBuy(roll_count), chainID) + op_in = OperationInput(creator_public_key, op, sender_private_key) return op_in.__dict__ @@ -208,8 +209,8 @@ def create_roll_sell( roll_count: int, chainID: int, ): - op = Operation(fee, expire_period, RollSell(roll_count)) - op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) + op = Operation(fee, expire_period, RollSell(roll_count), chainID) + op_in = OperationInput(creator_public_key, op, sender_private_key) return op_in.__dict__ @@ -222,8 +223,8 @@ def create_transaction( amount: int, chainID: int, ): - op = Operation(fee, expire_period, Transaction(recipient_address, amount)) - op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) + op = Operation(fee, expire_period, Transaction(recipient_address, amount), chainID) + op_in = OperationInput(creator_public_key, op, sender_private_key) return op_in.__dict__ @@ -240,9 +241,9 @@ def create_call_sc( chainID: int, ): op = Operation( - fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins) + fee, expire_period, CallSC(target_address, target_func, param, max_gas, coins), chainID ) - op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) + op_in = OperationInput(creator_public_key, op, sender_private_key) return op_in.__dict__ @@ -257,6 +258,6 @@ def create_execute_sc( datastore: Datastore, chainID: int, ): - op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore)) - op_in = OperationInput(creator_public_key, op, sender_private_key, chainID) + op = Operation(fee, expire_period, ExecuteSC(data, max_gas, max_coins, datastore), chainID) + op_in = OperationInput(creator_public_key, op, sender_private_key) return op_in.__dict__