mirror of
https://github.com/aljazceru/breez-sdk-liquid.git
synced 2026-01-29 10:54:36 +01:00
Add Config (#267)
* Add config * Add rustdocs to Config, send_payment (#271) --------- Co-authored-by: ok300 <106775972+ok300@users.noreply.github.com>
This commit is contained in:
@@ -34,32 +34,78 @@ fun asBackupRequestList(arr: ReadableArray): List<BackupRequest> {
|
||||
return list
|
||||
}
|
||||
|
||||
fun asConnectRequest(connectRequest: ReadableMap): ConnectRequest? {
|
||||
fun asConfig(config: ReadableMap): Config? {
|
||||
if (!validateMandatoryFields(
|
||||
connectRequest,
|
||||
config,
|
||||
arrayOf(
|
||||
"mnemonic",
|
||||
"boltzUrl",
|
||||
"electrumUrl",
|
||||
"workingDir",
|
||||
"network",
|
||||
"paymentTimeoutSec",
|
||||
),
|
||||
)
|
||||
) {
|
||||
return null
|
||||
}
|
||||
val mnemonic = connectRequest.getString("mnemonic")!!
|
||||
val network = connectRequest.getString("network")?.let { asNetwork(it) }!!
|
||||
val dataDir = if (hasNonNullKey(connectRequest, "dataDir")) connectRequest.getString("dataDir") else null
|
||||
return ConnectRequest(
|
||||
mnemonic,
|
||||
val boltzUrl = config.getString("boltzUrl")!!
|
||||
val electrumUrl = config.getString("electrumUrl")!!
|
||||
val workingDir = config.getString("workingDir")!!
|
||||
val network = config.getString("network")?.let { asNetwork(it) }!!
|
||||
val paymentTimeoutSec = config.getDouble("paymentTimeoutSec").toULong()
|
||||
return Config(
|
||||
boltzUrl,
|
||||
electrumUrl,
|
||||
workingDir,
|
||||
network,
|
||||
dataDir,
|
||||
paymentTimeoutSec,
|
||||
)
|
||||
}
|
||||
|
||||
fun readableMapOf(config: Config): ReadableMap {
|
||||
return readableMapOf(
|
||||
"boltzUrl" to config.boltzUrl,
|
||||
"electrumUrl" to config.electrumUrl,
|
||||
"workingDir" to config.workingDir,
|
||||
"network" to config.network.name.lowercase(),
|
||||
"paymentTimeoutSec" to config.paymentTimeoutSec,
|
||||
)
|
||||
}
|
||||
|
||||
fun asConfigList(arr: ReadableArray): List<Config> {
|
||||
val list = ArrayList<Config>()
|
||||
for (value in arr.toArrayList()) {
|
||||
when (value) {
|
||||
is ReadableMap -> list.add(asConfig(value)!!)
|
||||
else -> throw LiquidSdkException.Generic(errUnexpectedType("${value::class.java.name}"))
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
fun asConnectRequest(connectRequest: ReadableMap): ConnectRequest? {
|
||||
if (!validateMandatoryFields(
|
||||
connectRequest,
|
||||
arrayOf(
|
||||
"config",
|
||||
"mnemonic",
|
||||
),
|
||||
)
|
||||
) {
|
||||
return null
|
||||
}
|
||||
val config = connectRequest.getMap("config")?.let { asConfig(it) }!!
|
||||
val mnemonic = connectRequest.getString("mnemonic")!!
|
||||
return ConnectRequest(
|
||||
config,
|
||||
mnemonic,
|
||||
)
|
||||
}
|
||||
|
||||
fun readableMapOf(connectRequest: ConnectRequest): ReadableMap {
|
||||
return readableMapOf(
|
||||
"config" to readableMapOf(connectRequest.config),
|
||||
"mnemonic" to connectRequest.mnemonic,
|
||||
"network" to connectRequest.network.name.lowercase(),
|
||||
"dataDir" to connectRequest.dataDir,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.breezliquidsdk
|
||||
import breez_liquid_sdk.*
|
||||
import com.facebook.react.bridge.*
|
||||
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
@@ -34,12 +35,44 @@ class BreezLiquidSDKModule(reactContext: ReactApplicationContext) : ReactContext
|
||||
throw LiquidSdkException.Generic("Not initialized")
|
||||
}
|
||||
|
||||
@Throws(LiquidSdkException::class)
|
||||
private fun ensureWorkingDir(workingDir: String) {
|
||||
try {
|
||||
val workingDirFile = File(workingDir)
|
||||
|
||||
if (!workingDirFile.exists() && !workingDirFile.mkdirs()) {
|
||||
throw LiquidSdkException.Generic("Mandatory field workingDir must contain a writable directory")
|
||||
}
|
||||
} catch (e: SecurityException) {
|
||||
throw LiquidSdkException.Generic("Mandatory field workingDir must contain a writable directory")
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun addListener(eventName: String) {}
|
||||
|
||||
@ReactMethod
|
||||
fun removeListeners(count: Int) {}
|
||||
|
||||
@ReactMethod
|
||||
fun defaultConfig(
|
||||
network: String,
|
||||
promise: Promise,
|
||||
) {
|
||||
executor.execute {
|
||||
try {
|
||||
val networkTmp = asNetwork(network)
|
||||
val res = defaultConfig(networkTmp)
|
||||
val workingDir = File(reactApplicationContext.filesDir.toString() + "/breezLiquidSdk")
|
||||
|
||||
res.workingDir = workingDir.absolutePath
|
||||
promise.resolve(readableMapOf(res))
|
||||
} catch (e: Exception) {
|
||||
promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun parseInvoice(
|
||||
invoice: String,
|
||||
@@ -86,9 +119,9 @@ class BreezLiquidSDKModule(reactContext: ReactApplicationContext) : ReactContext
|
||||
asConnectRequest(
|
||||
req,
|
||||
) ?: run { throw LiquidSdkException.Generic(errMissingMandatoryField("req", "ConnectRequest")) }
|
||||
connectRequest.dataDir = connectRequest.dataDir?.takeUnless {
|
||||
it.isEmpty()
|
||||
} ?: run { reactApplicationContext.filesDir.toString() + "/breezLiquidSdk" }
|
||||
|
||||
ensureWorkingDir(connectRequest.config.workingDir)
|
||||
|
||||
bindingLiquidSdk = connect(connectRequest)
|
||||
promise.resolve(readableMapOf("status" to "ok"))
|
||||
} catch (e: Exception) {
|
||||
|
||||
Reference in New Issue
Block a user