Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
16 changes: 3 additions & 13 deletions android/src/main/java/com/libsodium/LibsodiumModule.java
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -34,4 +24,4 @@ public boolean install() {
return false;
}
}
}
}
41 changes: 30 additions & 11 deletions android/src/main/java/com/libsodium/LibsodiumPackage.java
Original file line number Diff line number Diff line change
@@ -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<NativeModule> 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<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return () -> {
Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
moduleInfos.put(
LibsodiumModule.NAME,
new ReactModuleInfo(
LibsodiumModule.NAME,
LibsodiumModule.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
true // isTurboModule
)
);
return moduleInfos;
};
}
}
}
10 changes: 3 additions & 7 deletions ios/Libsodium.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#import "react-native-libsodium.h"
#import <React/RCTBridgeModule.h>
#import "sodium.h"
#import <LibsodiumSpec/LibsodiumSpec.h>

@interface Libsodium : NSObject <RCTBridgeModule>
@interface Libsodium : NSObject <NativeLibsodiumSpec>

@property(nonatomic, assign) BOOL setBridgeOnMainQueue;

@end
@end
22 changes: 15 additions & 7 deletions ios/Libsodium.mm
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
#import "Libsodium.h"
#import <React/RCTBridge+Private.h>
#import <React/RCTUtils.h>
#import <React/RCTLog.h>
#import "react-native-libsodium.h"

@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<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeLibsodiumSpecJSI>(params);
}

+ (NSString *)moduleName
{
return @"Libsodium";
}

@end
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 1 addition & 23 deletions react-native-libsodium.podspec
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
7 changes: 7 additions & 0 deletions src/NativeLibsodium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { TurboModuleRegistry, type TurboModule } from 'react-native';

export interface Spec extends TurboModule {
install(): boolean;
}

export default TurboModuleRegistry.getEnforcing<Spec>('Libsodium');
14 changes: 3 additions & 11 deletions src/lib.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Loading