-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/example app #35
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
/target | ||
**/*.rs.bk | ||
/.vscode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment. .vscode and .cargo should not be part of the repo. |
||
.idea | ||
*.swp | ||
.cargo | ||
.npmrc | ||
|
||
bin/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
"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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
|
||
package-lock.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nodejs 20.9.0 |
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. |
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this |
||
"signet", | ||
externalDescriptor, | ||
internalDescriptor | ||
); | ||
|
||
console.log("Performing Full Scan..."); | ||
let full_scan_request = wallet.start_full_scan(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example to showcase |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example to showcase |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. example of |
||
console.log("Merged:", currentChangeSet.to_json()); | ||
Store.save(currentChangeSet.to_json()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can drop the |
||
console.log("new address saved"); | ||
} | ||
|
||
run().catch(console.error); | ||
|
||
// to clear local storage: | ||
// localStorage.removeItem("walletData"); |
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" | ||
} | ||
} |
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> |
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, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi!
This file should be
gitignore
as it is environment specific.