LWK-rn is a React Native module for Liquid Wallet Kit. Its goal is to provide all the necessary building blocks for mobile development of a liquid wallet.
NOTE: LWK and LWK-rn is in public beta and still undergoing significant development. Use it at your own risk.
Please consider reviewing, experimenting and contributing
Thanks for taking a look!
Using npm:
npm install lwk-rn
Using yarn:
yarn add lwk-rn
Note: Use android sdk version >= 24 and iOS >= v13 .
Import LWK-rn library
import { Mnemonic, Network, Signer, Wollet } from 'lwk-rn';
Create a signer for a mnemonic and a network
let mnemonic = new Mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about");
let network = Network.testnet();
let signer = new Signer(mnemonic, network);
console.log(mnemonic.toString());
Create and update a wollet from the signer descriptor
let singlesigDesc = signer.wpkhSlip77Descriptor();
let wollet = new Wollet(network, singlesigDesc, undefined);
let client = network.defaultElectrumClient();
let update = client.fullScan(wollet);
if (update) {
wollet.applyUpdate(update);
}
Get a new unused address
let latest_address = wollet.address(undefined);
console.log(latest_address.address().scriptPubkey().toString());
Get balance as [AssetId : UInt64]
let balance = wollet.balance();
console.log(balance);
for (var b of balance.entries()) {
console.log("asset: ", b[0], ", value: ", b[1]);
}
Get a transaction list
let txs = wollet.transactions();
console.log(txs);
for (var tx of txs) {
for (var output of tx.outputs()) {
let script_pubkey = output?.scriptPubkey().toString();
let value = output?.unblinded().value().toString();
console.log("script_pubkey: ", script_pubkey, ", value: ", value);
}
}
Build, sign, finalize and broadcast a transaction of policy asset
let latest_address = wollet.address(undefined);
let out_address = latest_address.address();
let satoshis = 900n;
let fee_rate = 280; // this seems like absolute fees
let builder = network.txBuilder();
builder.addLbtcRecipient(out_address, satoshis);
builder.feeRate(fee_rate);
let pset = builder.finish(wollet);
let signed_pset = signer.sign(pset);
let finalized_pset = wollet.finalize(signed_pset);
let txid = client
.broadcast(finalized_pset.extractTx());
console.log("BROADCASTED TX!\nTXID: ", txid);
Build, sign, finalize and broadcast a transaction of liquid asset
let latest_address = wollet.address(undefined);
let out_address = latest_address.address();
let satoshis = 900n;
let fee_rate = 280; // this seems like absolute fees
let builder = network.txBuilder();
builder.addLbtcRecipient(out_address, satoshis);
builder.feeRate(fee_rate);
// sign and send
let pset = builder.finish(wollet);
let signed_pset = signer.sign(pset);
let finalized_pset = wollet.finalize(signed_pset);
let txid = client.broadcast(finalized_pset.extractTx());
console.log("BROADCASTED TX!\nTXID: ", txid.toString());
Build, sign, finalize and broadcast a transaction of liquid asset
let builder = network.txBuilder();
let asset = '0f1040289a4e88e6acef89b65ce4847b6fd68ac39b89fa978bf23e2c039f5e27';
builder.addRecipient(out_address, 100n, asset);
builder.feeRate(fee_rate);
let pset = builder.finish(wollet);
let signed_pset = signer.sign(pset);
let finalized_pset = wollet.finalize(signed_pset);
let txid = client.broadcast(finalized_pset.extractTx());
console.log("BROADCASTED TX!\nTXID: ", txid.toString());
LWK-rn repository contains the pre-generated lwk bindings for android and ios.
Follow the steps to generate bindings by your own:
Install C++ tooling
# For MacOS, using homebrew:
brew install cmake ninja clang-format
# For Debian flavoured Linux:
apt-get install cmake ninja clang-format
Add the Android specific targets
rustup target add \
aarch64-linux-android \
armv7-linux-androideabi \
i686-linux-android \
x86_64-linux-android
# Install cargo-ndk
cargo install cargo-ndk
Add the iOS specific targets
rustup target add \
aarch64-apple-ios \
aarch64-apple-ios-sim \
x86_64-apple-ios
# Ensure xcodebuild is available
xcode-select --install
Install deps and uniffi-bindgen-react-native
.
$ yarn install
Fetch LWK library with some hacks.
The script changes path to avoid using workspace configuration and rust version. The project require rust >= v1.18 . The scipt replacing package name in
Cargo.toml
for a library name bug inuniffi-bindgen-react-native
.
$ sh fetch_lwk.sh
Generate bindings for android and ios:
$ yarn ubrn:android
$ yarn ubrn:ios
Open demo application in ./example/
folder and read the code in ./example/src/App.tsx
.
You could run the example on the Android demo app on device/emulator by:
$ yarn example android
Or you could run the iOS demo app by:
$ cd example
$ bundle install
$ bundle exec pod install # every time you update your native dependencies
$ cd ..
$ yarn example ios
See the contributing guide to learn how to contribute to the repository and the development workflow.
See the how-to to learn how works lwk-rn react native module and the repo code.
Thanks to all Blockstream LWK team for the amazing library.
A special thanks to @rcasatta to help me about packaging library for iOS and Android.
A big thanks to @BlakeKaufman because give me the opportunity to build this library with a Bounty and made contributions by itself.
MIT
Made with create-react-native-library using uniffi-bindgen-react-native