Skip to content

Instantly share code, notes, and snippets.

@CjS77
Created February 5, 2020 10:02
Show Gist options
  • Save CjS77/6184fc319a4ffc8d64c37a5dd1d7a091 to your computer and use it in GitHub Desktop.
Save CjS77/6184fc319a4ffc8d64c37a5dd1d7a091 to your computer and use it in GitHub Desktop.
Node.js use of tari crypto
let tari_crypto = require('./pkg');
let assert = require('assert');
let KeyRing = tari_crypto.KeyRing;
console.log(`Tari crypto. Version ${tari_crypto.version()}`);
// The WASM module holds the keys in a vector (keyring), which means that we can get at all the cryptoey goodness
// without having to expose tons of functions with unsafe pointers, or continuously do de- and serialisation to hex
// or base64.
const keys = KeyRing.new();
console.log("Creating new keypair");
keys.new_key("Alice");
console.log("Generating signature");
let pubkey = keys.public_key("Alice");
console.log(`POST /free_tari/allocate/${pubkey}`);
let [r, R] = tari_crypto.generate_keypair();
let msg = `Hello Tari from ${pubkey}`;
console.log(`Message: |${msg}|`);
let sig = keys.sign("Alice", r, msg);
if (sig.error) {
console.log(`Error getting signature ${sig.error}`);
} else {
let body = { public_nonce: sig.public_nonce, signature: sig.signature };
console.log(JSON.stringify(body));
let check = tari_crypto.check_signature(R, sig.signature, pubkey, msg);
if (check.result === true) {
console.log("Signature is valid!");
} else {
console.log(`Invalid signature: ${check.error}`);
}
}
keys.free();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment