diff --git a/libs/Cargo.toml b/libs/Cargo.toml index c27f40f3e..a782c38b2 100644 --- a/libs/Cargo.toml +++ b/libs/Cargo.toml @@ -21,7 +21,6 @@ aes = "0.8" anyhow = { version = "1.0.79", features = ["backtrace"] } base64 = "0.13.0" bitcoin = "=0.29.2" # Same version as used in gl-client -# Pin the reqwest dependency until macOS linker issue is fixed: https://github.com/seanmonstar/reqwest/issues/2006 hex = "0.4" lightning = "=0.0.118" # Same version as used in gl-client lightning-invoice = "=0.26.0" # Same version as used in gl-client @@ -30,6 +29,7 @@ mockito = "1" once_cell = "1" prost = "^0.11" regex = "1.8.1" +# Pin the reqwest dependency until macOS linker issue is fixed: https://github.com/seanmonstar/reqwest/issues/2006 reqwest = { version = "=0.11.20", features = ["json"] } rusqlite = { version = "0.29", features = [ "serde_json", diff --git a/libs/sdk-bindings/src/breez_sdk.udl b/libs/sdk-bindings/src/breez_sdk.udl index 828324273..f89659def 100644 --- a/libs/sdk-bindings/src/breez_sdk.udl +++ b/libs/sdk-bindings/src/breez_sdk.udl @@ -730,11 +730,27 @@ dictionary RecommendedFees { u64 minimum_fee; }; +[Enum] +interface Amount { + Bitcoin(u64 amount_msat); + Currency(string iso4217_code, u64 fractional_amount); +}; + +dictionary LNOffer { + string bolt12; + sequence chains; + string description; + string signing_pubkey; + Amount? amount; + u64? absolute_expiry; + string? issuer; +}; + [Enum] interface InputType { BitcoinAddress(BitcoinAddressData address); Bolt11(LNInvoice invoice); - Bolt12(string offer); + Bolt12(LNOffer offer); NodeId(string node_id); Url(string url); LnUrlPay(LnUrlPayRequestData data); diff --git a/libs/sdk-bindings/src/uniffi_binding.rs b/libs/sdk-bindings/src/uniffi_binding.rs index 1de3ff14d..ccc4dcd7a 100644 --- a/libs/sdk-bindings/src/uniffi_binding.rs +++ b/libs/sdk-bindings/src/uniffi_binding.rs @@ -5,12 +5,12 @@ use breez_sdk_core::lnurl::pay::{LnUrlPayResult, LnUrlPaySuccessData}; use breez_sdk_core::{ error::*, mnemonic_to_seed as sdk_mnemonic_to_seed, parse as sdk_parse_input, parse_invoice as sdk_parse_invoice, AesSuccessActionDataDecrypted, AesSuccessActionDataResult, - BackupFailedData, BackupStatus, BitcoinAddressData, BreezEvent, BreezServices, + Amount, BackupFailedData, BackupStatus, BitcoinAddressData, BreezEvent, BreezServices, BuyBitcoinProvider, BuyBitcoinRequest, BuyBitcoinResponse, ChannelState, CheckMessageRequest, CheckMessageResponse, ClosedChannelPaymentDetails, Config, ConfigureNodeRequest, ConnectRequest, CurrencyInfo, EnvironmentType, EventListener, FeeratePreset, FiatCurrency, GreenlightCredentials, GreenlightDeviceCredentials, GreenlightNodeConfig, HealthCheckStatus, - InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails, + InputType, InvoicePaidDetails, LNInvoice, LNOffer, ListPaymentsRequest, LnPaymentDetails, LnUrlAuthError, LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayError, LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, LnUrlWithdrawError, LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData, diff --git a/libs/sdk-common/src/input_parser.rs b/libs/sdk-common/src/input_parser.rs index 6cdd6aa2a..5d2753eb9 100644 --- a/libs/sdk-common/src/input_parser.rs +++ b/libs/sdk-common/src/input_parser.rs @@ -188,7 +188,19 @@ pub async fn parse(input: &str) -> Result { if let Ok(offer) = input.parse::() { return Ok(InputType::Bolt12 { - offer: offer.to_string(), + offer: LNOffer { + bolt12: input.to_string(), + chains: offer + .chains() + .iter() + .map(|chain| chain.to_string()) + .collect(), + amount: offer.amount().map(|amount| amount.clone().into()), + description: offer.description().to_string(), + absolute_expiry: offer.absolute_expiry().map(|expiry| expiry.as_secs()), + issuer: offer.issuer().map(|s| s.to_string()), + signing_pubkey: offer.signing_pubkey().to_string(), + }, }); } @@ -426,7 +438,7 @@ pub enum InputType { invoice: LNInvoice, }, Bolt12 { - offer: String, + offer: LNOffer, }, NodeId { node_id: String, diff --git a/libs/sdk-common/src/invoice.rs b/libs/sdk-common/src/invoice.rs index fe6b4c390..c59a6de78 100644 --- a/libs/sdk-common/src/invoice.rs +++ b/libs/sdk-common/src/invoice.rs @@ -101,6 +101,48 @@ fn format_short_channel_id(id: u64) -> String { format!("{block_num}x{tx_num}x{tx_out}") } +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub enum Amount { + Bitcoin { + amount_msat: u64, + }, + Currency { + // Reference to [FiatCurrency.id] + iso4217_code: String, + fractional_amount: u64, + }, +} + +impl From for Amount { + fn from(amount: lightning::offers::offer::Amount) -> Self { + match amount { + lightning::offers::offer::Amount::Bitcoin { amount_msats } => Amount::Bitcoin { + amount_msat: amount_msats, + }, + lightning::offers::offer::Amount::Currency { + iso4217_code, + amount, + } => Amount::Currency { + iso4217_code: String::from_utf8(iso4217_code.to_vec()) + .expect("Expecting a valid ISO 4217 character sequence"), + fractional_amount: amount, + }, + } + } +} + +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct LNOffer { + pub bolt12: String, + pub chains: Vec, + pub amount: Option, + pub description: String, + pub absolute_expiry: Option, + pub issuer: Option, + pub signing_pubkey: String, + // pub paths: Vec, +} + /// Wrapper for a BOLT11 LN invoice #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct LNInvoice { diff --git a/libs/sdk-core/src/binding.rs b/libs/sdk-core/src/binding.rs index 3d0b4409e..8c74cd0b2 100644 --- a/libs/sdk-core/src/binding.rs +++ b/libs/sdk-core/src/binding.rs @@ -19,12 +19,12 @@ use log::{Level, LevelFilter, Metadata, Record}; use once_cell::sync::{Lazy, OnceCell}; use sdk_common::invoice; pub use sdk_common::prelude::{ - parse, AesSuccessActionDataDecrypted, AesSuccessActionDataResult, BitcoinAddressData, - CurrencyInfo, FiatCurrency, InputType, LNInvoice, LnUrlAuthRequestData, LnUrlCallbackStatus, - LnUrlError, LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest, LnUrlPayRequestData, - LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, LnUrlWithdrawSuccessData, - LocaleOverrides, LocalizedName, MessageSuccessActionData, Network, Rate, RouteHint, - RouteHintHop, SuccessActionProcessed, Symbol, UrlSuccessActionData, + parse, AesSuccessActionDataDecrypted, AesSuccessActionDataResult, Amount, BitcoinAddressData, + CurrencyInfo, FiatCurrency, InputType, LNInvoice, LNOffer, LnUrlAuthRequestData, + LnUrlCallbackStatus, LnUrlError, LnUrlErrorData, LnUrlPayErrorData, LnUrlPayRequest, + LnUrlPayRequestData, LnUrlWithdrawRequest, LnUrlWithdrawRequestData, LnUrlWithdrawResult, + LnUrlWithdrawSuccessData, LocaleOverrides, LocalizedName, MessageSuccessActionData, Network, + Rate, RouteHint, RouteHintHop, SuccessActionProcessed, Symbol, UrlSuccessActionData, }; use tokio::sync::Mutex; @@ -158,11 +158,33 @@ pub struct _LnUrlWithdrawRequestData { pub max_withdrawable: u64, } +#[frb(mirror(Amount))] +pub enum _Amount { + Bitcoin { + amount_msat: u64, + }, + Currency { + iso4217_code: String, + fractional_amount: u64, + }, +} + +#[frb(mirror(LNOffer))] +pub struct _LNOffer { + pub bolt12: String, + pub chains: Vec, + pub amount: Option, + pub description: String, + pub absolute_expiry: Option, + pub issuer: Option, + pub signing_pubkey: String, +} + #[frb(mirror(InputType))] pub enum _InputType { BitcoinAddress { address: BitcoinAddressData }, Bolt11 { invoice: LNInvoice }, - Bolt12 { offer: String }, + Bolt12 { offer: LNOffer }, NodeId { node_id: String }, Url { url: String }, LnUrlPay { data: LnUrlPayRequestData }, diff --git a/libs/sdk-core/src/bridge_generated.rs b/libs/sdk-core/src/bridge_generated.rs index 7f1a6b48e..55680ab57 100644 --- a/libs/sdk-core/src/bridge_generated.rs +++ b/libs/sdk-core/src/bridge_generated.rs @@ -910,6 +910,9 @@ pub struct mirror_AesSuccessActionDataDecrypted(AesSuccessActionDataDecrypted); #[derive(Clone)] pub struct mirror_AesSuccessActionDataResult(AesSuccessActionDataResult); +#[derive(Clone)] +pub struct mirror_Amount(Amount); + #[derive(Clone)] pub struct mirror_BitcoinAddressData(BitcoinAddressData); @@ -925,6 +928,9 @@ pub struct mirror_InputType(InputType); #[derive(Clone)] pub struct mirror_LNInvoice(LNInvoice); +#[derive(Clone)] +pub struct mirror_LNOffer(LNOffer); + #[derive(Clone)] pub struct mirror_LnUrlAuthRequestData(LnUrlAuthRequestData); @@ -995,6 +1001,18 @@ const _: fn() = || { let _: String = reason; } } + match None::.unwrap() { + Amount::Bitcoin { amount_msat } => { + let _: u64 = amount_msat; + } + Amount::Currency { + iso4217_code, + fractional_amount, + } => { + let _: String = iso4217_code; + let _: u64 = fractional_amount; + } + } { let BitcoinAddressData = None::.unwrap(); let _: String = BitcoinAddressData.address; @@ -1026,7 +1044,7 @@ const _: fn() = || { let _: LNInvoice = invoice; } InputType::Bolt12 { offer } => { - let _: String = offer; + let _: LNOffer = offer; } InputType::NodeId { node_id } => { let _: String = node_id; @@ -1062,6 +1080,16 @@ const _: fn() = || { let _: Vec = LNInvoice.payment_secret; let _: u64 = LNInvoice.min_final_cltv_expiry_delta; } + { + let LNOffer = None::.unwrap(); + let _: String = LNOffer.bolt12; + let _: Vec = LNOffer.chains; + let _: Option = LNOffer.amount; + let _: String = LNOffer.description; + let _: Option = LNOffer.absolute_expiry; + let _: Option = LNOffer.issuer; + let _: String = LNOffer.signing_pubkey; + } { let LnUrlAuthRequestData = None::.unwrap(); let _: String = LnUrlAuthRequestData.k1; @@ -1337,6 +1365,31 @@ impl rust2dart::IntoIntoDart for AesSuccessAc } } +impl support::IntoDart for mirror_Amount { + fn into_dart(self) -> support::DartAbi { + match self.0 { + Amount::Bitcoin { amount_msat } => { + vec![0.into_dart(), amount_msat.into_into_dart().into_dart()] + } + Amount::Currency { + iso4217_code, + fractional_amount, + } => vec![ + 1.into_dart(), + iso4217_code.into_into_dart().into_dart(), + fractional_amount.into_into_dart().into_dart(), + ], + } + .into_dart() + } +} +impl support::IntoDartExceptPrimitive for mirror_Amount {} +impl rust2dart::IntoIntoDart for Amount { + fn into_into_dart(self) -> mirror_Amount { + mirror_Amount(self) + } +} + impl support::IntoDart for BackupFailedData { fn into_dart(self) -> support::DartAbi { vec![self.error.into_into_dart().into_dart()].into_dart() @@ -1686,6 +1739,27 @@ impl rust2dart::IntoIntoDart for LNInvoice { } } +impl support::IntoDart for mirror_LNOffer { + fn into_dart(self) -> support::DartAbi { + vec![ + self.0.bolt12.into_into_dart().into_dart(), + self.0.chains.into_into_dart().into_dart(), + self.0.amount.map(|v| mirror_Amount(v)).into_dart(), + self.0.description.into_into_dart().into_dart(), + self.0.absolute_expiry.into_dart(), + self.0.issuer.into_dart(), + self.0.signing_pubkey.into_into_dart().into_dart(), + ] + .into_dart() + } +} +impl support::IntoDartExceptPrimitive for mirror_LNOffer {} +impl rust2dart::IntoIntoDart for LNOffer { + fn into_into_dart(self) -> mirror_LNOffer { + mirror_LNOffer(self) + } +} + impl support::IntoDart for LnPaymentDetails { fn into_dart(self) -> support::DartAbi { vec![ diff --git a/libs/sdk-flutter/lib/bridge_generated.dart b/libs/sdk-flutter/lib/bridge_generated.dart index 427038d93..30d9cdb21 100644 --- a/libs/sdk-flutter/lib/bridge_generated.dart +++ b/libs/sdk-flutter/lib/bridge_generated.dart @@ -349,6 +349,17 @@ sealed class AesSuccessActionDataResult with _$AesSuccessActionDataResult { }) = AesSuccessActionDataResult_ErrorStatus; } +@freezed +sealed class Amount with _$Amount { + const factory Amount.bitcoin({ + required int amountMsat, + }) = Amount_Bitcoin; + const factory Amount.currency({ + required String iso4217Code, + required int fractionalAmount, + }) = Amount_Currency; +} + class BackupFailedData { final String error; @@ -671,7 +682,7 @@ sealed class InputType with _$InputType { required LNInvoice invoice, }) = InputType_Bolt11; const factory InputType.bolt12({ - required String offer, + required LNOffer offer, }) = InputType_Bolt12; const factory InputType.nodeId({ required String nodeId, @@ -761,6 +772,26 @@ class LNInvoice { }); } +class LNOffer { + final String bolt12; + final List chains; + final Amount? amount; + final String description; + final int? absoluteExpiry; + final String? issuer; + final String signingPubkey; + + const LNOffer({ + required this.bolt12, + required this.chains, + this.amount, + required this.description, + this.absoluteExpiry, + this.issuer, + required this.signingPubkey, + }); +} + /// Details of a LN payment, as included in a [Payment] class LnPaymentDetails { final String paymentHash; @@ -3096,6 +3127,22 @@ class BreezSdkCoreImpl implements BreezSdkCore { } } + Amount _wire2api_amount(dynamic raw) { + switch (raw[0]) { + case 0: + return Amount_Bitcoin( + amountMsat: _wire2api_u64(raw[1]), + ); + case 1: + return Amount_Currency( + iso4217Code: _wire2api_String(raw[1]), + fractionalAmount: _wire2api_u64(raw[2]), + ); + default: + throw Exception("unreachable"); + } + } + BackupFailedData _wire2api_backup_failed_data(dynamic raw) { final arr = raw as List; if (arr.length != 1) throw Exception('unexpected arr length: expect 1 but see ${arr.length}'); @@ -3137,6 +3184,10 @@ class BreezSdkCoreImpl implements BreezSdkCore { return _wire2api_aes_success_action_data_result(raw); } + Amount _wire2api_box_autoadd_amount(dynamic raw) { + return _wire2api_amount(raw); + } + BackupFailedData _wire2api_box_autoadd_backup_failed_data(dynamic raw) { return _wire2api_backup_failed_data(raw); } @@ -3173,6 +3224,10 @@ class BreezSdkCoreImpl implements BreezSdkCore { return _wire2api_ln_invoice(raw); } + LNOffer _wire2api_box_autoadd_ln_offer(dynamic raw) { + return _wire2api_ln_offer(raw); + } + LnPaymentDetails _wire2api_box_autoadd_ln_payment_details(dynamic raw) { return _wire2api_ln_payment_details(raw); } @@ -3425,7 +3480,7 @@ class BreezSdkCoreImpl implements BreezSdkCore { ); case 2: return InputType_Bolt12( - offer: _wire2api_String(raw[1]), + offer: _wire2api_box_autoadd_ln_offer(raw[1]), ); case 3: return InputType_NodeId( @@ -3533,6 +3588,20 @@ class BreezSdkCoreImpl implements BreezSdkCore { ); } + LNOffer _wire2api_ln_offer(dynamic raw) { + final arr = raw as List; + if (arr.length != 7) throw Exception('unexpected arr length: expect 7 but see ${arr.length}'); + return LNOffer( + bolt12: _wire2api_String(arr[0]), + chains: _wire2api_StringList(arr[1]), + amount: _wire2api_opt_box_autoadd_amount(arr[2]), + description: _wire2api_String(arr[3]), + absoluteExpiry: _wire2api_opt_box_autoadd_u64(arr[4]), + issuer: _wire2api_opt_String(arr[5]), + signingPubkey: _wire2api_String(arr[6]), + ); + } + LnPaymentDetails _wire2api_ln_payment_details(dynamic raw) { final arr = raw as List; if (arr.length != 16) throw Exception('unexpected arr length: expect 16 but see ${arr.length}'); @@ -3836,6 +3905,10 @@ class BreezSdkCoreImpl implements BreezSdkCore { return raw == null ? null : _wire2api_StringList(raw); } + Amount? _wire2api_opt_box_autoadd_amount(dynamic raw) { + return raw == null ? null : _wire2api_box_autoadd_amount(raw); + } + bool? _wire2api_opt_box_autoadd_bool(dynamic raw) { return raw == null ? null : _wire2api_box_autoadd_bool(raw); } diff --git a/libs/sdk-flutter/lib/bridge_generated.freezed.dart b/libs/sdk-flutter/lib/bridge_generated.freezed.dart index 3a169ac53..e37ad2035 100644 --- a/libs/sdk-flutter/lib/bridge_generated.freezed.dart +++ b/libs/sdk-flutter/lib/bridge_generated.freezed.dart @@ -347,6 +347,335 @@ abstract class AesSuccessActionDataResult_ErrorStatus implements AesSuccessActio get copyWith => throw _privateConstructorUsedError; } +/// @nodoc +mixin _$Amount { + @optionalTypeArgs + TResult when({ + required TResult Function(int amountMsat) bitcoin, + required TResult Function(String iso4217Code, int fractionalAmount) currency, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function(int amountMsat)? bitcoin, + TResult? Function(String iso4217Code, int fractionalAmount)? currency, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeWhen({ + TResult Function(int amountMsat)? bitcoin, + TResult Function(String iso4217Code, int fractionalAmount)? currency, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult map({ + required TResult Function(Amount_Bitcoin value) bitcoin, + required TResult Function(Amount_Currency value) currency, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(Amount_Bitcoin value)? bitcoin, + TResult? Function(Amount_Currency value)? currency, + }) => + throw _privateConstructorUsedError; + @optionalTypeArgs + TResult maybeMap({ + TResult Function(Amount_Bitcoin value)? bitcoin, + TResult Function(Amount_Currency value)? currency, + required TResult orElse(), + }) => + throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $AmountCopyWith<$Res> { + factory $AmountCopyWith(Amount value, $Res Function(Amount) then) = _$AmountCopyWithImpl<$Res, Amount>; +} + +/// @nodoc +class _$AmountCopyWithImpl<$Res, $Val extends Amount> implements $AmountCopyWith<$Res> { + _$AmountCopyWithImpl(this._value, this._then); + + // ignore: unused_field + final $Val _value; + // ignore: unused_field + final $Res Function($Val) _then; +} + +/// @nodoc +abstract class _$$Amount_BitcoinImplCopyWith<$Res> { + factory _$$Amount_BitcoinImplCopyWith( + _$Amount_BitcoinImpl value, $Res Function(_$Amount_BitcoinImpl) then) = + __$$Amount_BitcoinImplCopyWithImpl<$Res>; + @useResult + $Res call({int amountMsat}); +} + +/// @nodoc +class __$$Amount_BitcoinImplCopyWithImpl<$Res> extends _$AmountCopyWithImpl<$Res, _$Amount_BitcoinImpl> + implements _$$Amount_BitcoinImplCopyWith<$Res> { + __$$Amount_BitcoinImplCopyWithImpl(_$Amount_BitcoinImpl _value, $Res Function(_$Amount_BitcoinImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? amountMsat = null, + }) { + return _then(_$Amount_BitcoinImpl( + amountMsat: null == amountMsat + ? _value.amountMsat + : amountMsat // ignore: cast_nullable_to_non_nullable + as int, + )); + } +} + +/// @nodoc + +class _$Amount_BitcoinImpl implements Amount_Bitcoin { + const _$Amount_BitcoinImpl({required this.amountMsat}); + + @override + final int amountMsat; + + @override + String toString() { + return 'Amount.bitcoin(amountMsat: $amountMsat)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$Amount_BitcoinImpl && + (identical(other.amountMsat, amountMsat) || other.amountMsat == amountMsat)); + } + + @override + int get hashCode => Object.hash(runtimeType, amountMsat); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$Amount_BitcoinImplCopyWith<_$Amount_BitcoinImpl> get copyWith => + __$$Amount_BitcoinImplCopyWithImpl<_$Amount_BitcoinImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function(int amountMsat) bitcoin, + required TResult Function(String iso4217Code, int fractionalAmount) currency, + }) { + return bitcoin(amountMsat); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function(int amountMsat)? bitcoin, + TResult? Function(String iso4217Code, int fractionalAmount)? currency, + }) { + return bitcoin?.call(amountMsat); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function(int amountMsat)? bitcoin, + TResult Function(String iso4217Code, int fractionalAmount)? currency, + required TResult orElse(), + }) { + if (bitcoin != null) { + return bitcoin(amountMsat); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(Amount_Bitcoin value) bitcoin, + required TResult Function(Amount_Currency value) currency, + }) { + return bitcoin(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(Amount_Bitcoin value)? bitcoin, + TResult? Function(Amount_Currency value)? currency, + }) { + return bitcoin?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(Amount_Bitcoin value)? bitcoin, + TResult Function(Amount_Currency value)? currency, + required TResult orElse(), + }) { + if (bitcoin != null) { + return bitcoin(this); + } + return orElse(); + } +} + +abstract class Amount_Bitcoin implements Amount { + const factory Amount_Bitcoin({required final int amountMsat}) = _$Amount_BitcoinImpl; + + int get amountMsat; + @JsonKey(ignore: true) + _$$Amount_BitcoinImplCopyWith<_$Amount_BitcoinImpl> get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class _$$Amount_CurrencyImplCopyWith<$Res> { + factory _$$Amount_CurrencyImplCopyWith( + _$Amount_CurrencyImpl value, $Res Function(_$Amount_CurrencyImpl) then) = + __$$Amount_CurrencyImplCopyWithImpl<$Res>; + @useResult + $Res call({String iso4217Code, int fractionalAmount}); +} + +/// @nodoc +class __$$Amount_CurrencyImplCopyWithImpl<$Res> extends _$AmountCopyWithImpl<$Res, _$Amount_CurrencyImpl> + implements _$$Amount_CurrencyImplCopyWith<$Res> { + __$$Amount_CurrencyImplCopyWithImpl( + _$Amount_CurrencyImpl _value, $Res Function(_$Amount_CurrencyImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? iso4217Code = null, + Object? fractionalAmount = null, + }) { + return _then(_$Amount_CurrencyImpl( + iso4217Code: null == iso4217Code + ? _value.iso4217Code + : iso4217Code // ignore: cast_nullable_to_non_nullable + as String, + fractionalAmount: null == fractionalAmount + ? _value.fractionalAmount + : fractionalAmount // ignore: cast_nullable_to_non_nullable + as int, + )); + } +} + +/// @nodoc + +class _$Amount_CurrencyImpl implements Amount_Currency { + const _$Amount_CurrencyImpl({required this.iso4217Code, required this.fractionalAmount}); + + @override + final String iso4217Code; + @override + final int fractionalAmount; + + @override + String toString() { + return 'Amount.currency(iso4217Code: $iso4217Code, fractionalAmount: $fractionalAmount)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$Amount_CurrencyImpl && + (identical(other.iso4217Code, iso4217Code) || other.iso4217Code == iso4217Code) && + (identical(other.fractionalAmount, fractionalAmount) || + other.fractionalAmount == fractionalAmount)); + } + + @override + int get hashCode => Object.hash(runtimeType, iso4217Code, fractionalAmount); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$Amount_CurrencyImplCopyWith<_$Amount_CurrencyImpl> get copyWith => + __$$Amount_CurrencyImplCopyWithImpl<_$Amount_CurrencyImpl>(this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function(int amountMsat) bitcoin, + required TResult Function(String iso4217Code, int fractionalAmount) currency, + }) { + return currency(iso4217Code, fractionalAmount); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function(int amountMsat)? bitcoin, + TResult? Function(String iso4217Code, int fractionalAmount)? currency, + }) { + return currency?.call(iso4217Code, fractionalAmount); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function(int amountMsat)? bitcoin, + TResult Function(String iso4217Code, int fractionalAmount)? currency, + required TResult orElse(), + }) { + if (currency != null) { + return currency(iso4217Code, fractionalAmount); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(Amount_Bitcoin value) bitcoin, + required TResult Function(Amount_Currency value) currency, + }) { + return currency(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(Amount_Bitcoin value)? bitcoin, + TResult? Function(Amount_Currency value)? currency, + }) { + return currency?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(Amount_Bitcoin value)? bitcoin, + TResult Function(Amount_Currency value)? currency, + required TResult orElse(), + }) { + if (currency != null) { + return currency(this); + } + return orElse(); + } +} + +abstract class Amount_Currency implements Amount { + const factory Amount_Currency({required final String iso4217Code, required final int fractionalAmount}) = + _$Amount_CurrencyImpl; + + String get iso4217Code; + int get fractionalAmount; + @JsonKey(ignore: true) + _$$Amount_CurrencyImplCopyWith<_$Amount_CurrencyImpl> get copyWith => throw _privateConstructorUsedError; +} + /// @nodoc mixin _$BreezEvent { @optionalTypeArgs @@ -2180,7 +2509,7 @@ mixin _$InputType { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -2193,7 +2522,7 @@ mixin _$InputType { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2206,7 +2535,7 @@ mixin _$InputType { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2340,7 +2669,7 @@ class _$InputType_BitcoinAddressImpl implements InputType_BitcoinAddress { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -2356,7 +2685,7 @@ class _$InputType_BitcoinAddressImpl implements InputType_BitcoinAddress { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2372,7 +2701,7 @@ class _$InputType_BitcoinAddressImpl implements InputType_BitcoinAddress { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2515,7 +2844,7 @@ class _$InputType_Bolt11Impl implements InputType_Bolt11 { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -2531,7 +2860,7 @@ class _$InputType_Bolt11Impl implements InputType_Bolt11 { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2547,7 +2876,7 @@ class _$InputType_Bolt11Impl implements InputType_Bolt11 { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2629,7 +2958,7 @@ abstract class _$$InputType_Bolt12ImplCopyWith<$Res> { _$InputType_Bolt12Impl value, $Res Function(_$InputType_Bolt12Impl) then) = __$$InputType_Bolt12ImplCopyWithImpl<$Res>; @useResult - $Res call({String offer}); + $Res call({LNOffer offer}); } /// @nodoc @@ -2648,7 +2977,7 @@ class __$$InputType_Bolt12ImplCopyWithImpl<$Res> extends _$InputTypeCopyWithImpl offer: null == offer ? _value.offer : offer // ignore: cast_nullable_to_non_nullable - as String, + as LNOffer, )); } } @@ -2659,7 +2988,7 @@ class _$InputType_Bolt12Impl implements InputType_Bolt12 { const _$InputType_Bolt12Impl({required this.offer}); @override - final String offer; + final LNOffer offer; @override String toString() { @@ -2688,7 +3017,7 @@ class _$InputType_Bolt12Impl implements InputType_Bolt12 { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -2704,7 +3033,7 @@ class _$InputType_Bolt12Impl implements InputType_Bolt12 { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2720,7 +3049,7 @@ class _$InputType_Bolt12Impl implements InputType_Bolt12 { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2789,9 +3118,9 @@ class _$InputType_Bolt12Impl implements InputType_Bolt12 { } abstract class InputType_Bolt12 implements InputType { - const factory InputType_Bolt12({required final String offer}) = _$InputType_Bolt12Impl; + const factory InputType_Bolt12({required final LNOffer offer}) = _$InputType_Bolt12Impl; - String get offer; + LNOffer get offer; @JsonKey(ignore: true) _$$InputType_Bolt12ImplCopyWith<_$InputType_Bolt12Impl> get copyWith => throw _privateConstructorUsedError; } @@ -2861,7 +3190,7 @@ class _$InputType_NodeIdImpl implements InputType_NodeId { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -2877,7 +3206,7 @@ class _$InputType_NodeIdImpl implements InputType_NodeId { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -2893,7 +3222,7 @@ class _$InputType_NodeIdImpl implements InputType_NodeId { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3032,7 +3361,7 @@ class _$InputType_UrlImpl implements InputType_Url { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -3048,7 +3377,7 @@ class _$InputType_UrlImpl implements InputType_Url { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3064,7 +3393,7 @@ class _$InputType_UrlImpl implements InputType_Url { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3206,7 +3535,7 @@ class _$InputType_LnUrlPayImpl implements InputType_LnUrlPay { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -3222,7 +3551,7 @@ class _$InputType_LnUrlPayImpl implements InputType_LnUrlPay { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3238,7 +3567,7 @@ class _$InputType_LnUrlPayImpl implements InputType_LnUrlPay { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3381,7 +3710,7 @@ class _$InputType_LnUrlWithdrawImpl implements InputType_LnUrlWithdraw { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -3397,7 +3726,7 @@ class _$InputType_LnUrlWithdrawImpl implements InputType_LnUrlWithdraw { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3413,7 +3742,7 @@ class _$InputType_LnUrlWithdrawImpl implements InputType_LnUrlWithdraw { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3557,7 +3886,7 @@ class _$InputType_LnUrlAuthImpl implements InputType_LnUrlAuth { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -3573,7 +3902,7 @@ class _$InputType_LnUrlAuthImpl implements InputType_LnUrlAuth { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3589,7 +3918,7 @@ class _$InputType_LnUrlAuthImpl implements InputType_LnUrlAuth { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3732,7 +4061,7 @@ class _$InputType_LnUrlErrorImpl implements InputType_LnUrlError { TResult when({ required TResult Function(BitcoinAddressData address) bitcoinAddress, required TResult Function(LNInvoice invoice) bolt11, - required TResult Function(String offer) bolt12, + required TResult Function(LNOffer offer) bolt12, required TResult Function(String nodeId) nodeId, required TResult Function(String url) url, required TResult Function(LnUrlPayRequestData data) lnUrlPay, @@ -3748,7 +4077,7 @@ class _$InputType_LnUrlErrorImpl implements InputType_LnUrlError { TResult? whenOrNull({ TResult? Function(BitcoinAddressData address)? bitcoinAddress, TResult? Function(LNInvoice invoice)? bolt11, - TResult? Function(String offer)? bolt12, + TResult? Function(LNOffer offer)? bolt12, TResult? Function(String nodeId)? nodeId, TResult? Function(String url)? url, TResult? Function(LnUrlPayRequestData data)? lnUrlPay, @@ -3764,7 +4093,7 @@ class _$InputType_LnUrlErrorImpl implements InputType_LnUrlError { TResult maybeWhen({ TResult Function(BitcoinAddressData address)? bitcoinAddress, TResult Function(LNInvoice invoice)? bolt11, - TResult Function(String offer)? bolt12, + TResult Function(LNOffer offer)? bolt12, TResult Function(String nodeId)? nodeId, TResult Function(String url)? url, TResult Function(LnUrlPayRequestData data)? lnUrlPay, diff --git a/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKMapper.kt b/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKMapper.kt index c6fb8af6c..069f6a232 100644 --- a/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKMapper.kt +++ b/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKMapper.kt @@ -830,6 +830,59 @@ fun asLnInvoiceList(arr: ReadableArray): List { return list } +fun asLnOffer(lnOffer: ReadableMap): LnOffer? { + if (!validateMandatoryFields( + lnOffer, + arrayOf( + "bolt12", + "chains", + "description", + "signingPubkey", + ), + ) + ) { + return null + } + val bolt12 = lnOffer.getString("bolt12")!! + val chains = lnOffer.getArray("chains")?.let { asStringList(it) }!! + val description = lnOffer.getString("description")!! + val signingPubkey = lnOffer.getString("signingPubkey")!! + val amount = if (hasNonNullKey(lnOffer, "amount")) lnOffer.getMap("amount")?.let { asAmount(it) } else null + val absoluteExpiry = if (hasNonNullKey(lnOffer, "absoluteExpiry")) lnOffer.getDouble("absoluteExpiry").toULong() else null + val issuer = if (hasNonNullKey(lnOffer, "issuer")) lnOffer.getString("issuer") else null + return LnOffer( + bolt12, + chains, + description, + signingPubkey, + amount, + absoluteExpiry, + issuer, + ) +} + +fun readableMapOf(lnOffer: LnOffer): ReadableMap = + readableMapOf( + "bolt12" to lnOffer.bolt12, + "chains" to readableArrayOf(lnOffer.chains), + "description" to lnOffer.description, + "signingPubkey" to lnOffer.signingPubkey, + "amount" to lnOffer.amount?.let { readableMapOf(it) }, + "absoluteExpiry" to lnOffer.absoluteExpiry, + "issuer" to lnOffer.issuer, + ) + +fun asLnOfferList(arr: ReadableArray): List { + val list = ArrayList() + for (value in arr.toArrayList()) { + when (value) { + is ReadableMap -> list.add(asLnOffer(value)!!) + else -> throw SdkException.Generic(errUnexpectedType("${value::class.java.name}")) + } + } + return list +} + fun asListPaymentsRequest(listPaymentsRequest: ReadableMap): ListPaymentsRequest? { if (!validateMandatoryFields( listPaymentsRequest, @@ -3822,6 +3875,45 @@ fun asAesSuccessActionDataResultList(arr: ReadableArray): List { + pushToMap(map, "type", "bitcoin") + pushToMap(map, "amountMsat", amount.amountMsat) + } + is Amount.Currency -> { + pushToMap(map, "type", "currency") + pushToMap(map, "iso4217Code", amount.iso4217Code) + pushToMap(map, "fractionalAmount", amount.fractionalAmount) + } + } + return map +} + +fun asAmountList(arr: ReadableArray): List { + val list = ArrayList() + for (value in arr.toArrayList()) { + when (value) { + is ReadableMap -> list.add(asAmount(value)!!) + else -> throw SdkException.Generic(errUnexpectedType("${value::class.java.name}")) + } + } + return list +} + fun asBreezEvent(breezEvent: ReadableMap): BreezEvent? { val type = breezEvent.getString("type") @@ -3988,7 +4080,7 @@ fun asInputType(inputType: ReadableMap): InputType? { return InputType.Bolt11(inputType.getMap("invoice")?.let { asLnInvoice(it) }!!) } if (type == "bolt12") { - return InputType.Bolt12(inputType.getString("offer")!!) + return InputType.Bolt12(inputType.getMap("offer")?.let { asLnOffer(it) }!!) } if (type == "nodeId") { return InputType.NodeId(inputType.getString("nodeId")!!) @@ -4024,7 +4116,7 @@ fun readableMapOf(inputType: InputType): ReadableMap? { } is InputType.Bolt12 -> { pushToMap(map, "type", "bolt12") - pushToMap(map, "offer", inputType.offer) + pushToMap(map, "offer", readableMapOf(inputType.offer)) } is InputType.NodeId -> { pushToMap(map, "type", "nodeId") diff --git a/libs/sdk-react-native/ios/BreezSDKMapper.swift b/libs/sdk-react-native/ios/BreezSDKMapper.swift index 4bfe0a79c..2e72bb743 100644 --- a/libs/sdk-react-native/ios/BreezSDKMapper.swift +++ b/libs/sdk-react-native/ios/BreezSDKMapper.swift @@ -962,6 +962,79 @@ enum BreezSDKMapper { return lnInvoiceList.map { v -> [String: Any?] in return dictionaryOf(lnInvoice: v) } } + static func asLnOffer(lnOffer: [String: Any?]) throws -> LnOffer { + guard let bolt12 = lnOffer["bolt12"] as? String else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "bolt12", typeName: "LnOffer")) + } + guard let chains = lnOffer["chains"] as? [String] else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "chains", typeName: "LnOffer")) + } + guard let description = lnOffer["description"] as? String else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "description", typeName: "LnOffer")) + } + guard let signingPubkey = lnOffer["signingPubkey"] as? String else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "signingPubkey", typeName: "LnOffer")) + } + var amount: Amount? + if let amountTmp = lnOffer["amount"] as? [String: Any?] { + amount = try asAmount(amount: amountTmp) + } + + var absoluteExpiry: UInt64? + if hasNonNilKey(data: lnOffer, key: "absoluteExpiry") { + guard let absoluteExpiryTmp = lnOffer["absoluteExpiry"] as? UInt64 else { + throw SdkError.Generic(message: errUnexpectedValue(fieldName: "absoluteExpiry")) + } + absoluteExpiry = absoluteExpiryTmp + } + var issuer: String? + if hasNonNilKey(data: lnOffer, key: "issuer") { + guard let issuerTmp = lnOffer["issuer"] as? String else { + throw SdkError.Generic(message: errUnexpectedValue(fieldName: "issuer")) + } + issuer = issuerTmp + } + + return LnOffer( + bolt12: bolt12, + chains: chains, + description: description, + signingPubkey: signingPubkey, + amount: amount, + absoluteExpiry: absoluteExpiry, + issuer: issuer + ) + } + + static func dictionaryOf(lnOffer: LnOffer) -> [String: Any?] { + return [ + "bolt12": lnOffer.bolt12, + "chains": lnOffer.chains, + "description": lnOffer.description, + "signingPubkey": lnOffer.signingPubkey, + "amount": lnOffer.amount == nil ? nil : dictionaryOf(amount: lnOffer.amount!), + "absoluteExpiry": lnOffer.absoluteExpiry == nil ? nil : lnOffer.absoluteExpiry, + "issuer": lnOffer.issuer == nil ? nil : lnOffer.issuer, + ] + } + + static func asLnOfferList(arr: [Any]) throws -> [LnOffer] { + var list = [LnOffer]() + for value in arr { + if let val = value as? [String: Any?] { + var lnOffer = try asLnOffer(lnOffer: val) + list.append(lnOffer) + } else { + throw SdkError.Generic(message: errUnexpectedType(typeName: "LnOffer")) + } + } + return list + } + + static func arrayOf(lnOfferList: [LnOffer]) -> [Any] { + return lnOfferList.map { v -> [String: Any?] in return dictionaryOf(lnOffer: v) } + } + static func asListPaymentsRequest(listPaymentsRequest: [String: Any?]) throws -> ListPaymentsRequest { var filters: [PaymentTypeFilter]? if let filtersTmp = listPaymentsRequest["filters"] as? [String] { @@ -4330,6 +4403,62 @@ enum BreezSDKMapper { return list } + static func asAmount(amount: [String: Any?]) throws -> Amount { + let type = amount["type"] as! String + if type == "bitcoin" { + guard let _amountMsat = amount["amountMsat"] as? UInt64 else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "amountMsat", typeName: "Amount")) + } + return Amount.bitcoin(amountMsat: _amountMsat) + } + if type == "currency" { + guard let _iso4217Code = amount["iso4217Code"] as? String else { + throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "iso4217Code", typeName: "Amount")) + } + return Amount.currency(iso4217Code: _iso4217Code) + } + + throw SdkError.Generic(message: "Unexpected type \(type) for enum Amount") + } + + static func dictionaryOf(amount: Amount) -> [String: Any?] { + switch amount { + case let .bitcoin( + amountMsat + ): + return [ + "type": "bitcoin", + "amountMsat": amountMsat, + ] + + case let .currency( + iso4217Code, fractionalAmount + ): + return [ + "type": "currency", + "iso4217Code": iso4217Code, + "fractionalAmount": fractionalAmount, + ] + } + } + + static func arrayOf(amountList: [Amount]) -> [Any] { + return amountList.map { v -> [String: Any?] in return dictionaryOf(amount: v) } + } + + static func asAmountList(arr: [Any]) throws -> [Amount] { + var list = [Amount]() + for value in arr { + if let val = value as? [String: Any?] { + var amount = try asAmount(amount: val) + list.append(amount) + } else { + throw SdkError.Generic(message: errUnexpectedType(typeName: "Amount")) + } + } + return list + } + static func asBreezEvent(breezEvent: [String: Any?]) throws -> BreezEvent { let type = breezEvent["type"] as! String if type == "newBlock" { @@ -4723,9 +4852,11 @@ enum BreezSDKMapper { return InputType.bolt11(invoice: _invoice) } if type == "bolt12" { - guard let _offer = inputType["offer"] as? String else { + guard let offerTmp = inputType["offer"] as? [String: Any?] else { throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "offer", typeName: "InputType")) } + let _offer = try asLnOffer(lnOffer: offerTmp) + return InputType.bolt12(offer: _offer) } if type == "nodeId" { @@ -4799,7 +4930,7 @@ enum BreezSDKMapper { ): return [ "type": "bolt12", - "offer": offer, + "offer": dictionaryOf(lnOffer: offer), ] case let .nodeId( diff --git a/libs/sdk-react-native/src/index.ts b/libs/sdk-react-native/src/index.ts index 2bf4e885f..f514c2177 100644 --- a/libs/sdk-react-native/src/index.ts +++ b/libs/sdk-react-native/src/index.ts @@ -143,6 +143,16 @@ export interface LnInvoice { minFinalCltvExpiryDelta: number } +export interface LnOffer { + bolt12: string + chains: string[] + description: string + signingPubkey: string + amount?: Amount + absoluteExpiry?: number + issuer?: string +} + export interface ListPaymentsRequest { filters?: PaymentTypeFilter[] metadataFilters?: MetadataFilter[] @@ -601,6 +611,20 @@ export type AesSuccessActionDataResult = { reason: string } +export enum AmountVariant { + BITCOIN = "bitcoin", + CURRENCY = "currency" +} + +export type Amount = { + type: AmountVariant.BITCOIN, + amountMsat: number +} | { + type: AmountVariant.CURRENCY, + iso4217Code: string + fractionalAmount: number +} + export enum BreezEventVariant { NEW_BLOCK = "newBlock", INVOICE_PAID = "invoicePaid", @@ -691,7 +715,7 @@ export type InputType = { invoice: LnInvoice } | { type: InputTypeVariant.BOLT12, - offer: string + offer: LnOffer } | { type: InputTypeVariant.NODE_ID, nodeId: string