Skip to content

Commit

Permalink
chore_: use status.go v2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
qfrank committed Nov 11, 2024
1 parent a7d330e commit 73db641
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,25 @@ class AccountManager(private val reactContext: ReactApplicationContext) : ReactC
val absRootDirPath = utils.getNoBackupDirectory()
val newKeystoreDir = utils.pathCombine(absRootDirPath, "keystore")

val jsonParams = JSONObject()
jsonParams.put("keyStoreDir", newKeystoreDir)
jsonParams.put("address", address)
jsonParams.put("password", password)

utils.executeRunnableStatusGoMethod(
{ Statusgo.verifyAccountPassword(newKeystoreDir, address, password) },
{ Statusgo.verifyAccountPasswordV2(jsonParams.toString()) },
callback
)
}

@ReactMethod
fun verifyDatabasePassword(keyUID: String, password: String, callback: Callback) {
val jsonParams = JSONObject()
jsonParams.put("keyUID", keyUID)
jsonParams.put("password", password)

utils.executeRunnableStatusGoMethod(
{ Statusgo.verifyDatabasePassword(keyUID, password) },
{ Statusgo.verifyDatabasePasswordV2(jsonParams.toString()) },
callback
)
}
Expand Down Expand Up @@ -311,7 +320,12 @@ class AccountManager(private val reactContext: ReactApplicationContext) : ReactC
@ReactMethod
fun deleteMultiaccount(keyUID: String, callback: Callback) {
val keyStoreDir = utils.getKeyStorePath(keyUID)
utils.executeRunnableStatusGoMethod({ Statusgo.deleteMultiaccount(keyUID, keyStoreDir) }, callback)
val params = JSONObject().apply {
put("keyUID", keyUID)
put("keyStoreDir", keyStoreDir)
}
val jsonString = params.toString()
utils.executeRunnableStatusGoMethod({ Statusgo.deleteMultiaccountV2(jsonString) }, callback)
}

@ReactMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import statusgo.Statusgo
import java.io.File
import org.json.JSONObject
import org.json.JSONException

