realaravinth
2609ade2a5
- import frontend code from mCaptcha/benchs - setup TS compilation workflow - import vendor code from mCaptcha/mCaptcha/browser - rewrite bench.js import in bundle.js to include file's hash
139 lines
4.2 KiB
JavaScript
139 lines
4.2 KiB
JavaScript
import * as wasm from './mcaptcha_browser_bg.wasm';
|
|
|
|
let WASM_VECTOR_LEN = 0;
|
|
|
|
let cachegetUint8Memory0 = null;
|
|
function getUint8Memory0() {
|
|
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
|
|
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetUint8Memory0;
|
|
}
|
|
|
|
const lTextEncoder = typeof TextEncoder === 'undefined' ? (0, module.require)('util').TextEncoder : TextEncoder;
|
|
|
|
let cachedTextEncoder = new lTextEncoder('utf-8');
|
|
|
|
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
|
|
? function (arg, view) {
|
|
return cachedTextEncoder.encodeInto(arg, view);
|
|
}
|
|
: function (arg, view) {
|
|
const buf = cachedTextEncoder.encode(arg);
|
|
view.set(buf);
|
|
return {
|
|
read: arg.length,
|
|
written: buf.length
|
|
};
|
|
});
|
|
|
|
function passStringToWasm0(arg, malloc, realloc) {
|
|
|
|
if (realloc === undefined) {
|
|
const buf = cachedTextEncoder.encode(arg);
|
|
const ptr = malloc(buf.length);
|
|
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
WASM_VECTOR_LEN = buf.length;
|
|
return ptr;
|
|
}
|
|
|
|
let len = arg.length;
|
|
let ptr = malloc(len);
|
|
|
|
const mem = getUint8Memory0();
|
|
|
|
let offset = 0;
|
|
|
|
for (; offset < len; offset++) {
|
|
const code = arg.charCodeAt(offset);
|
|
if (code > 0x7F) break;
|
|
mem[ptr + offset] = code;
|
|
}
|
|
|
|
if (offset !== len) {
|
|
if (offset !== 0) {
|
|
arg = arg.slice(offset);
|
|
}
|
|
ptr = realloc(ptr, len, len = offset + arg.length * 3);
|
|
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
|
|
const ret = encodeString(arg, view);
|
|
|
|
offset += ret.written;
|
|
}
|
|
|
|
WASM_VECTOR_LEN = offset;
|
|
return ptr;
|
|
}
|
|
|
|
let cachegetInt32Memory0 = null;
|
|
function getInt32Memory0() {
|
|
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
|
|
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetInt32Memory0;
|
|
}
|
|
|
|
const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;
|
|
|
|
let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
|
|
cachedTextDecoder.decode();
|
|
|
|
function getStringFromWasm0(ptr, len) {
|
|
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
}
|
|
/**
|
|
* generate proof-of-work
|
|
* ```rust
|
|
* fn main() {
|
|
* use mcaptcha_browser::*;
|
|
* use pow_sha256::*;
|
|
*
|
|
*
|
|
* // salt using which PoW should be computed
|
|
* const SALT: &str = "yrandomsaltisnotlongenoug";
|
|
* // one-time phrase over which PoW should be computed
|
|
* const PHRASE: &str = "ironmansucks";
|
|
* // and the difficulty factor
|
|
* const DIFFICULTY: u32 = 1000;
|
|
*
|
|
* // currently gen_pow() returns a JSON formated string to better communicate
|
|
* // with JavaScript. See [PoW<T>][pow_sha256::PoW] for schema
|
|
* let serialised_work = gen_pow(SALT.into(), PHRASE.into(), DIFFICULTY);
|
|
*
|
|
*
|
|
* let work: Work = serde_json::from_str(&serialised_work).unwrap();
|
|
*
|
|
* let work = PoWBuilder::default()
|
|
* .result(work.result)
|
|
* .nonce(work.nonce)
|
|
* .build()
|
|
* .unwrap();
|
|
*
|
|
* let config = ConfigBuilder::default().salt(SALT.into()).build().unwrap();
|
|
* assert!(config.is_valid_proof(&work, &PHRASE.to_string()));
|
|
* assert!(config.is_sufficient_difficulty(&work, DIFFICULTY));
|
|
* }
|
|
* ```
|
|
* @param {string} salt
|
|
* @param {string} phrase
|
|
* @param {number} difficulty_factor
|
|
* @returns {string}
|
|
*/
|
|
export function gen_pow(salt, phrase, difficulty_factor) {
|
|
try {
|
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
var ptr0 = passStringToWasm0(salt, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
var len0 = WASM_VECTOR_LEN;
|
|
var ptr1 = passStringToWasm0(phrase, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
var len1 = WASM_VECTOR_LEN;
|
|
wasm.gen_pow(retptr, ptr0, len0, ptr1, len1, difficulty_factor);
|
|
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
return getStringFromWasm0(r0, r1);
|
|
} finally {
|
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
wasm.__wbindgen_free(r0, r1);
|
|
}
|
|
}
|
|
|