Skip to content

Commit

Permalink
feat: added JSI and fixed all the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
numandev1 committed May 9, 2023
1 parent 62193e5 commit 672289d
Show file tree
Hide file tree
Showing 129 changed files with 38,097 additions and 8,586 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ DerivedData
*.ipa
*.xcuserstate
project.xcworkspace
*.cxx
android/.cxx/*
crypto.cpp
KeysModule.java

# Android/IJ
#
.idea
.gradle
local.properties
android.iml
android/.cxx
*.hprof

# Cocoapods
#
Expand All @@ -61,3 +60,6 @@ android/keystores/debug.keystore

# generated by bob
lib/

**prebuild.log
example/ios/tmp.xcconfig
1 change: 0 additions & 1 deletion .mtslconfig.json

This file was deleted.

4 changes: 0 additions & 4 deletions .npmignore

This file was deleted.

3 changes: 3 additions & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Override Yarn command so we can automatically setup the repo on running `yarn`

yarn-path "scripts/bootstrap.js"
24 changes: 18 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ We want this community to be friendly and respectful to each other. Please follo

## Development workflow

To get started with the project, run `yarn bootstrap` in the root directory to install the required dependencies for each package:
To get started with the project, run `yarn` in the root directory to install the required dependencies for each package:

```sh
yarn bootstrap
yarn
```

While developing, you can run the [example app](/example/) to test your changes.
> While it's possible to use [`npm`](https://github.com/npm/cli), the tooling is built around [`yarn`](https://classic.yarnpkg.com/), so you'll have an easier time if you use `yarn` for development.
While developing, you can run the [example app](/example/) to test your changes. Any changes you make in your library's JavaScript code will be reflected in the example app without a rebuild. If you change any native code, then you'll need to rebuild the example app.

To start the packager:

Expand Down Expand Up @@ -49,9 +51,9 @@ Remember to add tests for your change if possible. Run the unit tests by:
yarn test
```

To edit the Objective-C files, open `example/ios/KeysExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > react-native-keys`.
To edit the Objective-C files, open `example/ios/KeysExample.xcworkspace` in XCode and find the source files at `Pods > Development Pods > react-native-simple-jsi`.

To edit the Kotlin files, open `example/android` in Android studio and find the source files at `rnkeys` under `Android`.
To edit the Kotlin files, open `example/android` in Android studio and find the source files at `reactnativeKeys` under `Android`.

### Commit message convention

Expand All @@ -61,7 +63,7 @@ We follow the [conventional commits specification](https://www.conventionalcommi
- `feat`: new features, e.g. add new method to the module.
- `refactor`: code refactor, e.g. migrate from class components to hooks.
- `docs`: changes into documentation, e.g. add usage example for the module..
- `test`: adding or updating tests, eg add integration tests using detox.
- `test`: adding or updating tests, e.g. add integration tests using detox.
- `chore`: tooling changes, e.g. change CI config.

Our pre-commit hooks verify that your commit message matches this format when committing.
Expand All @@ -74,6 +76,16 @@ We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint]

Our pre-commit hooks verify that the linter and tests pass when committing.

### Publishing to npm

We use [release-it](https://github.com/release-it/release-it) to make it easier to publish new versions. It handles common tasks like bumping version based on semver, creating tags and releases etc.

To publish new versions, run the following:

```sh
yarn release
```

### Scripts

The `package.json` file contains various scripts for common tasks:
Expand Down
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# react-native-keys

we are using [JSI](https://reactnative.dev/architecture/glossary#javascript-interfaces-jsi) for fast performance and [JNI](https://reactnative.dev/architecture/glossary#java-native-interface-jni) + encryption keys in c++ compiled file

Manage local **secure** and **unsecure** enviroment through react-native-keys supporting iOS and Android

**secure:** Secure enviroment use JNI to secure keys which we cannot easily decompile or hack
Expand All @@ -13,7 +15,7 @@ yarn add react-native-keys

## Basic Usage

Create a new file `keys.json` in the root of your React Native app and add keys in `secure` object for JNI and add keys in public for without jni usage like this:
Create a new file `keys.development.json` in the root of your React Native app and add keys in `secure` object for JNI and add keys in public for without jni usage like this:

```
{
Expand Down Expand Up @@ -44,14 +46,14 @@ Keys.API_URL; // https://example.com'
Keys.URI_SCHEME; // fb://
```

### Secure Keys (JNI)
### Secure Keys

```js
import Keys from 'react-native-keys';

await Keys.secureFor('API_TOKEN '); // 'ABCSE#$DDSD
await Keys.secureFor('GOOGLE_API_KEY '); // 'ABCSE#$DDSD
await Keys.secureFor('SECRET_KEY'); // 'ABCSE#$DDSD
Keys.secureFor('API_TOKEN '); // 'ABCSE#$DDSD
Keys.secureFor('GOOGLE_API_KEY '); // 'ABCSE#$DDSD
Keys.secureFor('SECRET_KEY'); // 'ABCSE#$DDSD
```

Keep in mind It's [basically impossible to prevent users from reverse engineering mobile app secrets](https://rammic.github.io/2015/07/28/hiding-secrets-in-android-apps/) but this librrary iis more secure.
Expand Down Expand Up @@ -110,7 +112,7 @@ if cocoapods are used in the project then pod has to be installed as well:
**MainApplication.java**

```diff
+ import com.rnkeys.KeysPackage;
+ import com.reactnativekeysjsi.KeysPackage;

@Override
protected List<ReactPackage> getPackages() {
Expand Down Expand Up @@ -158,7 +160,7 @@ versionCode project.env.get("VERSION_CODE").toInteger()
#### Secure Keys (JNI)

```java
import static com.rnkeys.KeysModule.getSecureFor;
import static com.reactnativekeysjsi.KeysModule.getSecureFor;

String secureValue=getSecureFor("BRANCH_KEY"); // key_test_omQ7YYKiq57vOqEJsdcsdfeEsiWkwxE
```
Expand All @@ -167,17 +169,20 @@ String secureValue=getSecureFor("BRANCH_KEY"); // key_test_omQ7YYKiq57vOqEJsdc

#### Public Keys

Read variables declared in `keys.json` from your Obj-C classes like:
Read variables declared in `keys.development.json` from your Obj-C classes like:

```objective-c
// import header
#import "Keys.h"

// then read individual keys like:
NSString *value = [Keys publicFor:@"API_URL"]; // https://example.com

// or just fetch all keys
NSDictionary *allKeys = [Keys public_keys];
```
#### Secure Keys (JNI)
#### Secure Keys
```objective-c
// import header
Expand Down Expand Up @@ -228,7 +233,7 @@ ios/tmp.xcconfig

Save config for different environments in different files: `keys.staging.json`, `keys.production.json`, etc.

By default react-native-keys will read from `keys.json`, but you can change it when building or releasing your app.
By default react-native-keys will read from `keys.development.json`, but you can change it when building or releasing your app.

The simplest approach is to tell it what file to read with an environment variable, like:

Expand All @@ -252,13 +257,25 @@ Alternatively, you can define a map in `build.gradle` associating builds with en

```
project.ext.keyFiles = [
debug: "keys.json",
debug: "keys.development.json",
release: "keys.staging.json",
]
apply from: project(':react-native-keys').projectDir.getPath() + "/RNKeys.gradle"
```

#### Advanced Android Setup

In `android/app/build.gradle`, if you use `applicationIdSuffix` or `applicationId` that is different from the package name indicated in `AndroidManifest.xml` in `<manifest package="...">` tag, for example, to support different build variants:
Add this in `android/app/build.gradle`

```
defaultConfig {
...
resValue "string", "build_config_package", "YOUR_PACKAGE_NAME_IN_ANDROIDMANIFEST_XML"
}
```

#### iOS

The basic idea in iOS is to have one scheme per environment file, so you can easily alternate between them.
Expand All @@ -278,8 +295,12 @@ Then edit the newly created scheme to make it use a different env file. From the
you can also set different file for debug and release build like this.

```sh
export DEBUG_KEYSFILE=keys.debug.json
export RELEASE_KEYSFILE=keys.staging.json
// DEBUG_KEYSFILE will choose env file
export KEYSFILE=keys.production.json

// if you wannna use different keys for same scheme
export DEBUG_KEYSFILE=keys.debug.json //in running metro
export RELEASE_KEYSFILE=keys.staging.json // in IPA

#above DEBUG_KEYSFILE and RELEASE_KEYSFILE variable are optional

Expand Down
17 changes: 0 additions & 17 deletions android/.project

This file was deleted.

13 changes: 0 additions & 13 deletions android/.settings/org.eclipse.buildship.core.prefs

This file was deleted.

62 changes: 62 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
project("react-native-keys")
cmake_minimum_required(VERSION 3.9.0)

set(PACKAGE_NAME "cpp")
set(BUILD_DIR ./build)
set(CMAKE_CXX_STANDARD 17)

# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)

include_directories(
${PACKAGE_NAME}
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"./cpp"
${FOLLY_INCLUDE_DIR}
"."
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule"
)

file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")
message(STATUS "LIBRN_DIR: ${LIBRN_DIR}")

add_library(
${PACKAGE_NAME}
SHARED
./cpp/mediator.cpp
./cpp/crypto.cpp
./cpp-adapter.cpp
)

find_library(
LOG_LIB
log
)

find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

set_target_properties(
${PACKAGE_NAME} PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::turbomodulejsijni
fbjni::fbjni
${LOG_LIB}
ReactAndroid::jsi
ReactAndroid::reactnativejni
ReactAndroid::react_nativemodule_core
android
)
6 changes: 3 additions & 3 deletions android/RNKeys.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def getCurrentFlavor() {

def loadKeys(){
def flavor = getCurrentFlavor()
def keysFile = "keys.json"
def keysFile = "keys.development.json"
def exportCommand="";
if (System.env['KEYSFILE']) {
keysFile = System.env['KEYSFILE']
Expand All @@ -51,7 +51,7 @@ def loadKeys(){
def proc =["/bin/sh", "-c", exportCommand].execute()
proc.consumeProcessOutput(System.out, System.err)

////
println(keysFile)
def keys = [:]
File f = new File("$project.rootDir/../$keysFile");
if (!f.exists()) {
Expand All @@ -63,7 +63,7 @@ def loadKeys(){

if (f.exists()) {
def parsedJson = new groovy.json.JsonSlurper().parseText(f.text)
println(parsedJson.public)
// println(parsedJson.public)
parsedJson.public.each { key, value ->
keys.put(key, value.replace('"', '\\"'))
}
Expand Down
Loading

0 comments on commit 672289d

Please sign in to comment.