package com.reactnativesealdaccelerator import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.Promise import react_native_seald_accelerator.React_native_seald_accelerator import android.util.Log import android.net.Uri import android.os.Environment import java.util.Base64 import java.io.File import java.io.OutputStream import java.util.Random import org.json.JSONObject private const val TAG = "SealdAcceleratorModule" class SealdAcceleratorModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { var reactContext: ReactApplicationContext init { this.reactContext = reactContext } override fun getName(): String { return "SealdAccelerator" } @ReactMethod fun encryptFileAsString(fileContent: String, filename: String, messageId: String, serializedSymKey: String, promise: Promise) { try { var foo = React_native_seald_accelerator.encryptFileFromSerializedSymKey( fileContent.toByteArray(), filename, messageId, Base64.getDecoder().decode(serializedSymKey) ) promise.resolve( Base64.getEncoder().encodeToString(foo) ) } catch (err: Throwable) { promise.reject(err) } } @ReactMethod fun decryptFileAsString(fileContent: String, serializedSymKey: String, promise: Promise) { try { var clearFile = React_native_seald_accelerator.decryptFileFromSerializedSymKey( Base64.getDecoder().decode(fileContent), Base64.getDecoder().decode(serializedSymKey) ) val obj = JSONObject() obj.put("filename", clearFile.getFilename()) obj.put("messageId", clearFile.getMessageId()) obj.put("fileContent", String(clearFile.getFileContent())) promise.resolve( obj.toString() ) } catch (err: Throwable) { promise.reject(err) } } private fun getFileUri(filepath: String, isDirectoryAllowed: Boolean): Uri { var uri: Uri = Uri.parse(filepath) if (uri.getScheme() == null) { // No prefix, assuming that provided path is absolute path to file val file: java.io.File = java.io.File(filepath) if (!isDirectoryAllowed && file.isDirectory()) { throw Exception( "EISDIR: illegal operation on a directory, read '$filepath'" ) } uri = Uri.parse("file://$filepath") } return uri } @ReactMethod fun encryptURI(filepath: String, filename: String, messageId: String, serializedSymKey: String, promise: Promise) { try { var uri: Uri = getFileUri(filepath, false) val outputPath = React_native_seald_accelerator.encryptFileFromUriAndSerializedSymKey(uri.getPath(), filename, messageId, Base64.getDecoder().decode(serializedSymKey)) promise.resolve(outputPath) } catch (err: Throwable) { promise.reject(err) } } @ReactMethod fun decryptURI(filepath: String, serializedSymKey: String, promise: Promise) { try { var uri: Uri = Uri.parse(filepath) var clearFile = React_native_seald_accelerator.decryptFileFromUriAndSerializedSymKey( uri.getPath(), Base64.getDecoder().decode(serializedSymKey) ) val obj = JSONObject() obj.put("filename", clearFile.getFilename()) obj.put("messageId", clearFile.getMessageId()) obj.put("path", clearFile.getPath()) promise.resolve( obj.toString() ) } catch (err: Throwable) { promise.reject(err) } } }