bitcoinjs-message
Advanced tools
Comparing version 2.1.1 to 2.1.2
52
index.js
@@ -116,6 +116,25 @@ const bs58check = require('bs58check') | ||
function verify (message, address, signature, messagePrefix) { | ||
function segwitRedeemHash (publicKeyHash) { | ||
const redeemScript = Buffer.concat([ | ||
Buffer.from('0014', 'hex'), | ||
publicKeyHash | ||
]) | ||
return hash160(redeemScript) | ||
} | ||
function decodeBech32 (address) { | ||
const result = bech32.decode(address) | ||
const data = bech32.fromWords(result.words.slice(1)) | ||
return Buffer.from(data) | ||
} | ||
function verify (message, address, signature, messagePrefix, checkSegwitAlways) { | ||
if (!Buffer.isBuffer(signature)) signature = Buffer.from(signature, 'base64') | ||
const parsed = decodeSignature(signature) | ||
if (checkSegwitAlways && !parsed.compressed) { | ||
throw new Error('checkSegwitAlways can only be used with a compressed pubkey signature flagbyte') | ||
} | ||
const hash = magicHash(message, messagePrefix) | ||
@@ -133,18 +152,27 @@ const publicKey = secp256k1.recover( | ||
if (parsed.segwitType === SEGWIT_TYPES.P2SH_P2WPKH) { | ||
const redeemScript = Buffer.concat([ | ||
Buffer.from('0014', 'hex'), | ||
publicKeyHash | ||
]) | ||
const redeemScriptHash = hash160(redeemScript) | ||
actual = redeemScriptHash | ||
actual = segwitRedeemHash(publicKeyHash) | ||
expected = bs58check.decode(address).slice(1) | ||
} else if (parsed.segwitType === SEGWIT_TYPES.P2WPKH) { | ||
const result = bech32.decode(address) | ||
const data = bech32.fromWords(result.words.slice(1)) | ||
actual = publicKeyHash | ||
expected = Buffer.from(data) | ||
expected = decodeBech32(address) | ||
} | ||
} else { | ||
actual = publicKeyHash | ||
expected = bs58check.decode(address).slice(1) | ||
if (checkSegwitAlways) { | ||
try { | ||
expected = decodeBech32(address) | ||
// if address is bech32 it is not p2sh | ||
return bufferEquals(publicKeyHash, expected) | ||
} catch (e) { | ||
const redeemHash = segwitRedeemHash(publicKeyHash) | ||
expected = bs58check.decode(address).slice(1) | ||
// base58 can be p2pkh or p2sh-p2wpkh | ||
return ( | ||
bufferEquals(publicKeyHash, expected) || | ||
bufferEquals(redeemHash, expected) | ||
) | ||
} | ||
} else { | ||
actual = publicKeyHash | ||
expected = bs58check.decode(address).slice(1) | ||
} | ||
} | ||
@@ -151,0 +179,0 @@ |
{ | ||
"name": "bitcoinjs-message", | ||
"version": "2.1.1", | ||
"version": "2.1.2", | ||
"description": "bitcoinjs-message", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,6 +8,6 @@ # bitcoinjs-message | ||
## Examples | ||
## Examples (Note about Electrum support at the bottom) | ||
``` javascript | ||
var bitcoin = require('bitcoinjs-lib') // v3.x.x | ||
var bitcoin = require('bitcoinjs-lib') // v4.x.x | ||
var bitcoinMessage = require('bitcoinjs-message') | ||
@@ -24,3 +24,3 @@ ``` | ||
``` javascript | ||
var keyPair = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss') | ||
var keyPair = bitcoin.ECPair.fromWIF('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1') | ||
var privateKey = keyPair.privateKey | ||
@@ -31,3 +31,3 @@ var message = 'This is an example of a signed message.' | ||
console.log(signature.toString('base64')) | ||
// => 'G9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=' | ||
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=' | ||
``` | ||
@@ -38,3 +38,3 @@ | ||
var { randomBytes } = require('crypto') | ||
var keyPair = bitcoin.ECPair.fromWIF('5KYZdUEo39z3FPrtuX2QbbwGnNP5zTd7yyr2SC1j299sBCnWjss') | ||
var keyPair = bitcoin.ECPair.fromWIF('L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1') | ||
var privateKey = keyPair.privateKey | ||
@@ -61,7 +61,7 @@ var message = 'This is an example of a signed message.' | ||
> verify(message, address, signature[, network.messagePrefix]) | ||
> verify(message, address, signature[, network.messagePrefix, checkSegwitAlways]) | ||
Verify a Bitcoin message | ||
``` javascript | ||
var address = '1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN' | ||
var address = '1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV' | ||
@@ -72,2 +72,7 @@ console.log(bitcoinMessage.verify(message, address, signature)) | ||
## About Electrum segwit signature support | ||
- For Signing: Use the non-segwit compressed signing parameters for both segwit types (p2sh-p2wpkh and p2wpkh) | ||
- For Verifying: Pass the checkSegwitAlways argument as true. (messagePrefix should be set to null to default to Bitcoin messagePrefix) | ||
## LICENSE [MIT](LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10649
168
73