@onekeyfe/react-native-lite-card 6.7.0-alpha.449 → 6.7.0-alpha.466
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/README.md +3 -1
- package/ReactNativeLiteCard.podspec +7 -1
- package/android/build.gradle +1 -0
- package/android/consumer-rules.pro +4 -0
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +37 -44
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +105 -38
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +19 -1
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +10 -2
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +34 -0
- package/ios/Classes/OKNFCBridge.mm +23 -0
- package/ios/Classes/OKNFTLite/OKLiteV1.m +13 -6
- package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
- package/ios/Classes/OKNFTLite/OKNFCManager.m +84 -12
- package/ios/ReactNativeLiteCard.mm +7 -4
- package/lib/module/index.js +4 -4
- package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +2 -2
- package/lib/typescript/src/index.d.ts +2 -2
- package/package.json +1 -1
- package/sdk/CMakeLists.txt +4 -3
- package/sdk/README.md +15 -0
- package/sdk/android/implJni.cpp +39 -0
- package/sdk/scripts/generate-source-id.rb +22 -0
- package/sdk/source-id-manifest.txt +19 -0
- package/sdk/source-id.cmake +31 -0
- package/sdk/src/GPChannelSDK.cpp +7 -1
- package/sdk/src/version.c +2 -1
- package/sdk/tests/android_verify.py +4 -1
- package/sdk/tests/safety.cpp +159 -5
- package/sdk/tests/verify.py +21 -4
- package/src/NativeReactNativeLiteCard.ts +2 -0
- package/src/index.tsx +14 -3
package/README.md
CHANGED
|
@@ -41,7 +41,9 @@ if (result.error) {
|
|
|
41
41
|
state, new-card status and remaining PIN retries.
|
|
42
42
|
- `setMnemonic(mnemonic, pwd, overwrite)` and `getMnemonicWithPin(pwd)` manage
|
|
43
43
|
the backup; `overwrite` defaults to `false`.
|
|
44
|
-
- `changePin(oldPin, newPin)`
|
|
44
|
+
- `changePin(oldPin, newPin, expectedSerial?)` checks a supplied card serial before
|
|
45
|
+
changing the PIN; `reset()` resets the card's state. The identity guard requires
|
|
46
|
+
a native rebuild on iOS and Android.
|
|
45
47
|
- `addConnectListener(listener)` subscribes to NFC UI events and returns the
|
|
46
48
|
native subscription. Keep it and call `remove()` when the owner finishes.
|
|
47
49
|
- `cancel()` invokes native cancellation on Android; `intoSetting()` opens
|
|
@@ -36,8 +36,14 @@ Pod::Spec.new do |s|
|
|
|
36
36
|
s.private_header_files = "ios/**/*.h"
|
|
37
37
|
s.preserve_paths = "sdk/**/*"
|
|
38
38
|
s.libraries = "c++"
|
|
39
|
+
s.script_phase = {
|
|
40
|
+
:name => 'Generate Lite native source identity',
|
|
41
|
+
:execution_position => :before_compile,
|
|
42
|
+
:show_env_vars_in_log => '0',
|
|
43
|
+
:script => 'set -e; ruby "${PODS_TARGET_SRCROOT}/sdk/scripts/generate-source-id.rb" "${DERIVED_FILE_DIR}/GPChannelSourceId.h"'
|
|
44
|
+
}
|
|
39
45
|
s.pod_target_xcconfig = {
|
|
40
|
-
"HEADER_SEARCH_PATHS" => '$(inherited) "$(PODS_TARGET_SRCROOT)/sdk/include" "$(PODS_TARGET_SRCROOT)/sdk/gpc/include" "$(PODS_TARGET_SRCROOT)/sdk/deps/trezor-crypto/include" "$(PODS_TARGET_SRCROOT)/sdk/deps/trezor-crypto/src"'
|
|
46
|
+
"HEADER_SEARCH_PATHS" => '$(inherited) "$(DERIVED_FILE_DIR)" "$(PODS_TARGET_SRCROOT)/sdk/include" "$(PODS_TARGET_SRCROOT)/sdk/gpc/include" "$(PODS_TARGET_SRCROOT)/sdk/deps/trezor-crypto/include" "$(PODS_TARGET_SRCROOT)/sdk/deps/trezor-crypto/src"'
|
|
41
47
|
}
|
|
42
48
|
|
|
43
49
|
|
package/android/build.gradle
CHANGED
|
@@ -37,6 +37,7 @@ android {
|
|
|
37
37
|
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
38
38
|
|
|
39
39
|
defaultConfig {
|
|
40
|
+
consumerProguardFiles "consumer-rules.pro"
|
|
40
41
|
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
41
42
|
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
42
43
|
|
|
@@ -16,6 +16,7 @@ import com.facebook.react.module.annotations.ReactModule
|
|
|
16
16
|
import com.onekeyfe.reactnativelitecard.nfc.NFCExceptions
|
|
17
17
|
import com.onekeyfe.reactnativelitecard.nfc.NfcUtils
|
|
18
18
|
import com.onekeyfe.reactnativelitecard.onekeyLite.OneKeyLiteCard
|
|
19
|
+
import com.onekeyfe.reactnativelitecard.onekeyLite.PendingTag
|
|
19
20
|
import com.onekeyfe.reactnativelitecard.onekeyLite.entitys.CardState
|
|
20
21
|
import com.onekeyfe.reactnativelitecard.utils.NfcPermissionUtils
|
|
21
22
|
import com.onekeyfe.reactnativelitecard.utils.Utils
|
|
@@ -66,12 +67,11 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
private val mNFCConnectedChannel = Channel<
|
|
70
|
+
private val mNFCConnectedChannel = Channel<PendingTag?>(1)
|
|
70
71
|
private var lastIsoDep: IsoDep? = null
|
|
71
72
|
private val diagnosticSequence = AtomicInteger(0)
|
|
72
73
|
private val mNFCState = AtomicInteger(NFCState.Dead)
|
|
73
74
|
private val mShowDialogNumber = AtomicInteger(0)
|
|
74
|
-
private var mCurrentCardState: CardState? = null
|
|
75
75
|
|
|
76
76
|
private val mActivityEventListener = object : BaseActivityEventListener() {
|
|
77
77
|
override fun onNewIntent(intent: Intent) {
|
|
@@ -97,15 +97,14 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
OneKeyLog.info("LiteCard", "stage=tag_received wallTimeMs=${System.currentTimeMillis()}")
|
|
100
|
+
val pendingTag = PendingTag(isoDep)
|
|
101
|
+
if (mNFCConnectedChannel.trySend(pendingTag).isFailure) return
|
|
100
102
|
launch(Dispatchers.IO) {
|
|
101
|
-
mNFCConnectedChannel.trySend(isoDep)
|
|
102
103
|
try {
|
|
103
104
|
// 处理主动触发 NFC
|
|
104
105
|
delay(100)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
// mNFCConnectedChannel.receive()
|
|
108
|
-
val startRequest = OneKeyLiteCard.initRequest(isoDep)
|
|
106
|
+
val startRequest = OneKeyLiteCard.tryDiscover(pendingTag)
|
|
107
|
+
if (startRequest != null && !pendingTag.isClaimed()) {
|
|
109
108
|
val dataMap = Arguments.createMap().apply {
|
|
110
109
|
putInt("code", -1)
|
|
111
110
|
putString("type", "OneKey_Lite")
|
|
@@ -117,6 +116,7 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
117
116
|
}
|
|
118
117
|
} catch (e: Exception) {
|
|
119
118
|
OneKeyLog.error("LiteCard", "stage=connection_failure type=${if (e is NFCExceptions) "nfc" else "other"}")
|
|
119
|
+
if (pendingTag.isClaimed()) return@launch
|
|
120
120
|
// 未知设备或连接失败
|
|
121
121
|
val dataMap = Arguments.createMap().apply {
|
|
122
122
|
putInt("code", -1)
|
|
@@ -137,7 +137,6 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
137
137
|
private fun releaseDevice() {
|
|
138
138
|
if (mShowDialogNumber.get() <= 0) return
|
|
139
139
|
|
|
140
|
-
mCurrentCardState = null
|
|
141
140
|
val decrementAndGet = mShowDialogNumber.decrementAndGet()
|
|
142
141
|
|
|
143
142
|
// 关闭连接结束 ui
|
|
@@ -174,11 +173,8 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
174
173
|
var receiveIsoDep: IsoDep? = null
|
|
175
174
|
|
|
176
175
|
if (tryReceiveResult.isSuccess) {
|
|
177
|
-
receiveIsoDep = tryReceiveResult.getOrNull()
|
|
176
|
+
receiveIsoDep = tryReceiveResult.getOrNull()?.claim()
|
|
178
177
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_source source=channel connected=${receiveIsoDep.isSafeConnected()}")
|
|
179
|
-
if (!receiveIsoDep.isSafeConnected()) {
|
|
180
|
-
receiveIsoDep = null
|
|
181
|
-
}
|
|
182
178
|
}
|
|
183
179
|
|
|
184
180
|
if (receiveIsoDep == null && lastIsoDep.isSafeConnected()) {
|
|
@@ -187,33 +183,18 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
187
183
|
}
|
|
188
184
|
if (receiveIsoDep == null) {
|
|
189
185
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_source source=wait")
|
|
190
|
-
receiveIsoDep = mNFCConnectedChannel.receive()
|
|
186
|
+
receiveIsoDep = mNFCConnectedChannel.receive()?.claim()
|
|
191
187
|
}
|
|
192
188
|
|
|
193
189
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_result available=${receiveIsoDep != null} connected=${receiveIsoDep.isSafeConnected()}")
|
|
194
190
|
lastIsoDep = receiveIsoDep
|
|
195
|
-
if (receiveIsoDep == null) {
|
|
196
|
-
// 取消连接
|
|
197
|
-
releaseDevice()
|
|
198
|
-
} else {
|
|
199
|
-
val initChannelRequest = OneKeyLiteCard.initRequest(receiveIsoDep)
|
|
200
|
-
|
|
201
|
-
mCurrentCardState = initChannelRequest
|
|
202
|
-
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=card_state isNewCard=${initChannelRequest.isNewCard} hasBackup=${initChannelRequest.hasBackup}")
|
|
203
|
-
|
|
204
|
-
// 展示连接结束 ui
|
|
205
|
-
emitOnNFCUIEvent(Arguments.createMap().also {
|
|
206
|
-
it.putInt("code", 2)
|
|
207
|
-
it.putString("message", "connected")
|
|
208
|
-
}.copy())
|
|
209
|
-
}
|
|
210
191
|
return receiveIsoDep
|
|
211
192
|
}
|
|
212
193
|
|
|
213
194
|
private suspend fun <T> handleOperation(
|
|
214
195
|
callback: Callback?,
|
|
215
196
|
operation: String,
|
|
216
|
-
execute: (
|
|
197
|
+
execute: (session: OneKeyLiteCard.CardSession) -> T
|
|
217
198
|
) {
|
|
218
199
|
val operationID = diagnosticSequence.incrementAndGet()
|
|
219
200
|
val started = SystemClock.elapsedRealtime()
|
|
@@ -223,15 +204,25 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
223
204
|
return
|
|
224
205
|
}
|
|
225
206
|
NfcPermissionUtils.checkPermission(topActivity) {
|
|
207
|
+
var cardState: CardState? = null
|
|
226
208
|
try {
|
|
227
209
|
OneKeyLog.debug("LiteCard","NFC permission check success")
|
|
228
210
|
val isoDep = acquireDevice() ?: return
|
|
229
|
-
val executeResult =
|
|
211
|
+
val executeResult = OneKeyLiteCard.withCardSession(isoDep) { session ->
|
|
212
|
+
cardState = session.cardState
|
|
213
|
+
OneKeyLog.info("LiteCard", "operation=$operation operationID=$operationID stage=card_state isNewCard=${session.cardState.isNewCard} hasBackup=${session.cardState.hasBackup}")
|
|
214
|
+
// This queues a React Native event; completion callbacks stay outside ownership.
|
|
215
|
+
emitOnNFCUIEvent(Arguments.createMap().also {
|
|
216
|
+
it.putInt("code", 2)
|
|
217
|
+
it.putString("message", "connected")
|
|
218
|
+
}.copy())
|
|
219
|
+
execute(session)
|
|
220
|
+
}
|
|
230
221
|
OneKeyLog.info("LiteCard", "operation=$operation operationID=$operationID stage=callback code=0 elapsedMs=${SystemClock.elapsedRealtime() - started}")
|
|
231
|
-
callback?.invoke(null, executeResult,
|
|
222
|
+
callback?.invoke(null, executeResult, cardState.createArguments())
|
|
232
223
|
} catch (e: NFCExceptions) {
|
|
233
224
|
OneKeyLog.info("LiteCard", "operation=$operation operationID=$operationID stage=callback code=${e.code} elapsedMs=${SystemClock.elapsedRealtime() - started}")
|
|
234
|
-
callback?.invoke(e.createArguments(), null,
|
|
225
|
+
callback?.invoke(e.createArguments(), null, cardState.createArguments())
|
|
235
226
|
} finally {
|
|
236
227
|
releaseDevice()
|
|
237
228
|
}
|
|
@@ -264,9 +255,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
264
255
|
override fun getLiteInfo(callback: Callback?) {
|
|
265
256
|
launch {
|
|
266
257
|
OneKeyLog.debug("LiteCard","getLiteInfo")
|
|
267
|
-
handleOperation<Any>(callback, "getLiteInfo") {
|
|
258
|
+
handleOperation<Any>(callback, "getLiteInfo") { session ->
|
|
268
259
|
OneKeyLog.debug("LiteCard","getLiteInfo Obtain the device")
|
|
269
|
-
val cardInfo = OneKeyLiteCard.getCardInfo(
|
|
260
|
+
val cardInfo = OneKeyLiteCard.getCardInfo(session)
|
|
270
261
|
OneKeyLog.debug("LiteCard","getLiteInfo result obtained")
|
|
271
262
|
cardInfo.createArguments()
|
|
272
263
|
}
|
|
@@ -307,6 +298,7 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
307
298
|
mnemonic: String?,
|
|
308
299
|
pwd: String?,
|
|
309
300
|
overwrite: Boolean,
|
|
301
|
+
expectedSerial: String?,
|
|
310
302
|
callback: Callback?
|
|
311
303
|
) {
|
|
312
304
|
if (pwd.isNullOrEmpty() || pwd.length != 6 || !pwd.all { it.isDigit() }) {
|
|
@@ -318,15 +310,15 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
318
310
|
return
|
|
319
311
|
}
|
|
320
312
|
launch {
|
|
321
|
-
handleOperation(callback, "setMnemonic") {
|
|
313
|
+
handleOperation(callback, "setMnemonic") { session ->
|
|
322
314
|
OneKeyLog.debug("LiteCard","setMnemonic Obtain the device")
|
|
323
315
|
val isSuccess =
|
|
324
316
|
OneKeyLiteCard.setMnemonic(
|
|
325
|
-
|
|
326
|
-
isoDep,
|
|
317
|
+
session,
|
|
327
318
|
mnemonic,
|
|
328
319
|
pwd,
|
|
329
|
-
overwrite
|
|
320
|
+
overwrite,
|
|
321
|
+
expectedSerial
|
|
330
322
|
)
|
|
331
323
|
if (!isSuccess) throw NFCExceptions.ExecFailureException()
|
|
332
324
|
OneKeyLog.debug("LiteCard","setMnemonic result success")
|
|
@@ -345,9 +337,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
345
337
|
}
|
|
346
338
|
launch {
|
|
347
339
|
OneKeyLog.debug("LiteCard","getMnemonicWithPin")
|
|
348
|
-
handleOperation(callback, "getMnemonicWithPin") {
|
|
340
|
+
handleOperation(callback, "getMnemonicWithPin") { session ->
|
|
349
341
|
OneKeyLog.debug("LiteCard","getMnemonicWithPin Obtain the device")
|
|
350
|
-
OneKeyLiteCard.getMnemonicWithPin(
|
|
342
|
+
OneKeyLiteCard.getMnemonicWithPin(session, pwd)
|
|
351
343
|
}
|
|
352
344
|
}
|
|
353
345
|
}
|
|
@@ -355,13 +347,14 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
355
347
|
override fun changePin(
|
|
356
348
|
oldPin: String,
|
|
357
349
|
newPin: String,
|
|
350
|
+
expectedSerial: String?,
|
|
358
351
|
callback: Callback?
|
|
359
352
|
) {
|
|
360
353
|
launch {
|
|
361
354
|
OneKeyLog.debug("LiteCard","changePin")
|
|
362
|
-
handleOperation(callback, "changePin") {
|
|
355
|
+
handleOperation(callback, "changePin") { session ->
|
|
363
356
|
OneKeyLog.debug("LiteCard","changePin Obtain the device")
|
|
364
|
-
OneKeyLiteCard.changPin(
|
|
357
|
+
OneKeyLiteCard.changPin(session, oldPin, newPin, expectedSerial)
|
|
365
358
|
}
|
|
366
359
|
}
|
|
367
360
|
}
|
|
@@ -369,9 +362,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
369
362
|
override fun reset(callback: Callback?) {
|
|
370
363
|
launch {
|
|
371
364
|
OneKeyLog.debug("LiteCard","reset")
|
|
372
|
-
handleOperation(callback, "reset") {
|
|
365
|
+
handleOperation(callback, "reset") { session ->
|
|
373
366
|
OneKeyLog.debug("LiteCard","reset Obtain the device")
|
|
374
|
-
val isSuccess = OneKeyLiteCard.reset(
|
|
367
|
+
val isSuccess = OneKeyLiteCard.reset(session)
|
|
375
368
|
if (!isSuccess) throw NFCExceptions.ExecFailureException()
|
|
376
369
|
OneKeyLog.debug("LiteCard","reset result success")
|
|
377
370
|
true
|
|
@@ -8,6 +8,10 @@ import androidx.lifecycle.Lifecycle
|
|
|
8
8
|
import com.google.gson.Gson
|
|
9
9
|
import kotlinx.coroutines.Dispatchers
|
|
10
10
|
import kotlinx.coroutines.withContext
|
|
11
|
+
import kotlinx.coroutines.sync.Mutex
|
|
12
|
+
import kotlinx.coroutines.sync.withLock
|
|
13
|
+
import org.haobtc.onekey.card.gpchannel.GPChannelNatives.nativeGPCFinalize
|
|
14
|
+
import java.util.concurrent.atomic.AtomicBoolean
|
|
11
15
|
import com.onekeyfe.reactnativelitecard.nfc.NFCExceptions
|
|
12
16
|
import com.onekeyfe.reactnativelitecard.nfc.NfcUtils
|
|
13
17
|
import com.onekeyfe.reactnativelitecard.onekeyLite.entitys.CardState
|
|
@@ -17,14 +21,30 @@ import com.onekeyfe.reactnativelitecard.onekeyLite.nfc.CommandGenerator
|
|
|
17
21
|
import com.onekeyfe.reactnativelitecard.utils.LogUtil.printLog
|
|
18
22
|
import com.onekeyfe.reactnativelitecard.utils.NfcPermissionUtils
|
|
19
23
|
|
|
24
|
+
internal class PendingTag(val isoDep: IsoDep) {
|
|
25
|
+
private val claimed = AtomicBoolean(false)
|
|
26
|
+
|
|
27
|
+
fun claim(): IsoDep {
|
|
28
|
+
claimed.set(true)
|
|
29
|
+
return isoDep
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fun isClaimed(): Boolean = claimed.get()
|
|
33
|
+
}
|
|
34
|
+
|
|
20
35
|
object OneKeyLiteCard {
|
|
21
36
|
const val TAG = "OneKeyLiteCard"
|
|
22
37
|
|
|
23
38
|
private val mCommandGenerator by lazy {
|
|
24
39
|
CommandGenerator()
|
|
25
40
|
}
|
|
26
|
-
|
|
27
|
-
private
|
|
41
|
+
// GPChannel owns one native context, including across React Native runtimes.
|
|
42
|
+
private val operationMutex = Mutex()
|
|
43
|
+
|
|
44
|
+
internal class CardSession internal constructor(
|
|
45
|
+
internal val connection: Connection,
|
|
46
|
+
val cardState: CardState
|
|
47
|
+
)
|
|
28
48
|
|
|
29
49
|
suspend fun startNfc(activity: FragmentActivity, callback: ((Boolean) -> Unit)? = null) =
|
|
30
50
|
withContext(Dispatchers.Main) {
|
|
@@ -61,55 +81,98 @@ object OneKeyLiteCard {
|
|
|
61
81
|
NfcUtils.mNfcAdapter?.disableForegroundDispatch(activity)
|
|
62
82
|
}
|
|
63
83
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
84
|
+
internal suspend fun <T> withCardSession(
|
|
85
|
+
isoDep: IsoDep,
|
|
86
|
+
execute: (CardSession) -> T
|
|
87
|
+
): T = withContext(Dispatchers.IO) {
|
|
88
|
+
operationMutex.withLock {
|
|
89
|
+
withInitializedSession(isoDep, execute)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
internal suspend fun tryDiscover(tag: PendingTag): CardState? = withContext(Dispatchers.IO) {
|
|
94
|
+
if (tag.isClaimed() || !operationMutex.tryLock()) return@withContext null
|
|
95
|
+
try {
|
|
96
|
+
if (tag.isClaimed()) return@withContext null
|
|
97
|
+
withInitializedSession(tag.isoDep) { it.cardState }.takeUnless { tag.isClaimed() }
|
|
98
|
+
} finally {
|
|
99
|
+
operationMutex.unlock()
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
private fun <T> withInitializedSession(isoDep: IsoDep, execute: (CardSession) -> T): T {
|
|
104
|
+
try {
|
|
105
|
+
val connection = Connection(isoDep, mCommandGenerator)
|
|
106
|
+
return execute(CardSession(connection, connection.getCardInfo()))
|
|
107
|
+
} finally {
|
|
108
|
+
// Construction itself reads the card and may fail after touching GPChannel.
|
|
109
|
+
nativeGPCFinalize()
|
|
69
110
|
}
|
|
70
|
-
mCardConnection = Connection(isoDep, mCommandGenerator)
|
|
71
|
-
return@withContext mCardConnection?.getCardInfo()
|
|
72
|
-
?: throw NFCExceptions.InitChannelException()
|
|
73
111
|
}
|
|
74
112
|
|
|
75
113
|
@Throws(NFCExceptions::class)
|
|
76
|
-
private fun setupNewPinRequest(
|
|
114
|
+
private fun setupNewPinRequest(session: CardSession, pin: String?): Boolean {
|
|
77
115
|
if (pin.isNullOrEmpty()) {
|
|
78
116
|
throw NFCExceptions.InputPasswordEmptyException()
|
|
79
117
|
}
|
|
80
|
-
return
|
|
118
|
+
return session.connection.setupNewPin(pin)
|
|
81
119
|
}
|
|
82
120
|
|
|
83
121
|
@Throws(NFCExceptions::class)
|
|
84
|
-
fun changePinRequest(
|
|
122
|
+
private fun changePinRequest(session: CardSession, oldPwd: String?, newPwd: String?): Int {
|
|
85
123
|
if (oldPwd.isNullOrEmpty()) {
|
|
86
124
|
throw NFCExceptions.InputPasswordEmptyException()
|
|
87
125
|
}
|
|
88
|
-
return
|
|
126
|
+
return session.connection.changePin(oldPwd, newPwd)
|
|
89
127
|
}
|
|
90
128
|
|
|
91
129
|
@Throws(NFCExceptions::class)
|
|
92
|
-
fun verifyPinBackupRequest(
|
|
130
|
+
private fun verifyPinBackupRequest(session: CardSession, verifyPin: String?): Int {
|
|
93
131
|
if (verifyPin.isNullOrEmpty()) {
|
|
94
132
|
throw NFCExceptions.InputPasswordEmptyException()
|
|
95
133
|
}
|
|
96
|
-
return
|
|
134
|
+
return session.connection.startVerifyPin(verifyPin)
|
|
97
135
|
}
|
|
98
136
|
|
|
99
137
|
@Throws(NFCExceptions::class)
|
|
100
|
-
fun getCardInfo(
|
|
101
|
-
return
|
|
138
|
+
internal fun getCardInfo(session: CardSession): CardState {
|
|
139
|
+
return session.connection.getCardInfo()
|
|
102
140
|
}
|
|
103
141
|
|
|
104
142
|
@Throws(NFCExceptions::class)
|
|
105
|
-
fun setMnemonic(
|
|
106
|
-
|
|
107
|
-
isoDep: IsoDep,
|
|
143
|
+
internal fun setMnemonic(
|
|
144
|
+
session: CardSession,
|
|
108
145
|
mnemonic: String?,
|
|
109
146
|
pwd: String?,
|
|
110
|
-
overwrite: Boolean = false
|
|
147
|
+
overwrite: Boolean = false,
|
|
148
|
+
expectedSerial: String? = null
|
|
111
149
|
): Boolean {
|
|
112
|
-
if (
|
|
150
|
+
val version = if (session.connection.getCardType() == AppleCardType.V2) 2 else 1
|
|
151
|
+
val maxBytes = if (version == 2) 65511 else 65535
|
|
152
|
+
val reason = when {
|
|
153
|
+
mnemonic == null -> "missing"
|
|
154
|
+
mnemonic.isEmpty() -> "empty"
|
|
155
|
+
mnemonic.length > maxBytes * 2 -> "too_large"
|
|
156
|
+
mnemonic.any { it !in '0'..'9' && it !in 'a'..'f' && it !in 'A'..'F' } -> "non_hex"
|
|
157
|
+
else -> "accepted"
|
|
158
|
+
}
|
|
159
|
+
val accepted = reason == "accepted"
|
|
160
|
+
val inputChars = mnemonic?.length ?: 0
|
|
161
|
+
try {
|
|
162
|
+
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=input_preflight inputPresent=${mnemonic != null} inputChars=$inputChars odd=${inputChars % 2 != 0} versionKnown=true cardVersion=$version overwrite=$overwrite reason=$reason accepted=$accepted")
|
|
163
|
+
} catch (_: Exception) {
|
|
164
|
+
// Diagnostics must not alter the input decision.
|
|
165
|
+
}
|
|
166
|
+
if (mnemonic == null || !accepted) {
|
|
167
|
+
throw NFCExceptions.ExecFailureException()
|
|
168
|
+
}
|
|
169
|
+
val cardState = session.cardState
|
|
170
|
+
val serial = cardState.serialNum
|
|
171
|
+
if (expectedSerial != null && (serial.isEmpty() ||
|
|
172
|
+
serial == NfcConstant.NOT_MATCH_DEVICE || expectedSerial != serial)) {
|
|
173
|
+
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=identity_mismatch")
|
|
174
|
+
throw NFCExceptions.DeviceMismatchException()
|
|
175
|
+
}
|
|
113
176
|
|
|
114
177
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=card_state isNewCard=${cardState.isNewCard} hasBackup=${cardState.hasBackup} overwrite=$overwrite wallTimeMs=${System.currentTimeMillis()}")
|
|
115
178
|
|
|
@@ -121,10 +184,10 @@ object OneKeyLiteCard {
|
|
|
121
184
|
}
|
|
122
185
|
}
|
|
123
186
|
|
|
124
|
-
if (overwrite &&
|
|
187
|
+
if (overwrite && session.connection.getCardType() == AppleCardType.V2) {
|
|
125
188
|
// Verify the password before clearing the card
|
|
126
189
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=overwrite_verify_begin")
|
|
127
|
-
val verifyPin = verifyPinBackupRequest(
|
|
190
|
+
val verifyPin = verifyPinBackupRequest(session, pwd)
|
|
128
191
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=overwrite_verify_result success=${verifyPin == NfcConstant.VERIFY_SUCCESS}")
|
|
129
192
|
if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
|
|
130
193
|
handlerVerifyPinError(cardState, verifyPin)
|
|
@@ -137,7 +200,7 @@ object OneKeyLiteCard {
|
|
|
137
200
|
if (cardState.isNewCard) {
|
|
138
201
|
// 如果是新卡设置密码
|
|
139
202
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=set_pin_begin")
|
|
140
|
-
val setupSuccess = setupNewPinRequest(
|
|
203
|
+
val setupSuccess = setupNewPinRequest(session, pwd)
|
|
141
204
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=set_pin_result success=$setupSuccess")
|
|
142
205
|
if (!setupSuccess) {
|
|
143
206
|
throw NFCExceptions.InitPasswordException()
|
|
@@ -145,7 +208,7 @@ object OneKeyLiteCard {
|
|
|
145
208
|
}
|
|
146
209
|
|
|
147
210
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=verify_pin_begin")
|
|
148
|
-
val verifyPin = verifyPinBackupRequest(
|
|
211
|
+
val verifyPin = verifyPinBackupRequest(session, pwd)
|
|
149
212
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=verify_pin_result success=${verifyPin == NfcConstant.VERIFY_SUCCESS}")
|
|
150
213
|
if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
|
|
151
214
|
if (overwrite) {
|
|
@@ -155,27 +218,26 @@ object OneKeyLiteCard {
|
|
|
155
218
|
}
|
|
156
219
|
}
|
|
157
220
|
|
|
158
|
-
if (mnemonic.isNullOrEmpty()) throw NFCExceptions.ExecFailureException()
|
|
159
221
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_begin")
|
|
160
|
-
val backupSuccess =
|
|
222
|
+
val backupSuccess = session.connection.backupData(mnemonic)
|
|
161
223
|
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_result success=$backupSuccess")
|
|
162
224
|
return backupSuccess
|
|
163
225
|
}
|
|
164
226
|
|
|
165
227
|
@Throws(NFCExceptions::class)
|
|
166
|
-
fun getMnemonicWithPin(
|
|
167
|
-
|
|
228
|
+
internal fun getMnemonicWithPin(session: CardSession, pwd: String): String {
|
|
229
|
+
val cardState = session.cardState
|
|
168
230
|
|
|
169
231
|
if (cardState.isNewCard || !cardState.hasBackup) {
|
|
170
232
|
throw NFCExceptions.NotInitializedException()
|
|
171
233
|
}
|
|
172
234
|
|
|
173
|
-
val verifyPin = verifyPinBackupRequest(
|
|
235
|
+
val verifyPin = verifyPinBackupRequest(session, pwd)
|
|
174
236
|
if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
|
|
175
237
|
handlerVerifyPinError(cardState, verifyPin)
|
|
176
238
|
}
|
|
177
239
|
|
|
178
|
-
val result =
|
|
240
|
+
val result = session.connection.exportData()
|
|
179
241
|
|
|
180
242
|
if (result.isNullOrEmpty()) {
|
|
181
243
|
throw NFCExceptions.NotInitializedException()
|
|
@@ -184,14 +246,19 @@ object OneKeyLiteCard {
|
|
|
184
246
|
}
|
|
185
247
|
|
|
186
248
|
@Throws(NFCExceptions::class)
|
|
187
|
-
fun changPin(
|
|
188
|
-
|
|
249
|
+
internal fun changPin(session: CardSession, oldPwd: String, newPwd: String, expectedSerial: String? = null): Boolean {
|
|
250
|
+
val cardState = session.cardState
|
|
251
|
+
val serial = cardState.serialNum
|
|
252
|
+
if (expectedSerial != null && (serial.isEmpty() ||
|
|
253
|
+
serial == NfcConstant.NOT_MATCH_DEVICE || expectedSerial != serial)) {
|
|
254
|
+
throw NFCExceptions.DeviceMismatchException()
|
|
255
|
+
}
|
|
189
256
|
|
|
190
257
|
if (cardState.isNewCard || !cardState.hasBackup) {
|
|
191
258
|
throw NFCExceptions.NotInitializedException()
|
|
192
259
|
}
|
|
193
260
|
|
|
194
|
-
val result = changePinRequest(
|
|
261
|
+
val result = changePinRequest(session, oldPwd, newPwd)
|
|
195
262
|
handlerVerifyPinError(cardState, result, true)
|
|
196
263
|
|
|
197
264
|
return when (result) {
|
|
@@ -209,8 +276,8 @@ object OneKeyLiteCard {
|
|
|
209
276
|
}
|
|
210
277
|
}
|
|
211
278
|
|
|
212
|
-
fun reset(
|
|
213
|
-
return
|
|
279
|
+
internal fun reset(session: CardSession): Boolean {
|
|
280
|
+
return session.connection.resetCard() == NfcConstant.RESET_PIN_SUCCESS
|
|
214
281
|
}
|
|
215
282
|
|
|
216
283
|
private fun handlerVerifyPinError(
|
package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
package com.onekeyfe.reactnativelitecard.onekeyLite.nfc
|
|
2
2
|
|
|
3
|
+
import com.margelo.nitro.nativelogger.OneKeyLog
|
|
4
|
+
import java.util.concurrent.atomic.AtomicLong
|
|
5
|
+
import org.haobtc.onekey.card.gpchannel.GPChannelNatives.nativeGetErrorCode
|
|
3
6
|
import com.google.gson.JsonObject
|
|
4
7
|
import com.google.gson.JsonParser
|
|
5
8
|
import org.haobtc.onekey.card.gpchannel.GPChannelNatives.nativeGPCParseAPDUResponse
|
|
@@ -48,6 +51,7 @@ enum class CommandType(val code: String) {
|
|
|
48
51
|
|
|
49
52
|
class Command(
|
|
50
53
|
private val cardType: AppleCardType,
|
|
54
|
+
private val type: CommandType,
|
|
51
55
|
val area: CommandArea,
|
|
52
56
|
val command: String,
|
|
53
57
|
private val ignoreSafeChannel: Boolean,
|
|
@@ -56,6 +60,10 @@ class Command(
|
|
|
56
60
|
private val hasOpenSafeChannel: Boolean,
|
|
57
61
|
val data: String,
|
|
58
62
|
) {
|
|
63
|
+
companion object {
|
|
64
|
+
private val diagnosticSequence = AtomicLong(0)
|
|
65
|
+
}
|
|
66
|
+
|
|
59
67
|
private fun beforeConnecting(connection: Connection) {
|
|
60
68
|
val needOpenSafeChannel = useSafeChannel || (useSafeChannelWhenOpen && hasOpenSafeChannel)
|
|
61
69
|
val needCloseSafeChannel =
|
|
@@ -106,16 +114,25 @@ class Command(
|
|
|
106
114
|
}
|
|
107
115
|
|
|
108
116
|
fun send(connection: Connection): SendResponse {
|
|
117
|
+
val commandID = diagnosticSequence.incrementAndGet()
|
|
118
|
+
val secure = useSafeChannel || (useSafeChannelWhenOpen && hasOpenSafeChannel)
|
|
119
|
+
OneKeyLog.info("LiteCard", "commandID=$commandID command=${type.code} stage=before_connect secure=$secure")
|
|
109
120
|
beforeConnecting(connection)
|
|
110
121
|
// buildApdu && parseResponse Use in pairs
|
|
111
122
|
val apdu = buildApdu()
|
|
123
|
+
OneKeyLog.info("LiteCard", "commandID=$commandID command=${type.code} stage=build buildSucceeded=${apdu.isNotEmpty()}")
|
|
112
124
|
if (apdu.isEmpty()) {
|
|
113
125
|
return SendResponse("0xFFFF", 0xFF.toByte(), 0xFF.toByte(), parseSuccess = false)
|
|
114
126
|
}
|
|
115
127
|
val result = Connection.send(connection.isoDep, apdu)
|
|
116
|
-
val
|
|
128
|
+
val rawResponseBytes = if (result.rawResponse == "0xFFFF") -1 else result.rawResponse.length / 2
|
|
129
|
+
OneKeyLog.info("LiteCard", "commandID=$commandID command=${type.code} stage=transport_result rawResponseBytes=$rawResponseBytes rawSW1=${result.sw1.toInt() and 0xFF} rawSW2=${result.sw2.toInt() and 0xFF}")
|
|
130
|
+
val parsedResponse = parseResponse(result.rawResponse)
|
|
131
|
+
val lastSdkError = if (parsedResponse == null) nativeGetErrorCode() else 0
|
|
132
|
+
val resultObject = CardResponse.objectFromData(parsedResponse)
|
|
117
133
|
result.result = resultObject?.response ?: ""
|
|
118
134
|
result.parseSuccess = resultObject != null && resultObject.wRet == 0x9000
|
|
135
|
+
OneKeyLog.info("LiteCard", "commandID=$commandID command=${type.code} stage=parse lastSdkError=$lastSdkError parsePresent=${resultObject != null} parsedSW=${resultObject?.wRet ?: -1} parseSuccess=${result.parseSuccess}")
|
|
119
136
|
return result
|
|
120
137
|
}
|
|
121
138
|
}
|
|
@@ -169,6 +186,7 @@ class CommandGenerator {
|
|
|
169
186
|
|
|
170
187
|
return Command(
|
|
171
188
|
cardType = cardType,
|
|
189
|
+
type = type,
|
|
172
190
|
area = area,
|
|
173
191
|
command = command,
|
|
174
192
|
ignoreSafeChannel = ignoreSafeChannel,
|
|
@@ -66,6 +66,8 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
|
|
|
66
66
|
private var hasOpenSafeChannel: Boolean = false
|
|
67
67
|
private var hasSetupPin: Boolean = false
|
|
68
68
|
private var hasBackup: Boolean = false
|
|
69
|
+
private var pinStatusValid: Boolean = false
|
|
70
|
+
private var backupStatusValid: Boolean = false
|
|
69
71
|
private var mCommandArea = CommandArea.None
|
|
70
72
|
|
|
71
73
|
init {
|
|
@@ -224,8 +226,10 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
|
|
|
224
226
|
)
|
|
225
227
|
val res = command.send(this)
|
|
226
228
|
printLog(TAG, "<--- getBackupStatus end: ${res.getCode()} area:${mCommandArea}")
|
|
229
|
+
backupStatusValid = res.isSuccess() && !res.isEmptyData()
|
|
230
|
+
OneKeyLog.info("LiteCard", "stage=backup_state_read readSucceeded=$backupStatusValid hasBackup=${res.isSuccess() && res.data == "02"}")
|
|
227
231
|
|
|
228
|
-
if (!
|
|
232
|
+
if (!backupStatusValid) {
|
|
229
233
|
return false
|
|
230
234
|
}
|
|
231
235
|
|
|
@@ -239,8 +243,10 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
|
|
|
239
243
|
)
|
|
240
244
|
val res = command.send(this)
|
|
241
245
|
printLog(TAG, "<--- getPinStatus end: ${res.getCode()} area:${mCommandArea}")
|
|
246
|
+
pinStatusValid = res.isSuccess() && !res.isEmptyData()
|
|
247
|
+
OneKeyLog.info("LiteCard", "stage=pin_state_read readSucceeded=$pinStatusValid isNewCard=${res.isSuccess() && res.data == "02"}")
|
|
242
248
|
|
|
243
|
-
if (!
|
|
249
|
+
if (!pinStatusValid) {
|
|
244
250
|
return false
|
|
245
251
|
}
|
|
246
252
|
|
|
@@ -298,8 +304,10 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
|
|
|
298
304
|
}
|
|
299
305
|
|
|
300
306
|
loadPinStatus()
|
|
307
|
+
if (!pinStatusValid) throw NFCExceptions.ConnectionFailException()
|
|
301
308
|
if (mCardType == AppleCardType.V2) {
|
|
302
309
|
loadBackupStatus()
|
|
310
|
+
if (!backupStatusValid) throw NFCExceptions.ConnectionFailException()
|
|
303
311
|
} else {
|
|
304
312
|
hasBackup = hasSetupPin
|
|
305
313
|
}
|