diff --git a/libs/sdk-bindings/src/uniffi_binding.rs b/libs/sdk-bindings/src/uniffi_binding.rs index 86d2d64ba..1e3e22533 100644 --- a/libs/sdk-bindings/src/uniffi_binding.rs +++ b/libs/sdk-bindings/src/uniffi_binding.rs @@ -13,10 +13,11 @@ use breez_sdk_core::{ FeeratePreset, FiatCurrency, GreenlightCredentials, GreenlightNodeConfig, InputType, InvoicePaidDetails, LNInvoice, ListPaymentsRequest, LnPaymentDetails, LnUrlAuthRequestData, LnUrlCallbackStatus, LnUrlErrorData, LnUrlPayRequestData, LnUrlPayResult, - LnUrlWithdrawRequestData, LocaleOverrides, LocalizedName, LogEntry, LogStream, LspInformation, - MessageSuccessActionData, MetadataItem, Network, NodeConfig, NodeState, OpenChannelFeeRequest, - OpenChannelFeeResponse, OpeningFeeParams, OpeningFeeParamsMenu, Payment, PaymentDetails, - PaymentFailedData, PaymentStatus, PaymentType, PaymentTypeFilter, Rate, ReceiveOnchainRequest, + LnUrlWithdrawCallbackStatus, LnUrlWithdrawOkData, LnUrlWithdrawRequestData, LocaleOverrides, + LocalizedName, LogEntry, LogStream, LspInformation, MessageSuccessActionData, MetadataItem, + Network, NodeConfig, NodeState, OpenChannelFeeRequest, OpenChannelFeeResponse, + OpeningFeeParams, OpeningFeeParamsMenu, Payment, PaymentDetails, PaymentFailedData, + PaymentStatus, PaymentType, PaymentTypeFilter, Rate, ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop, SignMessageRequest, SignMessageResponse, StaticBackupRequest, StaticBackupResponse, diff --git a/libs/sdk-core/src/binding.rs b/libs/sdk-core/src/binding.rs index 9ff8a6f4d..0a9ca2dc1 100644 --- a/libs/sdk-core/src/binding.rs +++ b/libs/sdk-core/src/binding.rs @@ -32,10 +32,10 @@ use crate::lsp::LspInformation; use crate::models::{Config, LogEntry, NodeState, Payment, SwapInfo}; use crate::{ BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse, - EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, NodeConfig, ReceiveOnchainRequest, - ReceivePaymentRequest, ReceivePaymentResponse, ReverseSwapFeesRequest, ReverseSwapInfo, - ReverseSwapPairInfo, SignMessageRequest, SignMessageResponse, StaticBackupRequest, - StaticBackupResponse, + EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, LnUrlWithdrawCallbackStatus, + NodeConfig, ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, + ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, SignMessageRequest, + SignMessageResponse, StaticBackupRequest, StaticBackupResponse, }; static BREEZ_SERVICES_INSTANCE: OnceCell> = OnceCell::new(); 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 1d593187d..3925be80a 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 @@ -944,6 +944,39 @@ fun asLnUrlPayRequestDataList(arr: ReadableArray): List { return list } +fun asLnUrlWithdrawOkData(data: ReadableMap): LnUrlWithdrawOkData? { + if (!validateMandatoryFields( + data, + arrayOf( + "invoice", + ), + ) + ) { + return null + } + val invoice = data.getMap("invoice")?.let { asLnInvoice(it) }!! + return LnUrlWithdrawOkData( + invoice, + ) +} + +fun readableMapOf(lnUrlWithdrawOkData: LnUrlWithdrawOkData): ReadableMap { + return readableMapOf( + "invoice" to readableMapOf(lnUrlWithdrawOkData.invoice), + ) +} + +fun asLnUrlWithdrawOkDataList(arr: ReadableArray): List { + val list = ArrayList() + for (value in arr.toArrayList()) { + when (value) { + is ReadableMap -> list.add(asLnUrlWithdrawOkData(value)!!) + else -> throw IllegalArgumentException("Unsupported type ${value::class.java.name}") + } + } + return list +} + fun asLnUrlWithdrawRequestData(data: ReadableMap): LnUrlWithdrawRequestData? { if (!validateMandatoryFields( data, @@ -2621,6 +2654,33 @@ fun readableMapOf(lnUrlPayResult: LnUrlPayResult): ReadableMap? { return map } +fun asLnUrlWithdrawCallbackStatus(data: ReadableMap): LnUrlWithdrawCallbackStatus? { + val type = data.getString("type") + + if (type == "ok") { + return LnUrlWithdrawCallbackStatus.Ok(data.getMap("data")?.let { asLnUrlWithdrawOkData(it) }!!) + } + if (type == "errorStatus") { + return LnUrlWithdrawCallbackStatus.ErrorStatus(data.getMap("data")?.let { asLnUrlErrorData(it) }!!) + } + return null +} + +fun readableMapOf(lnUrlWithdrawCallbackStatus: LnUrlWithdrawCallbackStatus): ReadableMap? { + val map = Arguments.createMap() + when (lnUrlWithdrawCallbackStatus) { + is LnUrlWithdrawCallbackStatus.Ok -> { + pushToMap(map, "type", "ok") + pushToMap(map, "data", readableMapOf(lnUrlWithdrawCallbackStatus.data)) + } + is LnUrlWithdrawCallbackStatus.ErrorStatus -> { + pushToMap(map, "type", "errorStatus") + pushToMap(map, "data", readableMapOf(lnUrlWithdrawCallbackStatus.data)) + } + } + return map +} + fun asNetwork(type: String): Network { return Network.valueOf(type.uppercase()) } diff --git a/libs/sdk-react-native/ios/BreezSDKMapper.swift b/libs/sdk-react-native/ios/BreezSDKMapper.swift index e8eaee0c8..eb4f87276 100644 --- a/libs/sdk-react-native/ios/BreezSDKMapper.swift +++ b/libs/sdk-react-native/ios/BreezSDKMapper.swift @@ -873,6 +873,37 @@ class BreezSDKMapper { return lnUrlPayRequestDataList.map { v -> [String: Any?] in dictionaryOf(lnUrlPayRequestData: v) } } + static func asLnUrlWithdrawOkData(data: [String: Any?]) throws -> LnUrlWithdrawOkData { + guard let invoiceTmp = data["invoice"] as? [String: Any?] else { throw SdkError.Generic(message: "Missing mandatory field invoice for type LnUrlWithdrawOkData") } + let invoice = try asLnInvoice(data: invoiceTmp) + + return LnUrlWithdrawOkData( + invoice: invoice) + } + + static func dictionaryOf(lnUrlWithdrawOkData: LnUrlWithdrawOkData) -> [String: Any?] { + return [ + "invoice": dictionaryOf(lnInvoice: lnUrlWithdrawOkData.invoice), + ] + } + + static func asLnUrlWithdrawOkDataList(arr: [Any]) throws -> [LnUrlWithdrawOkData] { + var list = [LnUrlWithdrawOkData]() + for value in arr { + if let val = value as? [String: Any?] { + var lnUrlWithdrawOkData = try asLnUrlWithdrawOkData(data: val) + list.append(lnUrlWithdrawOkData) + } else { + throw SdkError.Generic(message: "Invalid element type LnUrlWithdrawOkData") + } + } + return list + } + + static func arrayOf(lnUrlWithdrawOkDataList: [LnUrlWithdrawOkData]) -> [Any] { + return lnUrlWithdrawOkDataList.map { v -> [String: Any?] in dictionaryOf(lnUrlWithdrawOkData: v) } + } + static func asLnUrlWithdrawRequestData(data: [String: Any?]) throws -> LnUrlWithdrawRequestData { guard let callback = data["callback"] as? String else { throw SdkError.Generic(message: "Missing mandatory field callback for type LnUrlWithdrawRequestData") } guard let k1 = data["k1"] as? String else { throw SdkError.Generic(message: "Missing mandatory field k1 for type LnUrlWithdrawRequestData") } @@ -2596,6 +2627,44 @@ class BreezSDKMapper { } } + static func asLnUrlWithdrawCallbackStatus(data: [String: Any?]) throws -> LnUrlWithdrawCallbackStatus { + let type = data["type"] as! String + if type == "ok" { + guard let dataTmp = data["data"] as? [String: Any?] else { throw SdkError.Generic(message: "Missing mandatory field data for type LnUrlWithdrawCallbackStatus") } + let _data = try asLnUrlWithdrawOkData(data: dataTmp) + + return LnUrlWithdrawCallbackStatus.ok(data: _data) + } + if type == "errorStatus" { + guard let dataTmp = data["data"] as? [String: Any?] else { throw SdkError.Generic(message: "Missing mandatory field data for type LnUrlWithdrawCallbackStatus") } + let _data = try asLnUrlErrorData(data: dataTmp) + + return LnUrlWithdrawCallbackStatus.errorStatus(data: _data) + } + + throw SdkError.Generic(message: "Invalid enum variant \(type) for enum LnUrlWithdrawCallbackStatus") + } + + static func dictionaryOf(lnUrlWithdrawCallbackStatus: LnUrlWithdrawCallbackStatus) -> [String: Any?] { + switch lnUrlWithdrawCallbackStatus { + case let .ok( + data + ): + return [ + "type": "ok", + "data": dictionaryOf(lnUrlWithdrawOkData: data), + ] + + case let .errorStatus( + data + ): + return [ + "type": "errorStatus", + "data": dictionaryOf(lnUrlErrorData: data), + ] + } + } + static func asNetwork(type: String) throws -> Network { switch type { case "bitcoin": diff --git a/libs/sdk-react-native/ios/RNBreezSDK.swift b/libs/sdk-react-native/ios/RNBreezSDK.swift index 08722b496..d837b0b1d 100644 --- a/libs/sdk-react-native/ios/RNBreezSDK.swift +++ b/libs/sdk-react-native/ios/RNBreezSDK.swift @@ -180,7 +180,7 @@ class RNBreezSDK: RCTEventEmitter { let lnUrlWithdrawRequestData = try BreezSDKMapper.asLnUrlWithdrawRequestData(data: reqData) let descriptionTmp = description.isEmpty ? nil : description var res = try getBreezServices().withdrawLnurl(reqData: lnUrlWithdrawRequestData, amountSats: amountSats, description: descriptionTmp) - resolve(BreezSDKMapper.dictionaryOf(lnUrlCallbackStatus: res)) + resolve(BreezSDKMapper.dictionaryOf(lnUrlWithdrawCallbackStatus: res)) } catch let err { rejectErr(err: err, reject: reject) } diff --git a/libs/sdk-react-native/src/index.ts b/libs/sdk-react-native/src/index.ts index 105730e60..abbc480b2 100644 --- a/libs/sdk-react-native/src/index.ts +++ b/libs/sdk-react-native/src/index.ts @@ -163,6 +163,10 @@ export type LnUrlPayRequestData = { lnAddress?: string } +export type LnUrlWithdrawOkData = { + invoice: LnInvoice +} + export type LnUrlWithdrawRequestData = { callback: string k1: string @@ -513,6 +517,19 @@ export type LnUrlPayResult = { data: LnUrlErrorData } +export enum LnUrlWithdrawCallbackStatusVariant { + OK = "ok", + ERROR_STATUS = "errorStatus" +} + +export type LnUrlWithdrawCallbackStatus = { + type: LnUrlWithdrawCallbackStatusVariant.OK, + data: LnUrlWithdrawOkData +} | { + type: LnUrlWithdrawCallbackStatusVariant.ERROR_STATUS, + data: LnUrlErrorData +} + export enum Network { BITCOIN = "bitcoin", TESTNET = "testnet", @@ -659,7 +676,7 @@ export const payLnurl = async (reqData: LnUrlPayRequestData, amountSats: number, return response } -export const withdrawLnurl = async (reqData: LnUrlWithdrawRequestData, amountSats: number, description: string = ""): Promise => { +export const withdrawLnurl = async (reqData: LnUrlWithdrawRequestData, amountSats: number, description: string = ""): Promise => { const response = await BreezSDK.withdrawLnurl(reqData, amountSats, description) return response }