1

I am writing a program in c++ for generating and signing transactions. The transaction it generates seem to be invalid. Every time I try to broadcast it, I get an error saying insufficient funds on sender address.

RLP rlp;
uint8_t *privatekey = charArrayToByteArray("*******************************************");

TX tx;
tx.nonce="0x06";
tx.gasPrice="0x01a13b8600";
tx.gasLimit="0x5208";
tx.to="0x8f9ad0411a887c3243bcded341ed63f90b3e1417";
tx.value="0x2386f26fc10000";
tx.data="0x";
tx.v="";
tx.r="";
tx.s="";

string enc = rlp.bytesToHex(rlp.encode(tx, true)); //Encode raw transaction

uint8_t *hashval = new uint8_t[HASH_LENGTH];
keccak256((uint8_t*)enc.c_str(), enc.size(), hashval); //Generate hash

uint8_t* signature = new uint8_t[SIGNATURE_LENGTH];
uECC_sign(privatekey,hashval, HASH_LENGTH, signature, uECC_secp256k1()); //Sign the hash

uint8_t *r = new uint8_t[32];
uint8_t *s = new uint8_t[32];
splitArray(signature,r,0,32);
splitArray(signature,s,32,64);

tx.r = string("0x") + byteArrayToCharArray(r, 32);
tx.s = string("0x") + byteArrayToCharArray(s, 32);
tx.v = "0x1c";

string encoded = rlp.bytesToHex(rlp.encode(tx, false)); //Encode transaction with signature

uECC library: https://github.com/kmackay/micro-ecc/blob/master/uECC.h

RLP library: https://github.com/kvhnuke/Ethereum-Arduino/blob/master/Ethereum-Arduino/RLP.cpp

Resulting transaction: http://ehrs.info/images/g7HkznUS3C.png

  • I got this problem too, after this problem I try to using r,s and "1b" as v construct my TX through Ethereum-tx, and using getSenderAddress() to verify "sender address" I got a wrong sender – Dalton Hsu Sep 6 '18 at 9:36
  • The problem was in the RLP library that I was using. I made some edits to it and it started generating the correct transactions. Here's the completed project: github.com/Kidades/hwwallet , you can take the RLP library from there. – Kidades Sep 6 '18 at 14:05
  • It still just uses 1b for the v value though, which is not always correct. – Kidades Sep 6 '18 at 14:07
  • in my case, I'm building a usb cold wallet device, raw transaction and keccak256(SHA3) hash is generate in browser side. so that my device just doing "sign" process, and only put "r", "s" result from device into RLP of browser side code i tried sign process in browser is fine, but using micro-ecc got wrong – Dalton Hsu Sep 7 '18 at 6:27
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – cleanunicorn Sep 8 '18 at 13:45
0

Your raw transaction before the first RLP encoding needs the chain ID in place of the empty v value per EIP155.

Then afterwards, you replace the v with (chainID << 1) + 35, and the r & s with their respective values from the signature. Finally, another RLPEncoding of this object gives you the final transaction data.

Not having done this is why you're getting the No funds errors, since the chain your transaction is destined for can't be fathomed properly from the signed transaction data blob.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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