react-native-quick-crypto 0.3.1 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/README.md +10 -6
  2. package/android/CMakeLists.txt +10 -2
  3. package/android/gradle.properties +1 -1
  4. package/android/src/main/AndroidManifest.xml +1 -1
  5. package/android/src/main/cpp/cpp-adapter.cpp +1 -1
  6. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +70 -0
  7. package/android/src/main/java/com/{reactnativequickcrypto → margelo/quickcrypto}/QuickCryptoPackage.java +11 -12
  8. package/cpp/Cipher/MGLCipherHostObject.cpp +4 -5
  9. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +1 -3
  10. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +6 -3
  11. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +5 -3
  12. package/cpp/Cipher/MGLPublicCipher.h +1 -1
  13. package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
  14. package/cpp/Cipher/MGLRsa.h +5 -1
  15. package/cpp/JSIUtils/MGLJSIMacros.h +69 -6
  16. package/cpp/{Cipher/MGLCipherKeys.cpp → MGLKeys.cpp} +47 -49
  17. package/cpp/{Cipher/MGLCipherKeys.h → MGLKeys.h} +29 -30
  18. package/cpp/MGLQuickCryptoHostObject.cpp +12 -0
  19. package/cpp/Sig/MGLSignHostObjects.cpp +889 -0
  20. package/cpp/Sig/MGLSignHostObjects.h +88 -0
  21. package/cpp/Sig/MGLSignInstaller.cpp +24 -0
  22. package/cpp/Sig/MGLSignInstaller.h +29 -0
  23. package/cpp/Sig/MGLVerifyInstaller.cpp +24 -0
  24. package/cpp/Sig/MGLVerifyInstaller.h +22 -0
  25. package/cpp/Utils/MGLUtils.cpp +67 -29
  26. package/cpp/Utils/MGLUtils.h +17 -17
  27. package/lib/commonjs/@types/crypto-browserify.d.js +2 -0
  28. package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -0
  29. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  30. package/lib/commonjs/NativeQuickCrypto/sig.js +2 -0
  31. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -0
  32. package/lib/commonjs/QuickCrypto.js +4 -0
  33. package/lib/commonjs/QuickCrypto.js.map +1 -1
  34. package/lib/commonjs/index.js +7 -7
  35. package/lib/commonjs/index.js.map +1 -1
  36. package/lib/commonjs/keys.js +1 -4
  37. package/lib/commonjs/keys.js.map +1 -1
  38. package/lib/commonjs/sig.js +170 -0
  39. package/lib/commonjs/sig.js.map +1 -0
  40. package/lib/module/@types/crypto-browserify.d.js +2 -0
  41. package/lib/module/@types/crypto-browserify.d.js.map +1 -0
  42. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
  43. package/lib/module/NativeQuickCrypto/sig.js +2 -0
  44. package/lib/module/NativeQuickCrypto/sig.js.map +1 -0
  45. package/lib/module/QuickCrypto.js +3 -0
  46. package/lib/module/QuickCrypto.js.map +1 -1
  47. package/lib/module/index.js +6 -9
  48. package/lib/module/index.js.map +1 -1
  49. package/lib/module/keys.js +1 -4
  50. package/lib/module/keys.js.map +1 -1
  51. package/lib/module/sig.js +155 -0
  52. package/lib/module/sig.js.map +1 -0
  53. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +3 -0
  54. package/lib/typescript/NativeQuickCrypto/sig.d.ts +12 -0
  55. package/lib/typescript/QuickCrypto.d.ts +3 -0
  56. package/lib/typescript/index.d.ts +206 -1
  57. package/lib/typescript/sig.d.ts +35 -0
  58. package/package.json +3 -2
  59. package/src/@types/crypto-browserify.d.ts +4 -0
  60. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +3 -0
  61. package/src/NativeQuickCrypto/sig.ts +17 -0
  62. package/src/QuickCrypto.ts +3 -0
  63. package/src/index.ts +6 -5
  64. package/src/keys.ts +18 -13
  65. package/src/sig.ts +179 -0
  66. package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoModule.java +0 -70
