Changeset View
Changeset View
Standalone View
Standalone View
native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
Show First 20 Lines • Show All 1,098 Lines • ▼ Show 20 Lines | return createPromiseAsJSIValue( | ||||
jsiClientPublicKeys.setProperty(innerRt, "signature", signature); | jsiClientPublicKeys.setProperty(innerRt, "signature", signature); | ||||
promise->resolve(std::move(jsiClientPublicKeys)); | promise->resolve(std::move(jsiClientPublicKeys)); | ||||
}); | }); | ||||
}; | }; | ||||
this->cryptoThread->scheduleTask(job); | this->cryptoThread->scheduleTask(job); | ||||
}); | }); | ||||
} | } | ||||
jsi::Value CommCoreModule::getUserOneTimeKeys(jsi::Runtime &rt) { | jsi::Object parseOLMOneTimeKeys(jsi::Runtime &rt, std::string oneTimeKeysBlob) { | ||||
folly::dynamic parsedOneTimeKeys = folly::parseJson(oneTimeKeysBlob); | |||||
auto jsiOneTimeKeysInner = jsi::Object(rt); | |||||
for (auto &kvPair : parsedOneTimeKeys["curve25519"].items()) { | |||||
jsiOneTimeKeysInner.setProperty( | |||||
rt, | |||||
kvPair.first.asString().c_str(), | |||||
jsi::String::createFromUtf8(rt, kvPair.second.asString())); | |||||
} | |||||
auto jsiOneTimeKeys = jsi::Object(rt); | |||||
jsiOneTimeKeys.setProperty(rt, "curve25519", jsiOneTimeKeysInner); | |||||
return jsiOneTimeKeys; | |||||
} | |||||
jsi::Value CommCoreModule::getPrimaryOneTimeKeys( | |||||
jsi::Runtime &rt, | |||||
double oneTimeKeysAmount) { | |||||
return createPromiseAsJSIValue( | return createPromiseAsJSIValue( | ||||
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | ||||
taskType job = [=, &innerRt]() { | taskType job = [=, &innerRt]() { | ||||
std::string error; | std::string error; | ||||
std::string result; | std::string result; | ||||
if (this->cryptoModule == nullptr) { | if (this->cryptoModule == nullptr) { | ||||
error = "user has not been initialized"; | error = "user has not been initialized"; | ||||
} else { | } else { | ||||
result = this->cryptoModule->getOneTimeKeys(); | result = this->cryptoModule->getOneTimeKeys(oneTimeKeysAmount); | ||||
} | } | ||||
this->jsInvoker_->invokeAsync([=, &innerRt]() { | this->jsInvoker_->invokeAsync([=, &innerRt]() { | ||||
if (error.size()) { | if (error.size()) { | ||||
promise->reject(error); | promise->reject(error); | ||||
return; | return; | ||||
} | } | ||||
promise->resolve(jsi::String::createFromUtf8(innerRt, result)); | promise->resolve(parseOLMOneTimeKeys(innerRt, result)); | ||||
}); | |||||
}; | |||||
this->cryptoThread->scheduleTask(job); | |||||
}); | |||||
} | |||||
jsi::Value CommCoreModule::initializeNotificationsSession( | |||||
jsi::Runtime &rt, | |||||
jsi::String identityKeys, | |||||
jsi::String prekey, | |||||
jsi::String prekeySignature, | |||||
jsi::String oneTimeKeys) { | |||||
auto identityKeysCpp{identityKeys.utf8(rt)}; | |||||
auto prekeyCpp{prekey.utf8(rt)}; | |||||
auto prekeySignatureCpp{prekeySignature.utf8(rt)}; | |||||
auto oneTimeKeysCpp{oneTimeKeys.utf8(rt)}; | |||||
return createPromiseAsJSIValue( | |||||
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | |||||
taskType job = [=, &innerRt]() { | |||||
std::string error; | |||||
crypto::EncryptedData result; | |||||
try { | |||||
result = NotificationsCryptoModule::initializeNotificationsSession( | |||||
identityKeysCpp, | |||||
prekeyCpp, | |||||
prekeySignatureCpp, | |||||
oneTimeKeysCpp, | |||||
"Comm"); | |||||
} catch (const std::exception &e) { | |||||
error = e.what(); | |||||
} | |||||
this->jsInvoker_->invokeAsync([=, &innerRt]() { | |||||
if (error.size()) { | |||||
promise->reject(error); | |||||
return; | |||||
} | |||||
promise->resolve(jsi::String::createFromUtf8( | |||||
innerRt, | |||||
std::string{result.message.begin(), result.message.end()})); | |||||
}); | |||||
}; | |||||
this->cryptoThread->scheduleTask(job); | |||||
}); | |||||
} | |||||
jsi::Value CommCoreModule::isNotificationsSessionInitialized(jsi::Runtime &rt) { | |||||
return createPromiseAsJSIValue( | |||||
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | |||||
taskType job = [=, &innerRt]() { | |||||
std::string error; | |||||
bool result; | |||||
try { | |||||
result = | |||||
NotificationsCryptoModule::isNotificationsSessionInitialized( | |||||
"Comm"); | |||||
} catch (const std::exception &e) { | |||||
error = e.what(); | |||||
} | |||||
this->jsInvoker_->invokeAsync([=, &innerRt]() { | |||||
if (error.size()) { | |||||
promise->reject(error); | |||||
return; | |||||
} | |||||
promise->resolve(result); | |||||
}); | }); | ||||
}; | }; | ||||
this->cryptoThread->scheduleTask(job); | this->cryptoThread->scheduleTask(job); | ||||
}); | }); | ||||
} | } | ||||
CommCoreModule::CommCoreModule( | CommCoreModule::CommCoreModule( | ||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) | std::shared_ptr<facebook::react::CallInvoker> jsInvoker) | ||||
▲ Show 20 Lines • Show All 320 Lines • Show Last 20 Lines |