Skip to content

Commit

Permalink
Extract LNURL-auth structs to sdk-common (#1012)
Browse files Browse the repository at this point in the history
* Move LnUrlAuthError to sdk-common

* Re-export bitcoin crate

* Fix clippy warning

* sdk-common: Add CI step to run tests

* Re-export lightning, lightning_invoice

* Use protobuf in sdk-common CI step
  • Loading branch information
ok300 authored Jun 20, 2024
1 parent 7dc7ac1 commit 65b3643
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 60 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,32 @@ jobs:
working-directory: tools/sdk-cli
run: cargo test

build-common:
name: Test sdk-common
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Install rust
run: |
rustup set auto-self-update disable
rustup toolchain install stable --profile minimal
- uses: Swatinem/rust-cache@v2
with:
workspaces: libs/sdk-common -> ../target

- name: Install Protoc
uses: arduino/setup-protoc@v2
with:
version: "23.4"
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Run sdk-common tests
working-directory: libs/sdk-common
run: cargo test

clippy:
name: Clippy
runs-on: ubuntu-latest
Expand Down
37 changes: 19 additions & 18 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ use breez_sdk_core::{
ConnectRequest, CurrencyInfo, EnvironmentType, EventListener, FeeratePreset, FiatCurrency,
GreenlightCredentials, GreenlightDeviceCredentials, GreenlightNodeConfig, HealthCheckStatus,
InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails,
LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayError, LnUrlPayErrorData,
LnUrlPayRequest, LnUrlPayRequestData, LnUrlWithdrawError, LnUrlWithdrawRequest,
LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData, LocaleOverrides,
LocalizedName, LogEntry, LogStream, LspInformation, MaxReverseSwapAmountResponse,
MessageSuccessActionData, MetadataFilter, MetadataItem, Network, NodeConfig, NodeCredentials,
NodeState, OnchainPaymentLimitsResponse, OpenChannelFeeRequest, OpenChannelFeeResponse,
OpeningFeeParams, OpeningFeeParamsMenu, PayOnchainRequest, PayOnchainResponse, Payment,
PaymentDetails, PaymentFailedData, PaymentStatus, PaymentType, PaymentTypeFilter,
PrepareOnchainPaymentRequest, PrepareOnchainPaymentResponse, PrepareRedeemOnchainFundsRequest,
PrepareRedeemOnchainFundsResponse, PrepareRefundRequest, PrepareRefundResponse, Rate,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees,
RedeemOnchainFundsRequest, RedeemOnchainFundsResponse, RefundRequest, RefundResponse,
ReportIssueRequest, ReportPaymentFailureDetails, ReverseSwapFeesRequest, ReverseSwapInfo,
ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop, SendOnchainRequest,
SendOnchainResponse, SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest,
ServiceHealthCheckResponse, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SuccessActionProcessed, SwapAmountType, SwapInfo, SwapStatus, Symbol,
TlvEntry, UnspentTransactionOutput, UrlSuccessActionData,
LnUrlAuthError, LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayError,
LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlWithdrawError,
LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData,
LocaleOverrides, LocalizedName, LogEntry, LogStream, LspInformation,
MaxReverseSwapAmountResponse, MessageSuccessActionData, MetadataFilter, MetadataItem, Network,
NodeConfig, NodeCredentials, NodeState, OnchainPaymentLimitsResponse, OpenChannelFeeRequest,
OpenChannelFeeResponse, OpeningFeeParams, OpeningFeeParamsMenu, PayOnchainRequest,
PayOnchainResponse, Payment, PaymentDetails, PaymentFailedData, PaymentStatus, PaymentType,
PaymentTypeFilter, PrepareOnchainPaymentRequest, PrepareOnchainPaymentResponse,
PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse, PrepareRefundRequest,
PrepareRefundResponse, Rate, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, RecommendedFees, RedeemOnchainFundsRequest, RedeemOnchainFundsResponse,
RefundRequest, RefundResponse, ReportIssueRequest, ReportPaymentFailureDetails,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint,
RouteHintHop, SendOnchainRequest, SendOnchainResponse, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, ServiceHealthCheckResponse, SignMessageRequest,
SignMessageResponse, StaticBackupRequest, StaticBackupResponse, SuccessActionProcessed,
SwapAmountType, SwapInfo, SwapStatus, Symbol, TlvEntry, UnspentTransactionOutput,
UrlSuccessActionData,
};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
Expand Down
9 changes: 9 additions & 0 deletions libs/sdk-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ mod lnurl;
mod model;
mod utils;

// Re-export commonly used crates, to make it easy for callers to use the specific versions we're using.
// For example, for the bitcoin crate, this is important because certain error conversions defined in
// sdk-common map to or from structs from this specific bitcoin crate version. If the caller would
// use a different version, the Into traits defined here would not be usable by them
// (e.g. impl From<bip32::Error> for LnUrlError)
pub use bitcoin;
pub use lightning;
pub use lightning_invoice;

// We don't include grpc::* in the prelude exports, to force callers to use the grpc path prefix.
#[rustfmt::skip]
pub mod prelude {
Expand Down
32 changes: 32 additions & 0 deletions libs/sdk-common/src/lnurl/specs/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ fn build_path_element_u32(hmac_bytes: [u8; 4]) -> u32 {

pub mod model {
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::prelude::LnUrlError;

/// Wrapped in a [LnUrlAuth], this is the result of [parse] when given a LNURL-auth endpoint.
///
Expand All @@ -139,4 +142,33 @@ pub mod model {
#[serde(skip_serializing, skip_deserializing)]
pub url: String,
}

/// Error returned by [crate::breez_services::BreezServices::lnurl_auth]
#[derive(Debug, Error)]
pub enum LnUrlAuthError {
/// This error is raised when a general error occurs not specific to other error variants
/// in this enum.
#[error("Generic: {err}")]
Generic { err: String },

/// This error is raised when the decoded LNURL URI is not compliant to the specification.
#[error("Invalid uri: {err}")]
InvalidUri { err: String },

/// This error is raised when a connection to an external service fails.
#[error("Service connectivity: {err}")]
ServiceConnectivity { err: String },
}

impl From<LnUrlError> for LnUrlAuthError {
fn from(value: LnUrlError) -> Self {
match value {
LnUrlError::InvalidUri(err) => Self::InvalidUri { err },
LnUrlError::ServiceConnectivity(err) => Self::ServiceConnectivity { err },
_ => Self::Generic {
err: value.to_string(),
},
}
}
}
}
6 changes: 3 additions & 3 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ use tokio::sync::Mutex;
use crate::breez_services::{self, BreezEvent, BreezServices, EventListener};
use crate::chain::RecommendedFees;
use crate::error::{
ConnectError, LnUrlAuthError, ReceiveOnchainError, ReceivePaymentError, SdkError,
SendOnchainError, SendPaymentError,
ConnectError, ReceiveOnchainError, ReceivePaymentError, SdkError, SendOnchainError,
SendPaymentError,
};
use crate::fiat::{FiatCurrency, Rate};
use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, SwapInfo};
use crate::{
BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse,
ConfigureNodeRequest, ConnectRequest, EnvironmentType, ListPaymentsRequest,
ConfigureNodeRequest, ConnectRequest, EnvironmentType, ListPaymentsRequest, LnUrlAuthError,
MaxReverseSwapAmountResponse, NodeConfig, NodeCredentials, OnchainPaymentLimitsResponse,
OpenChannelFeeRequest, OpenChannelFeeResponse, PayOnchainRequest, PayOnchainResponse,
PrepareOnchainPaymentRequest, PrepareOnchainPaymentResponse, PrepareRedeemOnchainFundsRequest,
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::chain::{
DEFAULT_MEMPOOL_SPACE_URL,
};
use crate::error::{
ConnectError, LnUrlAuthError, ReceiveOnchainError, ReceiveOnchainResult, ReceivePaymentError,
SdkError, SdkResult, SendOnchainError, SendPaymentError,
ConnectError, ReceiveOnchainError, ReceiveOnchainResult, ReceivePaymentError, SdkError,
SdkResult, SendOnchainError, SendPaymentError,
};
use crate::fiat::{FiatCurrency, Rate};
use crate::greenlight::{GLBackupTransport, Greenlight};
Expand Down
29 changes: 0 additions & 29 deletions libs/sdk-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,6 @@ impl From<SdkError> for ConnectError {
}
}

/// Error returned by [crate::breez_services::BreezServices::lnurl_auth]
#[derive(Debug, Error)]
pub enum LnUrlAuthError {
/// This error is raised when a general error occurs not specific to other error variants
/// in this enum.
#[error("Generic: {err}")]
Generic { err: String },

/// This error is raised when the decoded LNURL URI is not compliant to the specification.
#[error("Invalid uri: {err}")]
InvalidUri { err: String },

/// This error is raised when a connection to an external service fails.
#[error("Service connectivity: {err}")]
ServiceConnectivity { err: String },
}

impl From<LnUrlError> for LnUrlAuthError {
fn from(value: LnUrlError) -> Self {
match value {
LnUrlError::InvalidUri(err) => Self::InvalidUri { err },
LnUrlError::ServiceConnectivity(err) => Self::ServiceConnectivity { err },
_ => Self::Generic {
err: value.to_string(),
},
}
}
}

impl From<SdkError> for LnUrlAuthError {
fn from(value: SdkError) -> Self {
match value {
Expand Down
5 changes: 0 additions & 5 deletions libs/sdk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,6 @@ mod swap_out;
mod test_utils;
mod tonic_wrap;

// Re-use crates from gl_client for consistency
use gl_client::bitcoin;
use gl_client::lightning;
use gl_client::lightning_invoice;

pub use breez_services::{
mnemonic_to_seed, BackupFailedData, BreezEvent, BreezServices, CheckMessageRequest,
CheckMessageResponse, EventListener, InvoicePaidDetails, LogStream, PaymentFailedData,
Expand Down
5 changes: 2 additions & 3 deletions libs/sdk-core/src/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ use crate::{
lightning_invoice::RawBolt11Invoice,
persist::error::PersistError,
CustomMessage, LspInformation, MaxChannelAmount, NodeCredentials, Payment, PaymentResponse,
PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse,
SyncResponse, TlvEntry,
PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse, RouteHint, RouteHintHop,
SyncResponse, TlvEntry, LnUrlAuthError
};
use crate::error::LnUrlAuthError;

pub type NodeResult<T, E = NodeError> = Result<T, E>;

Expand Down

0 comments on commit 65b3643

Please sign in to comment.