Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Signing identical transaction results in different raw tx #53

Closed
ashchan opened this issue Apr 13, 2018 · 6 comments
Closed

Signing identical transaction results in different raw tx #53

ashchan opened this issue Apr 13, 2018 · 6 comments
Labels

Comments

@ashchan
Copy link

ashchan commented Apr 13, 2018

When creating EthereumTransaction multiple times (all data including nonce are identical) and signing with the same private key, it gets different raw tx(rlped tx) with EthereumTransaction.encode(forSignature: false). Is this expected behavior?

Looks like r and s from secp256k1 are changing each time.

@shamatar
Copy link
Contributor

Hello @ashchan

It's indeed an expected behavior. Secp256k1 library from Bitcoin allows to supply an additional entropy when signing, and it's utilized by web3swift. In addition, an explicit public key recovery from the signature is done to ensure that recovered signer key matches the private key.

Sincerely, Alex

@ashchan
Copy link
Author

ashchan commented Apr 13, 2018

@shamatar

Thanks for responding quickly.

I looked at the Secp256k1 wrapper and found that it indeed uses a random entropy while calling secp256k1_ecdsa_sign_recoverable. (go-ethereum and other libraries don't seem to do so).

On a second thoughts, one issue with this behavior is the txhash would change when signing the same tx multiple times.

Closing this. Thank you.

@ashchan ashchan closed this as completed Apr 13, 2018
@ashchan ashchan reopened this Apr 13, 2018
@shamatar
Copy link
Contributor

@ashchan

Yes, TX hash can change, but nevertheless it's completely determined by the content and signature and can not change after being signed and sent, so identifier after a successfully sending it will stay. I'm not aware of any case when knowledge of completely deterministic signature is required before production of the signature (you can not get it without a private key anyway), but if you do - please post here.

Sincerely, Alex

@shamatar
Copy link
Contributor

@ashchan

I've added an extra test for deterministic (without extra entropy) signature, although it fails in 50% cases in simulator and on iOS device.

Sincerely, Alex

@shamatar
Copy link
Contributor

I've found an error that made a signature fail 50% of time without extra entropy. Now deterministic signatures work and I can set them as defaults if necessary, although having some extra entropy is usually a good option.

@ashchan
Copy link
Author

ashchan commented Apr 14, 2018

@shamatar Awesome! Just did some tests, now Ethereum transaction sign is consistent. Also checked that with MEW and they produce the same raw tx for the same transaction.

Thank you!

@skywinder skywinder added bug and removed question labels Apr 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants