2024-01-15 13:27:35 +00:00
|
|
|
#import "EncryptionUtils.h"
|
|
|
|
#import "React/RCTBridge.h"
|
|
|
|
#import "React/RCTEventDispatcher.h"
|
|
|
|
#import "Statusgo.h"
|
|
|
|
#import "Utils.h"
|
2024-11-19 10:50:01 +00:00
|
|
|
#import "StatusBackendClient.h"
|
2024-01-15 13:27:35 +00:00
|
|
|
|
|
|
|
@implementation EncryptionUtils
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
|
|
|
#pragma mark - InitKeystore method
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(initKeystore:(NSString *)keyUID
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"initKeystore() method called");
|
|
|
|
#endif
|
2024-11-19 10:50:01 +00:00
|
|
|
NSURL *multiaccountKeystoreDir = [Utils getKeyStoreDirForKeyUID:keyUID];
|
|
|
|
|
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"InitKeystore"
|
|
|
|
body:multiaccountKeystoreDir.path
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoInitKeystore(multiaccountKeystoreDir.path);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(reEncryptDbAndKeystore:(NSString *)keyUID
|
|
|
|
currentPassword:(NSString *)currentPassword
|
|
|
|
newPassword:(NSString *)newPassword
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"reEncryptDbAndKeystore() method called");
|
|
|
|
#endif
|
2024-11-19 10:50:01 +00:00
|
|
|
// Construct params into JSON string
|
|
|
|
NSDictionary *params = @{
|
|
|
|
@"keyUID": keyUID,
|
|
|
|
@"oldPassword": currentPassword,
|
|
|
|
@"newPassword": newPassword
|
|
|
|
};
|
|
|
|
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];
|
|
|
|
|
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"ChangeDatabasePasswordV2"
|
|
|
|
body:jsonString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoChangeDatabasePasswordV2(jsonString);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(convertToKeycardAccount:(NSString *)keyUID
|
|
|
|
accountData:(NSString *)accountData
|
|
|
|
settings:(NSString *)settings
|
|
|
|
keycardUID:(NSString *)keycardUID
|
|
|
|
currentPassword:(NSString *)currentPassword
|
|
|
|
newPassword:(NSString *)newPassword
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"convertToKeycardAccount() method called");
|
|
|
|
#endif
|
|
|
|
NSURL *multiaccountKeystoreDir = [Utils getKeyStoreDirForKeyUID:keyUID];
|
2024-11-19 10:50:01 +00:00
|
|
|
|
|
|
|
// First initialize keystore
|
|
|
|
[StatusBackendClient executeStatusGoRequest:@"InitKeystore"
|
|
|
|
body:multiaccountKeystoreDir.path
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoInitKeystore(multiaccountKeystoreDir.path);
|
|
|
|
}];
|
|
|
|
|
|
|
|
// Prepare parameters for conversion
|
|
|
|
NSDictionary *params = @{
|
|
|
|
@"keyUID": keyUID,
|
|
|
|
@"account": [NSJSONSerialization JSONObjectWithData:[accountData dataUsingEncoding:NSUTF8StringEncoding]
|
|
|
|
options:0
|
|
|
|
error:nil],
|
|
|
|
@"settings": [NSJSONSerialization JSONObjectWithData:[settings dataUsingEncoding:NSUTF8StringEncoding]
|
|
|
|
options:0
|
|
|
|
error:nil],
|
|
|
|
@"keycardUID": keycardUID,
|
|
|
|
@"oldPassword": currentPassword,
|
|
|
|
@"newPassword": newPassword
|
|
|
|
};
|
|
|
|
|
|
|
|
NSError *error;
|
|
|
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Error creating JSON: %@", [error localizedDescription]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"ConvertToKeycardAccountV2"
|
|
|
|
body:jsonString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoConvertToKeycardAccountV2(jsonString);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(encodeTransfer:(NSString *)to
|
2024-11-19 10:50:01 +00:00
|
|
|
value:(NSString *)value) {
|
|
|
|
NSDictionary *params = @{
|
|
|
|
@"to": to,
|
|
|
|
@"value": value
|
|
|
|
};
|
|
|
|
NSError *error;
|
|
|
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Error creating JSON: %@", [error localizedDescription]);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"EncodeTransferV2"
|
|
|
|
body:jsonString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoEncodeTransferV2(jsonString);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(encodeFunctionCall:(NSString *)method
|
2024-11-19 10:50:01 +00:00
|
|
|
paramsJSON:(NSString *)paramsJSON) {
|
|
|
|
NSDictionary *params = @{
|
|
|
|
@"method": method,
|
|
|
|
@"paramsJSON": paramsJSON
|
|
|
|
};
|
|
|
|
NSError *error;
|
|
|
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
|
|
|
|
if (error) {
|
|
|
|
NSLog(@"Error creating JSON: %@", [error localizedDescription]);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"EncodeFunctionCallV2"
|
|
|
|
body:jsonString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoEncodeFunctionCallV2(jsonString);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(decodeParameters:(NSString *)decodeParamJSON) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"DecodeParameters"
|
|
|
|
body:decodeParamJSON
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoDecodeParameters(decodeParamJSON);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(hexToNumber:(NSString *)hex) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"HexToNumber"
|
|
|
|
body:hex
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoHexToNumber(hex);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(numberToHex:(NSString *)numString) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"NumberToHex"
|
|
|
|
body:numString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoNumberToHex(numString);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(sha3:(NSString *)str) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"Sha3"
|
|
|
|
body:str
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoSha3(str);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(utf8ToHex:(NSString *)str) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"Utf8ToHex"
|
|
|
|
body:str
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoUtf8ToHex(str);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(hexToUtf8:(NSString *)str) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"HexToUtf8"
|
|
|
|
body:str
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoHexToUtf8(str);
|
|
|
|
}];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-12 05:42:49 +00:00
|
|
|
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(serializeLegacyKey:(NSString *)str) {
|
2024-11-19 10:50:01 +00:00
|
|
|
return [StatusBackendClient executeStatusGoRequestWithResult:@"SerializeLegacyKey"
|
|
|
|
body:str
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoSerializeLegacyKey(str);
|
|
|
|
}];
|
2024-07-12 05:42:49 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 13:27:35 +00:00
|
|
|
RCT_EXPORT_METHOD(setBlankPreviewFlag:(BOOL *)newValue)
|
|
|
|
{
|
|
|
|
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
|
|
|
[userDefaults setBool:newValue forKey:@"BLANK_PREVIEW"];
|
|
|
|
|
|
|
|
[userDefaults synchronize];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(hashTransaction:(NSString *)txArgsJSON
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
2024-11-19 10:50:01 +00:00
|
|
|
NSLog(@"hashTransaction() method called");
|
2024-01-15 13:27:35 +00:00
|
|
|
#endif
|
2024-11-19 10:50:01 +00:00
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"HashTransaction"
|
|
|
|
body:txArgsJSON
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoHashTransaction(txArgsJSON);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(hashMessage:(NSString *)message
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"hashMessage() method called");
|
|
|
|
#endif
|
2024-11-19 10:50:01 +00:00
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"HashMessage"
|
|
|
|
body:message
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoHashMessage(message);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(localPairingPreflightOutboundCheck:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"LocalPairingPreflightOutboundCheck() method called");
|
|
|
|
#endif
|
|
|
|
NSString *result = StatusgoLocalPairingPreflightOutboundCheck();
|
|
|
|
callback(@[result]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(multiformatDeserializePublicKey:(NSString *)multiCodecKey
|
|
|
|
base58btc:(NSString *)base58btc
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
2024-11-19 10:50:01 +00:00
|
|
|
NSDictionary *params = @{
|
|
|
|
@"key": multiCodecKey,
|
|
|
|
@"outBase": base58btc
|
|
|
|
};
|
|
|
|
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];
|
|
|
|
|
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"MultiformatDeserializePublicKeyV2"
|
|
|
|
body:jsonString
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoMultiformatDeserializePublicKeyV2(jsonString);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(deserializeAndCompressKey:(NSString *)desktopKey
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
2024-11-19 10:50:01 +00:00
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"DeserializeAndCompressKey"
|
|
|
|
body:desktopKey
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoDeserializeAndCompressKey(desktopKey);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(hashTypedData:(NSString *)data
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"hashTypedData() method called");
|
|
|
|
#endif
|
|
|
|
NSString *result = StatusgoHashTypedData(data);
|
|
|
|
callback(@[result]);
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(hashTypedDataV4:(NSString *)data
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"hashTypedDataV4() method called");
|
|
|
|
#endif
|
|
|
|
NSString *result = StatusgoHashTypedDataV4(data);
|
|
|
|
callback(@[result]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - SignMessage
|
|
|
|
|
2024-11-19 10:50:01 +00:00
|
|
|
RCT_EXPORT_METHOD(signMessage:(NSString *)rpcParams
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
[StatusBackendClient executeStatusGoRequestWithCallback:@"SignMessage"
|
|
|
|
body:rpcParams
|
|
|
|
statusgoFunction:^NSString *{
|
|
|
|
return StatusgoSignMessage(rpcParams);
|
|
|
|
}
|
|
|
|
callback:callback];
|
2024-01-15 13:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - SignTypedData
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(signTypedData:(NSString *)data
|
|
|
|
account:(NSString *)account
|
|
|
|
password:(NSString *)password
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"SignTypedData() method called");
|
|
|
|
#endif
|
|
|
|
NSString *result = StatusgoSignTypedData(data, account, password);
|
|
|
|
callback(@[result]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - SignTypedDataV4
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(signTypedDataV4:(NSString *)data
|
|
|
|
account:(NSString *)account
|
|
|
|
password:(NSString *)password
|
|
|
|
callback:(RCTResponseSenderBlock)callback) {
|
|
|
|
#if DEBUG
|
|
|
|
NSLog(@"SignTypedDataV4() method called");
|
|
|
|
#endif
|
|
|
|
NSString *result = StatusgoSignTypedDataV4(data, account, password);
|
|
|
|
callback(@[result]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|