diff --git a/android/build.gradle b/android/build.gradle index 49282df..82ab33d 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -11,19 +11,10 @@ buildscript { } } -def isNewArchitectureEnabled() { - return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true" -} - apply plugin: "com.android.library" +apply plugin: "com.facebook.react" -def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') } - -if (isNewArchitectureEnabled()) { - apply plugin: "com.facebook.react" -} - def getExtOrDefault(name) { return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Libsodium_" + name] } @@ -54,7 +45,6 @@ android { defaultConfig { minSdkVersion getExtOrIntegerDefault("minSdkVersion") targetSdkVersion getExtOrIntegerDefault("targetSdkVersion") - buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString() externalNativeBuild { cmake { cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all" @@ -99,16 +89,11 @@ repositories { dependencies { - // For < 0.71, this will be from the local maven repo - // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin - //noinspection GradleDynamicVersion - implementation "com.facebook.react:react-native:+" + implementation "com.facebook.react:react-android" } -if (isNewArchitectureEnabled()) { - react { - jsRootDir = file("../src/") - libraryName = "Libsodium" - codegenJavaPackageName = "com.libsodium" - } +react { + jsRootDir = file("../src/") + libraryName = "Libsodium" + codegenJavaPackageName = "com.libsodium" } diff --git a/android/src/main/java/com/libsodium/LibsodiumModule.java b/android/src/main/java/com/libsodium/LibsodiumModule.java index fc84679..a12c1f5 100644 --- a/android/src/main/java/com/libsodium/LibsodiumModule.java +++ b/android/src/main/java/com/libsodium/LibsodiumModule.java @@ -1,26 +1,16 @@ package com.libsodium; -import androidx.annotation.NonNull; - import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactContextBaseJavaModule; -import com.facebook.react.bridge.ReactMethod; -public class LibsodiumModule extends ReactContextBaseJavaModule { - public static final String NAME = "Libsodium"; +public class LibsodiumModule extends NativeLibsodiumSpec { + public static final String NAME = NativeLibsodiumSpec.NAME; private static native void initialize(long jsiPtr); public LibsodiumModule(ReactApplicationContext reactContext) { super(reactContext); } - @NonNull @Override - public String getName() { - return NAME; - } - - @ReactMethod(isBlockingSynchronousMethod = true) public boolean install() { try { System.loadLibrary("libsodium"); @@ -34,4 +24,4 @@ public boolean install() { return false; } } -} \ No newline at end of file +} diff --git a/android/src/main/java/com/libsodium/LibsodiumPackage.java b/android/src/main/java/com/libsodium/LibsodiumPackage.java index 3cc8bb9..2063ce6 100644 --- a/android/src/main/java/com/libsodium/LibsodiumPackage.java +++ b/android/src/main/java/com/libsodium/LibsodiumPackage.java @@ -1,26 +1,45 @@ package com.libsodium; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; -import com.facebook.react.ReactPackage; +import com.facebook.react.BaseReactPackage; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.uimanager.ViewManager; +import com.facebook.react.module.model.ReactModuleInfo; +import com.facebook.react.module.model.ReactModuleInfoProvider; -import java.util.Collections; -import java.util.List; +import java.util.HashMap; +import java.util.Map; -public class LibsodiumPackage implements ReactPackage { - @NonNull +public class LibsodiumPackage extends BaseReactPackage { @Override - public List createNativeModules(@NonNull ReactApplicationContext reactContext) { - return Collections.singletonList(new LibsodiumModule(reactContext)); + @Nullable + public NativeModule getModule(String name, ReactApplicationContext reactContext) { + if (name.equals(LibsodiumModule.NAME)) { + return new LibsodiumModule(reactContext); + } + return null; } @NonNull @Override - public List createViewManagers(@NonNull ReactApplicationContext reactContext) { - return Collections.emptyList(); + public ReactModuleInfoProvider getReactModuleInfoProvider() { + return () -> { + Map moduleInfos = new HashMap<>(); + moduleInfos.put( + LibsodiumModule.NAME, + new ReactModuleInfo( + LibsodiumModule.NAME, + LibsodiumModule.NAME, + false, // canOverrideExistingModule + false, // needsEagerInit + false, // isCxxModule + true // isTurboModule + ) + ); + return moduleInfos; + }; } -} \ No newline at end of file +} diff --git a/ios/Libsodium.h b/ios/Libsodium.h index 53c0ab4..dab053a 100644 --- a/ios/Libsodium.h +++ b/ios/Libsodium.h @@ -1,9 +1,5 @@ -#import "react-native-libsodium.h" -#import -#import "sodium.h" +#import -@interface Libsodium : NSObject +@interface Libsodium : NSObject -@property(nonatomic, assign) BOOL setBridgeOnMainQueue; - -@end \ No newline at end of file +@end diff --git a/ios/Libsodium.mm b/ios/Libsodium.mm index fbc745d..8ef3829 100644 --- a/ios/Libsodium.mm +++ b/ios/Libsodium.mm @@ -1,6 +1,5 @@ #import "Libsodium.h" #import -#import #import #import "react-native-libsodium.h" @@ -8,20 +7,29 @@ @implementation Libsodium @synthesize bridge=_bridge; -RCT_EXPORT_MODULE() - -RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) { - +- (BOOL)install +{ RCTLogInfo(@"installing libsodium"); RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge; if (!cxxBridge.runtime) { RCTLogInfo(@"libsodium install failure: no cxx bridge runtime"); - return nil; + return NO; } RCTLogInfo(@"calling installLibsodium with cxx bridge runtime"); ReactNativeLibsodium::installLibsodium(*(facebook::jsi::Runtime *)cxxBridge.runtime); - return nil; + return YES; +} + +- (std::shared_ptr)getTurboModule: + (const facebook::react::ObjCTurboModule::InitParams &)params +{ + return std::make_shared(params); +} + ++ (NSString *)moduleName +{ + return @"Libsodium"; } @end diff --git a/package.json b/package.json index 6e60716..3162e0f 100644 --- a/package.json +++ b/package.json @@ -141,6 +141,14 @@ ] ] }, + "codegenConfig": { + "name": "LibsodiumSpec", + "type": "modules", + "jsSrcsDir": "src", + "android": { + "javaPackageName": "com.libsodium" + } + }, "dependencies": { "@noble/hashes": "^1.3.2", "libsodium-wrappers": "^0.8.2", diff --git a/react-native-libsodium.podspec b/react-native-libsodium.podspec index 601b70e..0a31e48 100644 --- a/react-native-libsodium.podspec +++ b/react-native-libsodium.podspec @@ -1,7 +1,6 @@ require "json" package = JSON.parse(File.read(File.join(__dir__, "package.json"))) -folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32' Pod::Spec.new do |s| s.name = "react-native-libsodium" @@ -27,27 +26,6 @@ Pod::Spec.new do |s| s.vendored_frameworks = "libsodium/build/libsodium-apple/Clibsodium.xcframework" - # Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0. - # See https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/cocoapods/new_architecture.rb#L79. - if respond_to?(:install_modules_dependencies, true) - install_modules_dependencies(s) - else - s.dependency "React-Core" - - # Don't install the dependencies when we run `pod install` in the old architecture. - if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then - s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1" - s.pod_target_xcconfig = { - "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"", - "OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1", - "CLANG_CXX_LANGUAGE_STANDARD" => "c++17" - } - s.dependency "React-Codegen" - s.dependency "RCT-Folly" - s.dependency "RCTRequired" - s.dependency "RCTTypeSafety" - s.dependency "ReactCommon/turbomodule/core" - end - end + install_modules_dependencies(s) end diff --git a/src/NativeLibsodium.ts b/src/NativeLibsodium.ts new file mode 100644 index 0000000..9655284 --- /dev/null +++ b/src/NativeLibsodium.ts @@ -0,0 +1,7 @@ +import { TurboModuleRegistry, type TurboModule } from 'react-native'; + +export interface Spec extends TurboModule { + install(): boolean; +} + +export default TurboModuleRegistry.getEnforcing('Libsodium'); diff --git a/src/lib.native.ts b/src/lib.native.ts index bfa7ea8..9c00aa5 100644 --- a/src/lib.native.ts +++ b/src/lib.native.ts @@ -8,7 +8,7 @@ import { base64_variants, to_string } from './libsodium-js-utils'; import type { OutputFormat } from './types'; import { convertToOutputFormat } from './utils'; -import { NativeModules } from 'react-native'; +import Libsodium from './NativeLibsodium'; const toArrayBuffer = (input: Uint8Array): ArrayBuffer => { const buffer = input.buffer; @@ -22,16 +22,8 @@ const toArrayBuffer = (input: Uint8Array): ArrayBuffer => { return input.slice().buffer; }; -const Libsodium = NativeModules.Libsodium; - -if (Libsodium && typeof Libsodium.install === 'function') { - console.log('calling Libsodium.install'); - Libsodium.install(); -} else if (!Libsodium) { - console.warn('Libsodium module not defined'); -} else { - console.warn('Libsodium.install not a function'); -} +console.log('calling Libsodium.install'); +Libsodium.install(); declare global { var jsi_crypto_auth_BYTES: number;