From 0ebb22274672a3bdd94d5d48d2c2ce81a7fce1a9 Mon Sep 17 00:00:00 2001 From: Ross Savage Date: Fri, 19 Apr 2024 17:21:48 +0200 Subject: [PATCH] Rebase and regenerate --- .../java/com/lssdk/LiquidSwapSDKMapper.kt | 159 +++++++++++------ .../java/com/lssdk/LiquidSwapSDKModule.kt | 50 +++++- .../ios/LiquidSwapSDKMapper.swift | 161 ++++++++++++------ lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.m | 12 ++ .../ios/RNLiquidSwapSDK.swift | 29 +++- lib/ls-sdk-react-native/src/index.ts | 38 +++-- 6 files changed, 318 insertions(+), 131 deletions(-) diff --git a/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt b/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt index b25a40e92..315549601 100644 --- a/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt +++ b/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKMapper.kt @@ -3,93 +3,146 @@ import com.facebook.react.bridge.* import ls_sdk.* import java.util.* -fun asPreparePaymentResponse(preparePaymentResponse: ReadableMap): PreparePaymentResponse? { +fun asPrepareReceiveRequest(prepareReceiveRequest: ReadableMap): PrepareReceiveRequest? { if (!validateMandatoryFields( - preparePaymentResponse, + prepareReceiveRequest, + arrayOf(), + ) + ) { + return null + } + val payerAmountSat = + if (hasNonNullKey( + prepareReceiveRequest, + "payerAmountSat", + ) + ) { + prepareReceiveRequest.getDouble("payerAmountSat").toULong() + } else { + null + } + val receiverAmountSat = + if (hasNonNullKey( + prepareReceiveRequest, + "receiverAmountSat", + ) + ) { + prepareReceiveRequest.getDouble("receiverAmountSat").toULong() + } else { + null + } + return PrepareReceiveRequest( + payerAmountSat, + receiverAmountSat, + ) +} + +fun readableMapOf(prepareReceiveRequest: PrepareReceiveRequest): ReadableMap { + return readableMapOf( + "payerAmountSat" to prepareReceiveRequest.payerAmountSat, + "receiverAmountSat" to prepareReceiveRequest.receiverAmountSat, + ) +} + +fun asPrepareReceiveRequestList(arr: ReadableArray): List { + val list = ArrayList() + for (value in arr.toArrayList()) { + when (value) { + is ReadableMap -> list.add(asPrepareReceiveRequest(value)!!) + else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}")) + } + } + return list +} + +fun asPrepareReceiveResponse(prepareReceiveResponse: ReadableMap): PrepareReceiveResponse? { + if (!validateMandatoryFields( + prepareReceiveResponse, arrayOf( - "id", - "fundingAmount", - "fundingAddress", + "pairHash", + "payerAmountSat", + "feesSat", ), ) ) { return null } - val id = preparePaymentResponse.getString("id")!! - val fundingAmount = preparePaymentResponse.getDouble("fundingAmount").toULong() - val fundingAddress = preparePaymentResponse.getString("fundingAddress")!! - return PreparePaymentResponse( - id, - fundingAmount, - fundingAddress, + val pairHash = prepareReceiveResponse.getString("pairHash")!! + val payerAmountSat = prepareReceiveResponse.getDouble("payerAmountSat").toULong() + val feesSat = prepareReceiveResponse.getDouble("feesSat").toULong() + return PrepareReceiveResponse( + pairHash, + payerAmountSat, + feesSat, ) } -fun readableMapOf(preparePaymentResponse: PreparePaymentResponse): ReadableMap { +fun readableMapOf(prepareReceiveResponse: PrepareReceiveResponse): ReadableMap { return readableMapOf( - "id" to preparePaymentResponse.id, - "fundingAmount" to preparePaymentResponse.fundingAmount, - "fundingAddress" to preparePaymentResponse.fundingAddress, + "pairHash" to prepareReceiveResponse.pairHash, + "payerAmountSat" to prepareReceiveResponse.payerAmountSat, + "feesSat" to prepareReceiveResponse.feesSat, ) } -fun asPreparePaymentResponseList(arr: ReadableArray): List { - val list = ArrayList() +fun asPrepareReceiveResponseList(arr: ReadableArray): List { + val list = ArrayList() for (value in arr.toArrayList()) { when (value) { - is ReadableMap -> list.add(asPreparePaymentResponse(value)!!) + is ReadableMap -> list.add(asPrepareReceiveResponse(value)!!) else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}")) } } return list } -fun asReceivePaymentRequest(receivePaymentRequest: ReadableMap): ReceivePaymentRequest? { +fun asPrepareSendResponse(prepareSendResponse: ReadableMap): PrepareSendResponse? { if (!validateMandatoryFields( - receivePaymentRequest, - arrayOf(), + prepareSendResponse, + arrayOf( + "id", + "payerAmountSat", + "receiverAmountSat", + "totalFees", + "fundingAddress", + "invoice", + ), ) ) { return null } - val invoiceAmountSat = - if (hasNonNullKey( - receivePaymentRequest, - "invoiceAmountSat", - ) - ) { - receivePaymentRequest.getDouble("invoiceAmountSat").toULong() - } else { - null - } - val onchainAmountSat = - if (hasNonNullKey( - receivePaymentRequest, - "onchainAmountSat", - ) - ) { - receivePaymentRequest.getDouble("onchainAmountSat").toULong() - } else { - null - } - return ReceivePaymentRequest( - invoiceAmountSat, - onchainAmountSat, + val id = prepareSendResponse.getString("id")!! + val payerAmountSat = prepareSendResponse.getDouble("payerAmountSat").toULong() + val receiverAmountSat = prepareSendResponse.getDouble("receiverAmountSat").toULong() + val totalFees = prepareSendResponse.getDouble("totalFees").toULong() + val fundingAddress = prepareSendResponse.getString("fundingAddress")!! + val invoice = prepareSendResponse.getString("invoice")!! + return PrepareSendResponse( + id, + payerAmountSat, + receiverAmountSat, + totalFees, + fundingAddress, + invoice, ) } -fun readableMapOf(receivePaymentRequest: ReceivePaymentRequest): ReadableMap { +fun readableMapOf(prepareSendResponse: PrepareSendResponse): ReadableMap { return readableMapOf( - "invoiceAmountSat" to receivePaymentRequest.invoiceAmountSat, - "onchainAmountSat" to receivePaymentRequest.onchainAmountSat, + "id" to prepareSendResponse.id, + "payerAmountSat" to prepareSendResponse.payerAmountSat, + "receiverAmountSat" to prepareSendResponse.receiverAmountSat, + "totalFees" to prepareSendResponse.totalFees, + "fundingAddress" to prepareSendResponse.fundingAddress, + "invoice" to prepareSendResponse.invoice, ) } -fun asReceivePaymentRequestList(arr: ReadableArray): List { - val list = ArrayList() +fun asPrepareSendResponseList(arr: ReadableArray): List { + val list = ArrayList() for (value in arr.toArrayList()) { when (value) { - is ReadableMap -> list.add(asReceivePaymentRequest(value)!!) + is ReadableMap -> list.add(asPrepareSendResponse(value)!!) else -> throw LsSdkException.Generic(errUnexpectedType("${value::class.java.name}")) } } @@ -172,7 +225,6 @@ fun asWalletInfo(walletInfo: ReadableMap): WalletInfo? { arrayOf( "balanceSat", "pubkey", - "activeAddress", ), ) ) { @@ -180,11 +232,9 @@ fun asWalletInfo(walletInfo: ReadableMap): WalletInfo? { } val balanceSat = walletInfo.getDouble("balanceSat").toULong() val pubkey = walletInfo.getString("pubkey")!! - val activeAddress = walletInfo.getString("activeAddress")!! return WalletInfo( balanceSat, pubkey, - activeAddress, ) } @@ -192,7 +242,6 @@ fun readableMapOf(walletInfo: WalletInfo): ReadableMap { return readableMapOf( "balanceSat" to walletInfo.balanceSat, "pubkey" to walletInfo.pubkey, - "activeAddress" to walletInfo.activeAddress, ) } diff --git a/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt b/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt index a217f04a3..796012bfc 100644 --- a/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt +++ b/lib/ls-sdk-react-native/android/src/main/java/com/lssdk/LiquidSwapSDKModule.kt @@ -78,6 +78,21 @@ class LiquidSwapSDKModule(reactContext: ReactApplicationContext) : ReactContextB } } + @ReactMethod + fun prepareSendPayment( + invoice: String, + promise: Promise, + ) { + executor.execute { + try { + val res = getBindingWallet().prepareSendPayment(invoice) + promise.resolve(readableMapOf(res)) + } catch (e: Exception) { + promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e) + } + } + } + @ReactMethod fun sendPayment( req: ReadableMap, @@ -85,11 +100,30 @@ class LiquidSwapSDKModule(reactContext: ReactApplicationContext) : ReactContextB ) { executor.execute { try { - val preparePaymentResponse = - asPreparePaymentResponse(req) ?: run { - throw LsSdkException.Generic(errMissingMandatoryField("req", "PreparePaymentResponse")) + val prepareSendResponse = + asPrepareSendResponse(req) ?: run { + throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareSendResponse")) + } + val res = getBindingWallet().sendPayment(prepareSendResponse) + promise.resolve(readableMapOf(res)) + } catch (e: Exception) { + promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e) + } + } + } + + @ReactMethod + fun prepareReceivePayment( + req: ReadableMap, + promise: Promise, + ) { + executor.execute { + try { + val prepareReceiveRequest = + asPrepareReceiveRequest(req) ?: run { + throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareReceiveRequest")) } - val res = getBindingWallet().sendPayment(preparePaymentResponse) + val res = getBindingWallet().prepareReceivePayment(prepareReceiveRequest) promise.resolve(readableMapOf(res)) } catch (e: Exception) { promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e) @@ -104,11 +138,11 @@ class LiquidSwapSDKModule(reactContext: ReactApplicationContext) : ReactContextB ) { executor.execute { try { - val receivePaymentRequest = - asReceivePaymentRequest(req) ?: run { - throw LsSdkException.Generic(errMissingMandatoryField("req", "ReceivePaymentRequest")) + val prepareReceiveResponse = + asPrepareReceiveResponse(req) ?: run { + throw LsSdkException.Generic(errMissingMandatoryField("req", "PrepareReceiveResponse")) } - val res = getBindingWallet().receivePayment(receivePaymentRequest) + val res = getBindingWallet().receivePayment(prepareReceiveResponse) promise.resolve(readableMapOf(res)) } catch (e: Exception) { promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e) diff --git a/lib/ls-sdk-react-native/ios/LiquidSwapSDKMapper.swift b/lib/ls-sdk-react-native/ios/LiquidSwapSDKMapper.swift index e54366c74..86f4c8cb6 100644 --- a/lib/ls-sdk-react-native/ios/LiquidSwapSDKMapper.swift +++ b/lib/ls-sdk-react-native/ios/LiquidSwapSDKMapper.swift @@ -2,93 +2,151 @@ import Foundation import LiquidSwapSDK enum LiquidSwapSDKMapper { - static func asPreparePaymentResponse(preparePaymentResponse: [String: Any?]) throws -> PreparePaymentResponse { - guard let id = preparePaymentResponse["id"] as? String else { - throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "id", typeName: "PreparePaymentResponse")) - } - guard let fundingAmount = preparePaymentResponse["fundingAmount"] as? UInt64 else { - throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "fundingAmount", typeName: "PreparePaymentResponse")) + static func asPrepareReceiveRequest(prepareReceiveRequest: [String: Any?]) throws -> PrepareReceiveRequest { + var payerAmountSat: UInt64? + if hasNonNilKey(data: prepareReceiveRequest, key: "payerAmountSat") { + guard let payerAmountSatTmp = prepareReceiveRequest["payerAmountSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errUnexpectedValue(fieldName: "payerAmountSat")) + } + payerAmountSat = payerAmountSatTmp } - guard let fundingAddress = preparePaymentResponse["fundingAddress"] as? String else { - throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "fundingAddress", typeName: "PreparePaymentResponse")) + var receiverAmountSat: UInt64? + if hasNonNilKey(data: prepareReceiveRequest, key: "receiverAmountSat") { + guard let receiverAmountSatTmp = prepareReceiveRequest["receiverAmountSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errUnexpectedValue(fieldName: "receiverAmountSat")) + } + receiverAmountSat = receiverAmountSatTmp } - return PreparePaymentResponse( - id: id, - fundingAmount: fundingAmount, - fundingAddress: fundingAddress + return PrepareReceiveRequest( + payerAmountSat: payerAmountSat, + receiverAmountSat: receiverAmountSat ) } - static func dictionaryOf(preparePaymentResponse: PreparePaymentResponse) -> [String: Any?] { + static func dictionaryOf(prepareReceiveRequest: PrepareReceiveRequest) -> [String: Any?] { return [ - "id": preparePaymentResponse.id, - "fundingAmount": preparePaymentResponse.fundingAmount, - "fundingAddress": preparePaymentResponse.fundingAddress, + "payerAmountSat": prepareReceiveRequest.payerAmountSat == nil ? nil : prepareReceiveRequest.payerAmountSat, + "receiverAmountSat": prepareReceiveRequest.receiverAmountSat == nil ? nil : prepareReceiveRequest.receiverAmountSat, ] } - static func asPreparePaymentResponseList(arr: [Any]) throws -> [PreparePaymentResponse] { - var list = [PreparePaymentResponse]() + static func asPrepareReceiveRequestList(arr: [Any]) throws -> [PrepareReceiveRequest] { + var list = [PrepareReceiveRequest]() for value in arr { if let val = value as? [String: Any?] { - var preparePaymentResponse = try asPreparePaymentResponse(preparePaymentResponse: val) - list.append(preparePaymentResponse) + var prepareReceiveRequest = try asPrepareReceiveRequest(prepareReceiveRequest: val) + list.append(prepareReceiveRequest) } else { - throw LsSdkError.Generic(message: errUnexpectedType(typeName: "PreparePaymentResponse")) + throw LsSdkError.Generic(message: errUnexpectedType(typeName: "PrepareReceiveRequest")) } } return list } - static func arrayOf(preparePaymentResponseList: [PreparePaymentResponse]) -> [Any] { - return preparePaymentResponseList.map { v -> [String: Any?] in dictionaryOf(preparePaymentResponse: v) } + static func arrayOf(prepareReceiveRequestList: [PrepareReceiveRequest]) -> [Any] { + return prepareReceiveRequestList.map { v -> [String: Any?] in dictionaryOf(prepareReceiveRequest: v) } } - static func asReceivePaymentRequest(receivePaymentRequest: [String: Any?]) throws -> ReceivePaymentRequest { - var invoiceAmountSat: UInt64? - if hasNonNilKey(data: receivePaymentRequest, key: "invoiceAmountSat") { - guard let invoiceAmountSatTmp = receivePaymentRequest["invoiceAmountSat"] as? UInt64 else { - throw LsSdkError.Generic(message: errUnexpectedValue(fieldName: "invoiceAmountSat")) - } - invoiceAmountSat = invoiceAmountSatTmp + static func asPrepareReceiveResponse(prepareReceiveResponse: [String: Any?]) throws -> PrepareReceiveResponse { + guard let pairHash = prepareReceiveResponse["pairHash"] as? String else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "pairHash", typeName: "PrepareReceiveResponse")) + } + guard let payerAmountSat = prepareReceiveResponse["payerAmountSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "payerAmountSat", typeName: "PrepareReceiveResponse")) } - var onchainAmountSat: UInt64? - if hasNonNilKey(data: receivePaymentRequest, key: "onchainAmountSat") { - guard let onchainAmountSatTmp = receivePaymentRequest["onchainAmountSat"] as? UInt64 else { - throw LsSdkError.Generic(message: errUnexpectedValue(fieldName: "onchainAmountSat")) + guard let feesSat = prepareReceiveResponse["feesSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "feesSat", typeName: "PrepareReceiveResponse")) + } + + return PrepareReceiveResponse( + pairHash: pairHash, + payerAmountSat: payerAmountSat, + feesSat: feesSat + ) + } + + static func dictionaryOf(prepareReceiveResponse: PrepareReceiveResponse) -> [String: Any?] { + return [ + "pairHash": prepareReceiveResponse.pairHash, + "payerAmountSat": prepareReceiveResponse.payerAmountSat, + "feesSat": prepareReceiveResponse.feesSat, + ] + } + + static func asPrepareReceiveResponseList(arr: [Any]) throws -> [PrepareReceiveResponse] { + var list = [PrepareReceiveResponse]() + for value in arr { + if let val = value as? [String: Any?] { + var prepareReceiveResponse = try asPrepareReceiveResponse(prepareReceiveResponse: val) + list.append(prepareReceiveResponse) + } else { + throw LsSdkError.Generic(message: errUnexpectedType(typeName: "PrepareReceiveResponse")) } - onchainAmountSat = onchainAmountSatTmp + } + return list + } + + static func arrayOf(prepareReceiveResponseList: [PrepareReceiveResponse]) -> [Any] { + return prepareReceiveResponseList.map { v -> [String: Any?] in dictionaryOf(prepareReceiveResponse: v) } + } + + static func asPrepareSendResponse(prepareSendResponse: [String: Any?]) throws -> PrepareSendResponse { + guard let id = prepareSendResponse["id"] as? String else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "id", typeName: "PrepareSendResponse")) + } + guard let payerAmountSat = prepareSendResponse["payerAmountSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "payerAmountSat", typeName: "PrepareSendResponse")) + } + guard let receiverAmountSat = prepareSendResponse["receiverAmountSat"] as? UInt64 else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "receiverAmountSat", typeName: "PrepareSendResponse")) + } + guard let totalFees = prepareSendResponse["totalFees"] as? UInt64 else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "totalFees", typeName: "PrepareSendResponse")) + } + guard let fundingAddress = prepareSendResponse["fundingAddress"] as? String else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "fundingAddress", typeName: "PrepareSendResponse")) + } + guard let invoice = prepareSendResponse["invoice"] as? String else { + throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "invoice", typeName: "PrepareSendResponse")) } - return ReceivePaymentRequest( - invoiceAmountSat: invoiceAmountSat, - onchainAmountSat: onchainAmountSat + return PrepareSendResponse( + id: id, + payerAmountSat: payerAmountSat, + receiverAmountSat: receiverAmountSat, + totalFees: totalFees, + fundingAddress: fundingAddress, + invoice: invoice ) } - static func dictionaryOf(receivePaymentRequest: ReceivePaymentRequest) -> [String: Any?] { + static func dictionaryOf(prepareSendResponse: PrepareSendResponse) -> [String: Any?] { return [ - "invoiceAmountSat": receivePaymentRequest.invoiceAmountSat == nil ? nil : receivePaymentRequest.invoiceAmountSat, - "onchainAmountSat": receivePaymentRequest.onchainAmountSat == nil ? nil : receivePaymentRequest.onchainAmountSat, + "id": prepareSendResponse.id, + "payerAmountSat": prepareSendResponse.payerAmountSat, + "receiverAmountSat": prepareSendResponse.receiverAmountSat, + "totalFees": prepareSendResponse.totalFees, + "fundingAddress": prepareSendResponse.fundingAddress, + "invoice": prepareSendResponse.invoice, ] } - static func asReceivePaymentRequestList(arr: [Any]) throws -> [ReceivePaymentRequest] { - var list = [ReceivePaymentRequest]() + static func asPrepareSendResponseList(arr: [Any]) throws -> [PrepareSendResponse] { + var list = [PrepareSendResponse]() for value in arr { if let val = value as? [String: Any?] { - var receivePaymentRequest = try asReceivePaymentRequest(receivePaymentRequest: val) - list.append(receivePaymentRequest) + var prepareSendResponse = try asPrepareSendResponse(prepareSendResponse: val) + list.append(prepareSendResponse) } else { - throw LsSdkError.Generic(message: errUnexpectedType(typeName: "ReceivePaymentRequest")) + throw LsSdkError.Generic(message: errUnexpectedType(typeName: "PrepareSendResponse")) } } return list } - static func arrayOf(receivePaymentRequestList: [ReceivePaymentRequest]) -> [Any] { - return receivePaymentRequestList.map { v -> [String: Any?] in dictionaryOf(receivePaymentRequest: v) } + static func arrayOf(prepareSendResponseList: [PrepareSendResponse]) -> [Any] { + return prepareSendResponseList.map { v -> [String: Any?] in dictionaryOf(prepareSendResponse: v) } } static func asReceivePaymentResponse(receivePaymentResponse: [String: Any?]) throws -> ReceivePaymentResponse { @@ -168,14 +226,10 @@ enum LiquidSwapSDKMapper { guard let pubkey = walletInfo["pubkey"] as? String else { throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "pubkey", typeName: "WalletInfo")) } - guard let activeAddress = walletInfo["activeAddress"] as? String else { - throw LsSdkError.Generic(message: errMissingMandatoryField(fieldName: "activeAddress", typeName: "WalletInfo")) - } return WalletInfo( balanceSat: balanceSat, - pubkey: pubkey, - activeAddress: activeAddress + pubkey: pubkey ) } @@ -183,7 +237,6 @@ enum LiquidSwapSDKMapper { return [ "balanceSat": walletInfo.balanceSat, "pubkey": walletInfo.pubkey, - "activeAddress": walletInfo.activeAddress, ] } diff --git a/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.m b/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.m index 656ef8f7e..525536132 100644 --- a/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.m +++ b/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.m @@ -17,12 +17,24 @@ @interface RCT_EXTERN_MODULE(RNLiquidSwapSDK, RCTEventEmitter) reject: (RCTPromiseRejectBlock)reject ) +RCT_EXTERN_METHOD( + prepareSendPayment: (NSString*)invoice + resolve: (RCTPromiseResolveBlock)resolve + reject: (RCTPromiseRejectBlock)reject +) + RCT_EXTERN_METHOD( sendPayment: (NSDictionary*)req resolve: (RCTPromiseResolveBlock)resolve reject: (RCTPromiseRejectBlock)reject ) +RCT_EXTERN_METHOD( + prepareReceivePayment: (NSDictionary*)req + resolve: (RCTPromiseResolveBlock)resolve + reject: (RCTPromiseRejectBlock)reject +) + RCT_EXTERN_METHOD( receivePayment: (NSDictionary*)req resolve: (RCTPromiseResolveBlock)resolve diff --git a/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.swift b/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.swift index 5644b0759..fa59865ad 100644 --- a/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.swift +++ b/lib/ls-sdk-react-native/ios/RNLiquidSwapSDK.swift @@ -78,22 +78,43 @@ class RNLiquidSwapSDK: RCTEventEmitter { } } + @objc(prepareSendPayment:resolve:reject:) + func prepareSendPayment(_ invoice: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { + do { + var res = try getBindingWallet().prepareSendPayment(invoice: invoice) + resolve(LiquidSwapSDKMapper.dictionaryOf(prepareSendResponse: res)) + } catch let err { + rejectErr(err: err, reject: reject) + } + } + @objc(sendPayment:resolve:reject:) func sendPayment(_ req: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { do { - let preparePaymentResponse = try LiquidSwapSDKMapper.asPreparePaymentResponse(preparePaymentResponse: req) - var res = try getBindingWallet().sendPayment(req: preparePaymentResponse) + let prepareSendResponse = try LiquidSwapSDKMapper.asPrepareSendResponse(prepareSendResponse: req) + var res = try getBindingWallet().sendPayment(req: prepareSendResponse) resolve(LiquidSwapSDKMapper.dictionaryOf(sendPaymentResponse: res)) } catch let err { rejectErr(err: err, reject: reject) } } + @objc(prepareReceivePayment:resolve:reject:) + func prepareReceivePayment(_ req: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { + do { + let prepareReceiveRequest = try LiquidSwapSDKMapper.asPrepareReceiveRequest(prepareReceiveRequest: req) + var res = try getBindingWallet().prepareReceivePayment(req: prepareReceiveRequest) + resolve(LiquidSwapSDKMapper.dictionaryOf(prepareReceiveResponse: res)) + } catch let err { + rejectErr(err: err, reject: reject) + } + } + @objc(receivePayment:resolve:reject:) func receivePayment(_ req: [String: Any], resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { do { - let receivePaymentRequest = try LiquidSwapSDKMapper.asReceivePaymentRequest(receivePaymentRequest: req) - var res = try getBindingWallet().receivePayment(req: receivePaymentRequest) + let prepareReceiveResponse = try LiquidSwapSDKMapper.asPrepareReceiveResponse(prepareReceiveResponse: req) + var res = try getBindingWallet().receivePayment(req: prepareReceiveResponse) resolve(LiquidSwapSDKMapper.dictionaryOf(receivePaymentResponse: res)) } catch let err { rejectErr(err: err, reject: reject) diff --git a/lib/ls-sdk-react-native/src/index.ts b/lib/ls-sdk-react-native/src/index.ts index a7d998b41..8ca4a7f4f 100644 --- a/lib/ls-sdk-react-native/src/index.ts +++ b/lib/ls-sdk-react-native/src/index.ts @@ -17,15 +17,24 @@ const LiquidSwapSDK = NativeModules.RNLiquidSwapSDK } ) -export interface PreparePaymentResponse { - id: string - fundingAmount: number - fundingAddress: string +export interface PrepareReceiveRequest { + payerAmountSat?: number + receiverAmountSat?: number +} + +export interface PrepareReceiveResponse { + pairHash: string + payerAmountSat: number + feesSat: number } -export interface ReceivePaymentRequest { - invoiceAmountSat?: number - onchainAmountSat?: number +export interface PrepareSendResponse { + id: string + payerAmountSat: number + receiverAmountSat: number + totalFees: number + fundingAddress: string + invoice: string } export interface ReceivePaymentResponse { @@ -40,7 +49,6 @@ export interface SendPaymentResponse { export interface WalletInfo { balanceSat: number pubkey: string - activeAddress: string } export enum Network { @@ -58,12 +66,22 @@ export const getInfo = async (withScan: boolean): Promise => { return response } -export const sendPayment = async (req: PreparePaymentResponse): Promise => { +export const prepareSendPayment = async (invoice: string): Promise => { + const response = await LiquidSwapSDK.prepareSendPayment(invoice) + return response +} + +export const sendPayment = async (req: PrepareSendResponse): Promise => { const response = await LiquidSwapSDK.sendPayment(req) return response } -export const receivePayment = async (req: ReceivePaymentRequest): Promise => { +export const prepareReceivePayment = async (req: PrepareReceiveRequest): Promise => { + const response = await LiquidSwapSDK.prepareReceivePayment(req) + return response +} + +export const receivePayment = async (req: PrepareReceiveResponse): Promise => { const response = await LiquidSwapSDK.receivePayment(req) return response }