trezor / python-trezor

:snake: Don't use this repo, use the new monorepo instead:
https://github.com/trezor/trezor-firmware
GNU Lesser General Public License v3.0
201 stars 194 forks source link

Could somebody please tell me what is the output format of the sign_message function (in client.py)? #255

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hello,

I am playing around with the sign_message function and am trying to decipher the output. I am assuming that this is an ecdsa (secp256k1) signature, but is that even correct? The first byte seems like a length, perhaps of r? Followed by r, then by s? But this does not seem to be the case. Any help would be appreciated.

Thank you!

matejcik commented 6 years ago

the first byte is a flag, followed by 32byte R and 32byte S

The purpose of the flag is to allow reconstructing the address from the signature. The meaning is somewhat obscure; i'm recovering it from the code (and asking my colleagues :) ).

basically:

27
+ <key recovery bit>
+ <4 if compressed key>
+ <4 if P2SH-segwit>
+ <8 if native segwit>

where:

So:

and the resulting flag is 27 + <key recovery bit> + <address type>. This is enough information to recover the btc address used to sign this message.

ghost commented 6 years ago

Thank you! This is very helpful. I was under the impression that R and S could be different than 32 bytes, but perhaps that only in the DER encoding? In any case, I will continue to play around and see if what I want to do works. Thank you again.