package/README.md CHANGED
@@ -9,11 +9,10 @@ A fast implementation of Node's `crypto` module.
9
9
  Unlike any other current JS-based polyfills, react-native-quick-crypto is written in C/C++ JSI and provides much greater performance - especially on mobile devices.
10
10
  QuickCrypto can be used as a drop-in replacement for your Web3/Crypto apps to speed up common cryptography functions.
11
11
 
12
- * 🏎️ Up to 300x faster than all other solutions
12
+ * 🏎️ Up to 58x faster than all other solutions
13
13
  * ⚡️ Lightning fast implementation with pure C++ and JSI, instead of JS
14
14
  * 🧪 Well tested in JS and C++ (OpenSSL)
15
15
  * 💰 Made for crypto apps and Wallets
16
- * 🤌 Up to 5x smaller in JS-bundle size
17
16
  * 🔢 Secure native compiled cryptography
18
17
  * 🔁 Easy drop-in replacement for [crypto-browserify](https://github.com/crypto-browserify/crypto-browserify) or [react-native-crypto](https://github.com/tradle/react-native-crypto)
19
18
 
@@ -23,19 +22,19 @@ For example, creating a Wallet using ethers.js uses complex algorithms to genera
23
22
  const start = performance.now()
24
23
  const wallet = ethers.Wallet.createRandom()
25
24
  const end = performance.now()
26
- console.log(`Creating a Wallet took ${end - start}ms.`)
25
+ console.log(`Creating a Wallet took ${end - start} ms.`)
27
26
  ```
28
27
 
29
28
  **Without** react-native-crypto 🐢:
30
29
 
31
30
  ```
32
- Creating a Wallet took xxxms
31
+ Creating a Wallet took 16862 ms
33
32
  ```
34
33
 
35
34
  **With** react-native-crypto ⚡️:
36
35
 
37
36
  ```
38
- Creating a Wallet took yyyms
37
+ Creating a Wallet took 289 ms
39
38
  ```
40
39
 
41
40
  ---
@@ -89,7 +88,12 @@ Now, all imports for `crypto` will be resolved as `react-native-quick-crypto` in
89
88
 
90
89
  ## Sponsors
91
90
 
92
- - TODO: List sponsors here
91
+ <div align="center">
92
+
93
+ <img width="350" src="./img/sponsors/litentry.png" align="center"><br/>
94
+ <a href="https://litentry.com"><b>Litentry</b></a> - A decentralized identity aggregator, providing the structure and tools to empower you and your identity.
95
+
96
+ </div>
93
97
 
94
98
  ## Limitations
95
99
 
@@ -6,8 +6,8 @@ file (GLOB LIBFBJNI_INCLUDES "${BUILD_DIR}/fbjni-*-headers.jar/")
6
6
  file (GLOB LIBRN_SO "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
7
7
  set (CMAKE_CXX_STANDARD 17)
8
8
  # TODO(osp) remove before release
9
- set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
10
- set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
9
+ #set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
10
+ #set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
11
11
 
12
12
  include_directories(
13
13
  ../cpp
@@ -48,6 +48,14 @@ add_library(reactnativequickcrypto # <-- Library name
48
48
  "../cpp/Cipher/MGLCipherHostObject.cpp"
49
49
  "../cpp/Cipher/MGLCreateCipherInstaller.cpp"
50
50
  "../cpp/Cipher/MGLCreateDecipherInstaller.cpp"
51
+ "../cpp/MGLKeys.cpp"
52
+ "../cpp/Utils/MGLUtils.cpp"
53
+ "../cpp/Cipher/MGLRsa.cpp"
54
+ "../cpp/Cipher/MGLGenerateKeyPairInstaller.cpp"
55
+ "../cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp"
56
+ "../cpp/Sig/MGLSignInstaller.cpp"
57
+ "../cpp/Sig/MGLVerifyInstaller.cpp"
58
+ "../cpp/Sig/MGLSignHostObjects.cpp"
51
59
  ${INCLUDE_JSI_CPP} # only on older RN versions
52
60
  )
53
61
 
@@ -3,4 +3,4 @@ QuickCrypto_buildToolsVersion=30.0.2
3
3
  QuickCrypto_targetSdkVersion=30
4
4
  android.useAndroidX=true
5
5
  # Enables Prefab
6
- android.enablePrefab=true
6
+ #android.enablePrefab=true
@@ -1,4 +1,4 @@
1
1
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="com.reactnativebignumber">
2
+ package="com.margelo.quickcrypto">
3
3
 
4
4
  </manifest>
@@ -10,7 +10,7 @@ using namespace facebook;
10
10
  class CryptoCppAdapter : public jni::HybridClass<CryptoCppAdapter> {
11
11
  public:
12
12
  static auto constexpr kJavaDescriptor =
13
- "Lcom/reactnativequickcrypto/QuickCryptoModule;";
13
+ "Lcom/margelo/quickcrypto/QuickCryptoModule;";
14
14
 
15
15
  static jni::local_ref<jni::HybridClass<CryptoCppAdapter>::jhybriddata>
16
16
  initHybrid(jni::alias_ref<jhybridobject> jThis) {
@@ -0,0 +1,70 @@
1
+ package com.margelo.quickcrypto;
2
+
3
+ import android.util.Log;
4
+
5
+ import androidx.annotation.NonNull;
6
+
7
+ import com.facebook.jni.HybridData;
8
+ import com.facebook.proguard.annotations.DoNotStrip;
9
+ import com.facebook.react.bridge.JavaScriptContextHolder;
10
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
11
+ import com.facebook.react.bridge.ReactApplicationContext;
12
+ import com.facebook.react.bridge.ReactMethod;
13
+ import com.facebook.react.module.annotations.ReactModule;
14
+ import com.facebook.react.turbomodule.core.CallInvokerHolderImpl;
15
+
16
+ @ReactModule(name = QuickCryptoModule.NAME)
17
+ public class QuickCryptoModule extends ReactContextBaseJavaModule {
18
+ public static final String NAME = "QuickCrypto";
19
+
20
+ @DoNotStrip
21
+ private HybridData mHybridData;
22
+
23
+ private native HybridData initHybrid();
24
+
25
+ public QuickCryptoModule(ReactApplicationContext reactContext) {
26
+ super(reactContext);
27
+ }
28
+
29
+ @NonNull
30
+ @Override
31
+ public String getName() {
32
+ return NAME;
33
+ }
34
+
35
+ @ReactMethod(isBlockingSynchronousMethod = true)
36
+ public boolean install() {
37
+ try {
38
+ if (mHybridData != null) {
39
+ return false;
40
+ }
41
+ Log.i(NAME, "Loading C++ library...");
42
+ System.loadLibrary("reactnativequickcrypto");
43
+
44
+ JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
45
+ CallInvokerHolderImpl jsCallInvokerHolder = (CallInvokerHolderImpl) getReactApplicationContext()
46
+ .getCatalystInstance()
47
+ .getJSCallInvokerHolder();
48
+
49
+
50
+ Log.i(NAME, "Installing JSI Bindings for react-native-quick-crypto...");
51
+ mHybridData = initHybrid();
52
+ nativeInstall(jsContext.get(), jsCallInvokerHolder);
53
+ Log.i(NAME, "Successfully installed JSI Bindings for react-native-quick-crypto!");
54
+
55
+ return true;
56
+ } catch (Exception exception) {
57
+ Log.e(NAME, "Failed to install JSI Bindings for react-native-quick-crypto!", exception);
58
+ return false;
59
+ }
60
+ }
61
+
62
+ public void destroy() {
63
+ if (mHybridData == null) {
64
+ return;
65
+ }
66
+ mHybridData.resetNative();
67
+ }
68
+
69
+ private native void nativeInstall(long jsiPtr, CallInvokerHolderImpl jsCallInvokerHolder);
70
+ }
@@ -1,4 +1,4 @@
1
- package com.reactnativequickcrypto;
1
+ package com.margelo.quickcrypto;
2
2
 
3
3
  import androidx.annotation.NonNull;
4
4
 
@@ -10,17 +10,16 @@ import com.facebook.react.uimanager.ViewManager;
10
10
  import java.util.Collections;
11
11
  import java.util.List;
12
12
 
13
-
14
13
  public class QuickCryptoPackage implements ReactPackage {
15
- @NonNull
16
- @Override
17
- public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18
- return Collections.singletonList(new QuickCryptoModule(reactContext));
19
- }
14
+ @NonNull
15
+ @Override
16
+ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
17
+ return Collections.singletonList(new QuickCryptoModule(reactContext));
18
+ }
20
19
 
21
- @NonNull
22
- @Override
23
- public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
24
- return Collections.emptyList();
25
- }
20
+ @NonNull
21
+ @Override
22
+ public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
23
+ return Collections.emptyList();
24
+ }
26
25
  }
@@ -252,7 +252,7 @@ void MGLCipherHostObject::installMethods() {
252
252
  // On the node version there are several layers of wrapping and errors
253
253
  // are not immediately surfaced On our version we can simply throw an
254
254
  // error as soon as something goes wrong
255
- throw jsi::JSError(runtime, 'kErrorState');
255
+ throw jsi::JSError(runtime, "kErrorState");
256
256
  }
257
257
 
258
258
  const int mode = EVP_CIPHER_CTX_mode(ctx_);
@@ -502,10 +502,9 @@ void MGLCipherHostObject::installMethods() {
502
502
  }
503
503
 
504
504
  if (!is_valid) {
505
- jsi::detail::throwJSError(
506
- runtime, "Invalid authentication tag length" + tag_len);
507
- throw jsi::JSError(runtime,
508
- "Invalid authentication tag length: " + tag_len);
505
+ jsi::detail::throwJSError(runtime,
506
+ "Invalid authentication tag length");
507
+ throw jsi::JSError(runtime, "Invalid authentication tag length");
509
508
  }
510
509
 
511
510
  auth_tag_len_ = tag_len;
@@ -3,14 +3,12 @@
3
3
 
4
4
  #include <memory>
5
5
 
6
+ #include "MGLCipherHostObject.h"
6
7
  #ifdef ANDROID
7
8
  #include "JSIUtils/MGLJSIMacros.h"
8
9
  #else
9
10
  #include "MGLJSIMacros.h"
10
11
  #endif
11
- #include "MGLCipherHostObject.h"
12
-
13
- using namespace facebook;
14
12
 
15
13
  namespace margelo {
16
14
 
@@ -13,14 +13,17 @@
13
13
 
14
14
  #include <memory>
15
15
 
16
+ #include "MGLKeys.h"
17
+
16
18
  #ifdef ANDROID
19
+ #include "Cipher/MGLRsa.h"
17
20
  #include "JSIUtils/MGLSmartHostObject.h"
21
+ #include "Utils/MGLUtils.h"
18
22
  #else
19
- #include "MGLSmartHostObject.h"
20
- #endif
21
- #include "MGLCipherKeys.h"
22
23
  #include "MGLRsa.h"
24
+ #include "MGLSmartHostObject.h"
23
25
  #include "MGLUtils.h"
26
+ #endif
24
27
 
25
28
  namespace margelo {
26
29
 
@@ -13,13 +13,15 @@
13
13
  #include <memory>
14
14
 
15
15
  #ifdef ANDROID
16
+ #include "Cipher/MGLRsa.h"
16
17
  #include "JSIUtils/MGLSmartHostObject.h"
18
+ #include "Utils/MGLUtils.h"
17
19
  #else
18
- #include "MGLSmartHostObject.h"
19
- #endif
20
- #include "MGLCipherKeys.h"
21
20
  #include "MGLRsa.h"
21
+ #include "MGLSmartHostObject.h"
22
22
  #include "MGLUtils.h"
23
+ #endif
24
+ #include "MGLKeys.h"
23
25
 
24
26
  namespace margelo {
25
27
 
@@ -14,7 +14,7 @@
14
14
  #include <optional>
15
15
  #include <vector>
16
16
 
17
- #include "MGLCipherKeys.h"
17
+ #include "MGLKeys.h"
18
18
  #ifdef ANDROID
19
19
  #include "JSIUtils/MGLJSIUtils.h"
20
20
  #include "JSIUtils/MGLTypedArray.h"
@@ -18,7 +18,7 @@
18
18
  #include <utility>
19
19
  #include <vector>
20
20
 
21
- #include "MGLCipherKeys.h"
21
+ #include "MGLKeys.h"
22
22
  #include "MGLPublicCipher.h"
23
23
 
24
24
  #ifdef ANDROID
@@ -14,8 +14,12 @@
14
14
  #include <optional>
15
15
  #include <utility>
16
16
 
17
- #include "MGLCipherKeys.h"
17
+ #include "MGLKeys.h"
18
+ #ifdef ANDROID
19
+ #include "Utils/MGLUtils.h"
20
+ #else
18
21
  #include "MGLUtils.h"
22
+ #endif
19
23
 
20
24
  namespace margelo {
21
25
 
@@ -1,14 +1,36 @@
1
- //
2
- // Created by Szymon on 24/02/2022.
3
- //
4
-
5
1
  #ifndef MGL_JSIMACROS_H
6
2
  #define MGL_JSIMACROS_H
7
3
 
8
4
  #include <utility>
9
5
 
10
- // if you want to create a new marco use (https://godbolt.org) provide flag -E
11
- // in right panel
6
+ // Windows 8+ does not like abort() in Release mode
7
+ #ifdef _WIN32
8
+ #define ABORT_NO_BACKTRACE() _exit(134)
9
+ #else
10
+ #define ABORT_NO_BACKTRACE() abort()
11
+ #endif
12
+
13
+ struct AssertionInfo {
14
+ const char *file_line; // filename:line
15
+ const char *message;
16
+ const char *function;
17
+ };
18
+
19
+ inline void Abort() {
20
+ // DumpBacktrace(stderr);
21
+ fflush(stderr);
22
+ ABORT_NO_BACKTRACE();
23
+ }
24
+
25
+ inline void Assert(const AssertionInfo &info) {
26
+ // std::string name = GetHumanReadableProcessName();
27
+
28
+ fprintf(stderr, "%s:%s%s Assertion `%s' failed.\n", info.file_line,
29
+ info.function, *info.function ? ":" : "", info.message);
30
+ fflush(stderr);
31
+
32
+ Abort();
33
+ }
12
34
 
