Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V in Signed Message Hash not being calculated properly #191

Closed
DomAmato opened this issue May 7, 2019 · 4 comments · Fixed by #201
Closed

V in Signed Message Hash not being calculated properly #191

DomAmato opened this issue May 7, 2019 · 4 comments · Fixed by #201

Comments

@DomAmato
Copy link

DomAmato commented May 7, 2019

I have a python application that we were using to work with ethereum and now we are trying to do something similar on osx/ios. I wanted to test signing a string similar to how we were doing it but kept running into an issue where the last 2 characters of the signed message were different.

Here is the swift code:

let message = Web3.Utils.sha3("HelloABC123".data(using: .utf8)!)
let (compressedSignature, _) = SECP256K1.signForRecovery(hash: message!, privateKey: Data(hex: privateKey), useExtraEntropy: false)

Here is the python code:

msg_hash = ethereum.utils.sha3("HelloABC123")
sig_object = w3.eth.account.signHash(msg_hash, private_key=key)

I confirmed the sha3 hash is the same between them
58b105f41aa555ea93c3f91eeebac19de54cd71e528b3317e6c0bbd49aa8f5b1

But the signature comes back and is just ever so slightly different
Swift
5c2b257aaa8810ae7648c15d4361e4c3f080922b98cccfa7304db470ca7fddd16fd1b1d2a42ba4d65787bc3c1971ec728c34bd5baf88a226db17550c54d591b101
Python
5c2b257aaa8810ae7648c15d4361e4c3f080922b98cccfa7304db470ca7fddd16fd1b1d2a42ba4d65787bc3c1971ec728c34bd5baf88a226db17550c54d591b11c

Basically this library always ends the signature with 01 or 00 and the python library ends it in 1c or 1b

According to this discussion about how v is calculated it seems this library is not properly handling it in this instance.

@ashishonmobile
Copy link

let web3 = Web3.InfuraRinkebyWeb3()
let message = "HelloABC123"
let signature = try web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: , password: )

try this

@DomAmato
Copy link
Author

Actually this seems like a bug in the library. It appears that it is not properly handling v in that signature. Instead of doing 27 + (r%2) it might be just applying the modulo. See the discussion here about how v is calculated.
https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v

This would make sense why the signature is always 00 or 01 instead of 1b or 1c

@DomAmato DomAmato changed the title Signed Message Hash is very slightly different V in Signed Message Hash not being calculated properly May 10, 2019
@BaldyAsh
Copy link
Collaborator

Hi, @DomAmato, @ashishonmobile . Thank you for this question, I'll fix it

@BaldyAsh
Copy link
Collaborator

Hi! Sorry for waiting so long, was on my vacation. Made a fix that just a kludge for now.
This whole problem with v arose because of the binding to secp256k1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants