@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
@@ -0,0 +1,112 @@
1
+ import {
2
+ TurboModuleRegistry,
3
+ type CodegenTypes,
4
+ type TurboModule,
5
+ } from 'react-native';
6
+
7
+ export type CallbackError = { code: number; message: string | null } | null;
8
+
9
+ export type CardInfo = {
10
+ hasBackup: boolean;
11
+ isNewCard: boolean;
12
+ serialNum: string;
13
+ pinRetryCount: number;
14
+ } | null;
15
+
16
+ export type CallbackResult<T> = [
17
+ error: CallbackError | null,
18
+ data: T | null,
19
+ cardInfo: CardInfo | null
20
+ ];
21
+
22
+ export type PromiseResult<T> = {
23
+ error: CallbackError | null;
24
+ data: T | null;
25
+ cardInfo: CardInfo | null;
26
+ };
27
+
28
+ export enum CardErrors {
29
+ InitChannel = 1000,
30
+ NotExistsNFC = 1001,
31
+ NotEnableNFC = 1002,
32
+ NotNFCPermission = 1003,
33
+
34
+ ConnectionFail = 2001,
35
+ InterruptError = 2002,
36
+ DeviceMismatch = 2003,
37
+ UserCancel = 2004,
38
+
39
+ PasswordWrong = 3001,
40
+ InputPasswordEmpty = 3002,
41
+ NotSetPassword = 3003,
42
+ InitPasswordError = 3004,
43
+ CardLock = 3005,
44
+ UpperErrorAutoReset = 3006,
45
+
46
+ ExecFailure = 4000,
47
+ InitializedError = 4001,
48
+ NotInitializedError = 4002,
49
+ }
50
+
51
+ type KeyValuePair = {
52
+ code: string;
53
+ message: string;
54
+ };
55
+
56
+ export interface Spec extends TurboModule {
57
+ getLiteInfo(
58
+ callback: (
59
+ error: CallbackError | null,
60
+ data: CardInfo | null,
61
+ cardInfo: CardInfo | null
62
+ ) => void
63
+ ): void;
64
+ checkNFCPermission(
65
+ callback: (
66
+ error: CallbackError | null,
67
+ data: boolean | null,
68
+ cardInfo: CardInfo | null
69
+ ) => void
70
+ ): void;
71
+ setMnemonic(
72
+ mnemonic: string,
73
+ pwd: string,
74
+ overwrite: boolean,
75
+ callback: (
76
+ error: CallbackError | null,
77
+ data: boolean | null,
78
+ cardInfo: CardInfo | null
79
+ ) => void
80
+ ): void;
81
+ getMnemonicWithPin(
82
+ pwd: string,
83
+ callback: (
84
+ error: CallbackError | null,
85
+ data: string | null,
86
+ cardInfo: CardInfo | null
87
+ ) => void
88
+ ): void;
89
+ changePin(
90
+ oldPin: string,
91
+ newPin: string,
92
+ callback: (
93
+ error: CallbackError | null,
94
+ data: boolean | null,
95
+ cardInfo: CardInfo | null
96
+ ) => void
97
+ ): void;
98
+ reset(
99
+ callback: (
100
+ error: CallbackError | null,
101
+ data: boolean | null,
102
+ cardInfo: CardInfo | null
103
+ ) => void
104
+ ): void;
105
+ cancel(): void;
106
+ intoSetting(): void;
107
+
108
+ readonly onNFCActiveConnection: CodegenTypes.EventEmitter<KeyValuePair>;
109
+ readonly onNFCUIEvent: CodegenTypes.EventEmitter<KeyValuePair>;
110
+ }
111
+
112
+ export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeLiteCard');
@@ -1,40 +1,30 @@
1
- import {
2
- Linking,
3
- NativeEventEmitter,
4
- NativeModules,
5
- Platform,
6
- } from 'react-native';
7
-
8
- import type { CallbackResult, CardInfo, PromiseResult } from './types';
9
-
10
- const { OKLiteManager } = NativeModules;
1
+ import { Linking, Platform } from 'react-native';
2
+ import OKLiteManager from './NativeReactNativeLiteCard';
3
+ import type {
4
+ CallbackResult,
5
+ CardInfo,
6
+ PromiseResult,
7
+ } from './NativeReactNativeLiteCard';
11
8
 
