Add tx reverse re-pack
This commit is contained in:
parent
494cb1e0af
commit
b9788adb18
@ -14,6 +14,7 @@ from rlp import decode as rlp_decode
|
|||||||
from rlp import encode as rlp_encode
|
from rlp import encode as rlp_encode
|
||||||
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
from crypto_dev_signer.eth.transaction import EIP155Transaction
|
||||||
from crypto_dev_signer.encoding import public_key_to_address
|
from crypto_dev_signer.encoding import public_key_to_address
|
||||||
|
from crypto_dev_signer.eth.encoding import chain_id_to_v
|
||||||
from potaahto.symbols import snake_and_camel
|
from potaahto.symbols import snake_and_camel
|
||||||
|
|
||||||
|
|
||||||
@ -71,6 +72,25 @@ def count_confirmed(address):
|
|||||||
return count(address, True)
|
return count(address, True)
|
||||||
|
|
||||||
|
|
||||||
|
def pack(tx_src, chain_spec):
|
||||||
|
if isinstance(tx_src, Tx):
|
||||||
|
tx_src = tx_src.as_dict()
|
||||||
|
tx = EIP155Transaction(tx_src, tx_src['nonce'], chain_spec.chain_id())
|
||||||
|
|
||||||
|
signature = bytearray(65)
|
||||||
|
cursor = 0
|
||||||
|
for a in [
|
||||||
|
tx_src['r'],
|
||||||
|
tx_src['s'],
|
||||||
|
]:
|
||||||
|
for b in bytes.fromhex(strip_0x(a)):
|
||||||
|
signature[cursor] = b
|
||||||
|
cursor += 1
|
||||||
|
signature[cursor] = tx_src['v']
|
||||||
|
tx.apply_signature(chain_spec.chain_id(), signature, literal_v=True)
|
||||||
|
return tx.rlp_serialize()
|
||||||
|
|
||||||
|
|
||||||
def unpack(tx_raw_bytes, chain_spec):
|
def unpack(tx_raw_bytes, chain_spec):
|
||||||
chain_id = chain_spec.chain_id()
|
chain_id = chain_spec.chain_id()
|
||||||
tx = __unpack_raw(tx_raw_bytes, chain_id)
|
tx = __unpack_raw(tx_raw_bytes, chain_id)
|
||||||
@ -154,10 +174,11 @@ def __unpack_raw(tx_raw_bytes, chain_id=1):
|
|||||||
'gas': d[2],
|
'gas': d[2],
|
||||||
'value': d[4],
|
'value': d[4],
|
||||||
'data': data,
|
'data': data,
|
||||||
'v': chain_id,
|
'v': v,
|
||||||
|
'recovery_byte': vb,
|
||||||
'r': add_0x(sig[:32].hex()),
|
'r': add_0x(sig[:32].hex()),
|
||||||
's': add_0x(sig[32:64].hex()),
|
's': add_0x(sig[32:64].hex()),
|
||||||
'chainId': chain_id,
|
'chainId': vb, #chain_id,
|
||||||
'hash': add_0x(signed_hash.hex()),
|
'hash': add_0x(signed_hash.hex()),
|
||||||
'hash_unsigned': add_0x(unsigned_hash.hex()),
|
'hash_unsigned': add_0x(unsigned_hash.hex()),
|
||||||
}
|
}
|
||||||
@ -377,6 +398,10 @@ class Tx:
|
|||||||
return snake_and_camel(src)
|
return snake_and_camel(src)
|
||||||
|
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
return self.src
|
||||||
|
|
||||||
|
|
||||||
def apply_receipt(self, rcpt):
|
def apply_receipt(self, rcpt):
|
||||||
rcpt = self.src_normalize(rcpt)
|
rcpt = self.src_normalize(rcpt)
|
||||||
logg.debug('rcpt {}'.format(rcpt))
|
logg.debug('rcpt {}'.format(rcpt))
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
crypto-dev-signer~=0.4.14b3
|
crypto-dev-signer~=0.4.14b5
|
||||||
pysha3==1.0.2
|
pysha3==1.0.2
|
||||||
hexathon~=0.0.1a7
|
hexathon~=0.0.1a7
|
||||||
websocket-client==0.57.0
|
websocket-client==0.57.0
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = chainlib
|
name = chainlib
|
||||||
version = 0.0.3rc3
|
version = 0.0.3rc4
|
||||||
description = Generic blockchain access library and tooling
|
description = Generic blockchain access library and tooling
|
||||||
author = Louis Holbrook
|
author = Louis Holbrook
|
||||||
author_email = dev@holbrook.no
|
author_email = dev@holbrook.no
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
# standard imports
|
# standard imports
|
||||||
import unittest
|
import os
|
||||||
|
import unittest
|
||||||
|
import logging
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||||
@ -10,9 +12,26 @@ from chainlib.eth.gas import (
|
|||||||
)
|
)
|
||||||
from chainlib.eth.tx import (
|
from chainlib.eth.tx import (
|
||||||
unpack,
|
unpack,
|
||||||
|
pack,
|
||||||
|
raw,
|
||||||
|
transaction,
|
||||||
TxFormat,
|
TxFormat,
|
||||||
|
TxFactory,
|
||||||
|
Tx,
|
||||||
)
|
)
|
||||||
from hexathon import strip_0x
|
from chainlib.eth.contract import (
|
||||||
|
ABIContractEncoder,
|
||||||
|
ABIContractType,
|
||||||
|
)
|
||||||
|
from chainlib.eth.address import to_checksum_address
|
||||||
|
from hexathon import (
|
||||||
|
strip_0x,
|
||||||
|
add_0x,
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
class TxTestCase(EthTesterCase):
|
class TxTestCase(EthTesterCase):
|
||||||
|
|
||||||
@ -26,5 +45,35 @@ class TxTestCase(EthTesterCase):
|
|||||||
self.assertEqual(tx['to'], self.accounts[1])
|
self.assertEqual(tx['to'], self.accounts[1])
|
||||||
|
|
||||||
|
|
||||||
|
def test_tx_pack(self):
|
||||||
|
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||||
|
gas_oracle = RPCGasOracle(self.rpc)
|
||||||
|
|
||||||
|
mock_contract = to_checksum_address(add_0x(os.urandom(20).hex()))
|
||||||
|
|
||||||
|
f = TxFactory(self.chain_spec, signer=self.rpc)
|
||||||
|
enc = ABIContractEncoder()
|
||||||
|
enc.method('fooMethod')
|
||||||
|
enc.typ(ABIContractType.UINT256)
|
||||||
|
enc.uint256(13)
|
||||||
|
data = enc.get()
|
||||||
|
tx = f.template(self.accounts[0], mock_contract, use_nonce=True)
|
||||||
|
tx = f.set_code(tx, data)
|
||||||
|
(tx_hash, tx_signed_raw_hex) = f.finalize(tx, TxFormat.RLP_SIGNED)
|
||||||
|
logg.debug('tx result {}'.format(tx))
|
||||||
|
o = raw(tx_signed_raw_hex)
|
||||||
|
r = self.rpc.do(o)
|
||||||
|
o = transaction(tx_hash)
|
||||||
|
tx_rpc_src = self.rpc.do(o)
|
||||||
|
logg.debug('rpc src {}'.format(tx_rpc_src))
|
||||||
|
|
||||||
|
tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex))
|
||||||
|
tx_src = unpack(tx_signed_raw_bytes, self.chain_spec)
|
||||||
|
txo = Tx(tx_src)
|
||||||
|
tx_signed_raw_bytes_recovered = pack(txo, self.chain_spec)
|
||||||
|
logg.debug('o {}'.format(tx_signed_raw_bytes.hex()))
|
||||||
|
logg.debug('r {}'.format(tx_signed_raw_bytes_recovered.hex()))
|
||||||
|
self.assertEqual(tx_signed_raw_bytes, tx_signed_raw_bytes_recovered)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user