Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Implement BDK Database types for gloo::LocalStorage #5

Merged
merged 8 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ jobs:
name: Browser Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
toolchain: nightly
components: clippy
target: wasm32-unknown-unknown
override: true
profile: minimal

- uses: actions-rs/clippy-check@v1
working-directory: ./node-manager
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features

- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

Expand Down
5 changes: 4 additions & 1 deletion node-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ crate-type = ["cdylib"]
cfg-if = "1.0.0"
wasm-bindgen = "0.2.83"
bip39 = { version = "1.0.1" }
bitcoin = "0.29.1"
bitcoin = "0.28.1"
bdk = { git = "https://www.nakamoto.codes/BitcoinDevShop/bdk", branch="feat/use-external-esplora-client", default-features = false}
getrandom = { version = "0.2", features = ["js"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1.0" }
gloo-storage = "0.2.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
Expand Down
87 changes: 87 additions & 0 deletions node-manager/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use gloo_storage::errors::StorageError;
use std::fmt;

#[derive(Debug)]
#[allow(dead_code)]
// copied from LDK lite
/// An error that possibly needs to be handled by the user.
pub enum Error {
/// Returned when trying to start Mutiny while it is already running.
AlreadyRunning,
/// Returned when trying to stop Mutiny while it is not running.
NotRunning,
/// The funding transaction could not be created.
FundingTxCreationFailed,
/// A network connection has been closed.
ConnectionFailed,
/// Payment of the given invoice has already been initiated.
NonUniquePaymentHash,
/// The given invoice is invalid.
InvoiceInvalid,
/// Invoice creation failed.
InvoiceCreationFailed,
/// No route for the given target could be found.
RoutingFailed,
/// A given peer info could not be parsed.
PeerInfoParseFailed,
/// A channel could not be opened.
ChannelCreationFailed,
/// A channel could not be closed.
ChannelClosingFailed,
/// Persistence failed.
PersistenceFailed,
/// A wallet operation failed.
WalletOperationFailed,
/// A signing operation failed.
WalletSigningFailed,
/// A chain access operation failed.
ChainAccessFailed,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::AlreadyRunning => write!(f, "Mutiny is already running."),
Self::NotRunning => write!(f, "Mutiny is not running."),
Self::FundingTxCreationFailed => {
write!(f, "Funding transaction could not be created.")
}
Self::ConnectionFailed => write!(f, "Network connection closed."),
Self::NonUniquePaymentHash => write!(f, "An invoice must not get payed twice."),
Self::InvoiceInvalid => write!(f, "The given invoice is invalid."),
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
Self::RoutingFailed => write!(f, "Failed to find route."),
Self::PeerInfoParseFailed => write!(f, "Failed to parse the given peer information."),
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
Self::ChannelClosingFailed => write!(f, "Failed to close channel."),
Self::PersistenceFailed => write!(f, "Failed to persist data."),
Self::WalletOperationFailed => write!(f, "Failed to conduct wallet operation."),
Self::WalletSigningFailed => write!(f, "Failed to sign given transaction."),
Self::ChainAccessFailed => write!(f, "Failed to conduct chain access operation."),
}
}
}

impl std::error::Error for Error {}

impl From<bdk::Error> for Error {
fn from(e: bdk::Error) -> Self {
match e {
bdk::Error::Signer(_) => Self::WalletSigningFailed,
_ => Self::WalletOperationFailed,
}
}
}

// todo uncomment when we add esplora stuff
// impl From<esplora::EsploraError> for Error {
// fn from(_e: esplora::EsploraError) -> Self {
// Self::ChainAccessFailed
// }
// }

impl From<StorageError> for Error {
fn from(_e: StorageError) -> Self {
Self::PersistenceFailed
}
}
11 changes: 11 additions & 0 deletions node-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// wasm_bindgen uses improper casing and it needs to be turned off:
// https://github.com/rustwasm/wasm-bindgen/issues/2882

mod error;
mod localstorage;
mod nodemanager;
mod seedgen;
mod storage;
Expand All @@ -26,3 +28,12 @@ pub async fn main_js() -> Result<(), JsValue> {
debug!("Main function ends");
Ok(())
}

#[cfg(test)]
mod test {
use gloo_storage::{LocalStorage, Storage};

pub(crate) fn cleanup_test() {
LocalStorage::clear();
}
}
Loading