12
9
  export type NfcConnectUiState = {
13
10
  code: number;
14
11
  message: string;
15
12
  };
16
-
17
13
  class OnekeyLite {
18
- UiEventEmitter: NativeEventEmitter | null = null;
19
-
20
- constructor() {
21
- if (Platform.OS !== 'android') return;
22
- this.UiEventEmitter = new NativeEventEmitter(OKLiteManager);
23
- }
24
-
25
14
  addConnectListener(listener: (event: NfcConnectUiState) => void) {
26
15
  this.removeConnectListeners();
27
- return this.UiEventEmitter?.addListener('nfc_ui_event', listener);
16
+ return OKLiteManager.onNFCUIEvent((event) => {
17
+ listener({
18
+ code: Number(event.code),
19
+ message: event.message,
20
+ });
21
+ });
28
22
  }
29
23
 
30
- removeConnectListeners() {
31
- return this.UiEventEmitter?.removeAllListeners('nfc_ui_event');
32
- }
24
+ removeConnectListeners() {}
33
25
 
34
26
  addAccordListener() {
35
- if (Platform.OS !== 'android') return;
36
- const eventEmitter = new NativeEventEmitter(OKLiteManager);
37
- return eventEmitter.addListener('nfc_active_connection', () => {});
27
+ return OKLiteManager.onNFCActiveConnection(() => {});
38
28
  }
39
29
 
40
30
  getLiteInfo() {
@@ -101,3 +91,4 @@ class OnekeyLite {
101
91
 
102
92
  const onekeyLite = new OnekeyLite();
103
93
  export default onekeyLite;
94
+ export type * from './NativeReactNativeLiteCard';
package/LICENSE.md DELETED
@@ -1,51 +0,0 @@
1
- # ONEKEY STANDARD SOURCE LICENSE (O-SSL)
2
-
3
- This license governs use of the accompanying software. If you use the software,
4
- you accept this license. If you do not accept the license, do not use the
5
- software.
6
-
7
- ## 1. Definitions
8
-
9
- The terms "reproduce," "reproduction" and "distribution" have the same meaning
10
- here as under Hong Kong copyright law.
11
-
12
- "You" means the licensee of the software.
13
-
14
- "Your company" means the company you worked for when you downloaded the
15
- software.
16
-
17
- "Reference use" means use of the software within your company as a reference,
18
- in read only form, for the sole purposes of debugging your products,
19
- maintaining your products, or enhancing the interoperability of your products
20
- with the software, and specifically excludes the right to distribute the
21
- software outside of your company.
22
-
23
- "Licensed patents" means any Licensor patent claims which read directly on the
24
- software as distributed by the Licensor under this license.
25
-
26
- ## 2. Grant of Rights
27
-
28
- (A) Copyright Grant - Subject to the terms of this license, the Licensor grants
29
- you a non-transferable, non-exclusive, worldwide, royalty-free copyright
30
- license to reproduce the software for reference use.
31
-
32
- (B) Patent Grant - Subject to the terms of this license, the Licensor grants
33
- you a non-transferable, non-exclusive, worldwide, royalty-free patent license
34
- under licensed patents for reference use.
35
-
36
- ## 3. Limitations
37
-
38
- (A) No Trademark License - This license does not grant you any rights to use
39
- the Licensor's name, logo, or trademarks.
40
-
41
- (B) If you begin patent litigation against the Licensor over patents that you
42
- think may apply to the software (including a cross-claim or counterclaim in
43
- a lawsuit), your license to the software ends automatically.
44
-
45
- (C) The software is licensed "as-is." You bear the risk of using it. The
46
- Licensor gives no express warranties, guarantees or conditions. You may have
47
- additional consumer rights under your local laws which this license cannot
48
- change. To the extent permitted under your local laws, the Licensor excludes
49
- the implied warranties of merchantability, fitness for a particular purpose and
50
- non-infringement.This license agreement is governed by the laws of Hong Kong,
51
- and any disputes related to this license agreement shall be resolved in accordance with Hong Kong law.
@@ -1,431 +0,0 @@
1
- package so.onekey.app.wallet.lite
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 com.facebook.react.bridge.*
11
- import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
12
- import kotlinx.coroutines.*
13
- import kotlinx.coroutines.channels.Channel
14
- import so.onekey.app.wallet.lite.nfc.NFCExceptions
15
- import so.onekey.app.wallet.lite.nfc.NfcUtils
16
- import so.onekey.app.wallet.lite.nfc.broadcast.NfcStatusChangeBroadcastReceiver
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.Executors
22
- import java.util.concurrent.atomic.AtomicInteger
23
-
24
-
25
- public fun NFCScope(): CoroutineScope = CoroutineScope(SupervisorJob() + NFCDispatcher)
26
- private val NFCDispatcher = Executors.newFixedThreadPool(1).asCoroutineDispatcher()
27
-
28
- class OKLiteManager(private val context: ReactApplicationContext) :
29
- ReactContextBaseJavaModule(context), LifecycleEventListener, CoroutineScope by NFCScope() {
30
- companion object {
31
- private val TAG = OKLiteManager::class.simpleName
32
-
33
- // NFC UI 事件
34
- private const val NFC_UI_EVENT = "nfc_ui_event"
35
-
36
- // NFC 主动连接
37
- private const val NFC_ACTIVE_CONNECTION = "nfc_active_connection"
38
- }
39
-
40
- @IntDef(NFCState.Dead, NFCState.Started)
41
- annotation class NFCState {
42
- companion object {
43
- const val Dead = -1
44
- const val Started = 0
45
- }
46
- }
47
-
48
- private val mNFCConnectedChannel = Channel<IsoDep?>(1)
49
- private var lastIsoDep: IsoDep? = null
50
- private val mNFCState = AtomicInteger(NFCState.Dead)
51
- private val mShowDialogNumber = AtomicInteger(0)
52
- private var mCurrentCardState: CardState? = null
53
-
54
- private val mActivityEventListener = object : BaseActivityEventListener() {
55
- override fun onNewIntent(intent: Intent) {
56
- super.onNewIntent(intent)
57
- val action = intent.action
58
- if (action == null) {
59
- return
60
- }
61
- if ((action == NfcAdapter.ACTION_NDEF_DISCOVERED)
62
- || action == NfcAdapter.ACTION_TECH_DISCOVERED
63
- || action == NfcAdapter.ACTION_TAG_DISCOVERED
64
- ) {
65
- val tags = intent.getParcelableExtra<Tag>(NfcAdapter.EXTRA_TAG)
66
- val isoDep: IsoDep? = IsoDep.get(tags)
67
- if (isoDep == null) {
68
- // 未知设备
69
- val dataMap = Arguments.createMap().apply {
70
- putString("type", "unknown")
71
- }
72
- sendEvent(context, NFC_ACTIVE_CONNECTION, dataMap)
73
- Log.d(TAG, "Unknown device")
74
- return
75
- }
76
-
77
- Log.d(TAG, isoDep.toString())
78
- launch(Dispatchers.IO) {
79
- mNFCConnectedChannel.trySend(isoDep)
80
- try {
81
- // 处理主动触发 NFC
82
- delay(100)
83
- if (!mNFCConnectedChannel.isEmpty) {
84
- Log.e(TAG, "There is no way to use NFC")
85
- // mNFCConnectedChannel.receive()
86
- val startRequest = OneKeyLiteCard.initRequest(isoDep)
87
- val dataMap = Arguments.createMap().apply {
88
- putInt("code", -1)
89
- putString("type", "OneKey_Lite")
90
- putString("serialNum", startRequest.serialNum)
91
- putBoolean("isNewCard", startRequest.isNewCard)
92
- putBoolean("hasBackup", startRequest.hasBackup)
93
- }
94
- sendEvent(context, NFC_ACTIVE_CONNECTION, dataMap)
95
- }
96
- } catch (e: Exception) {
97
- e.printStackTrace()
98
- // 未知设备或连接失败
99
- val dataMap = Arguments.createMap().apply {
100
- putInt("code", -1)
101
- putString("type", "unknown")
102
- }
103
- sendEvent(context, NFC_ACTIVE_CONNECTION, dataMap)
104
- }
105
- }
106
- }
107
- }
108
- }
109
-
110
- init {
111
- context.addLifecycleEventListener(this)
112
- context.addActivityEventListener(mActivityEventListener)
113
- }
114
-
115
- override fun getName() = "OKLiteManager"
116
-
117
- override fun initialize() {
118
- super.initialize()
119
- Utils.init(context)
120
- Utils.getActivityLifecycle()
121
- Utils.getTopActivity()?.registerReceiver(
122
- mNfcStateBroadcastReceiver,
123
- NfcStatusChangeBroadcastReceiver.nfcBroadcastReceiverIntentFilter
124
- )
125
- Utils.getTopActivity()?.let {
126
- launch(Dispatchers.IO) {
127
- OneKeyLiteCard.startNfc(it as FragmentActivity) {}
128
- }
129
- }
130
-
131
- }
132
-
133
- override fun onHostResume() {
134
- Utils.getTopActivity()?.let {
135
- launch(Dispatchers.IO) {
136
- if (it !is FragmentActivity) return@launch
137
- try {
138
- OneKeyLiteCard.startNfc(it) {
139
- mNFCState.set(NFCState.Started)
140
-
141
- Log.d(TAG, "NFC starting success")
142
- }
143
- } catch (e: Exception) {
144
- Log.e(TAG, "startNfc failed", e)
145
- }
146
- }
147
- }
148
- }
149
-
150
- override fun onHostPause() {
151
- Utils.getTopActivity()?.let {
152
- launch(Dispatchers.IO) {
153
- try {
154
- OneKeyLiteCard.stopNfc(it as FragmentActivity)
155
- mNFCState.set(NFCState.Dead)
156
- Log.d(TAG, "NFC 已关闭")
157
- } catch (e: Exception) {
158
- Log.e(TAG, "stopNfc failed", e)
159
- }
160
- }
161
- }
162
- }
163
-
164
- override fun onHostDestroy() {
165
- try {
166
- Utils.getTopActivity()?.unregisterReceiver(mNfcStateBroadcastReceiver)
167
- } catch (ignore: Exception) {
168
- }
169
- }
170
-
171
- private val mNfcStateBroadcastReceiver by lazy {
172
- object : NfcStatusChangeBroadcastReceiver() {
173
- override fun onCardPayMode() {
174
- super.onCardPayMode()
175
- }
176
-
177
- override fun onNfcOff() {
178
- super.onNfcOff()
179
- }
180
-
181
- override fun onNfcOn() {
182
- super.onNfcOn()
183
- Utils.getTopActivity()?.let {
184
- launch(Dispatchers.IO) {
185
- OneKeyLiteCard.startNfc(it as FragmentActivity) {}
186
- }
187
- }
188
- }
189
-
190
- override fun onNfcTurningOff() {
191
- super.onNfcTurningOff()
192
- }
193
-
194
- override fun onNfcTurningOn() {
195
- super.onNfcTurningOn()
196
- }
197
- }
198
- }
199
-
200
- private fun sendEvent(
201
- reactContext: ReactContext,
202
- eventName: String,
203
- params: WritableMap
204
- ) {
205
- reactContext
206
- .getJSModule(RCTDeviceEventEmitter::class.java)
207
- .emit(eventName, params)
208
- }
209
-
210
- @Throws(NFCExceptions::class)
211
- private suspend fun acquireDevice(): IsoDep? {
212
- // 展示连接 ui
213
- sendEvent(context, NFC_UI_EVENT, Arguments.createMap().also {
214
- it.putInt("code", 1)
215
- it.putString("message", "show_connect_ui")
216
- })
217
- mShowDialogNumber.incrementAndGet()
218
- val tryReceiveResult = mNFCConnectedChannel.tryReceive()
219
-
220
- fun IsoDep?.isSafeConnected(): Boolean {
221
- return runCatching { this?.isConnected ?: false }.getOrDefault(false)
222
- }
223
- var receiveIsoDep: IsoDep? = null
224
-
225
- if (tryReceiveResult.isSuccess) {
226
- receiveIsoDep = tryReceiveResult.getOrNull()
227
- if (!receiveIsoDep.isSafeConnected()) {
228
- receiveIsoDep = null
229
- }
230
- }
231
-
232
- if (receiveIsoDep == null && lastIsoDep.isSafeConnected()) {
233
- receiveIsoDep = lastIsoDep
234
- }
235
- if (receiveIsoDep == null) {
236
- receiveIsoDep = mNFCConnectedChannel.receive()
237
- }
238
-
239
- lastIsoDep = receiveIsoDep
240
- if (receiveIsoDep == null) {
241
- // 取消连接
242
- releaseDevice()
243
- } else {
244
- val initChannelRequest = OneKeyLiteCard.initRequest(receiveIsoDep)
245
-
246
- mCurrentCardState = initChannelRequest
247
-
248
- // 展示连接结束 ui
249
- sendEvent(context, NFC_UI_EVENT, Arguments.createMap().also {
250
- it.putInt("code", 2)
251
- it.putString("message", "connected")
252
- })
253
- }
254
- return receiveIsoDep
255
- }
256
-
257
- private fun releaseDevice() {
258
- if (mShowDialogNumber.get() <= 0) return
259
-
260
- mCurrentCardState = null
261
- val decrementAndGet = mShowDialogNumber.decrementAndGet()
262
-
263
- // 关闭连接结束 ui
264
- sendEvent(context, NFC_UI_EVENT, Arguments.createMap().also {
265
- it.putInt("code", 3)
266
- it.putString("message", "close_connect_ui")
267
- })
268
-
269
- // 还有需要处理的 NFC 事件
270
- if (decrementAndGet > 0) {
271
- // 展示连接 ui
272
- sendEvent(context, NFC_UI_EVENT, Arguments.createMap().also {
273
- it.putInt("code", 1)
274
- it.putString("message", "show_connect_ui")
275
- })
276
- }
277
- }
278
-
279
- private suspend fun <T> handleOperation(
280
- callback: Callback,
281
- execute: (isoDep: IsoDep) -> T
282
- ) {
283
- val topActivity = Utils.getTopActivity() ?: return
284
- NfcPermissionUtils.checkPermission(topActivity) {
285
- try {
286
- Log.d(TAG, "NFC permission check success")
287
- val isoDep = acquireDevice() ?: return
288
- val executeResult = execute(isoDep)
289
- callback.invoke(null, executeResult, mCurrentCardState.createArguments())
290
- } catch (e: NFCExceptions) {
291
- Log.e(TAG, "NFC device execute error", e)
292
- callback.invoke(e.createArguments(), null, mCurrentCardState.createArguments())
293
- } finally {
294
- releaseDevice()
295
- }
296
- return
297
- }
298
- // 没有 NFC 使用权限
299
- Log.d(TAG, "NFC device not permission")
300
- callback.invoke(NFCExceptions.NotNFCPermission().createArguments(), null, null)
301
- }
302
-
303
- private fun CardState?.createArguments(): WritableMap {
304
- val map = Arguments.createMap()
305
- if (this == null) return map
306
- map.putBoolean("hasBackup", this.hasBackup)
307
- map.putBoolean("isNewCard", this.isNewCard)
308
- map.putString("serialNum", this.serialNum)
309
- map.putInt("pinRetryCount", this.pinRetryCount)
310
- return map
311
- }
312
-
313
- private fun NFCExceptions?.createArguments(): WritableMap {
314
- val map = Arguments.createMap()
315
- if (this == null) return map
316
- map.putInt("code", this.code)
317
- map.putString("message", this.message)
318
- return map
319
- }
320
-
321
- @ReactMethod
322
- fun cancel() {
323
- if (mNFCConnectedChannel.isEmpty) {
324
- mNFCConnectedChannel.trySend(null)
325
- }
326
- }
327
-
328
- @ReactMethod
329
- fun getLiteInfo(callback: Callback) {
330
- launch {
331
- Log.d(TAG, "getLiteInfo")
332
- handleOperation(callback) { isoDep ->
333
- Log.e(TAG, "getLiteInfo Obtain the device")
334
- val cardInfo = OneKeyLiteCard.getCardInfo(isoDep)
335
- Log.e(TAG, "getLiteInfo result $cardInfo")
336
- cardInfo.createArguments()
337
- }
338
- }
339
- }
340
-
341
- @ReactMethod
342
- fun setMnemonic(mnemonic: String, pwd: String, overwrite: Boolean, callback: Callback) {
343
- launch {
344
- handleOperation(callback) { isoDep ->
345
- Log.e(TAG, "setMnemonic Obtain the device")
346
- val isSuccess =
347
- OneKeyLiteCard.setMnemonic(mCurrentCardState, isoDep, mnemonic, pwd, overwrite)
348
- if (!isSuccess) throw NFCExceptions.ExecFailureException()
349
- Log.e(TAG, "setMnemonic result success")
350
- true
351
- }
352
- }
353
- }
354
-
355
- @ReactMethod
356
- fun getMnemonicWithPin(pwd: String, callback: Callback) {
357
- launch {
358
- Log.d(TAG, "getMnemonicWithPin")
359
- handleOperation(callback) { isoDep ->
360
- Log.e(TAG, "getMnemonicWithPin Obtain the device")
361
- OneKeyLiteCard.getMnemonicWithPin(mCurrentCardState, isoDep, pwd)
362
- }
363
- }
364
- }
365
-
366
- @ReactMethod
367
- fun changePin(oldPwd: String, newPwd: String, callback: Callback) {
368
- launch {
369
- Log.d(TAG, "changePin")
370
- handleOperation(callback) { isoDep ->
371
- Log.e(TAG, "changePin Obtain the device")
372
- OneKeyLiteCard.changPin(mCurrentCardState, isoDep, oldPwd, newPwd)
373
- }
374
- }
375
- }
376
-
377
- @ReactMethod
378
- fun checkNFCPermission(callback: Callback) {
379
- val topActivity = Utils.getTopActivity()
380
- if (topActivity == null) {
381
- callback.invoke(NFCExceptions.InitializedException().createArguments(), null, null)
382
- return
383
- }
384
- val isNfcExists = NfcUtils.isNfcExits(topActivity)
385
- if (!isNfcExists) {
386
- // 没有 NFC 设备
387
- Log.d(TAG, "NFC device not found")
388
- callback.invoke(NFCExceptions.NotExistsNFC().createArguments(), null, null)
389
- return
390
- }
391
-
392
- val isNfcEnable = NfcUtils.isNfcEnable(topActivity)
393
- if (!isNfcEnable) {
394
- // 没有打开 NFC 开关
395
- Log.d(TAG, "NFC device not enable")
396
- callback.invoke(NFCExceptions.NotEnableNFC().createArguments(), null, null)
397
- return
398
- }
399
- NfcPermissionUtils.checkPermission(topActivity) {
400
- callback.invoke(null, null, null)
401
- return
402
- }
403
- // 没有 NFC 使用权限
404
- Log.d(TAG, "NFC device not permission")
405
- callback.invoke(NFCExceptions.NotNFCPermission().createArguments(), null, null)
406
- }
407
-
408
- @ReactMethod
409
- fun reset(callback: Callback) {
410
- launch {
411
- Log.d(TAG, "reset")
412
- handleOperation(callback) { isoDep ->
413
- Log.e(TAG, "reset Obtain the device")
414
- val isSuccess = OneKeyLiteCard.reset(isoDep)
415
- if (!isSuccess) throw NFCExceptions.ExecFailureException()
416
- Log.e(TAG, "reset result success")
417
- true
418
- }
419
- }
420
- }
421
-
422
- @ReactMethod
423
- fun intoSetting() {
424
- launch {
425
- Log.d(TAG, "intoSetting")
426
- Utils.getTopActivity()?.let {
427
- NfcUtils.intentToNfcSetting(it)
428
- }
429
- }
430
- }
431
- }
@@ -1,16 +0,0 @@
1
- package so.onekey.app.wallet.lite
2
-
3
- import com.facebook.react.ReactPackage
4
- import com.facebook.react.bridge.NativeModule
5
- import com.facebook.react.bridge.ReactApplicationContext
6
- import com.facebook.react.uimanager.ViewManager
7
-
8
- class OKLitePackage : ReactPackage {
9
- override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> =
10
- listOf<NativeModule>(
11
- OKLiteManager(reactContext),
12
- )
13
-
14
- override fun createViewManagers(reactContext: ReactApplicationContext) =
15
- emptyList<ViewManager<*, *>>()
16
- }
@@ -1,17 +0,0 @@
1
- //
2
- // RNEventManager.h
3
- // OneKeyWallet
4
- //
5
- // Created by linleiqin on 2021/11/8.
6
- //
7
-
8
- #import <Foundation/Foundation.h>
9
- #import <React/RCTBridgeModule.h>
10
-
11
- NS_ASSUME_NONNULL_BEGIN
12
-
13
- @interface OKLiteManager : NSObject <RCTBridgeModule>
14
-
15
- @end
16
-
17
- NS_ASSUME_NONNULL_END