Skip to content

Instantly share code, notes, and snippets.

@sandoche
Created June 15, 2022 09:02
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 sandoche/e5cdfd5af1e50e892e5a536b7c3d74bf to your computer and use it in GitHub Desktop.
Save sandoche/e5cdfd5af1e50e892e5a536b7c3d74bf to your computer and use it in GitHub Desktop.
An example of how to verify a message signature with Near api js
const nearApi = require("near-api-js");
const { sha256 } = require("js-sha256");
const run = async () => {
const keyPairA = nearApi.utils.key_pair.KeyPairEd25519.fromRandom();
const publicKeyAString = keyPairA.getPublicKey();
const keyPairB = nearApi.utils.key_pair.KeyPairEd25519.fromRandom();
const publicKeyA = nearApi.utils.key_pair.PublicKey.from(publicKeyAString);
const message = new Uint8Array(sha256.array("message"));
const signature = keyPairA.sign(message);
console.log(keyPairA.verify(message, signature.signature)); // true
console.log(keyPairB.verify(message, signature.signature)); // false
console.log(publicKeyA.verify(message, signature.signature)); // true
};
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment