2

I'm trying to sign my raw transaction and each time I'm getting a different signature (r, s). Please help me how to sign transactions properly in C++ OpenSSL. I'm trying the following way but Etherscan doesn't push this transaction because of invalid sender or insufficient funds (I'm sure this is because the signature is invalid). I have a NodeJs example that calculates signatures and for one hash it generates the same signature each time. And one more question - how to get a RecoveryID from the signature to set V variable properly? Thank you

OpenSSL_add_all_algorithms();

const unsigned char* privKey = /* Private Key 32 bytes */;
const char* rlpTx = /* nonce, gas limit etc encoded to RLP */;

const uint8_t c_chainID = 1; // Mainnet

BIGNUM* bn  = BN_bin2bn(privKey, 32, nullptr);
EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1);

EC_KEY_set_private_key(key, bn);

unsigned char* hash = nullptr;
int size = 0;

Keccak256::calcHash(rlpTx, hash, &size);

ECDSA_SIG* sign = ECDSA_do_sign(hash, key);

unsigned char* r = new unsigned char[BN_num_bytes(sign->r)];
unsigned char* s = new unsigned char[BN_num_bytes(sign->s)];
uint8_t        v = c_chainID * 2 + 35;

BN_bn2bin(sign->r, r);
BN_bn2bin(sign->s, s);

ECDSA_SIG_free(sign);
BN_free(bn); 
EC_KEY_free(privKey);

delete[] s;
delete[] r;
1
  • Are you using a library for RLP encoding?
    – onepix
    Feb 3, 2020 at 16:51

0

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.

Browse other questions tagged or ask your own question.