Using npm to install the WASM-based JavaScript bindings

The Libwally repository has a page outling how to use npm to install the WASM-based JavaScript bindings for Libwally, for Node.js and the browser.

In the example below we install wallycore using npm and then run the example code on the repository .

Installing wallycore using npm

mkdir wallycore-npm-example  
cd wallycore-npm-example
npm install wallycore

You can copy and paste the example code into a new file named example.js which you can place in the wallycore-npm-example/node_modules/wallycore/ directory. The example code is shown below:

import wally from 'wallycore'

const word = wally.bip39_get_word(null, 10)

const script = wally.address_to_scriptpubkey("1EMBaSSyxMQPV2fmUsdB7mMfMoocgfiMNw", wally.WALLY_NETWORK_BITCOIN_MAINNET)

const tx = wally.tx_from_hex('020000000100000000000000000000000000000000000000000000000000000000000000000000000000fdffffff0101000000000000000000000000', 0)  
console.log(wally.tx_get_txid(tx).toString('hex'))  
wally.tx_free(tx)

And then you can run the example code like this:

node node_modules/wallycore/example.js

Which will output the transaction id from the example to the console as:

a9e91ee6030a8887712754ca2774301c51ae3d26562c22daad37183c770ed4b4

The repository also has advice on using Libwally from a browser.