Skip to content

Instantly share code, notes, and snippets.

@alamshafil
Created August 17, 2022 02:05
Show Gist options
  • Save alamshafil/383fcb4b9b3bad160a7a988aa9938465 to your computer and use it in GitHub Desktop.
Save alamshafil/383fcb4b9b3bad160a7a988aa9938465 to your computer and use it in GitHub Desktop.
Compiling libdogecoin to JS using Emscripten

Compiling libdogecoin to JS using Emscripten

This guide assumes you are using a Linux distro with build tools installed.

Step 1: Install Emscripten toolchain

You can read https://emscripten.org/docs/getting_started/downloads.html to help you install the Emscripten toolchain.

Step 2: Get libdogecoin source code

You can use git to clone the repo.

git clone github.com/dogecoinfoundation/libdogecoin

Step 3: Configure the code

First run:

./autogen.sh

Then run the configure script:

Note: Change /usr/lib/emscripten to your SDK path. <emsdk root directory>

/usr/lib/emscripten/emconfigure ./configure CC=/usr/lib/emscripten/emcc AR=/usr/lib/emscripten/emar --host wasm32-emscripten --disable-net --disable-dependency-tracking

Step 4. Once the configure script is done, compile!

To compile, type:

/usr/lib/emscripten/emmake make

Wait until the compiling process is finished.

Step 5. Convert library to usable JS

To convert the resulting library (libdogecoin.a) into a usable JS file, run these commands:

Note: Change /usr/lib/emscripten to your SDK path. <emsdk root directory>

Functions that you want to use in the JS needs to maunally exported. To add a function, add it to sEXPORTED_FUNCTIONS= by typing a _ and the function name.

The resulting file will be called libdogecoin.js

cd .libs

/usr/lib/emscripten/emcc -sEXPORTED_FUNCTIONS=_dogecoin_ecc_start,_dogecoin_ecc_stop,_generatePrivPubKeypair,_free,_malloc -sEXPORTED_RUNTIME_METHODS=ccall,cwrap,allocate libdogecoin.a ../src/secp256k1/.libs/libsecp256k1.a -o libdogecoin.js

Extra info

To use this JS file, you can include it in a HTML file:

<script src="libdogecoin.js"/>

<script>

Module.onRuntimeInitialized = _ => {

_dogecoin_ecc_start()

var privatePtr = allocate(intArrayFromString(""), ALLOC_NORMAL);
var publicPtr = allocate(intArrayFromString(""), ALLOC_NORMAL);

_generatePrivPubKeypair(privatePtr, publicPtr, false)

var privKey = UTF8ToString(privatePtr)
var pubKey = UTF8ToString(publicPtr)

document.write(`Public key: ${pubKey} <br> Private Ket ${privKey}`);

_dogecoin_ecc_stop()

_free(privatePtr)
_free(publicPtr)

}

</script>

NOTE: A web server is required for this to work. You can easily run a server using python3 by typing:

python -m http.server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment