bitauth / libauth

An ultra-lightweight, zero-dependency TypeScript library for Bitcoin Cash, Bitcoin, and Bitauth applications.

Home Page:https://libauth.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question regarding signing and verifying messages

2qx opened this issue · comments

commented

Hello @bitjson , trying to get signed and verified string messages on mainnet-js but hitting a bit of an issue with the signatures from libauth.

For testing, the example from electron-cash was taken, but implemented for mainnet-js here:

https://github.com/mainnet-cash/mainnet-js/blob/c4345dd17e2e1c83509dad661ed745fde33efaa4/src/message/signed.test.ts#L27

message: "Chancellor on brink of second bailout for banks" 
PrivateKey: L1TnU2zbNaAqMoVh65Cyvmcjzbrj41Gs9iTLcWbpJCMynXuap6UN
Cash Address: bitcoincash:qqehccy89v7ftlfgr9v0zvhjzyy7eatdkqt05lt3nw

The implementation to sign is here:

https://github.com/mainnet-cash/mainnet-js/blob/c4345dd17e2e1c83509dad661ed745fde33efaa4/src/message/signed.ts#L57

After applying the message formatting and using secp256k1.signMessageHashRecoverableCompact, this returns a signature of:

/2Mw6ePgwVsfd3u3jIJD2LsOBlT9VnbvzDf7JK/YXjIix2qOxzmeDeSY3w5kBOGDJ8Jk5DFkJbNr1XlfOVVjRg==

From what I understand, with different nonces there can be different there can be different signatures. However, the resulting signature from libauth with secp256k1.signMessageHashRecoverableCompact() throws an error on https://tools.bitcoin.com/verify-message/ as being too short.

There have been separate tests to break out and test the message magic formatting and they appear to match what is created by electron-cash.

It's not clear from the documentation what the RecoveryId is used for. I'm thinking that perhaps this id should have been included in the signature.

Its there anything that clearly stands out as being incorrect. Would it be possible to use signMessageHashSchnorr and future-proof against leaking keys using RFC6979?

commented

I think may have figured out the recoveryId and the signature length outlined above.

It seems that the recoveryId and compression is encoded and prepended to the signature like so:

https://github.com/Electron-Cash/Electron-Cash/blob/49f9f672364f50053a026e4a5cb30e92db2d195d/electroncash/bitcoin.py#L662

Further, the signatures (from libauth) without the prefix seem to match electron-cash exactly.