Last active
April 16, 2023 23:23
-
-
Save mattlockyer/e9602796deab409e5ca9d067aeef3670 to your computer and use it in GitHub Desktop.
NEAR Protocol - matching base64 strings in JS and Rust
This file contains hidden or 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
// 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') | |
}) | |
This file contains hidden or 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
// 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