Created
June 15, 2022 09:02
-
-
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
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
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