Skip to content

Commit

Permalink
Add React Native bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Sep 19, 2023
1 parent 834fd0b commit e779168
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 10 deletions.
9 changes: 5 additions & 4 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<BreezServices>> = OnceCell::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,39 @@ fun asLnUrlPayRequestDataList(arr: ReadableArray): List<LnUrlPayRequestData> {
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<LnUrlWithdrawOkData> {
val list = ArrayList<LnUrlWithdrawOkData>()
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,
Expand Down Expand Up @@ -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())
}
Expand Down
69 changes: 69 additions & 0 deletions libs/sdk-react-native/ios/BreezSDKMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down Expand Up @@ -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":
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-react-native/ios/RNBreezSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 18 additions & 1 deletion libs/sdk-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ export type LnUrlPayRequestData = {
lnAddress?: string
}

export type LnUrlWithdrawOkData = {
invoice: LnInvoice
}

export type LnUrlWithdrawRequestData = {
callback: string
k1: string
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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<LnUrlCallbackStatus> => {
export const withdrawLnurl = async (reqData: LnUrlWithdrawRequestData, amountSats: number, description: string = ""): Promise<LnUrlWithdrawCallbackStatus> => {
const response = await BreezSDK.withdrawLnurl(reqData, amountSats, description)
return response
}
Expand Down

0 comments on commit e779168

Please sign in to comment.