@onekeyfe/react-native-keychain-module 1.1.6
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.
- package/KeychainModule.podspec +29 -0
- package/LICENSE +20 -0
- package/README.md +39 -0
- package/android/CMakeLists.txt +24 -0
- package/android/build.gradle +128 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/cpp-adapter.cpp +6 -0
- package/android/src/main/java/com/margelo/nitro/keychainmodule/KeychainModule.kt +28 -0
- package/android/src/main/java/com/margelo/nitro/keychainmodule/KeychainModulePackage.kt +22 -0
- package/ios/KeychainModule.swift +81 -0
- package/ios/KeychainModuleCore.swift +174 -0
- package/lib/module/KeychainModule.nitro.js +4 -0
- package/lib/module/KeychainModule.nitro.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/KeychainModule.nitro.d.ts +32 -0
- package/lib/typescript/src/KeychainModule.nitro.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/nitro.json +17 -0
- package/nitrogen/generated/android/c++/JGetItemParams.hpp +57 -0
- package/nitrogen/generated/android/c++/JGetItemResult.hpp +61 -0
- package/nitrogen/generated/android/c++/JHasItemParams.hpp +57 -0
- package/nitrogen/generated/android/c++/JHybridKeychainModuleSpec.cpp +153 -0
- package/nitrogen/generated/android/c++/JHybridKeychainModuleSpec.hpp +69 -0
- package/nitrogen/generated/android/c++/JRemoveItemParams.hpp +57 -0
- package/nitrogen/generated/android/c++/JSetItemParams.hpp +74 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_GetItemResult.cpp +26 -0
- package/nitrogen/generated/android/c++/JVariant_NullType_GetItemResult.hpp +72 -0
- package/nitrogen/generated/android/keychainmodule+autolinking.cmake +82 -0
- package/nitrogen/generated/android/keychainmodule+autolinking.gradle +27 -0
- package/nitrogen/generated/android/keychainmoduleOnLoad.cpp +44 -0
- package/nitrogen/generated/android/keychainmoduleOnLoad.hpp +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/GetItemParams.kt +38 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/GetItemResult.kt +41 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/HasItemParams.kt +38 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/HybridKeychainModuleSpec.kt +75 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/RemoveItemParams.kt +38 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/SetItemParams.kt +50 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/Variant_NullType_GetItemResult.kt +59 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/keychainmodule/keychainmoduleOnLoad.kt +35 -0
- package/nitrogen/generated/ios/KeychainModule+autolinking.rb +60 -0
- package/nitrogen/generated/ios/KeychainModule-Swift-Cxx-Bridge.cpp +57 -0
- package/nitrogen/generated/ios/KeychainModule-Swift-Cxx-Bridge.hpp +219 -0
- package/nitrogen/generated/ios/KeychainModule-Swift-Cxx-Umbrella.hpp +63 -0
- package/nitrogen/generated/ios/KeychainModuleAutolinking.mm +33 -0
- package/nitrogen/generated/ios/KeychainModuleAutolinking.swift +25 -0
- package/nitrogen/generated/ios/c++/HybridKeychainModuleSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridKeychainModuleSpecSwift.hpp +126 -0
- package/nitrogen/generated/ios/swift/Func_void_bool.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +47 -0
- package/nitrogen/generated/ios/swift/Func_void_std__variant_nitro__NullType__GetItemResult_.swift +59 -0
- package/nitrogen/generated/ios/swift/GetItemParams.swift +36 -0
- package/nitrogen/generated/ios/swift/GetItemResult.swift +47 -0
- package/nitrogen/generated/ios/swift/HasItemParams.swift +36 -0
- package/nitrogen/generated/ios/swift/HybridKeychainModuleSpec.swift +60 -0
- package/nitrogen/generated/ios/swift/HybridKeychainModuleSpec_cxx.swift +221 -0
- package/nitrogen/generated/ios/swift/RemoveItemParams.swift +36 -0
- package/nitrogen/generated/ios/swift/SetItemParams.swift +137 -0
- package/nitrogen/generated/ios/swift/Variant_NullType_GetItemResult.swift +18 -0
- package/nitrogen/generated/shared/c++/GetItemParams.hpp +75 -0
- package/nitrogen/generated/shared/c++/GetItemResult.hpp +79 -0
- package/nitrogen/generated/shared/c++/HasItemParams.hpp +75 -0
- package/nitrogen/generated/shared/c++/HybridKeychainModuleSpec.cpp +25 -0
- package/nitrogen/generated/shared/c++/HybridKeychainModuleSpec.hpp +82 -0
- package/nitrogen/generated/shared/c++/RemoveItemParams.hpp +75 -0
- package/nitrogen/generated/shared/c++/SetItemParams.hpp +92 -0
- package/package.json +174 -0
- package/src/KeychainModule.nitro.ts +35 -0
- package/src/index.tsx +8 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "KeychainModule"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
14
|
+
s.source = { :git => "https://github.com/OneKeyHQ/app-modules/react-native-keychain-module.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = [
|
|
17
|
+
"ios/**/*.{swift}",
|
|
18
|
+
"ios/**/*.{m,mm}",
|
|
19
|
+
"cpp/**/*.{hpp,cpp}",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
s.dependency 'React-jsi'
|
|
23
|
+
s.dependency 'React-callinvoker'
|
|
24
|
+
|
|
25
|
+
load 'nitrogen/generated/ios/KeychainModule+autolinking.rb'
|
|
26
|
+
add_nitrogen_files(s)
|
|
27
|
+
|
|
28
|
+
install_modules_dependencies(s)
|
|
29
|
+
end
|
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 onekeyfe
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# react-native-keychain-module
|
|
2
|
+
|
|
3
|
+
react-native-keychain-module
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install react-native-keychain-module react-native-nitro-modules
|
|
10
|
+
|
|
11
|
+
> `react-native-nitro-modules` is required as this library relies on [Nitro Modules](https://nitro.margelo.com/).
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { multiply } from 'react-native-keychain-module';
|
|
20
|
+
|
|
21
|
+
// ...
|
|
22
|
+
|
|
23
|
+
const result = multiply(3, 7);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
- [Development workflow](CONTRIBUTING.md#development-workflow)
|
|
30
|
+
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
31
|
+
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
32
|
+
|
|
33
|
+
## License
|
|
34
|
+
|
|
35
|
+
MIT
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
project(keychainmodule)
|
|
2
|
+
cmake_minimum_required(VERSION 3.9.0)
|
|
3
|
+
|
|
4
|
+
set(PACKAGE_NAME keychainmodule)
|
|
5
|
+
set(CMAKE_VERBOSE_MAKEFILE ON)
|
|
6
|
+
set(CMAKE_CXX_STANDARD 20)
|
|
7
|
+
|
|
8
|
+
# Define C++ library and add all sources
|
|
9
|
+
add_library(${PACKAGE_NAME} SHARED src/main/cpp/cpp-adapter.cpp)
|
|
10
|
+
|
|
11
|
+
# Add Nitrogen specs :)
|
|
12
|
+
include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/keychainmodule+autolinking.cmake)
|
|
13
|
+
|
|
14
|
+
# Set up local includes
|
|
15
|
+
include_directories("src/main/cpp" "../cpp")
|
|
16
|
+
|
|
17
|
+
find_library(LOG_LIB log)
|
|
18
|
+
|
|
19
|
+
# Link all libraries together
|
|
20
|
+
target_link_libraries(
|
|
21
|
+
${PACKAGE_NAME}
|
|
22
|
+
${LOG_LIB}
|
|
23
|
+
android # <-- Android core
|
|
24
|
+
)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
ext.getExtOrDefault = {name ->
|
|
3
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['KeychainModule_' + name]
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
repositories {
|
|
7
|
+
google()
|
|
8
|
+
mavenCentral()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
dependencies {
|
|
12
|
+
classpath "com.android.tools.build:gradle:8.7.2"
|
|
13
|
+
// noinspection DifferentKotlinGradleVersion
|
|
14
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
def reactNativeArchitectures() {
|
|
19
|
+
def value = rootProject.getProperties().get("reactNativeArchitectures")
|
|
20
|
+
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
apply plugin: "com.android.library"
|
|
24
|
+
apply plugin: "kotlin-android"
|
|
25
|
+
apply from: '../nitrogen/generated/android/keychainmodule+autolinking.gradle'
|
|
26
|
+
|
|
27
|
+
apply plugin: "com.facebook.react"
|
|
28
|
+
|
|
29
|
+
def getExtOrIntegerDefault(name) {
|
|
30
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["KeychainModule_" + name]).toInteger()
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
android {
|
|
34
|
+
namespace "com.margelo.nitro.keychainmodule"
|
|
35
|
+
|
|
36
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
37
|
+
|
|
38
|
+
defaultConfig {
|
|
39
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
40
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
41
|
+
|
|
42
|
+
externalNativeBuild {
|
|
43
|
+
cmake {
|
|
44
|
+
cppFlags "-frtti -fexceptions -Wall -fstack-protector-all"
|
|
45
|
+
arguments "-DANDROID_STL=c++_shared", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
|
46
|
+
abiFilters (*reactNativeArchitectures())
|
|
47
|
+
|
|
48
|
+
buildTypes {
|
|
49
|
+
debug {
|
|
50
|
+
cppFlags "-O1 -g"
|
|
51
|
+
}
|
|
52
|
+
release {
|
|
53
|
+
cppFlags "-O2"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
externalNativeBuild {
|
|
61
|
+
cmake {
|
|
62
|
+
path "CMakeLists.txt"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
packagingOptions {
|
|
67
|
+
excludes = [
|
|
68
|
+
"META-INF",
|
|
69
|
+
"META-INF/**",
|
|
70
|
+
"**/libc++_shared.so",
|
|
71
|
+
"**/libfbjni.so",
|
|
72
|
+
"**/libjsi.so",
|
|
73
|
+
"**/libfolly_json.so",
|
|
74
|
+
"**/libfolly_runtime.so",
|
|
75
|
+
"**/libglog.so",
|
|
76
|
+
"**/libhermes.so",
|
|
77
|
+
"**/libhermes-executor-debug.so",
|
|
78
|
+
"**/libhermes_executor.so",
|
|
79
|
+
"**/libreactnative.so",
|
|
80
|
+
"**/libreactnativejni.so",
|
|
81
|
+
"**/libturbomodulejsijni.so",
|
|
82
|
+
"**/libreact_nativemodule_core.so",
|
|
83
|
+
"**/libjscexecutor.so"
|
|
84
|
+
]
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
buildFeatures {
|
|
88
|
+
buildConfig true
|
|
89
|
+
prefab true
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
buildTypes {
|
|
93
|
+
release {
|
|
94
|
+
minifyEnabled false
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lintOptions {
|
|
99
|
+
disable "GradleCompatible"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
compileOptions {
|
|
103
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
104
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sourceSets {
|
|
108
|
+
main {
|
|
109
|
+
java.srcDirs += [
|
|
110
|
+
"generated/java",
|
|
111
|
+
"generated/jni"
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
repositories {
|
|
118
|
+
mavenCentral()
|
|
119
|
+
google()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
123
|
+
|
|
124
|
+
dependencies {
|
|
125
|
+
implementation "com.facebook.react:react-android"
|
|
126
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
127
|
+
implementation project(":react-native-nitro-modules")
|
|
128
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package com.margelo.nitro.keychainmodule
|
|
2
|
+
|
|
3
|
+
import com.facebook.proguard.annotations.DoNotStrip
|
|
4
|
+
import com.margelo.nitro.core.NullType
|
|
5
|
+
import com.margelo.nitro.core.Promise
|
|
6
|
+
|
|
7
|
+
@DoNotStrip
|
|
8
|
+
class KeychainModule : HybridKeychainModuleSpec() {
|
|
9
|
+
override fun setItem(params: SetItemParams): Promise<Boolean> {
|
|
10
|
+
return Promise.resolved(false)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override fun getItem(params: GetItemParams): Promise<Variant_NullType_GetItemResult> {
|
|
14
|
+
return Promise.resolved(Variant_NullType_GetItemResult.First(NullType.NULL))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override fun removeItem(params: RemoveItemParams): Promise<Boolean> {
|
|
18
|
+
return Promise.resolved(false)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override fun hasItem(params: HasItemParams): Promise<Boolean> {
|
|
22
|
+
return Promise.resolved(false)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
override fun isICloudSyncEnabled(): Promise<Boolean> {
|
|
26
|
+
return Promise.resolved(false)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
package com.margelo.nitro.keychainmodule
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
|
|
8
|
+
class KeychainModulePackage : BaseReactPackage() {
|
|
9
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
10
|
+
return null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
14
|
+
return ReactModuleInfoProvider { HashMap() }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
companion object {
|
|
18
|
+
init {
|
|
19
|
+
System.loadLibrary("keychainmodule")
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import NitroModules
|
|
2
|
+
|
|
3
|
+
class KeychainModule: HybridKeychainModuleSpec {
|
|
4
|
+
|
|
5
|
+
private let moduleCore = KeychainModuleCore()
|
|
6
|
+
|
|
7
|
+
public func setItem(params: SetItemParams) throws -> Promise<Bool> {
|
|
8
|
+
let typedParams = params
|
|
9
|
+
do {
|
|
10
|
+
try moduleCore.setItem(params: typedParams)
|
|
11
|
+
return Promise.resolved(withResult: true)
|
|
12
|
+
} catch let error as KeychainModuleError {
|
|
13
|
+
switch error {
|
|
14
|
+
case .encodingFailed:
|
|
15
|
+
return Promise.rejected(withError: NSError(domain: "keychain_set_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to encode value"]))
|
|
16
|
+
case .operationFailed(let status):
|
|
17
|
+
return Promise.rejected(withError: NSError(domain: NSOSStatusErrorDomain, code: Int(status), userInfo: nil))
|
|
18
|
+
default:
|
|
19
|
+
return Promise.rejected(withError: NSError(domain: "keychain_set_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to set keychain item"]))
|
|
20
|
+
}
|
|
21
|
+
} catch {
|
|
22
|
+
return Promise.rejected(withError: NSError(domain: "keychain_set_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to set keychain item", NSUnderlyingErrorKey: error]))
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public func getItem(params : GetItemParams) throws -> Promise<Variant_NullType_GetItemResult> {
|
|
27
|
+
let typedParams = params
|
|
28
|
+
do {
|
|
29
|
+
if let result = try moduleCore.getItem(params: typedParams) {
|
|
30
|
+
return Promise.resolved(withResult: Variant_NullType_GetItemResult.second(result))
|
|
31
|
+
} else {
|
|
32
|
+
return Promise.resolved(withResult: Variant_NullType_GetItemResult.first(NullType.null))
|
|
33
|
+
}
|
|
34
|
+
} catch let error as KeychainModuleError {
|
|
35
|
+
switch error {
|
|
36
|
+
case .operationFailed(let status):
|
|
37
|
+
return Promise.rejected(withError: NSError(domain: NSOSStatusErrorDomain, code: Int(status), userInfo: [NSLocalizedDescriptionKey: "Failed to get keychain item: \(status)"]))
|
|
38
|
+
default:
|
|
39
|
+
return Promise.rejected(withError: NSError(domain: "keychain_get_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to get keychain item", NSUnderlyingErrorKey: error as NSError]))
|
|
40
|
+
}
|
|
41
|
+
} catch {
|
|
42
|
+
return Promise.rejected(withError: NSError(domain: "keychain_get_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to get keychain item", NSUnderlyingErrorKey: error]))
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public func removeItem(params: RemoveItemParams) throws -> Promise<Bool> {
|
|
47
|
+
let typedParams = params
|
|
48
|
+
do {
|
|
49
|
+
try moduleCore.removeItem(params: typedParams)
|
|
50
|
+
return Promise.resolved(withResult: true)
|
|
51
|
+
} catch let error as KeychainModuleError {
|
|
52
|
+
switch error {
|
|
53
|
+
case .operationFailed(let status):
|
|
54
|
+
return Promise.rejected(withError: NSError(domain: NSOSStatusErrorDomain, code: Int(status), userInfo: [NSLocalizedDescriptionKey: "Failed to remove keychain item: \(status)"]))
|
|
55
|
+
default:
|
|
56
|
+
return Promise.rejected(withError: NSError(domain: "keychain_remove_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to remove keychain item", NSUnderlyingErrorKey: error as NSError]))
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
return Promise.rejected(withError: NSError(domain: "keychain_remove_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to remove keychain item", NSUnderlyingErrorKey: error]))
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public func hasItem(params: HasItemParams) throws -> Promise<Bool> {
|
|
64
|
+
let typedParams = params
|
|
65
|
+
do {
|
|
66
|
+
let exists = try moduleCore.hasItem(params: typedParams)
|
|
67
|
+
return Promise.resolved(withResult: exists)
|
|
68
|
+
} catch {
|
|
69
|
+
return Promise.rejected(withError: NSError(domain: "keychain_has_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to check keychain item", NSUnderlyingErrorKey: error]))
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public func isICloudSyncEnabled() throws -> Promise<Bool> {
|
|
74
|
+
do {
|
|
75
|
+
let enabled = try moduleCore.isICloudSyncEnabled()
|
|
76
|
+
return Promise.resolved(withResult: enabled)
|
|
77
|
+
} catch {
|
|
78
|
+
return Promise.rejected(withError: NSError(domain: "keychain_sync_check_error", code: -1000, userInfo: [NSLocalizedDescriptionKey: "Failed to check iCloud sync status", NSUnderlyingErrorKey: error]))
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
//
|
|
2
|
+
// KeychainModuleCore.swift
|
|
3
|
+
// OneKeyWallet
|
|
4
|
+
//
|
|
5
|
+
// Created by OneKey on 2025-01-27.
|
|
6
|
+
// Core implementation layer for Keychain operations (React Native independent)
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import Foundation
|
|
10
|
+
import Security
|
|
11
|
+
|
|
12
|
+
// MARK: - Constants
|
|
13
|
+
|
|
14
|
+
struct KeychainConstants {
|
|
15
|
+
// DO NOT Change this value, otherwise user cannot access the keychain items
|
|
16
|
+
static let serviceIdentifier = Bundle.main.bundleIdentifier
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// MARK: - Error Types
|
|
20
|
+
|
|
21
|
+
enum KeychainModuleError: Error {
|
|
22
|
+
case invalidParameters(String)
|
|
23
|
+
case encodingFailed
|
|
24
|
+
case operationFailed(OSStatus)
|
|
25
|
+
case itemNotFound
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// MARK: - Keychain Module Core
|
|
29
|
+
|
|
30
|
+
class KeychainModuleCore {
|
|
31
|
+
|
|
32
|
+
// MARK: - Set Item
|
|
33
|
+
|
|
34
|
+
func setItem(params: SetItemParams) throws {
|
|
35
|
+
guard let valueData = params.value.data(using: .utf8) else {
|
|
36
|
+
throw KeychainModuleError.encodingFailed
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let enableSync = params.enableSync ?? true // Default to enabled for iCloud sync
|
|
40
|
+
|
|
41
|
+
var query: [String: Any] = [
|
|
42
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
43
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
44
|
+
kSecAttrAccount as String: params.key,
|
|
45
|
+
kSecValueData as String: valueData,
|
|
46
|
+
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked,
|
|
47
|
+
kSecAttrSynchronizable as String: enableSync // Use parameter value
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
// Optional: Set label for friendly display in Keychain Access app
|
|
51
|
+
if let label = params.label {
|
|
52
|
+
query[kSecAttrLabel as String] = label
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Optional: Set description for additional context
|
|
56
|
+
if let description = params.description {
|
|
57
|
+
query[kSecAttrDescription as String] = description
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Delete existing item if it exists
|
|
61
|
+
SecItemDelete(query as CFDictionary)
|
|
62
|
+
|
|
63
|
+
// Add new item
|
|
64
|
+
let status = SecItemAdd(query as CFDictionary, nil)
|
|
65
|
+
|
|
66
|
+
guard status == errSecSuccess else {
|
|
67
|
+
throw KeychainModuleError.operationFailed(status)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// MARK: - Get Item
|
|
72
|
+
|
|
73
|
+
func getItem(params: GetItemParams) throws -> GetItemResult? {
|
|
74
|
+
let query: [String: Any] = [
|
|
75
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
76
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
77
|
+
kSecAttrAccount as String: params.key,
|
|
78
|
+
kSecReturnData as String: true,
|
|
79
|
+
kSecMatchLimit as String: kSecMatchLimitOne,
|
|
80
|
+
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny // Query both local and synced items
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
var result: AnyObject?
|
|
84
|
+
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
|
85
|
+
|
|
86
|
+
if status == errSecSuccess {
|
|
87
|
+
if let valueData = result as? Data,
|
|
88
|
+
let value = String(data: valueData, encoding: .utf8) {
|
|
89
|
+
return GetItemResult(key: params.key, value: value)
|
|
90
|
+
}
|
|
91
|
+
return nil
|
|
92
|
+
} else if status == errSecItemNotFound {
|
|
93
|
+
return nil
|
|
94
|
+
} else {
|
|
95
|
+
throw KeychainModuleError.operationFailed(status)
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// MARK: - Remove Item
|
|
100
|
+
|
|
101
|
+
func removeItem(params: RemoveItemParams) throws {
|
|
102
|
+
let query: [String: Any] = [
|
|
103
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
104
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
105
|
+
kSecAttrAccount as String: params.key,
|
|
106
|
+
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny // Delete both local and synced items
|
|
107
|
+
]
|
|
108
|
+
|
|
109
|
+
let status = SecItemDelete(query as CFDictionary)
|
|
110
|
+
|
|
111
|
+
// Both success and item not found are acceptable for delete
|
|
112
|
+
guard status == errSecSuccess || status == errSecItemNotFound else {
|
|
113
|
+
throw KeychainModuleError.operationFailed(status)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// MARK: - Check Item Existence
|
|
118
|
+
|
|
119
|
+
func hasItem(params: HasItemParams) throws -> Bool {
|
|
120
|
+
let query: [String: Any] = [
|
|
121
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
122
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
123
|
+
kSecAttrAccount as String: params.key,
|
|
124
|
+
kSecReturnData as String: false,
|
|
125
|
+
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny // Check both local and synced items
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
let status = SecItemCopyMatching(query as CFDictionary, nil)
|
|
129
|
+
return status == errSecSuccess
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// MARK: - Check iCloud Keychain Sync Status
|
|
133
|
+
|
|
134
|
+
func isICloudSyncEnabled() throws -> Bool {
|
|
135
|
+
// Create a test key with synchronizable attribute
|
|
136
|
+
let testKey = "__onekey_icloud_sync_test__"
|
|
137
|
+
let testValue = "test"
|
|
138
|
+
|
|
139
|
+
guard let valueData = testValue.data(using: .utf8) else {
|
|
140
|
+
throw KeychainModuleError.encodingFailed
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// First, clean up any existing test key
|
|
144
|
+
let deleteQuery: [String: Any] = [
|
|
145
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
146
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
147
|
+
kSecAttrAccount as String: testKey,
|
|
148
|
+
kSecAttrSynchronizable as String: kSecAttrSynchronizableAny
|
|
149
|
+
]
|
|
150
|
+
SecItemDelete(deleteQuery as CFDictionary)
|
|
151
|
+
|
|
152
|
+
// Try to add a synchronizable item
|
|
153
|
+
let addQuery: [String: Any] = [
|
|
154
|
+
kSecClass as String: kSecClassGenericPassword,
|
|
155
|
+
kSecAttrService as String: KeychainConstants.serviceIdentifier ?? "",
|
|
156
|
+
kSecAttrAccount as String: testKey,
|
|
157
|
+
kSecValueData as String: valueData,
|
|
158
|
+
kSecAttrAccessible as String: kSecAttrAccessibleWhenUnlocked,
|
|
159
|
+
kSecAttrSynchronizable as String: true // Try to enable sync
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
let addStatus = SecItemAdd(addQuery as CFDictionary, nil)
|
|
163
|
+
|
|
164
|
+
// Clean up test item
|
|
165
|
+
SecItemDelete(deleteQuery as CFDictionary)
|
|
166
|
+
|
|
167
|
+
// Successfully added synchronizable item - iCloud Keychain is available
|
|
168
|
+
// Failed to add synchronizable item - iCloud Keychain may be disabled
|
|
169
|
+
// Common error codes:
|
|
170
|
+
// errSecMissingEntitlement (-34018): Missing iCloud Keychain entitlement
|
|
171
|
+
// errSecNotAvailable (-25291): iCloud Keychain not available/signed out
|
|
172
|
+
return addStatus == errSecSuccess
|
|
173
|
+
}
|
|
174
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../src","sources":["KeychainModule.nitro.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NitroModules","KeychainModuleHybridObject","createHybridObject","KeychainModule"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;AAAA,SAASA,YAAY,QAAQ,4BAA4B;AAGzD,MAAMC,0BAA0B,GAC9BD,YAAY,CAACE,kBAAkB,CAAqB,gBAAgB,CAAC;AAEvE,OAAO,MAAMC,cAAc,GAAGF,0BAA0B","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HybridObject } from 'react-native-nitro-modules';
|
|
2
|
+
export interface SetItemParams {
|
|
3
|
+
key: string;
|
|
4
|
+
value: string;
|
|
5
|
+
enableSync?: boolean;
|
|
6
|
+
label?: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface GetItemParams {
|
|
10
|
+
key: string;
|
|
11
|
+
}
|
|
12
|
+
export interface RemoveItemParams {
|
|
13
|
+
key: string;
|
|
14
|
+
}
|
|
15
|
+
export interface HasItemParams {
|
|
16
|
+
key: string;
|
|
17
|
+
}
|
|
18
|
+
export interface GetItemResult {
|
|
19
|
+
key: string;
|
|
20
|
+
value: string;
|
|
21
|
+
}
|
|
22
|
+
export interface KeychainModule extends HybridObject<{
|
|
23
|
+
ios: 'swift';
|
|
24
|
+
android: 'kotlin';
|
|
25
|
+
}> {
|
|
26
|
+
setItem(params: SetItemParams): Promise<boolean>;
|
|
27
|
+
getItem(params: GetItemParams): Promise<GetItemResult | null>;
|
|
28
|
+
removeItem(params: RemoveItemParams): Promise<boolean>;
|
|
29
|
+
hasItem(params: HasItemParams): Promise<boolean>;
|
|
30
|
+
isICloudSyncEnabled(): Promise<boolean>;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=KeychainModule.nitro.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KeychainModule.nitro.d.ts","sourceRoot":"","sources":["../../../src/KeychainModule.nitro.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAE/D,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,cACf,SAAQ,YAAY,CAAC;IAAE,GAAG,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,QAAQ,CAAA;CAAE,CAAC;IACzD,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACjD,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAC;IAC9D,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACvD,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACjD,mBAAmB,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CACzC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,IAAI,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAKnF,eAAO,MAAM,cAAc,oBAA6B,CAAC;AACzD,mBAAmB,wBAAwB,CAAC"}
|
package/nitro.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cxxNamespace": ["keychainmodule"],
|
|
3
|
+
"ios": {
|
|
4
|
+
"iosModuleName": "KeychainModule"
|
|
5
|
+
},
|
|
6
|
+
"android": {
|
|
7
|
+
"androidNamespace": ["keychainmodule"],
|
|
8
|
+
"androidCxxLibName": "keychainmodule"
|
|
9
|
+
},
|
|
10
|
+
"autolinking": {
|
|
11
|
+
"KeychainModule": {
|
|
12
|
+
"swift": "KeychainModule",
|
|
13
|
+
"kotlin": "KeychainModule"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"ignorePaths": ["node_modules"]
|
|
17
|
+
}
|