Skip to content

Commit

Permalink
feat: add example app
Browse files Browse the repository at this point in the history
  • Loading branch information
riverKanies committed Feb 19, 2025
1 parent 6f1f4d8 commit c3e9267
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[env]
AR = "/opt/homebrew/opt/llvm/bin/llvm-ar"
CC = "/opt/homebrew/opt/llvm/bin/clang"
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/target
**/*.rs.bk
/.vscode
.idea
*.swp
.cargo
.npmrc

bin/
Expand Down
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rust-analyzer.server.extraEnv": {
"AR": "/opt/homebrew/opt/llvm/bin/llvm-ar",
"CC": "/opt/homebrew/opt/llvm/bin/clang"
},
"rust-analyzer.cargo.target": "wasm32-unknown-unknown"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ For a lightweight library providing stateless utility functions, see [`bitcoinjs
## Browser Usage

```sh
yarn add bdk
yarn add bitcoindevkit
```

## Notes on WASM Specific Considerations
Expand Down
3 changes: 3 additions & 0 deletions examples/browser/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules

package-lock.json
1 change: 1 addition & 0 deletions examples/browser/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.9.0
27 changes: 27 additions & 0 deletions examples/browser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# WASM App Example

## Server app to test a WASM package

`npm install`
`npm start`

open browser to http://localhost:8080/
open browser console to see scan rusults


## Build WASM package

By default the example will pull the bitcoindevkit package from npm.
However, if you want to pull from the local package (say for development) modify the dependency in the index.js file, and build:

From parent folder (the wasm-package):
`wasm-pack build --features esplora`

### Mac Users

Note: to build successfully on mac required installing llvm with homebrew (even though there's a default version) https://github.com/bitcoindevkit/bdk/issues/1671#issuecomment-2456858895
And properly pointing to it in which is being done in .cargo and .vscode folders

### Non-Mac Users

You may need to delete the .cargo and .vscode folders, our point them to the appropriate llvm version.
93 changes: 93 additions & 0 deletions examples/browser/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { Wallet, EsploraClient, ChangeSet } from 'bitcoindevkit';// pull from npm
// import { Wallet, EsploraClient, ChangeSet } from 'bdk';//pulling from local package

// simple string storage example
const Store = {
save: data => {
if (!data) {
console.log("No data to save");
return;
}
localStorage.setItem("walletData", data); // data is already a JSON string
},
load: () => {
return localStorage.getItem("walletData"); // return the JSON string directly
}
}

const externalDescriptor = "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/0/*)#z3x5097m";
const internalDescriptor = "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/1/*)#n9r4jswr";

async function run() {
let walletDataString = Store.load();
console.log("Wallet data:", walletDataString);

let wallet;
let client = new EsploraClient("https://mutinynet.com/api");
if (!walletDataString) {
console.log("Creating new wallet");
wallet = Wallet.create(
"signet",
externalDescriptor,
internalDescriptor
);

console.log("Performing Full Scan...");
let full_scan_request = wallet.start_full_scan();
let update = await client.full_scan(full_scan_request, 1);
wallet.apply_update(update);

const stagedDataString = wallet.take_staged().to_json();
console.log("Staged:", stagedDataString);

Store.save(stagedDataString);
console.log("Wallet data saved to local storage");
walletDataString = stagedDataString;
} else {
console.log("Loading wallet");
let changeSet = ChangeSet.from_json(walletDataString);
wallet = Wallet.load(
changeSet,
externalDescriptor,
internalDescriptor
);

console.log("Syncing...");
let sync_request = wallet.start_sync_with_revealed_spks();
let update = await client.sync(sync_request, 1);
wallet.apply_update(update);

const updateChangeSet = wallet.take_staged();
if (updateChangeSet) {
console.log("Update:", updateChangeSet.to_json());
let currentChangeSet = ChangeSet.from_json(walletDataString);
console.log("Current:", currentChangeSet.to_json());
currentChangeSet.merge(updateChangeSet);
console.log("Merged:", currentChangeSet.to_json());
Store.save(currentChangeSet.to_json());
}
}

// Test balance
console.log("Balance:", wallet.balance().confirmed.to_sat());

// Test address generation
console.log("New address:", wallet.reveal_next_address().address);


// handle merging
walletDataString = Store.load();
const updateChangeSet = wallet.take_staged();
console.log("Update:", updateChangeSet.to_json());
let currentChangeSet = ChangeSet.from_json(walletDataString);
console.log("Current:", currentChangeSet.to_json());
currentChangeSet.merge(updateChangeSet);
console.log("Merged:", currentChangeSet.to_json());
Store.save(currentChangeSet.to_json());
console.log("new address saved");
}

run().catch(console.error);

// to clear local storage:
// localStorage.removeItem("walletData");
23 changes: 23 additions & 0 deletions examples/browser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "example-bdk-wasm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npx webpack serve --mode development",
"build": "npx webpack --mode production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.96.1",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.1.0"
},
"dependencies": {
"bdk": "file:../../pkg",
"bitcoindevkit": "^0.1.3"
}
}
12 changes: 12 additions & 0 deletions examples/browser/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>BDK WASM Test</title>
</head>
<body>
<h1>BDK-WASM Test</h1>
<p>Open the console to see the output.</p>
<script src="bundle.js"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/browser/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');

module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'public'),
filename: 'bundle.js',
},
mode: 'development',
experiments: {
asyncWebAssembly: true,
},
devServer: {
static: {
directory: path.join(__dirname, 'public'),
},
compress: true,
port: 8080,
},
};

0 comments on commit c3e9267

Please sign in to comment.