bitcoinjs / bitcoinjs-message

MIT License
129 stars 79 forks source link

directly from docs, getting an "off-by-one" error #24

Closed pyramation closed 4 years ago

pyramation commented 4 years ago

Directly from the docs, I've created a test case. Instead of the expected value, I'm getting H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=, so the G and the H are the only difference.

Did I do anything wrong?

const bitcoin = require("bitcoinjs-lib"); // v4.x.x
const bitcoinMessage = require("bitcoinjs-message");

describe(
  'straight from the docs', () => {
    it('should work but does not', () => {

      const keyPair = bitcoin.ECPair.fromWIF('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')
      var privateKey = keyPair.privateKey
      var message = 'This is an example of a signed message.'

      var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)
      console.log(signature.toString('base64'))
      // => 'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
      expect(signature.toString('base64')).toEqual(
        'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=')
    })
  }
)
pyramation commented 4 years ago

I made a test repo for anyone to try and verify or reproduce: https://github.com/pyramation/bitcoinjs-message-issue-24

pyramation commented 4 years ago

looking at fixtures, and trying to guess a little, is there some mixup w compressed or uncompressed happening?

pyramation commented 4 years ago

If I set compressed manually to false, it works. Is this an issue with bitcoinjs or message?

describe(
  'straight from the docs', () => {
    it('example from doc fails', () => {

      const keyPair = bitcoin.ECPair.fromWIF('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')
      var privateKey = keyPair.privateKey
      var message = 'This is an example of a signed message.'

      var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed)
      console.log(signature.toString('base64'))
      // => 'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
      expect(signature.toString('base64')).toEqual(
        'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=')
    })
    it('compressed set to false works', () => {

      const keyPair = bitcoin.ECPair.fromWIF('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1')
      var privateKey = keyPair.privateKey
      var message = 'This is an example of a signed message.'

      var signature = bitcoinMessage.sign(message, privateKey, false)
      console.log(signature.toString('base64'))
      // => 'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
      expect(signature.toString('base64')).toEqual(
        'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=')
    })

  }
)
junderw commented 4 years ago

Thanks for the catch. Someone asked us to change the README examples to compressed but I forgot to change the signatures themselves.

If you could make a PR I'll merge it.

junderw commented 4 years ago

Fixed in a5107632abe1477b337adffd1f4cd69652a2cbbf