@onekeyfe/react-native-lite-card 1.0.18 → 1.1.2

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 (65) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +37 -0
  3. package/ReactNativeLiteCard.podspec +22 -0
  4. package/android/build.gradle +21 -44
  5. package/android/gradle.properties +5 -5
  6. package/android/src/main/AndroidManifest.xml +1 -2
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +420 -0
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardPackage.kt +33 -0
  9. package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/OnekeyLiteCard.kt +7 -7
  10. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  11. package/ios/ReactNativeLiteCard.h +5 -0
  12. package/ios/{Classes/OKLiteManager.m → ReactNativeLiteCard.mm} +39 -25
  13. package/lib/module/{types.js → NativeReactNativeLiteCard.js} +5 -1
  14. package/lib/module/NativeReactNativeLiteCard.js.map +1 -0
  15. package/lib/module/index.js +12 -16
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/package.json +1 -0
  18. package/lib/typescript/package.json +1 -0
  19. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +59 -0
  20. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts.map +1 -0
  21. package/lib/typescript/src/index.d.ts +6 -8
  22. package/lib/typescript/src/index.d.ts.map +1 -1
  23. package/package.json +88 -86
  24. package/src/NativeReactNativeLiteCard.ts +112 -0
  25. package/src/{index.ts → index.tsx} +16 -25
  26. package/LICENSE.md +0 -51
  27. package/android/src/main/java/so/onekey/app/wallet/lite/OKLiteManager.kt +0 -431
  28. package/android/src/main/java/so/onekey/app/wallet/lite/OKLitePackage.kt +0 -16
  29. package/ios/Classes/OKLiteManager.h +0 -17
  30. package/keys/keys.c +0 -51
  31. package/keys/keys.h +0 -4
  32. package/lib/commonjs/index.js +0 -81
  33. package/lib/commonjs/index.js.map +0 -1
  34. package/lib/commonjs/types.js +0 -27
  35. package/lib/commonjs/types.js.map +0 -1
  36. package/lib/module/types.js.map +0 -1
  37. package/lib/typescript/src/types.d.ts +0 -40
  38. package/lib/typescript/src/types.d.ts.map +0 -1
  39. package/react-native-lite-card.podspec +0 -42
  40. package/src/types.ts +0 -43
  41. /package/android/src/main/{assets/config → config}/command.json +0 -0
  42. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/LoggerManager.kt +0 -0
  43. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/keys/KeysNativeProvider.kt +0 -0
  44. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/nfc/Exceptions.kt +0 -0
  45. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/nfc/NfcUtils.kt +0 -0
  46. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/nfc/broadcast/NfcStatusChangeBroadcastReceiver.kt +0 -0
  47. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/nfc/gpchannel/GPChannelNatives.kt +0 -0
  48. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/NfcConstant.kt +0 -0
  49. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/APDUParam.kt +0 -0
  50. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/CardInfo.java +0 -0
  51. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/CardResponse.java +0 -0
  52. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/CardState.kt +0 -0
  53. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/ParsedCertInfo.java +0 -0
  54. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/SecureChanelParam.java +0 -0
  55. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/entitys/SendResponse.kt +0 -0
  56. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/nfc/CommandGenerator.kt +0 -0
  57. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/nfc/Connection.kt +0 -0
  58. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/onekeyLite/nfc/GPCAPDUGenerator.kt +0 -0
  59. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/EventUtils.kt +0 -0
  60. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/GpsUtil.kt +0 -0
  61. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/HexUtils.java +0 -0
  62. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/LogUtil.kt +0 -0
  63. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/MiUtil.kt +0 -0
  64. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/NfcPermissionUtils.kt +0 -0
  65. /package/android/src/main/java/{so/onekey/app/wallet/lite → com/onekeyfe/reactnativelitecard}/utils/Utils.java +0 -0
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,37 @@
1
+ # @onekeyfe/react-native-lite-card
2
+
3
+ lite card
4
+
5
+ ## Installation
6
+
7
+
8
+ ```sh
9
+ npm install @onekeyfe/react-native-lite-card
10
+ ```
11
+
12
+
13
+ ## Usage
14
+
15
+
16
+ ```js
17
+ import { multiply } from '@onekeyfe/react-native-lite-card';
18
+
19
+ // ...
20
+
21
+ const result = multiply(3, 7);
22
+ ```
23
+
24
+
25
+ ## Contributing
26
+
27
+ - [Development workflow](CONTRIBUTING.md#development-workflow)
28
+ - [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
29
+ - [Code of conduct](CODE_OF_CONDUCT.md)
30
+
31
+ ## License
32
+
33
+ MIT
34
+
35
+ ---
36
+
37
+ Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
@@ -0,0 +1,22 @@
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 = "ReactNativeLiteCard"
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/onekeyfe-react-native-lite-card.git", :tag => "#{s.version}" }
15
+
16
+ s.source_files = "ios/**/*.{h,m,mm,swift,cpp}", "keys/**/*.{h,c,m,mm,swift,cpp}"
17
+ s.vendored_framework = "ios/GPChannelSDKCore.xcframework"
18
+ s.private_header_files = "ios/**/*.h"
19
+
20
+
21
+ install_modules_dependencies(s)
22
+ end
@@ -1,6 +1,7 @@
1
1
  buildscript {
2
- // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3
- def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["LiteCard_kotlinVersion"]
2
+ ext.getExtOrDefault = {name ->
3
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ReactNativeLiteCard_' + name]
4
+ }
4
5
 
5
6
  repositories {
6
7
  google()
@@ -8,63 +9,34 @@ buildscript {
8
9
  }
9
10
 
10
11
  dependencies {
11
- classpath "com.android.tools.build:gradle:8.0.2"
12
+ classpath "com.android.tools.build:gradle:8.7.2"
12
13
  // noinspection DifferentKotlinGradleVersion
13
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
14
15
  }
15
16
  }
16
17
 
17
- def isNewArchitectureEnabled() {
18
- return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
19
- }
20
18
 
21
19
  apply plugin: "com.android.library"
22
20
  apply plugin: "kotlin-android"
23
21
 
24
- if (isNewArchitectureEnabled()) {
25
- apply plugin: "com.facebook.react"
26
- }
27
-
28
- def getExtOrDefault(name) {
29
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["LiteCard_" + name]
30
- }
22
+ apply plugin: "com.facebook.react"
31
23
 
32
24
  def getExtOrIntegerDefault(name) {
33
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["LiteCard_" + name]).toInteger()
34
- }
35
-
36
- def supportsNamespace() {
37
- def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
38
- def major = parsed[0].toInteger()
39
- def minor = parsed[1].toInteger()
40
-
41
- // Namespace support was added in 7.3.0
42
- return (major == 7 && minor >= 3) || major >= 8
25
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ReactNativeLiteCard_" + name]).toInteger()
43
26
  }
44
27
 
45
28
  android {
46
- if (supportsNamespace()) {
47
- namespace "so.onekey.app.wallet.lite"
48
-
49
- sourceSets {
50
- main {
51
- manifest.srcFile "src/main/AndroidManifestNew.xml"
52
- }
53
- }
54
- }
55
-
56
- externalNativeBuild {
57
- cmake {
58
- path "src/main/cpp/CMakeLists.txt"
59
- }
60
- }
29
+ namespace "com.onekeyfe.reactnativelitecard"
61
30
 
62
31
  compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
63
32
 
64
33
  defaultConfig {
65
34
  minSdkVersion getExtOrIntegerDefault("minSdkVersion")
66
35
  targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
36
+ }
67
37
 
38
+ buildFeatures {
39
+ buildConfig true
68
40
  }
69
41
 
70
42
  buildTypes {
@@ -81,6 +53,15 @@ android {
81
53
  sourceCompatibility JavaVersion.VERSION_1_8
82
54
  targetCompatibility JavaVersion.VERSION_1_8
83
55
  }
56
+
57
+ sourceSets {
58
+ main {
59
+ java.srcDirs += [
60
+ "generated/java",
61
+ "generated/jni"
62
+ ]
63
+ }
64
+ }
84
65
  }
85
66
 
86
67
  repositories {
@@ -91,12 +72,8 @@ repositories {
91
72
  def kotlin_version = getExtOrDefault("kotlinVersion")
92
73
 
93
74
  dependencies {
94
- // For < 0.71, this will be from the local maven repo
95
- // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
96
- //noinspection GradleDynamicVersion
97
- implementation "com.facebook.react:react-native:+"
75
+ implementation "com.facebook.react:react-android"
98
76
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
99
77
  implementation 'com.google.code.gson:gson:2.10.1'
100
78
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
101
79
  }
102
-
@@ -1,5 +1,5 @@
1
- LiteCard_kotlinVersion=2.1.20
2
- LiteCard_minSdkVersion=21
3
- LiteCard_targetSdkVersion=35
4
- LiteCard_compileSdkVersion=35
5
- LiteCard_ndkversion=26.1.10909125
1
+ ReactNativeLiteCard_kotlinVersion=2.0.21
2
+ ReactNativeLiteCard_minSdkVersion=24
3
+ ReactNativeLiteCard_targetSdkVersion=34
4
+ ReactNativeLiteCard_compileSdkVersion=35
5
+ ReactNativeLiteCard_ndkVersion=27.1.12297006
@@ -1,3 +1,2 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="so.onekey.app.wallet.lite">
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
2
  </manifest>
@@ -0,0 +1,420 @@
1
+ package com.onekeyfe.reactnativelitecard
2
+
3
+ import android.content.Intent
4
+ import android.nfc.NfcAdapter
5
+ import android.nfc.Tag
6
+ import android.nfc.tech.IsoDep
7
+ import android.util.Log
8
+ import androidx.annotation.IntDef
9
+ import androidx.fragment.app.FragmentActivity
10
+ import kotlinx.coroutines.*
11
+ import kotlinx.coroutines.channels.Channel
12
+ import com.facebook.react.bridge.Callback
13
+ import com.facebook.react.bridge.ReactApplicationContext
14
+ import com.facebook.react.module.annotations.ReactModule
15
+ import so.onekey.app.wallet.lite.nfc.NFCExceptions
16
+ import so.onekey.app.wallet.lite.nfc.NfcUtils
17
+ import so.onekey.app.wallet.lite.onekeyLite.OneKeyLiteCard
18
+ import so.onekey.app.wallet.lite.onekeyLite.entitys.CardState
19
+ import so.onekey.app.wallet.lite.utils.NfcPermissionUtils
20
+ import so.onekey.app.wallet.lite.utils.Utils
21
+ import java.util.concurrent.atomic.AtomicInteger
22
+ import com.facebook.react.bridge.Arguments
23
+ import com.facebook.react.bridge.BaseActivityEventListener
24
+ import com.facebook.react.bridge.LifecycleEventListener
25
+ import com.facebook.react.bridge.WritableMap
26
+ import so.onekey.app.wallet.lite.nfc.broadcast.NfcStatusChangeBroadcastReceiver
27
+ import java.util.concurrent.Executors.newFixedThreadPool
28
+
29
+ private val NFCDispatcher = newFixedThreadPool(1).asCoroutineDispatcher()
30
+ public fun NFCScope(): CoroutineScope = CoroutineScope(SupervisorJob() + NFCDispatcher)
31
+
32
+ @ReactModule(name = ReactNativeLiteCardModule.NAME)
33
+ class ReactNativeLiteCardModule(reactContext: ReactApplicationContext) :
34
+ NativeReactNativeLiteCardSpec(reactContext), LifecycleEventListener, CoroutineScope by NFCScope() {
35
+
36
+ companion object {
37
+ const val NAME = "ReactNativeLiteCard"
38
+ private val TAG = ReactNativeLiteCardModule::class.simpleName
39
+ }
40
+
41
+ @IntDef(NFCState.Dead, NFCState.Started)
42
+ annotation class NFCState {
43
+ companion object {
44
+ const val Dead = -1
45
+ const val Started = 0
46
+ }
47
+ }
48
+
49
+ override fun getName(): String {
50
+ return NAME
51
+ }
52
+
53
+ private val mNFCConnectedChannel = Channel<IsoDep?>(1)
54
+ private var lastIsoDep: IsoDep? = null
55
+ private val mNFCState = AtomicInteger(NFCState.Dead)
56
+ private val mShowDialogNumber = AtomicInteger(0)
57
+ private var mCurrentCardState: CardState? = null
58
+
59
+ private val mActivityEventListener = object : BaseActivityEventListener() {
60
+ override fun onNewIntent(intent: Intent) {
61
+ super.onNewIntent(intent)
62
+ val action = intent.action
63
+ if (action == null) {
64
+ return
65
+ }
66
+ if ((action == NfcAdapter.ACTION_NDEF_DISCOVERED)
67
+ || action == NfcAdapter.ACTION_TECH_DISCOVERED
68
+ || action == NfcAdapter.ACTION_TAG_DISCOVERED
69
+ ) {
70
+ val tags = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
71
+ val isoDep: IsoDep? = IsoDep.get(tags)
72
+ if (isoDep == null) {
73
+ // 未知设备
74
+ val dataMap = Arguments.createMap().apply {
75
+ putString("type", "unknown")
76
+ }
77
+ emitOnNFCActiveConnection(dataMap)
78
+ Log.d(TAG, "Unknown device")
79
+ return
80
+ }
81
+
82
+ Log.d(TAG, isoDep.toString())
83
+ launch(Dispatchers.IO) {
84
+ mNFCConnectedChannel.trySend(isoDep)
85
+ try {
86
+ // 处理主动触发 NFC
87
+ delay(100)
88
+ if (!mNFCConnectedChannel.isEmpty) {
89
+ Log.e(TAG, "There is no way to use NFC")
90
+ // mNFCConnectedChannel.receive()
91
+ val startRequest = OneKeyLiteCard.initRequest(isoDep)
92
+ val dataMap = Arguments.createMap().apply {
93
+ putInt("code", -1)
94
+ putString("type", "OneKey_Lite")
95
+ putString("serialNum", startRequest.serialNum)
96
+ putBoolean("isNewCard", startRequest.isNewCard)
97
+ putBoolean("hasBackup", startRequest.hasBackup)
98
+ }
99
+ emitOnNFCActiveConnection(dataMap)
100
+ }
101
+ } catch (e: Exception) {
102
+ e.printStackTrace()
103
+ // 未知设备或连接失败
104
+ val dataMap = Arguments.createMap().apply {
105
+ putInt("code", -1)
106
+ putString("type", "unknown")
107
+ }
108
+ emitOnNFCActiveConnection(dataMap)
109
+ emitOnNFCActiveConnection(dataMap)
110
+ }
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ init {
117
+ reactContext.addLifecycleEventListener(this)
118
+ reactContext.addActivityEventListener(mActivityEventListener)
119
+ }
120
+
121
+ private fun releaseDevice() {
122
+ if (mShowDialogNumber.get() <= 0) return
123
+
124
+ mCurrentCardState = null
125
+ val decrementAndGet = mShowDialogNumber.decrementAndGet()
126
+
127
+ // 关闭连接结束 ui
128
+ emitOnNFCUIEvent(Arguments.createMap().also {
129
+ it.putInt("code", 3)
130
+ it.putString("message", "close_connect_ui")
131
+ })
132
+
133
+ // 还有需要处理的 NFC 事件
134
+ if (decrementAndGet > 0) {
135
+ // 展示连接 ui
136
+ emitOnNFCUIEvent(Arguments.createMap().also {
137
+ it.putInt("code", 1)
138
+ it.putString("message", "show_connect_ui")
139
+ })
140
+ }
141
+ }
142
+
143
+ @Throws(NFCExceptions::class)
144
+ private suspend fun acquireDevice(): IsoDep? {
145
+ // 展示连接 ui
146
+ emitOnNFCUIEvent(Arguments.createMap().also {
147
+ it.putInt("code", 1)
148
+ it.putString("message", "show_connect_ui")
149
+ })
150
+ mShowDialogNumber.incrementAndGet()
151
+ val tryReceiveResult = mNFCConnectedChannel.tryReceive()
152
+
153
+ fun IsoDep?.isSafeConnected(): Boolean {
154
+ return runCatching { this?.isConnected ?: false }.getOrDefault(false)
155
+ }
156
+ var receiveIsoDep: IsoDep? = null
157
+
158
+ if (tryReceiveResult.isSuccess) {
159
+ receiveIsoDep = tryReceiveResult.getOrNull()
160
+ if (!receiveIsoDep.isSafeConnected()) {
161
+ receiveIsoDep = null
162
+ }
163
+ }
164
+
165
+ if (receiveIsoDep == null && lastIsoDep.isSafeConnected()) {
166
+ receiveIsoDep = lastIsoDep
167
+ }
168
+ if (receiveIsoDep == null) {
169
+ receiveIsoDep = mNFCConnectedChannel.receive()
170
+ }
171
+
172
+ lastIsoDep = receiveIsoDep
173
+ if (receiveIsoDep == null) {
174
+ // 取消连接
175
+ releaseDevice()
176
+ } else {
177
+ val initChannelRequest = OneKeyLiteCard.initRequest(receiveIsoDep)
178
+
179
+ mCurrentCardState = initChannelRequest
180
+
181
+ // 展示连接结束 ui
182
+ emitOnNFCUIEvent(Arguments.createMap().also {
183
+ it.putInt("code", 2)
184
+ it.putString("message", "connected")
185
+ })
186
+ }
187
+ return receiveIsoDep
188
+ }
189
+
190
+ private suspend fun <T> handleOperation(
191
+ callback: Callback?,
192
+ execute: (isoDep: IsoDep) -> T
193
+ ) {
194
+ val topActivity = Utils.getTopActivity() ?: return
195
+ NfcPermissionUtils.checkPermission(topActivity) {
196
+ try {
197
+ Log.d(TAG, "NFC permission check success")
198
+ val isoDep = acquireDevice() ?: return
199
+ val executeResult = execute(isoDep)
200
+ callback?.invoke(null, executeResult, mCurrentCardState.createArguments())
201
+ } catch (e: NFCExceptions) {
202
+ Log.e(TAG, "NFC device execute error", e)
203
+ callback?.invoke(e.createArguments(), null, mCurrentCardState.createArguments())
204
+ } finally {
205
+ releaseDevice()
206
+ }
207
+ return
208
+ }
209
+ // 没有 NFC 使用权限
210
+ Log.d(TAG, "NFC device not permission")
211
+ callback?.invoke(NFCExceptions.NotNFCPermission().createArguments(), null, null)
212
+ }
213
+
214
+ private fun CardState?.createArguments(): WritableMap {
215
+ val map = Arguments.createMap()
216
+ if (this == null) return map
217
+ map.putBoolean("hasBackup", this.hasBackup)
218
+ map.putBoolean("isNewCard", this.isNewCard)
219
+ map.putString("serialNum", this.serialNum)
220
+ map.putInt("pinRetryCount", this.pinRetryCount)
221
+ return map
222
+ }
223
+
224
+ private fun NFCExceptions?.createArguments(): WritableMap {
225
+ val map = Arguments.createMap()
226
+ if (this == null) return map
227
+ map.putInt("code", this.code)
228
+ map.putString("message", this.message)
229
+ return map
230
+ }
231
+
232
+ override fun getLiteInfo(callback: Callback?) {
233
+ launch {
234
+ Log.d(TAG, "getLiteInfo")
235
+ handleOperation<Any>(callback) { isoDep ->
236
+ Log.e(TAG, "getLiteInfo Obtain the device")
237
+ val cardInfo = OneKeyLiteCard.getCardInfo(isoDep)
238
+ Log.e(TAG, "getLiteInfo result $cardInfo")
239
+ cardInfo.createArguments()
240
+ }
241
+ }
242
+ }
243
+
244
+ override fun checkNFCPermission(callback: Callback?) {
245
+ val topActivity = Utils.getTopActivity()
246
+ if (topActivity == null) {
247
+ callback?.invoke(NFCExceptions.InitializedException().createArguments(), null, null)
248
+ return
249
+ }
250
+ val isNfcExists = NfcUtils.isNfcExits(topActivity)
251
+ if (!isNfcExists) {
252
+ // 没有 NFC 设备
253
+ Log.d(TAG, "NFC device not found")
254
+ callback?.invoke(NFCExceptions.NotExistsNFC().createArguments(), null, null)
255
+ return
256
+ }
257
+
258
+ val isNfcEnable = NfcUtils.isNfcEnable(topActivity)
259
+ if (!isNfcEnable) {
260
+ // 没有打开 NFC 开关
261
+ Log.d(TAG, "NFC device not enable")
262
+ callback?.invoke(NFCExceptions.NotEnableNFC().createArguments(), null, null)
263
+ return
264
+ }
265
+ NfcPermissionUtils.checkPermission(topActivity) {
266
+ callback?.invoke(null, null, null)
267
+ return
268
+ }
269
+ // 没有 NFC 使用权限
270
+ Log.d(TAG, "NFC device not permission")
271
+ callback?.invoke(NFCExceptions.NotNFCPermission().createArguments(), null, null)
272
+ }
273
+
274
+ override fun setMnemonic(
275
+ mnemonic: String?,
276
+ pwd: String?,
277
+ overwrite: Boolean,
278
+ callback: Callback?
279
+ ) {
280
+ launch {
281
+ handleOperation(callback) { isoDep ->
282
+ Log.e(TAG, "setMnemonic Obtain the device")
283
+ val isSuccess =
284
+ OneKeyLiteCard.setMnemonic(
285
+ mCurrentCardState,
286
+ isoDep,
287
+ mnemonic,
288
+ pwd,
289
+ overwrite
290
+ )
291
+ if (!isSuccess) throw NFCExceptions.ExecFailureException()
292
+ Log.e(TAG, "setMnemonic result success")
293
+ true
294
+ }
295
+ }
296
+ }
297
+
298
+ override fun getMnemonicWithPin(
299
+ pwd: String,
300
+ callback: Callback?
301
+ ) {
302
+ launch {
303
+ Log.d(TAG, "getMnemonicWithPin")
304
+ handleOperation(callback) { isoDep ->
305
+ Log.e(TAG, "getMnemonicWithPin Obtain the device")
306
+ OneKeyLiteCard.getMnemonicWithPin(mCurrentCardState, isoDep, pwd)
307
+ }
308
+ }
309
+ }
310
+
311
+ override fun changePin(
312
+ oldPin: String,
313
+ newPin: String,
314
+ callback: Callback?
315
+ ) {
316
+ launch {
317
+ Log.d(TAG, "changePin")
318
+ handleOperation(callback) { isoDep ->
319
+ Log.e(TAG, "changePin Obtain the device")
320
+ OneKeyLiteCard.changPin(mCurrentCardState, isoDep, oldPin, newPin)
321
+ }
322
+ }
323
+ }
324
+
325
+ override fun reset(callback: Callback?) {
326
+ launch {
327
+ Log.d(TAG, "reset")
328
+ handleOperation(callback) { isoDep ->
329
+ Log.e(TAG, "reset Obtain the device")
330
+ val isSuccess = OneKeyLiteCard.reset(isoDep)
331
+ if (!isSuccess) throw NFCExceptions.ExecFailureException()
332
+ Log.e(TAG, "reset result success")
333
+ true
334
+ }
335
+ }
336
+ }
337
+
338
+ override fun cancel() {
339
+ if (mNFCConnectedChannel.isEmpty) {
340
+ mNFCConnectedChannel.trySend(null)
341
+ }
342
+ }
343
+
344
+ override fun intoSetting() {
345
+ launch {
346
+ Log.d(TAG, "intoSetting")
347
+ Utils.getTopActivity()?.let {
348
+ NfcUtils.intentToNfcSetting(it)
349
+ }
350
+ }
351
+ }
352
+
353
+ private val mNfcStateBroadcastReceiver by lazy {
354
+ object : NfcStatusChangeBroadcastReceiver() {
355
+ override fun onCardPayMode() {
356
+ super.onCardPayMode()
357
+ }
358
+
359
+ override fun onNfcOff() {
360
+ super.onNfcOff()
361
+ }
362
+
363
+ override fun onNfcOn() {
364
+ super.onNfcOn()
365
+ Utils.getTopActivity()?.let {
366
+ launch(Dispatchers.IO) {
367
+ OneKeyLiteCard.startNfc(it as FragmentActivity) {}
368
+ }
369
+ }
370
+ }
371
+
372
+ override fun onNfcTurningOff() {
373
+ super.onNfcTurningOff()
374
+ }
375
+
376
+ override fun onNfcTurningOn() {
377
+ super.onNfcTurningOn()
378
+ }
379
+ }
380
+ }
381
+
382
+
383
+ override fun onHostResume() {
384
+ Utils.getTopActivity()?.let {
385
+ launch(Dispatchers.IO) {
386
+ if (it !is FragmentActivity) return@launch
387
+ try {
388
+ OneKeyLiteCard.startNfc(it) {
389
+ mNFCState.set(NFCState.Started)
390
+
391
+ Log.d(TAG, "NFC starting success")
392
+ }
393
+ } catch (e: Exception) {
394
+ Log.e(TAG, "startNfc failed", e)
395
+ }
396
+ }
397
+ }
398
+ }
399
+
400
+ override fun onHostPause() {
401
+ Utils.getTopActivity()?.let {
402
+ launch(Dispatchers.IO) {
403
+ try {
404
+ OneKeyLiteCard.stopNfc(it as FragmentActivity)
405
+ mNFCState.set(NFCState.Dead)
406
+ Log.d(TAG, "NFC 已关闭")
407
+ } catch (e: Exception) {
408
+ Log.e(TAG, "stopNfc failed", e)
409
+ }
410
+ }
411
+ }
412
+ }
413
+
414
+ override fun onHostDestroy() {
415
+ try {
416
+ Utils.getTopActivity()?.unregisterReceiver(mNfcStateBroadcastReceiver)
417
+ } catch (ignore: Exception) {
418
+ }
419
+ }
420
+ }
@@ -0,0 +1,33 @@
1
+ package com.onekeyfe.reactnativelitecard
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.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+ import java.util.HashMap
9
+
10
+ class ReactNativeLiteCardPackage : BaseReactPackage() {
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
12
+ return if (name == ReactNativeLiteCardModule.NAME) {
13
+ ReactNativeLiteCardModule(reactContext)
14
+ } else {
15
+ null
16
+ }
17
+ }
18
+
19
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
20
+ return ReactModuleInfoProvider {
21
+ val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
22
+ moduleInfos[ReactNativeLiteCardModule.NAME] = ReactModuleInfo(
23
+ ReactNativeLiteCardModule.NAME,
24
+ ReactNativeLiteCardModule.NAME,
25
+ false, // canOverrideExistingModule
26
+ false, // needsEagerInit
27
+ false, // isCxxModule
28
+ true // isTurboModule
29
+ )
30
+ moduleInfos
31
+ }
32
+ }
33
+ }
@@ -102,11 +102,11 @@ object OneKeyLiteCard {
102
102
 
103
103
  @Throws(NFCExceptions::class)
104
104
  fun setMnemonic(
105
- cardState: CardState?,
106
- isoDep: IsoDep,
107
- mnemonic: String,
108
- pwd: String,
109
- overwrite: Boolean = false
105
+ cardState: CardState?,
106
+ isoDep: IsoDep,
107
+ mnemonic: String?,
108
+ pwd: String?,
109
+ overwrite: Boolean = false
110
110
  ): Boolean {
111
111
  if (cardState == null) throw NFCExceptions.ConnectionFailException()
112
112
 
@@ -146,7 +146,7 @@ object OneKeyLiteCard {
146
146
  }
147
147
  }
148
148
 
149
- return mCardConnection?.backupData(mnemonic) == true
149
+ return mCardConnection?.backupData(mnemonic ?: "") == true
150
150
  }
151
151
 
152
152
  @Throws(NFCExceptions::class)
@@ -233,4 +233,4 @@ object OneKeyLiteCard {
233
233
  }
234
234
  }
235
235
  }
236
- }
236
+ }