A JavaScript library used to verify a message that had been signed using Bitcoin's approach.
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.
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.
On Node.js:
npm install btc-message-verifier
On the browser:
<script src="https://unpkg.com/btc-message-verifier"></script>
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.
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:
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>
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>
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>
This JavaScript library is released under the MIT License. Feel free to fork, and modify!
Copyright © 2020, Blockchain of Things Inc.