Skip to content

Commit

Permalink
Merge pull request #4 from alexkoeberl/legacy_support
Browse files Browse the repository at this point in the history
Support chain_id=None for backward compatibility (prevent EIP-155 additions).
  • Loading branch information
kdembler authored Feb 15, 2021
2 parents ded0e1d + 19cc7e1 commit 2ff3ff6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pip install blocksec2go-ethereum

After creating an instance of `Blocksec2Go` you can use it to generate signatures for transaction dicts. When passed raw tx, `sign_transaction()` will return a hex string of RLP-encoded signed transaction that can be directly consumed by `web3.eth.sendRawTransaction()`.

The replay attack protection introduced with [EIP-155](https://eips.ethereum.org/EIPS/eip-155) is used by default. Set `chain_id=None` to force the legacy behaviour for backward compatibility.
### Transfer Ether

Below you will find an example of signing a simple Ether transfer:
Expand Down
4 changes: 2 additions & 2 deletions blocksec2go_ethereum/_signer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import time
from typing import Tuple
from typing import Tuple, Optional

import blocksec2go
from blocksec2go.comm.pyscard import open_pyscard
Expand All @@ -18,7 +18,7 @@


class Blocksec2GoSigner:
def __init__(self, key_id: int = 1, chain_id: int = 1, connect_retry_count: int = 5):
def __init__(self, key_id: int = 1, chain_id: Optional[int] = 1, connect_retry_count: int = 5):
self._key_id = key_id
self._chain_id = chain_id
self._connect_retry_count = connect_retry_count
Expand Down
7 changes: 5 additions & 2 deletions blocksec2go_ethereum/_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, Optional

import ecdsa
from eth_typing import ChecksumAddress
Expand Down Expand Up @@ -34,6 +34,9 @@ def address_from_public_key(public_key: bytes) -> ChecksumAddress:
return Web3.toChecksumAddress(address)


def get_v(sig: UnrecoverableSignature, tx_hash: bytes, pub_key: bytes, chain_id: int) -> int:
def get_v(sig: UnrecoverableSignature, tx_hash: bytes, pub_key: bytes, chain_id: Optional[int]) -> int:
recovery_id = find_recovery_id(sig, tx_hash, pub_key)

if not chain_id:
return 27 + recovery_id
return 35 + recovery_id + (chain_id * 2)
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ def test_address_from_public_key():
def test_get_v():
returned_v = get_v(test_unrecoverable_signature, test_tx_hash, test_pub_key, test_chain_id)
assert test_signature[0] == returned_v


def test_get_v_legacy():
returned_v = get_v(test_unrecoverable_signature, test_tx_hash, test_pub_key, None)
legacy_v = test_signature[0] - (2 * test_chain_id) - 35 + 27
assert legacy_v == returned_v

0 comments on commit 2ff3ff6

Please sign in to comment.