1

I'm trying to generate ethereum raw transactions.

this is keccak hash of my transaction:

f2ad2e2b6f398f76df2f5adacc84e39e926871bd2f82a3055319f8d4f4eeb8ce

after signing it with my private key I obtained this values:

r= 4041e0cf580308bcedc1f0a136029ff5200827f9dacad8c3b4884ab1350c031f
s= 0e0a5e601489fbd5a647e0423f0cf84c80b7d14cb3bf61e066184b2d94c7a267

now at the end I must calculate value of v

I found two formulas for it:

# CHAIN_ID=1
1) v = CHAIN_ID * 2 + 35
2) v = CHAIN_ID * 2 + 36

I don't know which formulas must I use.

now exactly how must I detect which formulas must I use ?

2

2 Answers 2

2

For the library you're using, see https://github.com/simplito/elliptic-php/blob/d6c5ecaaf434a52449f5e8faa350311c9fd1dd7a/tests/ECDSATest.php#L305:

$recid = $ec->getKeyRecoveryParam($msg, $signature, $key->getPublic());

Or perhaps $recoverParam is already a member of Signature? See https://github.com/simplito/elliptic-php/blob/master/lib/EC/Signature.php#L12.

In any case, I imagine that the recovery param is 0 or 1. You'll need to add to it to get the right v value for the chain you're using.

5
0

According to the document (EIP-155): v of the signature MUST be set to {0,1} + CHAIN_ID * 2 + 35 where {0,1} is the parity of the y value of the curve point for which r is the x-value in the secp256k1 signing process. which means {0,1} value is not calculateable. you must recover the sender's address from each 2 amounts of v for each recovered address is equal to the sender's address that v is the correct one.

y_parity_range = [0, 1]

for y_parity in y_parity_range:
    temp_v = chainId * 2 + 35 + y_parity
    recovered_address = Account.recoverHash(
        message_hash=unsigned_hash,
        vrs=(temp_v, r, s)
    )

    if recovered_address == origin_address:
        v = temp_v

Your Answer

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

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