Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccamrobertson/31f42783a20b57f18d511d67089d6a45 to your computer and use it in GitHub Desktop.
Save ccamrobertson/31f42783a20b57f18d511d67089d6a45 to your computer and use it in GitHub Desktop.
HaLo c5+ rndsig Verification Example
// HaLo Core version 01.C5 onwards
// npm i --save elliptic sha.js
const EC = require('elliptic').ec;
const shajs = require('sha.js');
const ec = new EC('secp256k1');
// --- INPUT PARAMETERS ---
// just as they appear in the URL
const pk2 = "0481AD57A1D12B58D46DACE9746250725889EC7677CF865F5B02C9A0768DECB64FEE6F3CD094C86F7841A55C5286B256DF70986864CA788CF2AD91C5DBACC4BC72";
const rnd = "000000020A56B892CDC5A52E2964ACC88F79BFE6234BC864BE2ED2481E937398";
const rndsig = "3045022100B49164BEC8377DDE39BFDA54A8258578494CC562D75A4056E059A636760D68BD02202539D8A55DA3C652222387C208DC7BF72E07DA25042986F5B2A8ED2C1598327004";
// --- VERIFIER CODE ---
function verifyURLSig(pk2, rnd, rndsig) {
const rndBuf = Buffer.from(rnd, "hex");
if (rndBuf.length !== 32) {
throw new Error("Incorrect length of the rnd parameter or hex decoding failure.");
}
const msgHashed = shajs('sha256')
// static prefix: b'\x19Attest counter pk2:\n'.hex()
.update(Buffer.concat([
Buffer.from([0x19]),
Buffer.from("Attest counter pk2:\n", "utf8"),
]))
.update(rndBuf)
.digest('hex');
const sigBuf = Buffer.from(rndsig, "hex");
if (sigBuf.length < 2 || 2 + sigBuf[1] > sigBuf.length) {
throw new Error("Malformed signature in the rndsig field.");
}
const cutSig = sigBuf.subarray(0, 2 + sigBuf[1]).toString("hex");
let key = null;
try {
key = ec.keyFromPublic(pk2, 'hex');
} catch (e) {
throw new Error("Unable to decode public key.");
}
const pk2Exported = key.getPublic(/* compact: */ false, 'hex');
if (key.verify(msgHashed, cutSig)) {
const counter = rndBuf.readUInt32BE(0);
return {
// always returns uncompressed public key
"publicKey2": pk2Exported,
// the scan counter that was validated
"scanCounter": counter
};
} else {
throw new Error("Failed to verify signature!");
}
}
// --- USAGE ---
try {
const validationResult = verifyURLSig(pk2, rnd, rndsig);
console.log('The URL is correctly signed with:');
console.log(validationResult);
} catch (e) {
console.error('Failed to validate the URL\'s signature.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment