diff --git a/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKModule.kt b/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKModule.kt index 13308dc52..9b6908637 100644 --- a/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKModule.kt +++ b/libs/sdk-react-native/android/src/main/java/com/breezsdk/BreezSDKModule.kt @@ -100,10 +100,6 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa val res = defaultConfig(envTypeTmp, apiKey, nodeConfigTmp) val workingDir = File(reactApplicationContext.filesDir.toString() + "/breezSdk") - if (!workingDir.exists()) { - workingDir.mkdirs() - } - res.workingDir = workingDir.absolutePath promise.resolve(readableMapOf(res)) } catch (e: Exception) { @@ -157,7 +153,18 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa try { val configTmp = asConfig(config) ?: run { throw SdkException.Generic("Missing mandatory field config of type Config") } + + if (!configTmp.workingDir.contains(reactApplicationContext.filesDir.toString())) { + promise.reject("Generic", "Mandatory field workingDir must contain application directory") + return + } + val emitter = reactApplicationContext.getJSModule(RCTDeviceEventEmitter::class.java) + val workingDir = File(configTmp.workingDir) + + if (!workingDir.exists()) { + workingDir.mkdirs() + } breezServices = connect(configTmp, asUByteList(seed), BreezSDKListener(emitter)) promise.resolve(readableMapOf("status" to "ok")) @@ -172,6 +179,7 @@ class BreezSDKModule(reactContext: ReactApplicationContext) : ReactContextBaseJa executor.execute { try { getBreezServices().disconnect() + breezServices = null promise.resolve(readableMapOf("status" to "ok")) } catch (e: Exception) { promise.reject(e.javaClass.simpleName.replace("Exception", "Error"), e.message, e) diff --git a/libs/sdk-react-native/ios/RNBreezSDK.swift b/libs/sdk-react-native/ios/RNBreezSDK.swift index a5f973873..fa9b4daa2 100644 --- a/libs/sdk-react-native/ios/RNBreezSDK.swift +++ b/libs/sdk-react-native/ios/RNBreezSDK.swift @@ -7,8 +7,11 @@ class RNBreezSDK: RCTEventEmitter { private var breezServices: BlockingBreezServices! + static var applicationDirectory: URL { + return FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! + } + static var breezSdkDirectory: URL { - let applicationDirectory = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! let breezSdkDirectory = applicationDirectory.appendingPathComponent("breezSdk", isDirectory: true) if !FileManager.default.fileExists(atPath: breezSdkDirectory.path) { @@ -113,6 +116,16 @@ class RNBreezSDK: RCTEventEmitter { do { let configTmp = try BreezSDKMapper.asConfig(config: config) + + if !configTmp.workingDir.contains(RNBreezSDK.applicationDirectory.path) { + reject("Generic", "Mandatory field workingDir must contain application directory", nil) + return + } + + if !FileManager.default.fileExists(atPath: configTmp.workingDir) { + try! FileManager.default.createDirectory(atPath: configTmp.workingDir, withIntermediateDirectories: true) + } + breezServices = try BreezSDK.connect(config: configTmp, seed: seed, listener: BreezSDKListener(emitter: self)) resolve(["status": "ok"]) } catch let err { @@ -124,6 +137,7 @@ class RNBreezSDK: RCTEventEmitter { func disconnect(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) { do { try getBreezServices().disconnect() + breezServices = nil resolve(["status": "ok"]) } catch let err { rejectErr(err: err, reject: reject)