13
35
  #define HOST_LAMBDA(name, body) HOST_LAMBDA_CAP(name, [=], body)
14
36
 
@@ -34,4 +56,45 @@
34
56
  const jsi::Value *arguments, size_t count) \
35
57
  ->jsi::Value
36
58
 
59
+ // Macros stolen from Node
60
+ #define ABORT() node::Abort()
61
+
62
+ #define ERROR_AND_ABORT(expr) \
63
+ do { \
64
+ /* Make sure that this struct does not end up in inline code, but */ \
65
+ /* rather in a read-only data section when modifying this code. */ \
66
+ static const AssertionInfo args = {__FILE__ ":" STRINGIFY(__LINE__), \
67
+ #expr, PRETTY_FUNCTION_NAME}; \
68
+ Assert(args); \
69
+ } while (0)
70
+ #ifdef __GNUC__
71
+ #define LIKELY(expr) __builtin_expect(!!(expr), 1)
72
+ #define UNLIKELY(expr) __builtin_expect(!!(expr), 0)
73
+ #define PRETTY_FUNCTION_NAME __PRETTY_FUNCTION__
74
+ #else
75
+ #define LIKELY(expr) expr
76
+ #define UNLIKELY(expr) expr
77
+ #define PRETTY_FUNCTION_NAME ""
78
+ #endif
79
+
80
+ #define STRINGIFY_(x) #x
81
+ #define STRINGIFY(x) STRINGIFY_(x)
82
+
83
+ #define CHECK(expr) \
84
+ do { \
85
+ if (UNLIKELY(!(expr))) { \
86
+ ERROR_AND_ABORT(expr); \
87
+ } \
88
+ } while (0)
89
+
90
+ #define CHECK_EQ(a, b) CHECK((a) == (b))
91
+ #define CHECK_GE(a, b) CHECK((a) >= (b))
92
+ #define CHECK_GT(a, b) CHECK((a) > (b))
93
+ #define CHECK_LE(a, b) CHECK((a) <= (b))
94
+ #define CHECK_LT(a, b) CHECK((a) < (b))
95
+ #define CHECK_NE(a, b) CHECK((a) != (b))
96
+ #define CHECK_NULL(val) CHECK((val) == nullptr)
97
+ #define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
98
+ #define CHECK_IMPLIES(a, b) CHECK(!(a) || (b))
99
+
37
100
  #endif // MGL_JSIMACROS_H
@@ -5,7 +5,7 @@
5
5
  // Created by Oscar on 20.06.22.
6
6
  //
7
7
 
8
- #include "MGLCipherKeys.h"
8
+ #include "MGLKeys.h"
9
9
 
10
10
  #include <jsi/jsi.h>
11
11
  #include <openssl/bio.h>
@@ -15,12 +15,16 @@
15
15
  #include <utility>
16
16
  #include <vector>
17
17
 
18
+ #ifdef ANDROID
19
+ #include "JSIUtils/MGLJSIMacros.h"
20
+ #include "JSIUtils/MGLJSIUtils.h"
21
+ #include "JSIUtils/MGLTypedArray.h"
22
+ #include "Utils/MGLUtils.h"
23
+ #else
24
+ #include "MGLJSIMacros.h"
18
25
  #include "MGLJSIUtils.h"
19
26
  #include "MGLTypedArray.h"
20
27
  #include "MGLUtils.h"
21
- #ifdef ANDROID
22
- #else
23
- #include "logs.h"
24
28
  #endif
25
29
 
26
30
  namespace margelo {
@@ -33,8 +37,8 @@ void GetKeyFormatAndTypeFromJs(AsymmetricKeyEncodingConfig* config,
33
37
  // During key pair generation, it is possible not to specify a key encoding,
34
38
  // which will lead to a key object being returned.
35
39
  if (args[*offset].isUndefined()) {
36
- // CHECK_EQ(context, kKeyContextGenerate);
37
- // CHECK(args[*offset + 1].IsUndefined());
40
+ CHECK_EQ(context, kKeyContextGenerate);
41
+ CHECK(args[*offset + 1].isUndefined());
38
42
  config->output_key_object_ = true;
39
43
  } else {
40
44
  config->output_key_object_ = false;
@@ -44,16 +48,13 @@ void GetKeyFormatAndTypeFromJs(AsymmetricKeyEncodingConfig* config,
44
48
  config->format_ = static_cast<PKFormatType>((int)args[*offset].getNumber());
45
49
 
46
50
  if (args[*offset + 1].isNumber()) {
47
- config->type_ = std::optional<PKEncodingType>(
48
- static_cast<PKEncodingType>((int)args[*offset + 1].getNumber()));
51
+ config->type_ =
52
+ static_cast<PKEncodingType>((int)args[*offset + 1].getNumber());
49
53
  } else {
50
- // TODO(osp) implement checks
51
- // CHECK(
52
- // (context == kKeyContextInput &&
53
- // config->format_ == kKeyFormatPEM) ||
54
- // (context == kKeyContextGenerate &&
55
- // config->format_ == kKeyFormatJWK));
56
- // CHECK(args[*offset + 1]->IsNullOrUndefined());
54
+ CHECK(
55
+ (context == kKeyContextInput && config->format_ == kKeyFormatPEM) ||
56
+ (context == kKeyContextGenerate && config->format_ == kKeyFormatJWK));
57
+ CHECK(args[*offset + 1].isUndefined());
57
58
  config->type_ = std::nullopt;
58
59
  }
59
60
  }
@@ -193,11 +194,10 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
193
194
  pkey->reset(PEM_read_bio_PrivateKey(bio.get(), nullptr, PasswordCallback,
194
195
  &passphrase));
195
196
  } else {
196
- // CHECK_EQ(config.format_, kKeyFormatDER);
197
+ CHECK_EQ(config.format_, kKeyFormatDER);
197
198
 
198
199
  if (!config.type_.has_value()) {
199
- // TODO(osp) need to implement end of the world exception
200
- // throw new crashing exception
200
+ throw new std::runtime_error("ParsePrivateKey key config has no type!");
201
201
  }
202
202
 
203
203
  if (config.type_.value() == kKeyEncodingPKCS1) {
@@ -216,7 +216,7 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
216
216
  if (p8inf) pkey->reset(EVP_PKCS82PKEY(p8inf.get()));
217
217
  }
218
218
  } else {
219
- // CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSEC1);
219
+ CHECK_EQ(config.type_.value(), kKeyEncodingSEC1);
220
220
  const unsigned char* p = reinterpret_cast<const unsigned char*>(key);
221
221
  pkey->reset(d2i_PrivateKey(EVP_PKEY_EC, nullptr, &p, key_len));
222
222
  }
@@ -692,36 +692,34 @@ PublicKeyEncodingConfig ManagedEVPPKey::GetPublicKeyEncodingFromJs(
692
692
  return result;
693
693
  }
694
694
 
695
- // TODO(osp) I never quite manage to figure out whether this is really necessary
696
- // maybe is other crypto function, leaving it for future uncommenting
697
- // ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs(
698
- // const
699
- // FunctionCallbackInfo<Value>&
700
- // args, unsigned int*
701
- // offset, bool
702
- // allow_key_object) {
703
- // if (args[*offset]->IsString() || IsAnyByteSource(args[*offset])) {
704
- // Environment* env = Environment::GetCurrent(args);
705
- // ByteSource key = ByteSource::FromStringOrBuffer(env, args[(*offset)++]);
706
- // NonCopyableMaybe<PrivateKeyEncodingConfig> config =
707
- // GetPrivateKeyEncodingFromJs(args, offset, kKeyContextInput);
708
- // if (config.IsEmpty())
709
- // return ManagedEVPPKey();
710
- //
711
- // EVPKeyPointer pkey;
712
- // ParseKeyResult ret =
713
- // ParsePrivateKey(&pkey, config.Release(), key.data<char>(), key.size());
714
- // return GetParsedKey(env, std::move(pkey), ret,
715
- // "Failed to read private key");
716
- // } else {
717
- // CHECK(args[*offset]->IsObject() && allow_key_object);
718
- // KeyObjectHandle* key;
719
- // ASSIGN_OR_RETURN_UNWRAP(&key, args[*offset].As<Object>(),
720
- // ManagedEVPPKey()); CHECK_EQ(key->Data()->GetKeyType(), kKeyTypePrivate);
721
- // (*offset) += 4;
722
- // return key->Data()->GetAsymmetricKey();
723
- // }
724
- //}
695
+ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs(jsi::Runtime& runtime,
696
+ const jsi::Value* args,
697
+ unsigned int* offset,
698
+ bool allow_key_object) {
699
+ if (args[*offset].isString() ||
700
+ args[*offset].asObject(runtime).isArrayBuffer(runtime)) {
701
+ ByteSource key = ByteSource::FromStringOrBuffer(runtime, args[*offset]);
702
+ (*offset)++;
703
+ NonCopyableMaybe<PrivateKeyEncodingConfig> config =
704
+ GetPrivateKeyEncodingFromJs(runtime, args, offset, kKeyContextInput);
705
+ if (config.IsEmpty()) return ManagedEVPPKey();
706
+
707
+ EVPKeyPointer pkey;
708
+ ParseKeyResult ret =
709
+ ParsePrivateKey(&pkey, config.Release(), key.data<char>(), key.size());
710
+ return GetParsedKey(runtime, std::move(pkey), ret,
711
+ "Failed to read private key");
712
+ } else {
713
+ // CHECK(args[*offset]->IsObject() && allow_key_object);
714
+ // KeyObjectHandle* key;
715
+ // ASSIGN_OR_RETURN_UNWRAP(&key, args[*offset].As<Object>(),
716
+ // ManagedEVPPKey()); CHECK_EQ(key->Data()->GetKeyType(),
717
+ // kKeyTypePrivate);
718
+ // (*offset) += 4;
719
+ // return key->Data()->GetAsymmetricKey();
720
+ throw jsi::JSError(runtime, "KeyObject are not currently supported");
721
+ }
722
+ }
725
723
 
726
724
  ManagedEVPPKey ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
727
725
  jsi::Runtime& runtime, const jsi::Value* args, unsigned int* offset) {