Skip to content

Instantly share code, notes, and snippets.

@dev-bjoern
Last active August 31, 2022 15:24
Show Gist options
  • Save dev-bjoern/e4ecd3bd949d4824dde4b6ab77343729 to your computer and use it in GitHub Desktop.
Save dev-bjoern/e4ecd3bd949d4824dde4b6ab77343729 to your computer and use it in GitHub Desktop.
how to verify goby signature
const loadBls = require("bls-signatures");
const crypto = require("crypto");
(async () => {
const BLS = await loadBls();
const encoder = new TextEncoder('utf-8')
function formatMessage(message) {
const prefix = `\u0018Chia Signed Message:\n${message.length}`;
const hash = crypto.createHash('sha256');
hash.update(encoder.encode(prefix))
hash.update(message)
return hash.digest('hex')
}
function hexToBytes(hex) {
if (hex.startsWith("0x")) {
hex = hex.slice(2);
}
const bytes = Uint8Array.from(hex.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
return bytes
}
function verify(message, publicKey, signature) {
const syntheticMessage = formatMessage(message);
publicKey = BLS.G1Element.from_bytes(hexToBytes(publicKey));
signature = BLS.G2Element.from_bytes(hexToBytes(signature));
console.log("verified: ", BLS.AugSchemeMPL.verify(publicKey, hexToBytes(syntheticMessage), signature));
}
verify(encoder.encode("hello chia"), "0x858024f64b04a9abc6622a61e2c823b78a43d219c502719d12a78fd6cad5daa31f80254b1803689a9433960c609f9b92", "0x8ee997eb62ad11a9b781d123862d3fe9cfb65a7862c1e0a86618c739119d636a85722e87bdbc3da4cd5a1676dc3836c617a2b176e91dd7b8f8d126fbec509d751e2f811db009a01d0a4438b3c2eaf99b51b76d805700405564b9fc7b52a29625");
})();
@0xJohnZW
Copy link

// L16 Uint8Array concat error
hash.update(encoder.encode(prefix))
hash.update(message)


//L39 syntheticMessage should be Uint8Array
console.log("verified: ", BLS.AugSchemeMPL.verify(publicKey, hexToBytes(syntheticMessage), signature));

@dev-bjoern
Copy link
Author

dev-bjoern commented Aug 31, 2022

@roseiliend Ah I see, this totally works now. Thanks!

I updated the gist to a working version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment