Skip to content

Commit

Permalink
update scrypt_wasm.cjs lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Jan 5, 2024
1 parent 8267686 commit d772fb8
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 190 deletions.
11 changes: 5 additions & 6 deletions faucet-wasm/scrypt/build_wasm.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

const base32768 = require('base32768');
const fs = require('fs');
import { encode } from "base32768";
import fs from "fs";

const base32768WASM = base32768.encode(fs.readFileSync("scrypt-wasm/pkg/scrypt_wasm_bg.wasm"));
const base32768WASM = encode(fs.readFileSync("scrypt-wasm/pkg/scrypt_wasm_bg.wasm"));

const wasmWrappperJS = fs.readFileSync("scrypt-wasm/pkg/scrypt_wasm_bg.js", { encoding: "utf8" });
let lines = wasmWrappperJS.split("\n");
Expand Down Expand Up @@ -45,13 +45,12 @@ module.exports = {

// Now its time to load the wasm module.
// first, load the base32768 module into a global variable called "base32768"
console.log(fs.readFileSync("node_modules/base32768/dist/iife/base32768.js", { encoding: "utf8" }))
console.log(fs.readFileSync("node_modules/base32768/src/index.js", { encoding: "utf8" }).replace(/^export .*$/m, ""))

// now, decode the base32768 string into an ArrayBuffer and tell WebAssembly to load it
console.log(`
const base32768WASM = "${base32768WASM}";
const wasmBinary = base32768.decode(base32768WASM);
const wasmBinary = decode(base32768WASM);
scryptPromise = WebAssembly.instantiate(wasmBinary, {}).then(instantiatedModule => {
`);
Expand Down
31 changes: 16 additions & 15 deletions faucet-wasm/scrypt/build_wasm.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#!/bin/bash -e

if [ ! -f build_scrypt_wasm.sh ]; then
if [ ! -f build_wasm.sh ]; then
printf "Please run this script from the faucet-wasm folder.\n"
fi

nodejs_is_installed="$(which node | wc -l)"
npm_is_installed="$(which npm | wc -l)"

if [ "$nodejs_is_installed" == "0" ] || [ "$npm_is_installed" == "0" ]; then
printf "nodejs and npm are required for the next step. Please install them manually 😇"
exit 1
fi

if [ ! -d node_modules ]; then
printf "running npm install \n"
npm install
fi

if [ ! -d scrypt-wasm ]; then
printf "Cloning https://github.com/pk910/scrypt-wasm... \n"
git clone https://github.com/pk910/scrypt-wasm.git
Expand All @@ -29,24 +42,12 @@ fi

if [ ! -d pkg ]; then
printf "running Makefile for scrypt-wasm... \n"
make
rustup target add wasm32-unknown-unknown
$(pwd)/../node_modules/.bin/wasm-pack build --target browser
fi

cd ../

nodejs_is_installed="$(which node | wc -l)"
npm_is_installed="$(which npm | wc -l)"

if [ "$nodejs_is_installed" == "0" ] || [ "$npm_is_installed" == "0" ]; then
printf "nodejs and npm are required for the next step. Please install them manually 😇"
exit 1
fi

if [ ! -d node_modules ]; then
printf "running npm install \n"
npm install
fi

node build_wasm.js > "../../libs/scrypt_wasm.cjs"

printf "\n\nbuilt ../../libs/scrypt_wasm.cjs successfully!\n\n"
Expand Down
296 changes: 290 additions & 6 deletions faucet-wasm/scrypt/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d772fb8

Please sign in to comment.