Skip to content

Instantly share code, notes, and snippets.

@zurgl
Created July 12, 2021 15:29
Show Gist options
  • Save zurgl/e79b6a58e0476f221a383e48764c3f4d to your computer and use it in GitHub Desktop.
Save zurgl/e79b6a58e0476f221a383e48764c3f4d to your computer and use it in GitHub Desktop.
some near stuff
const { connect, keyStores } = require("near-api-js");
const path = require("path");
const homedir = require("os").homedir();
const nacl = require("tweetnacl");
const sha256 = require("js-sha256");

const ACCOUNT_ID = "near-example.testnet";
const CREDENTIALS_DIR = ".near-credentials";

const credentialsPath = path.join(homedir, CREDENTIALS_DIR);
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(credentialsPath);

const config = {
    keyStore,
    networkId: "testnet",
    nodeUrl: "https://rpc.testnet.near.org",
};

verifySignature();

async function verifySignature() {
    const near = await connect({ ...config, keyStore });
    const account = await near.account(ACCOUNT_ID);
    const tokenMessage = new TextEncoder().encode(`Hello world, ${ACCOUNT_ID}`);
    const signatureData = await account.connection.signer.signMessage(
        tokenMessage,
        ACCOUNT_ID,
        "testnet"
    );
    const hash = new Uint8Array(sha256.sha256.array(tokenMessage));
    let isValid = nacl.sign.detached.verify(
        hash,
        signatureData.signature,
        signatureData.publicKey.data
    );
    console.log(isValid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment