Skip to content

Instantly share code, notes, and snippets.

@matiu
Created March 7, 2019 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiu/5855088740bfd76bd5453179f777ef3e to your computer and use it in GitHub Desktop.
Save matiu/5855088740bfd76bd5453179f777ef3e to your computer and use it in GitHub Desktop.
Checking a jsonPaymentProtocol signature witih bitcore-lib
const B = require('bitcore-lib');
// Example signature, from an actual jsonPaymentProtocol header.
const h ='6d580c9bc2fbaff7614e3a030d536417d07d6dfb93f7c8887aac4e67e27c3e32';
const s ='e7a6490572855eb8feedabe32872424a2aef5b0b0f6e8b7d1959f6621de8c15a3871283a8fa78fc292444c50dc578dc58bd924d947f2ecc4bb91c250885a518a';
const p ='03159069584176096f1c89763488b94dbc8d5e1fa7bf91f50b42f4befe4e45295a';
hbuf = Buffer.from(h,'hex');
sbuf = Buffer.from(s,'hex');
pbuf = Buffer.from(p,'hex');
// Signature is in the form: r[0..31]s[0..31]
// split it and convert it to a Bitcore Signature
s_r = new Buffer(32);
s_s = new Buffer(32);
sbuf.copy(s_r,0,0);
sbuf.copy(s_s,0,32);
s_rBN = B.crypto.BN.fromBuffer(s_r);
s_sBN = B.crypto.BN.fromBuffer(s_s);
sig = new B.crypto.Signature();
sig.set({r: s_rBN, s: s_sBN});
// Public key
pub = B.PublicKey.fromBuffer(pbuf);
console.log('Bitcore VERIFY:', B.crypto.ECDSA.verify(hbuf, sig, pub));
// => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment