Skip to content

Commit

Permalink
compiles and passes
Browse files Browse the repository at this point in the history
  • Loading branch information
boorad committed May 20, 2024
1 parent a2032f9 commit 8b2a09e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 78 deletions.
6 changes: 3 additions & 3 deletions cpp/Cipher/MGLRsa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ std::pair<jsi::Value, jsi::Value> generateRsaKeyPair(
ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
config->private_key_encoding);

if (!publicBuffer.isUndefined() || !privateBuffer.isUndefined()) {
throw jsi::JSError(runtime, "Failed to encode public and/or private key");
if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
throw jsi::JSError(runtime, "Failed to encode public and/or private key (RSA)");
}

return {publicBuffer, privateBuffer};
return {std::move(publicBuffer), std::move(privateBuffer)};
}

jsi::Value ExportJWKRsaKey(jsi::Runtime &rt,
Expand Down
16 changes: 9 additions & 7 deletions cpp/MGLKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,9 @@ jsi::Value ManagedEVPPKey::ToEncodedPublicKey(jsi::Runtime& rt,
// Note that this has the downside of containing sensitive data of the
// private key.
auto data = KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
auto out = KeyObjectHandle::Create(rt, data);
return toJSI(rt, out);
auto handle = KeyObjectHandle::Create(rt, data);
auto out = jsi::Object::createFromHostObject(rt, handle);
return jsi::Value(std::move(out));
} else
if (config.format_ == kKeyFormatJWK) {
throw std::runtime_error("ToEncodedPublicKey 2 (JWK) not implemented from node");
Expand All @@ -581,8 +582,9 @@ jsi::Value ManagedEVPPKey::ToEncodedPrivateKey(jsi::Runtime& rt,
if (!key) return {};
if (config.output_key_object_) {
auto data = KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
auto out = KeyObjectHandle::Create(rt, data);
return toJSI(rt, out);
auto handle = KeyObjectHandle::Create(rt, data);
auto out = jsi::Object::createFromHostObject(rt, handle);
return jsi::Value(std::move(out));
} else
if (config.format_ == kKeyFormatJWK) {
throw std::runtime_error("ToEncodedPrivateKey 2 (JWK) not implemented from node");
Expand Down Expand Up @@ -892,9 +894,9 @@ jsi::Value KeyObjectHandle::get(
// registry->Register(Equals);
// }

KeyObjectHandle* KeyObjectHandle::Create(jsi::Runtime &rt,
std::shared_ptr<KeyObjectData> data) {
KeyObjectHandle* handle = new KeyObjectHandle();
std::shared_ptr<KeyObjectHandle> KeyObjectHandle::Create(jsi::Runtime &rt,
std::shared_ptr<KeyObjectData> data) {
auto handle = std::make_shared<KeyObjectHandle>();
handle->data_ = data;
return handle;
}
Expand Down
15 changes: 2 additions & 13 deletions cpp/MGLKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ class JSI_EXPORT KeyObjectHandle: public jsi::HostObject {
jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
const std::shared_ptr<KeyObjectData>& Data();

static KeyObjectHandle* Create(jsi::Runtime &rt,
std::shared_ptr<KeyObjectData> data);
static std::shared_ptr<KeyObjectHandle> Create(jsi::Runtime &rt,
std::shared_ptr<KeyObjectData> data);

protected:
jsi::Value Export(jsi::Runtime &rt);
Expand All @@ -190,17 +190,6 @@ class JSI_EXPORT KeyObjectHandle: public jsi::HostObject {
std::shared_ptr<KeyObjectData> data_;
};

// Convert KeyObjectHandle to jsi::Value for shipping back to JS side.
//
// Note: This `toJSI()` is in MGLKeys for access to KeyObjectHandle. The rest of the `toJSI()` functions are in MGLUtils.
inline jsi::Value toJSI(jsi::Runtime& rt, KeyObjectHandle* value) {
jsi::Function handle_ctor =
rt.global().getPropertyAsFunction(rt, "KeyObjectHandle");
jsi::Object o =
handle_ctor.callAsConstructor(rt, std::move(value)).getObject(rt);
return o;
}

} // namespace margelo

#endif /* MGLCipherKeys_h */
6 changes: 3 additions & 3 deletions cpp/webcrypto/crypto_ec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,11 @@ std::pair<jsi::Value, jsi::Value> generateEcKeyPair(jsi::Runtime& runtime,
ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
config->private_key_encoding);

if (!publicBuffer.isUndefined() || !privateBuffer.isUndefined()) {
throw jsi::JSError(runtime, "Failed to encode public and/or private key");
if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
throw jsi::JSError(runtime, "Failed to encode public and/or private key (EC)");
}

return {publicBuffer, privateBuffer};
return {std::move(publicBuffer), std::move(privateBuffer)};
}

} // namespace margelo
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ PODS:
- react-native-quick-base64 (2.1.2):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-quick-crypto (0.7.0-rc.5):
- react-native-quick-crypto (0.7.0-rc.6):
- OpenSSL-Universal
- RCT-Folly (= 2021.07.22.00)
- React
Expand Down Expand Up @@ -437,7 +437,7 @@ PODS:
- React-jsi (= 0.72.7)
- React-logger (= 0.72.7)
- React-perflogger (= 0.72.7)
- RNCCheckbox (0.5.16):
- RNCCheckbox (0.5.17):
- BEMCheckBox (~> 1.4)
- React-Core
- RNScreens (3.27.0):
Expand Down Expand Up @@ -620,7 +620,7 @@ SPEC CHECKSUMS:
React-jsinspector: 8baadae51f01d867c3921213a25ab78ab4fbcd91
React-logger: 8edc785c47c8686c7962199a307015e2ce9a0e4f
react-native-quick-base64: 61228d753294ae643294a75fece8e0e80b7558a6
react-native-quick-crypto: 419d628c781a5144fbed43d104fb1f314ecaeff4
react-native-quick-crypto: 0f1c9ae20bc06e10f0349887a9e3767ff683c5ac
react-native-safe-area-context: 2cd91d532de12acdb0a9cbc8d43ac72a8e4c897c
React-NativeModulesApple: b6868ee904013a7923128892ee4a032498a1024a
React-perflogger: 31ea61077185eb1428baf60c0db6e2886f141a5a
Expand All @@ -639,7 +639,7 @@ SPEC CHECKSUMS:
React-runtimescheduler: 7649c3b46c8dee1853691ecf60146a16ae59253c
React-utils: 56838edeaaf651220d1e53cd0b8934fb8ce68415
ReactCommon: 5f704096ccf7733b390f59043b6fa9cc180ee4f6
RNCCheckbox: 75255b03e604bbcc26411eb31c4cbe3e3865f538
RNCCheckbox: a3ca9978cb0846b981d28da4e9914bd437403d77
RNScreens: 3c2d122f5e08c192e254c510b212306da97d2581
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 4c3aa327e4a6a23eeacd71f61c81df1bcdf677d5
Expand Down
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1347,10 +1347,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@react-native-community/checkbox@^0.5.16":
version "0.5.16"
resolved "https://registry.yarnpkg.com/@react-native-community/checkbox/-/checkbox-0.5.16.tgz#3265260e5fd961a4f032d0c088bbfbdfa96ee44e"
integrity sha512-j4fmWe77EAayGnKJ52BljlN8apLT3xjxG/pJOA6HZ4ew63FiXmnY7VtxTzmvDKgSPrETdQc2lmx5mdXTAufJnw==
"@react-native-community/checkbox@^0.5.17":
version "0.5.17"
resolved "https://registry.yarnpkg.com/@react-native-community/checkbox/-/checkbox-0.5.17.tgz#80dd335ca5c61ac2ca4525a21d8fef3f7a021ef7"
integrity sha512-ZZx/eOH3SIxBg+hvA2zRd7lX5zPQEWcZU8C8Dn7WLdpJkiTkPmIUDFHWrmzxCnwZcyoD+BIt6gUZPKGGb9WmeQ==

"@react-native-community/cli-clean@11.3.10":
version "11.3.10"
Expand Down
29 changes: 5 additions & 24 deletions src/ec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { KeyObject } from 'crypto';
import { generateKeyPairPromise, type GenerateKeyPairOptions } from './Cipher';
import { NativeQuickCrypto } from './NativeQuickCrypto/NativeQuickCrypto';
import {
Expand Down Expand Up @@ -350,34 +349,16 @@ export const ecGenerateKey = async (

const keyAlgorithm = { name, namedCurve };

const publicKey = new InternalCryptoKey(
keypair.publicKey as KeyObjectHandle,
keyAlgorithm,
publicUsages,
true
);
// const publicKey = new PublicKeyObject(pub as KeyObjectHandle);
const pub = new PublicKeyObject(keypair?.publicKey as KeyObjectHandle);
const publicKey = new CryptoKey(pub, keyAlgorithm, publicUsages, true);

const privateKey = new InternalCryptoKey(
keypair.privateKey as KeyObjectHandle,
const priv = new PrivateKeyObject(keypair?.privateKey as KeyObjectHandle);
const privateKey = new CryptoKey(
priv,
keyAlgorithm,
privateUsages,
extractable
);

return { publicKey, privateKey };
};

// const publicKey =
// new InternalCryptoKey(
// keypair.publicKey,
// keyAlgorithm,
// publicUsages,
// true);

// const privateKey =
// new InternalCryptoKey(
// keypair.privateKey,
// keyAlgorithm,
// privateUsages,
// extractable);
20 changes: 0 additions & 20 deletions src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,18 +521,6 @@ export class CryptoKey {
}
}

// ObjectDefineProperties(CryptoKey.prototype, {
// type: kEnumerableProperty,
// extractable: kEnumerableProperty,
// algorithm: kEnumerableProperty,
// usages: kEnumerableProperty,
// [SymbolToStringTag]: {
// __proto__: null,
// configurable: true,
// value: 'CryptoKey',
// },
// });

class KeyObject {
handle: KeyObjectHandle;
type: 'public' | 'secret' | 'private' | 'unknown' = 'unknown';
Expand Down Expand Up @@ -573,14 +561,6 @@ class KeyObject {
// }
}

// ObjectDefineProperties(KeyObject.prototype, {
// [SymbolToStringTag]: {
// __proto__: null,
// configurable: true,
// value: 'KeyObject',
// },
// });

export class SecretKeyObject extends KeyObject {
constructor(handle: KeyObjectHandle) {
super('secret', handle);
Expand Down

0 comments on commit 8b2a09e

Please sign in to comment.