blockchainofthings / btc-message-verifier

JavaScript library used to verify a message that had been signed using Bitcoin's approach.
MIT License
2 stars 2 forks source link

Bitcoin Message Verifier

A JavaScript library used to verify a message that had been signed using Bitcoin's approach.

Rationale

Bitcoin's client reference implementation, Bitcoin Core, provides an RPC method to sign a message with a bitcoin address (signmessage) and another to verify the signed message (verifymessage). However, as of this writing, Bitcoin Core is not able to sign and verify messages using segregated witness (bech32) addresses. Also, it is not possible to verify a signed message using a public key hash instead of a bitcoin address.

This library was put together to overcome these two limitations.

Implementation

This library uses bitcoinjs-message module's approach to sign messages with bech32 addresses where a bit (0x08) in the signature's first byte is used to flag that a bech32 address was used to sign the message.

Installation

On Node.js:

npm install btc-message-verifier

On the browser:

<script src="https://unpkg.com/btc-message-verifier"></script>

Browser compatibility

The Bitcoin Message Verifier library is compatible with modern web browsers.

It has been tested on the following web browsers:

Note: Internet Explorer is not supported.

Usage

Call the library's single method verifyMessage() to verify a signed message. A boolean value is then returned indicating whether the message is valid or not.

On Node.js:

const btcMsgVerifier = require('btc-msg-verifier');

const result = btcMsgVerifier.verifyMessage(...);

On the browser:

<script>
const result = btcMsgVerifier.verifyMessage(...);
</script>

Input parameters:

Examples

Verify message signed with a legacy bitcoin address

On Node.js:

try {
  const result = btcMsgVerifier.verifyMessage(
    'mkPa87aADjgNYfiVNV7tUkxmbB3qmbE7cg',
    'H+dKQFYTbZwwqcExYap/ykVG23qRuxEbFOVfFoPBnBKWfI5jDPt61Y9pchdGkDULQ+x79ke8SosVpYygyVX8TDg=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}

On the browser:

<script>
try {
  const result = btcMsgVerifier.verifyMessage(
    'mkPa87aADjgNYfiVNV7tUkxmbB3qmbE7cg',
    'H+dKQFYTbZwwqcExYap/ykVG23qRuxEbFOVfFoPBnBKWfI5jDPt61Y9pchdGkDULQ+x79ke8SosVpYygyVX8TDg=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}
</script>

Verify message signed with a bech32 address

On Node.js:

try {
  const result = btcMsgVerifier.verifyMessage(
    'bcrt1qwfwu4tawzwdh233qlqwknk2r3quefyalvpwqyg',
    'KBbtz1fqBrQdioHfM452V8pHWsMAAxyakjmVkxq3BuJiZgU5ln5nepJfs/3MrY6eeivElMIOM+nVVbbocU6zzmU=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}

On the browser:

<script>
try {
  const result = btcMsgVerifier.verifyMessage(
    'bcrt1qwfwu4tawzwdh233qlqwknk2r3quefyalvpwqyg',
    'KBbtz1fqBrQdioHfM452V8pHWsMAAxyakjmVkxq3BuJiZgU5ln5nepJfs/3MrY6eeivElMIOM+nVVbbocU6zzmU=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}
</script>

Verify message passing a public key hash

On Node.js:

try {
  const result = btcMsgVerifier.verifyMessage(
    Buffer.from('543fc89cd1e5bda2ba3c82f4b48e3a82694498be', 'hex'),
    'IEk3JJnp8iKhAgp50hWCE7TCNciaDdrb8YNXWvqZzIUgcLvESYJO/ZFigJ726vpQ12tY6SDGg92aouzUMXKAgUE=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}

On the browser:

<script>
try {
  const result = btcMsgVerifier.verifyMessage(
    btcMsgVerifier.Buffer.from('543fc89cd1e5bda2ba3c82f4b48e3a82694498be', 'hex'),
    'IEk3JJnp8iKhAgp50hWCE7TCNciaDdrb8YNXWvqZzIUgcLvESYJO/ZFigJ726vpQ12tY6SDGg92aouzUMXKAgUE=',
    'Text to sign'
  );

  if (result) {
    console.log('Message successfully verified');
  }
  else {
    console.log('Message could not be verified');
  }
}
catch (err) {
  console.error(err);
}
</script>

License

This JavaScript library is released under the MIT License. Feel free to fork, and modify!

Copyright © 2020, Blockchain of Things Inc.