bitcoinjs / bitcoinjs-message

MIT License
128 stars 79 forks source link

invalid signature: keyPair.compressed is false #30

Closed roccomuso closed 4 years ago

roccomuso commented 4 years ago

Why this produce by default a wrong signature

const bip32 = require('bip32')
const bip39 = require('bip39')
const bitcoin = require('bitcoinjs-lib')
const bitcoinMessage = require('bitcoinjs-message')

const mnemonic = 'praise you muffin lion enable neck grocery crumble super myself license ghost'
const seed = bip39.mnemonicToSeedSync(mnemonic)
const root = bip32.fromSeed(seed)

const path = `m/44'/0'/0'/0/0`
const keyPair = root.derivePath(path)

const { privateKey, publicKey } = keyPair
const { address } = bitcoin.payments.p2pkh({ pubkey: publicKey })
console.log('address:', address)

const message = 'test'
const signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)
const verified = bitcoinMessage.verify(message, address, signature)

console.log('signature:', signature.toString('base64'))
console.log('valid:', verified)

I noticed the first byte of the signature is wrong ("H", but it should be "I"), and it's all about keyPair.compressed, why is that false? that should be true in order to get a valid signature

junderw commented 4 years ago

ECPair has compressed attribute, but bip32 is always compressed so it does not have a compressed element.

junderw commented 4 years ago

I agree it is confusing since a lot of tests in the bitcoinjs suite tend to use ECPair and bip32 interchangeably.

I have pushed a new patch for BIP32 v2.0.6

If you reinstall bitcoinjs-lib from scratch it should pull in the new bip32 patch with the fix.