Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Last active April 16, 2023 23:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattlockyer/e9602796deab409e5ca9d067aeef3670 to your computer and use it in GitHub Desktop.
Save mattlockyer/e9602796deab409e5ca9d067aeef3670 to your computer and use it in GitHub Desktop.
NEAR Protocol - matching base64 strings in JS and Rust
// JS (node - for browser you need to use btoa(String.fromCharCode(...new Uint8Array(...)))
const hash = sha256.create();
const hashesBase64 = [
'some string'
].map((src) => {
return Buffer.from(hash.update(src).array()).toString('base64')
})
// RUST
use near_sdk::{log, base64::{encode}};
let s = "some string".to_string()
let token_id = encode(env::sha256(format!("{}", s).as_bytes()));
// Notes for hex (hacky but matches JS sha256('some string'))
let s = "some string".to_string()
let token_id = format!("{:02x?}", env::sha256(
env::sha256(format!("{}", s).as_bytes()
)).replace(", ", "").replace("0x", "").replace("[", "").replace("]", "");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment