chainlib-eth

Ethereum implementation of the chainlib interface
Info | Log | Files | Refs | README | LICENSE

tx_object.py (1389B)


      1 # standard imports
      2 import os
      3 
      4 # external imports
      5 from crypto_dev_signer.keystore.dict import DictKeystore
      6 from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
      7 from crypto_dev_signer.eth.transaction import EIP155Transaction
      8 from hexathon import (
      9         add_0x,
     10         strip_0x,
     11         )
     12 
     13 # local imports
     14 from chainlib.chain import ChainSpec
     15 from chainlib.eth.tx import (
     16         unpack,
     17         Tx,
     18         )
     19 
     20 # eth transactions need an explicit chain parameter as part of their signature
     21 chain_spec = ChainSpec.from_chain_str('evm:ethereum:1')
     22 chain_id = chain_spec.chain_id()
     23 
     24 # create keystore and signer
     25 keystore = DictKeystore()
     26 signer = EIP155Signer(keystore)
     27 sender_address = keystore.new()
     28 recipient_address = keystore.new()
     29 
     30 
     31 # set up a transaction dict source
     32 tx_src = {
     33     'from': sender_address,
     34     'to': recipient_address,
     35     'gas': 21000,
     36     'gasPrice': 1000000000,
     37     'value': 1024,
     38     'data': '0xdeadbeef',
     39         }
     40 sender_nonce = 0
     41 tx = EIP155Transaction(tx_src, sender_nonce, chain_id)
     42 signature = signer.sign_transaction(tx)
     43 print('signature: {}'.format(signature.hex()))
     44 
     45 tx.apply_signature(chain_id, signature)
     46 print('tx with signature: {}'.format(tx.serialize()))
     47 
     48 tx_signed_raw_bytes = tx.rlp_serialize()
     49 tx_src = unpack(tx_signed_raw_bytes, chain_spec)
     50 tx_parsed = Tx(tx_src)
     51 print('parsed signed tx: {}'.format(tx_parsed.to_human()))