class DatabaseManager(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

Expand All @@ -23,16 +25,34 @@ class DatabaseManager(private val reactContext: ReactApplicationContext) : React

@ReactMethod
fun exportUnencryptedDatabase(accountData: String, password: String, callback: Callback) {
Log.d(TAG, "login")
Log.d(TAG, "exportUnencryptedDatabase")

val newFile = getExportDBFile()

utils.migrateKeyStoreDir(accountData, password)
val result = Statusgo.exportUnencryptedDatabase(accountData, password, newFile.absolutePath)
if (result.startsWith("{\"error\":\"\"")) {
Log.d(TAG, "Login result: $result")
} else {
Log.e(TAG, "Login failed: $result")

try {
val accountJson = JSONObject(accountData)

val params = JSONObject().apply {
put("account", accountJson)
put("password", password)
put("databasePath", newFile.absolutePath)
}

val jsonParams = params.toString()

val result = Statusgo.exportUnencryptedDatabaseV2(jsonParams)
if (result.startsWith("{\"error\":\"\"")) {
Log.d(TAG, "Export result: $result")
} else {
Log.e(TAG, "Export failed: $result")
}

callback.invoke(newFile.absolutePath)

} catch (e: JSONException) {
Log.e(TAG, "Error parsing account data: ${e.message}")
}
}

Expand All @@ -43,11 +63,26 @@ class DatabaseManager(private val reactContext: ReactApplicationContext) : React
val newFile = getExportDBFile()

utils.migrateKeyStoreDir(accountData, password)
val result = Statusgo.importUnencryptedDatabase(accountData, password, newFile.absolutePath)
if (result.startsWith("{\"error\":\"\"")) {
Log.d(TAG, "import result: $result")
} else {
Log.e(TAG, "import failed: $result")

try {
val accountJson = JSONObject(accountData)

val params = JSONObject().apply {
put("account", accountJson)
put("password", password)
put("databasePath", newFile.absolutePath)
}

val jsonParams = params.toString()

val result = Statusgo.importUnencryptedDatabaseV2(jsonParams)
if (result.startsWith("{\"error\":\"\"")) {
Log.d(TAG, "Import result: $result")
} else {
Log.e(TAG, "Import failed: $result")
}
} catch (e: JSONException) {
Log.e(TAG, "Error parsing account data: ${e.message}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.Log;
import statusgo.Statusgo;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.function.Function;
import android.app.Activity;
import android.view.WindowManager;
Expand Down Expand Up @@ -44,27 +45,58 @@ private void initKeystore(final String keyUID, final Callback callback) throws J

@ReactMethod
public void reEncryptDbAndKeystore(final String keyUID, final String password, final String newPassword, final Callback callback) throws JSONException {
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.changeDatabasePassword(keyUID, password, newPassword), callback);
JSONObject params = new JSONObject();
params.put("keyUID", keyUID);
params.put("oldPassword", password);
params.put("newPassword", newPassword);
String jsonParams = params.toString();
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.changeDatabasePasswordV2(jsonParams), callback);
}

@ReactMethod
public void convertToKeycardAccount(final String keyUID, final String accountData, final String options, final String keycardUID, final String password,
final String newPassword, final Callback callback) throws JSONException {
final String keyStoreDir = this.utils.getKeyStorePath(keyUID);
JSONObject params = new JSONObject();
params.put("keyUID", keyUID);
params.put("account", new JSONObject(accountData));
params.put("settings", new JSONObject(options));
params.put("keycardUID", keycardUID);
params.put("oldPassword", password);
params.put("newPassword", newPassword);
final String jsonParams = params.toString();
this.utils.executeRunnableStatusGoMethod(() -> {
Statusgo.initKeystore(keyStoreDir);
return Statusgo.convertToKeycardAccount(accountData, options, keycardUID, password, newPassword);
return Statusgo.convertToKeycardAccountV2(jsonParams);
}, callback);
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String encodeTransfer(final String to, final String value) {
return Statusgo.encodeTransfer(to, value);
try {
JSONObject params = new JSONObject();
params.put("to", to);
params.put("value", value);
String jsonParams = params.toString();
return Statusgo.encodeTransferV2(jsonParams);
} catch (JSONException e) {
Log.e(TAG, "Error creating JSON for encodeTransfer: " + e.getMessage());
return null;
}
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String encodeFunctionCall(final String method, final String paramsJSON) {
return Statusgo.encodeFunctionCall(method, paramsJSON);
try {
JSONObject params = new JSONObject();
params.put("method", method);
params.put("paramsJSON", new JSONObject(paramsJSON));
String jsonString = params.toString();
return Statusgo.encodeFunctionCallV2(jsonString);
} catch (JSONException e) {
Log.e(TAG, "Error creating JSON for encodeFunctionCall: " + e.getMessage());
return null;
}
}

@ReactMethod(isBlockingSynchronousMethod = true)
Expand Down Expand Up @@ -140,7 +172,11 @@ public void hashMessage(final String message, final Callback callback) throws JS

@ReactMethod
public void multiformatDeserializePublicKey(final String multiCodecKey, final String base58btc, final Callback callback) throws JSONException {
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.multiformatDeserializePublicKey(multiCodecKey,base58btc), callback);
JSONObject params = new JSONObject();
params.put("key", multiCodecKey);
params.put("outBase", base58btc);
String jsonParams = params.toString();
this.utils.executeRunnableStatusGoMethod(() -> Statusgo.multiformatDeserializePublicKeyV2(jsonParams), callback);
}

@ReactMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ class NetworkManager(private val reactContext: ReactApplicationContext) : ReactC

@ReactMethod
fun inputConnectionStringForBootstrapping(connectionString: String, configJSON: String, callback: Callback) {
val jsonConfig = JSONObject(configJSON)
val receiverClientConfig = JSONObject(configJSON)
val params = JSONObject().apply {
put("connectionString", connectionString)
put("receiverClientConfig", receiverClientConfig)
}
val jsonString = params.toString()
utils.executeRunnableStatusGoMethod(
{ Statusgo.inputConnectionStringForBootstrapping(connectionString, jsonConfig.toString()) },
{ Statusgo.inputConnectionStringForBootstrappingV2(jsonString) },
callback
)
}
Expand All @@ -52,7 +57,12 @@ class NetworkManager(private val reactContext: ReactApplicationContext) : ReactC

@ReactMethod
fun sendTransaction(txArgsJSON: String, password: String, callback: Callback) {
utils.executeRunnableStatusGoMethod({ Statusgo.sendTransaction(txArgsJSON, password) }, callback)
val jsonParams = JSONObject().apply {
put("txArgs", JSONObject(txArgsJSON))
put("password", password)
}
val jsonString = jsonParams.toString()
utils.executeRunnableStatusGoMethod({ Statusgo.sendTransactionV2(jsonString) }, callback)
}

@ReactMethod
Expand Down Expand Up @@ -85,13 +95,18 @@ class NetworkManager(private val reactContext: ReactApplicationContext) : ReactC

@ReactMethod
fun inputConnectionStringForImportingKeypairsKeystores(connectionString: String, configJSON: String, callback: Callback) {
val jsonConfig = JSONObject(configJSON)
val receiverConfig = jsonConfig.getJSONObject("receiverConfig")
val keystoreFilesReceiverClientConfig = JSONObject(configJSON)
val receiverConfig = keystoreFilesReceiverClientConfig.getJSONObject("receiverConfig")
val keyStorePath = utils.pathCombine(utils.getNoBackupDirectory(), "/keystore")
receiverConfig.put("keystorePath", keyStorePath)

val params = JSONObject().apply {
put("connectionString", connectionString)
put("keystoreFilesReceiverClientConfig", keystoreFilesReceiverClientConfig)
}

utils.executeRunnableStatusGoMethod(
{ Statusgo.inputConnectionStringForImportingKeypairsKeystores(connectionString, jsonConfig.toString()) },
{ Statusgo.inputConnectionStringForImportingKeypairsKeystoresV2(params.toString()) },
callback
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import statusgo.SignalHandler
import statusgo.Statusgo
import org.json.JSONException
import android.view.WindowManager
import org.json.JSONObject

class StatusModule(private val reactContext: ReactApplicationContext, private val rootedDevice: Boolean) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener, SignalHandler {

Expand Down Expand Up @@ -56,13 +57,20 @@ class StatusModule(private val reactContext: ReactApplicationContext, private va
@ReactMethod
fun connectionChange(type: String, isExpensive: Boolean) {
Log.d(TAG, "ConnectionChange: $type, is expensive $isExpensive")
Statusgo.connectionChange(type, if (isExpensive) 1 else 0)
val params = JSONObject().apply {
put("type", type)
put("expensive", isExpensive)
}
Statusgo.connectionChangeV2(params.toString())
}

@ReactMethod
fun appStateChange(type: String) {
Log.d(TAG, "AppStateChange: $type")
Statusgo.appStateChange(type)
fun appStateChange(state: String) {
Log.d(TAG, "AppStateChange: $state")
val params = JSONObject().apply {
put("state", state)
}
Statusgo.appStateChangeV2(params.toString())
}

@ReactMethod
Expand All @@ -89,7 +97,13 @@ class StatusModule(private val reactContext: ReactApplicationContext, private va
@ReactMethod
fun deleteImportedKey(keyUID: String, address: String, password: String, callback: Callback) {
val keyStoreDir = utils.getKeyStorePath(keyUID)
utils.executeRunnableStatusGoMethod({ Statusgo.deleteImportedKey(address, password, keyStoreDir) }, callback)
val params = JSONObject().apply {
put("address", address)
put("password", password)
put("keyStoreDir", keyStoreDir)
}
val jsonString = params.toString()
utils.executeRunnableStatusGoMethod({ Statusgo.deleteImportedKeyV2(jsonString) }, callback)
}

@ReactMethod(isBlockingSynchronousMethod = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas
val keydirFile = File(keydir)
if (!keydirFile.exists() || keydirFile.list().isEmpty()) {
Log.d(TAG, "migrateKeyStoreDir")
Statusgo.migrateKeyStoreDir(accountData, password, commonKeydir, keydir)
val jsonParams = JSONObject()
jsonParams.put("account", JSONObject(accountData)) // Remove 'new' keyword
jsonParams.put("password", password)
jsonParams.put("oldDir", commonKeydir)
jsonParams.put("newDir", keydir)
Statusgo.migrateKeyStoreDirV2(jsonParams.toString())
Statusgo.initKeystore(keydir)
}
} catch (e: JSONException) {
Expand Down Expand Up @@ -115,7 +120,9 @@ class Utils(private val reactContext: ReactApplicationContext) : ReactContextBas

@ReactMethod
fun validateMnemonic(seed: String, callback: Callback) {
executeRunnableStatusGoMethod({ Statusgo.validateMnemonic(seed) }, callback)
val jsonParams = JSONObject()
jsonParams.put("mnemonic", seed)
executeRunnableStatusGoMethod({ Statusgo.validateMnemonicV2(jsonParams.toString()) }, callback)
}

fun is24Hour(): Boolean {
Expand Down
38 changes: 32 additions & 6 deletions modules/react-native-status/ios/RCTStatus/AccountManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,18 @@ -(NSString *) prepareDirAndUpdateConfig:(NSString *)config
NSLog(@"DeleteMultiaccount() method called");
#endif
NSURL *multiaccountKeystoreDir = [Utils getKeyStoreDirForKeyUID:keyUID];
NSString *result = StatusgoDeleteMultiaccount(keyUID, multiaccountKeystoreDir.path);
NSDictionary *params = @{
@"keyUID": keyUID,
@"keyStoreDir": multiaccountKeystoreDir.path
};
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
if (error) {
NSLog(@"Error creating JSON: %@", error);
return;
}
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *result = StatusgoDeleteMultiaccountV2(jsonString);
callback(@[result]);
}

Expand Down Expand Up @@ -173,25 +184,40 @@ -(NSString *) prepareDirAndUpdateConfig:(NSString *)config
password:(NSString *)password
callback:(RCTResponseSenderBlock)callback) {
#if DEBUG
NSLog(@"VerifyAccountPassword() method called");
NSLog(@"VerifyAccountPasswordV2() method called");
#endif
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *rootUrl =[[fileManager
URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]
lastObject];
NSURL *absKeystoreUrl = [rootUrl URLByAppendingPathComponent:@"keystore"];

NSString *result = StatusgoVerifyAccountPassword(absKeystoreUrl.path, address, password);

NSDictionary *params = @{
@"keyStoreDir": absKeystoreUrl.path,
@"address": address,
@"password": password
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSString *result = StatusgoVerifyAccountPasswordV2(jsonString);
callback(@[result]);
}

RCT_EXPORT_METHOD(verifyDatabasePassword:(NSString *)keyUID
password:(NSString *)password
callback:(RCTResponseSenderBlock)callback) {
#if DEBUG
NSLog(@"VerifyDatabasePassword() method called");
NSLog(@"VerifyDatabasePasswordV2() method called");
#endif
NSString *result = StatusgoVerifyDatabasePassword(keyUID, password);
NSDictionary *params = @{
@"keyUID": keyUID,
@"password": password
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSString *result = StatusgoVerifyDatabasePasswordV2(jsonString);
callback(@[result]);
}

Expand Down
Loading

0 comments on commit 73db641

Please sign in to comment.