diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -172,6 +172,11 @@ jsi::String messageID) override; virtual jsi::Value signMessage(jsi::Runtime &rt, jsi::String message) override; + virtual jsi::Value signMessageUsingAccount( + jsi::Runtime &rt, + jsi::String message, + jsi::String pickledAccount, + jsi::String pickleKey) override; virtual jsi::Value verifySignature( jsi::Runtime &rt, jsi::String publicKey, diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -2077,6 +2077,43 @@ }); } +jsi::Value CommCoreModule::signMessageUsingAccount( + jsi::Runtime &rt, + jsi::String message, + jsi::String pickledAccount, + jsi::String pickleKey) { + std::string messageStr = message.utf8(rt); + std::string pickledAccountStr = pickledAccount.utf8(rt); + std::string pickleKeyStr = pickleKey.utf8(rt); + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::string signature; + try { + crypto::Persist persist; + persist.account = crypto::OlmBuffer( + pickledAccountStr.begin(), pickledAccountStr.end()); + auto cryptoModule = new crypto::CryptoModule(pickleKeyStr, persist); + signature = cryptoModule->signMessage(messageStr); + } catch (const std::exception &e) { + error = "signing message failed with: " + std::string(e.what()); + } + + this->jsInvoker_->invokeAsync([=, &innerRt]() { + if (error.size()) { + promise->reject(error); + return; + } + + auto jsiSignature{jsi::String::createFromUtf8(innerRt, signature)}; + promise->resolve(std::move(jsiSignature)); + }); + }; + this->cryptoThread->scheduleTask(job); + }); +} + jsi::Value CommCoreModule::verifySignature( jsi::Runtime &rt, jsi::String publicKey, diff --git a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp --- a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp +++ b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp @@ -120,6 +120,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_signMessage(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->signMessage(rt, args[0].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_signMessageUsingAccount(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->signMessageUsingAccount(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_verifySignature(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->verifySignature(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); } @@ -282,6 +285,7 @@ methodMap_["decrypt"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_decrypt}; methodMap_["decryptAndPersist"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_decryptAndPersist}; methodMap_["signMessage"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_signMessage}; + methodMap_["signMessageUsingAccount"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_signMessageUsingAccount}; methodMap_["verifySignature"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_verifySignature}; methodMap_["getCodeVersion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getCodeVersion}; methodMap_["terminate"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_terminate}; diff --git a/native/cpp/CommonCpp/_generated/commJSI.h b/native/cpp/CommonCpp/_generated/commJSI.h --- a/native/cpp/CommonCpp/_generated/commJSI.h +++ b/native/cpp/CommonCpp/_generated/commJSI.h @@ -55,6 +55,7 @@ virtual jsi::Value decrypt(jsi::Runtime &rt, jsi::Object encryptedData, jsi::String deviceID) = 0; virtual jsi::Value decryptAndPersist(jsi::Runtime &rt, jsi::Object encryptedData, jsi::String deviceID, jsi::String userID, jsi::String messageID) = 0; virtual jsi::Value signMessage(jsi::Runtime &rt, jsi::String message) = 0; + virtual jsi::Value signMessageUsingAccount(jsi::Runtime &rt, jsi::String message, jsi::String pickledAccount, jsi::String pickleKey) = 0; virtual jsi::Value verifySignature(jsi::Runtime &rt, jsi::String publicKey, jsi::String message, jsi::String signature) = 0; virtual double getCodeVersion(jsi::Runtime &rt) = 0; virtual void terminate(jsi::Runtime &rt) = 0; @@ -396,6 +397,14 @@ return bridging::callFromJs( rt, &T::signMessage, jsInvoker_, instance_, std::move(message)); } + jsi::Value signMessageUsingAccount(jsi::Runtime &rt, jsi::String message, jsi::String pickledAccount, jsi::String pickleKey) override { + static_assert( + bridging::getParameterCount(&T::signMessageUsingAccount) == 4, + "Expected signMessageUsingAccount(...) to have 4 parameters"); + + return bridging::callFromJs( + rt, &T::signMessageUsingAccount, jsInvoker_, instance_, std::move(message), std::move(pickledAccount), std::move(pickleKey)); + } jsi::Value verifySignature(jsi::Runtime &rt, jsi::String publicKey, jsi::String message, jsi::String signature) override { static_assert( bridging::getParameterCount(&T::verifySignature) == 4, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -130,6 +130,11 @@ messageID: string, ) => Promise; +signMessage: (message: string) => Promise; + +signMessageUsingAccount: ( + message: string, + pickledAccount: string, + pickleKey: string, + ) => Promise; +verifySignature: ( publicKey: string, message: string,