-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Dario Anongba Varela <[email protected]>
- Loading branch information
1 parent
ce4bfaf
commit 8c7fea0
Showing
7 changed files
with
208 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,98 @@ | ||
import { __wbg_set_wasm, WalletWrapper, greet } from '../rust/pkg/bdk_wasm_bg.js'; | ||
import * as wasm from '../rust/pkg/bdk_wasm_bg.wasm'; | ||
import { WalletWrapper, greet } from '../rust/pkg'; | ||
|
||
async function run() { | ||
// Initialize WASM | ||
__wbg_set_wasm(wasm); | ||
|
||
// --8<-- [start:store] | ||
// needed to handle js Map serialization | ||
const Store = { | ||
save: data => { | ||
if (!data) { | ||
console.log("No data to save"); | ||
return; | ||
} | ||
const serializedStaged = JSON.stringify(data, (key, value) => { | ||
if (value instanceof Map) { | ||
return { | ||
dataType: 'Map', | ||
value: Array.from(value.entries()) | ||
}; | ||
} | ||
return value; | ||
}); | ||
localStorage.setItem("walletData", serializedStaged); | ||
}, | ||
load: () => { | ||
const walletDataString = localStorage.getItem("walletData"); | ||
// Convert serialized Maps back to Map objects when loading | ||
const walletData = JSON.parse(walletDataString, (key, value) => { | ||
if (value?.dataType === 'Map') { | ||
return new Map(value.value); | ||
} | ||
return value; | ||
}); | ||
return walletData; | ||
} | ||
} | ||
// --8<-- [end:store] | ||
|
||
// --8<-- [start:descriptors] | ||
const externalDescriptor = "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/0/*)#z3x5097m"; | ||
const internalDescriptor = "tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/1/*)#n9r4jswr"; | ||
// --8<-- [end:descriptors] | ||
|
||
async function run() { | ||
console.log(greet()); // Should print "Hello, bdk-wasm!" | ||
|
||
// Test wallet creation | ||
// --8<-- [start:new] | ||
const wallet = new WalletWrapper( | ||
"signet", | ||
"tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/0/*)#z3x5097m", | ||
"tr([12071a7c/86'/1'/0']tpubDCaLkqfh67Qr7ZuRrUNrCYQ54sMjHfsJ4yQSGb3aBr1yqt3yXpamRBUwnGSnyNnxQYu7rqeBiPfw3mjBcFNX4ky2vhjj9bDrGstkfUbLB9T/1/*)#n9r4jswr", | ||
"https://mutinynet.com/api" | ||
); | ||
// --8<-- [end:new] | ||
|
||
// --8<-- [start:scan] | ||
// Test sync | ||
await wallet.sync(2); | ||
// --8<-- [end:scan] | ||
// --8<-- [start:wallet] | ||
const walletData = Store.load(); | ||
console.log("Wallet data:", walletData); | ||
|
||
let wallet; | ||
if (!walletData) { | ||
console.log("Creating new wallet"); | ||
wallet = new WalletWrapper( | ||
"signet", | ||
externalDescriptor, | ||
internalDescriptor, | ||
"https://mutinynet.com/api" | ||
); | ||
|
||
console.log("Performing Full Scan..."); | ||
await wallet.scan(2); | ||
|
||
const stagedData = wallet.take_staged(); | ||
console.log("Staged:", stagedData); | ||
|
||
Store.save(stagedData); | ||
console.log("Wallet data saved to local storage"); | ||
} else { | ||
console.log("Loading wallet"); | ||
wallet = WalletWrapper.load( | ||
walletData, | ||
"https://mutinynet.com/api", | ||
externalDescriptor, | ||
internalDescriptor | ||
); | ||
|
||
console.log("Syncing..."); | ||
await wallet.sync(2); | ||
} | ||
// --8<-- [end:wallet] | ||
|
||
// --8<-- [start:utils] | ||
// Test balance | ||
console.log("Balance:", wallet.balance()); | ||
|
||
// Test address generation | ||
console.log("New address:", wallet.get_new_address()); | ||
|
||
const mergedData = wallet.take_merged(walletData); | ||
console.log("Merged:", mergedData); | ||
|
||
Store.save(mergedData); | ||
console.log("new address saved"); | ||
// --8<-- [end:utils] | ||
} | ||
|
||
run().catch(console.error); | ||
run().catch(console.error); | ||
|
||
// to clear local storage: | ||
// localStorage.removeItem("walletData"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>BDK WASM Test</title> | ||
</head> | ||
<body> | ||
<script src="bundle.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.