Merge pull request #38 from MaxMustermann2/chain-id-signing-fix

fix(signing): drop shard ids from serializer
master
Max 2 years ago committed by GitHub
commit 961aea189d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      pyhmy/__init__.py
  2. 25
      pyhmy/signing.py
  3. 4
      pyproject.toml
  4. 20
      tests/sdk-pyhmy/test_signing.py

@ -4,7 +4,7 @@
import sys import sys
import warnings import warnings
__version__ = "0.1.0" __version__ = "0.1.1"
if sys.version_info.major < 3: if sys.version_info.major < 3:
warnings.simplefilter( "always", DeprecationWarning ) warnings.simplefilter( "always", DeprecationWarning )

@ -44,6 +44,7 @@ class UnsignedHarmonyTxData( HashableRLP ):
Includes `shardID` and `toShardID` Includes `shardID` and `toShardID`
as the difference against Eth as the difference against Eth
""" """
fields = ( fields = (
( "nonce", ( "nonce",
big_endian_int ), big_endian_int ),
@ -71,6 +72,7 @@ class SignedHarmonyTxData( HashableRLP ):
Includes `shardID` and `toShardID` Includes `shardID` and `toShardID`
as the difference against Eth as the difference against Eth
""" """
fields = UnsignedHarmonyTxData._meta.fields + ( fields = UnsignedHarmonyTxData._meta.fields + (
("v", big_endian_int), # Recovery value + 27 ("v", big_endian_int), # Recovery value + 27
("r", big_endian_int), # First 32 bytes ("r", big_endian_int), # First 32 bytes
@ -107,8 +109,12 @@ def encode_transaction( unsigned_transaction, vrs ):
def serialize_transaction( filled_transaction ): def serialize_transaction( filled_transaction ):
"""serialize a signed/unsigned transaction.""" """serialize a signed/unsigned transaction."""
# although this will always be present for this module
# keep this check anyway
if "v" in filled_transaction: if "v" in filled_transaction:
if "shardID" in filled_transaction: # https://github.com/harmony-one/harmony/blob/f8879f5e0288157bf95ae2898a9a27f0c85ff9ad/core/types/transaction_signing.go#L173
if "shardID" in filled_transaction and filled_transaction[
"v" ] < 1666600000:
serializer = SignedHarmonyTxData serializer = SignedHarmonyTxData
else: else:
serializer = SignedEthereumTxData serializer = SignedEthereumTxData
@ -132,10 +138,9 @@ def serialize_transaction( filled_transaction ):
def sanitize_transaction( transaction_dict, private_key ): def sanitize_transaction( transaction_dict, private_key ):
"""remove the originating address from the dict and convert chainId to """remove the originating address from the dict and convert chainId to
int.""" int."""
account = Account.from_key( # pylint: disable=no-value-for-parameter account = Account.from_key( private_key ) # pylint: disable=no-value-for-parameter
private_key sanitized_transaction = (
) transaction_dict.copy()
sanitized_transaction = transaction_dict.copy(
) # do not alter the original dictionary ) # do not alter the original dictionary
if "from" in sanitized_transaction: if "from" in sanitized_transaction:
sanitized_transaction[ "from" ] = convert_one_to_hex( sanitized_transaction[ "from" ] = convert_one_to_hex(
@ -227,12 +232,12 @@ def sign_transaction( transaction_dict, private_key ) -> SignedTransaction:
chain_id = None chain_id = None
else: else:
chain_id = unsigned_transaction.v chain_id = unsigned_transaction.v
( v, # pylint: disable=invalid-name (
v, # pylint: disable=invalid-name
r, # pylint: disable=invalid-name r, # pylint: disable=invalid-name
s ) = sign_transaction_hash( # pylint: disable=invalid-name s, # pylint: disable=invalid-name
account._key_obj, ) = sign_transaction_hash(
transaction_hash, account._key_obj, transaction_hash, chain_id
chain_id
) )
encoded_transaction = encode_transaction( encoded_transaction = encode_transaction(
unsigned_transaction, unsigned_transaction,

@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
[project] [project]
name = "pyhmy" name = "pyhmy"
version = "0.1.0" version = "0.1.1"
description = "A library for interacting and working the Harmony blockchain and related codebases" description = "A library for interacting and working the Harmony blockchain and related codebases"
readme = "README.md" readme = "README.md"
license = { text = "MIT" } license = { text = "MIT" }
@ -26,7 +26,7 @@ requires-python = ">=3.0"
dev = [ "black", "autopep8", "yapf", "twine", "build", "docformatter", "bumpver" ] dev = [ "black", "autopep8", "yapf", "twine", "build", "docformatter", "bumpver" ]
[tool.bumpver] [tool.bumpver]
current_version = "0.1.0" current_version = "0.1.1"
version_pattern = "MAJOR.MINOR.PATCH" version_pattern = "MAJOR.MINOR.PATCH"
commit_message = "chore: bump version {old_version} -> {new_version}" commit_message = "chore: bump version {old_version} -> {new_version}"
# git commit --amend -S # git commit --amend -S

@ -98,3 +98,23 @@ def test_hmy_transaction():
signed_tx.rawTransaction.hex() == signed_tx.rawTransaction.hex() ==
"0xf85f02016480019414791697260e4c9a71f18484c9f997b308e59325058026a02a203357ca6d7cdec981ad3d3692ad2c9e24536a9b6e7b486ce2f94f28c7563ea010d38cd0312a153af0aa7d8cd986040c36118bba373cb94e3e86fd4aedce904d" "0xf85f02016480019414791697260e4c9a71f18484c9f997b308e59325058026a02a203357ca6d7cdec981ad3d3692ad2c9e24536a9b6e7b486ce2f94f28c7563ea010d38cd0312a153af0aa7d8cd986040c36118bba373cb94e3e86fd4aedce904d"
) )
def test_hmy_eth_compatible_transaction():
transaction_dict = {
"chainId": 1666600000,
"gas": 21000,
"gasPrice": 100000000000,
"nonce": 0,
"shardID": 0,
"to": "0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a",
"toShardID": 1,
"value": 1000000000000000000
}
signed_tx = signing.sign_transaction(
transaction_dict,
"0x1111111111111111111111111111111111111111111111111111111111111111",
)
assert (
signed_tx.rawTransaction.hex() ==
"0xf8728085174876e80082520880019419e7e376e7c213b7e7e7e46cc70a5dd086daff2a880de0b6b3a76400008084c6ac98a3a0322cca082c3ca0a1d9ad5fffb4dc0e09ade49b4b0e3b0c9dfa5f6288bc7363d6a05604874964abaaf364e8b10108e8bfed5561c341aa5e4abb92b2c6f4c009ef4c"
)

Loading…
Cancel
Save