3

How do you use Pyethereum to generate a raw tx to send to the blockchain? I can do it successfully with a transaction with no data, but when I am attempting to use data, it does not function properly.

To expand on this, I have pinpointed the problem to the data encoding. I can send a 1 ETH transaction with no data perfectly. I can send that same transaction with a string of data, say '0xabcdef' and it will go through the same, except the 'data' section on etherscan.io does not read '0xabcdef'. I believe this is the issue, but I do not know how to fix it.

To show an example, I am using this to create the transaction, and the following code works:

nonce = 100
gas_price = 10000000000
gas_limit = 22000
value = 1000000000000000000
data = ""
privkey = private_key_here

unencoded_tx = rlp.encode(transactions.Transaction(nonce, gas_price, gas_limit, to_address, value, data).sign(privkey))
signedtx = '0x' + codecs.encode(unencoded_tx, 'hex').decode('utf-8')

The following code, which is the same, but with data, also goes through, but the data is not what I expect:

nonce = 100
gas_price = 10000000000
gas_limit = 22000
value = 1000000000000000000
data = "0xabcdef"
privkey = private_key_here

unencoded_tx = rlp.encode(transactions.Transaction(nonce, gas_price, gas_limit, to_address, value, data).sign(privkey))
signedtx = '0x' + codecs.encode(unencoded_tx, 'hex').decode('utf-8')

Data output (according to etherscan.io): 0x3078616263646566

Obviously that is not the data I wanted to see. How can I pass in the same data I see on etherscan.io?

1
  • I am having a simlar problem (signing token transaction with pyethereum), and thought to have solved the problem similarly to the post above. Eth is doing just fine, tokens don't work. I notoced the return that you get (as the example also shows) contains a long sequence of 03030303.... I checked what myetherwallet comes up with (send offline function) and it is nothing like the return the python code spits out. And not unexpected the ethereum network does not accept the transaction. Could anybody help me?
    – PyNoob
    Jul 13, 2018 at 10:55

2 Answers 2

3

0x3078616263646566 is the hex representation of the UTF-8 encoding of the string "0xabcdef". "0" is hex 30, "x" is hex 78, and so on. Try passing in b'\xab\xcd\xef' as the data, instead.

You can generate this byte string from hex with:

import codecs
raw_bytes = codecs.decode('abcdef', 'hex')
assert raw_bytes == b'\xab\xcd\xef'

It's also worth checking out the package ethereum-utils for eth_utils.decode_hex() and other useful tools.

0

This it the code I have in python3

from ethereum import transactions
from ethereum import utils
tx = transactions.Transaction(nonce=8, gasprice=5000000000, 
    startgas=45000, to=contract_address, value=0, 


data="0x957a0e210000000000000000
   000000000000000000000000000000000000000000000037").sign(private_key)
tx = tx.to_dict()

It returns this

  {'nonce': 8, 'gasprice': 5000000000, 'startgas': 45000, 'to': '0x4d21f3618bc4c50475d4b30993d80cffe78b3650', 'value': 0, 'data': '0x3078393537613065323130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303337', 'v': 27, 'r': 104827557381449577376519412355902868429829733841563149787296871707166833642558, 's': 29002009444020075645692529433910482836532514345220494513395292371410739844134, 'sender': '0xd1bb5d5c362bb4faf25c23b3c7c42c8ea30dc10d', 'hash': '0x20d1a85cf3e6f4d11655c9ba0f09602ea4881b6abcc192581fca18b24634cd2c'}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.