Created
August 25, 2023 06:13
-
-
Save 0xJohnZW/0d23fb3391a6089efb7576b8c993ab3a to your computer and use it in GitHub Desktop.
verify chip0002 signMessage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// yarn add bls-signatures clvm clvm_tools | |
import loadBls from 'bls-signatures'; | |
function verifySignature(bls: any, publicKey: string, signature: string, message: string) { | |
let bytes_to_verify = null; | |
if (message.slice(0, 2) === '0x') { | |
bytes_to_verify = clvm.Bytes.from(message, 'hex'); | |
} else { | |
bytes_to_verify = clvm.Bytes.from(message, 'utf8') | |
} | |
const synthetic_message = clvm_tools.sha256tree( | |
clvm.SExp.to("Chia Signed Message").cons(bytes_to_verify) as any | |
).raw(); | |
const is_valid = bls.AugSchemeMPL.verify( | |
bls.G1Element.from_bytes(clvm.Bytes.from(publicKey, 'hex').data()), | |
synthetic_message, | |
bls.G2Element.from_bytes(clvm.Bytes.from(signature, 'hex').data()), | |
) | |
return is_valid | |
} | |
async run() { | |
let publicKey = '0x8266b475a8e43ab2ac32f49d7cf7835b7f45b8c242288f6226e275ea5ccc29f199333ba894ba3782b963c79ea65fe792'; | |
let signature = '0xb8a24306ff71851b241c101e4265a8711b56f0f570c9d35b6465db7d218765503d109386220ae722702813a77c6fee5404fc2905612a8f78a25d8fb7d05afda7974e92fcbed02abbbcaec9e8ffb2cabe5721b9a322b666be6d9b1ae939089082'; | |
let message = '0x8266b475a8e43ab2ac32f49d7cf7835b7f45b8c242288f6226e275ea5ccc29f199333ba894ba3782b963c79ea65fe792'; | |
const bls = await loadBls(); | |
const is_valid = verifySignature(bls, publicKey, res, values.message) | |
console.log(is_valid); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment