diff --git a/native/cpp/CommonCpp/CryptoTools/Tools.h b/native/cpp/CommonCpp/CryptoTools/Tools.h --- a/native/cpp/CommonCpp/CryptoTools/Tools.h +++ b/native/cpp/CommonCpp/CryptoTools/Tools.h @@ -32,6 +32,7 @@ public: static std::string generateRandomString(size_t size); static std::string generateRandomHexString(size_t size); + static std::string generateRandomAlphanumericString(size_t size); }; } // namespace crypto diff --git a/native/cpp/CommonCpp/CryptoTools/Tools.cpp b/native/cpp/CommonCpp/CryptoTools/Tools.cpp --- a/native/cpp/CommonCpp/CryptoTools/Tools.cpp +++ b/native/cpp/CommonCpp/CryptoTools/Tools.cpp @@ -29,5 +29,11 @@ return Tools::generateRandomString(size, hexSigns); } +std::string Tools::generateRandomAlphanumericString(size_t size) { + static std::string alphanumericSigns = + "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + return Tools::generateRandomString(size, alphanumericSigns); +} + } // namespace crypto } // namespace comm 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 @@ -58,6 +58,8 @@ jsi::Value setNotifyToken(jsi::Runtime &rt, const jsi::String &token) override; jsi::Value clearNotifyToken(jsi::Runtime &rt) override; + jsi::String + generateDeviceID(jsi::Runtime &rt, const jsi::String &type) override; public: CommCoreModule(std::shared_ptr jsInvoker); 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 @@ -14,6 +14,8 @@ #include #include +#include "../CryptoTools/Tools.h" + namespace comm { using namespace facebook::react; @@ -955,4 +957,26 @@ }); }; +const int deviceIDLength = 64; + +jsi::String +CommCoreModule::generateDeviceID(jsi::Runtime &rt, const jsi::String &jsType) { + std::string id = ""; + std::string type = jsType.utf8(rt); + if (type == "KEYSERVER") { + id = "ks:"; + } else if (type == "WEB") { + id = "web:"; + } else if (type == "MOBILE") { + id = "mobile:"; + } else { + throw jsi::JSError( + rt, + "generateDeviceID: incorrect function argument. Must be one of: " + "KEYSERVER, WEB, MOBILE."); + } + id += crypto::Tools::generateRandomAlphanumericString(deviceIDLength); + return jsi::String::createFromUtf8(rt, id); +} + } // namespace comm diff --git a/native/cpp/CommonCpp/_generated/NativeModules.h b/native/cpp/CommonCpp/_generated/NativeModules.h --- a/native/cpp/CommonCpp/_generated/NativeModules.h +++ b/native/cpp/CommonCpp/_generated/NativeModules.h @@ -38,6 +38,7 @@ virtual double getCodeVersion(jsi::Runtime &rt) = 0; virtual jsi::Value setNotifyToken(jsi::Runtime &rt, const jsi::String &token) = 0; virtual jsi::Value clearNotifyToken(jsi::Runtime &rt) = 0; +virtual jsi::String generateDeviceID(jsi::Runtime &rt, const jsi::String &jsType) = 0; }; diff --git a/native/cpp/CommonCpp/_generated/NativeModules.cpp b/native/cpp/CommonCpp/_generated/NativeModules.cpp --- a/native/cpp/CommonCpp/_generated/NativeModules.cpp +++ b/native/cpp/CommonCpp/_generated/NativeModules.cpp @@ -72,6 +72,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearNotifyToken(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->clearNotifyToken(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateDeviceID(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->generateDeviceID(rt, args[0].getString(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -95,6 +98,7 @@ methodMap_["getCodeVersion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getCodeVersion}; methodMap_["setNotifyToken"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setNotifyToken}; methodMap_["clearNotifyToken"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearNotifyToken}; + methodMap_["generateDeviceID"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateDeviceID}; } diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -48,6 +48,7 @@ +getCodeVersion: () => number; +setNotifyToken: (token: string) => Promise; +clearNotifyToken: () => Promise; + +generateDeviceID: (jsType: string) => string; } export default (TurboModuleRegistry.getEnforcing(