@onekeyfe/react-native-lite-card 6.7.0-alpha.448 → 6.7.0-alpha.464
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 +36 -51
- package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +105 -38
- package/ios/Classes/OKNFCBridge.mm +22 -0
- package/ios/Classes/OKNFTLite/OKNFCManager.h +1 -1
- package/ios/Classes/OKNFTLite/OKNFCManager.m +68 -12
- package/ios/ReactNativeLiteCard.mm +5 -2
- package/lib/module/index.js +2 -2
- package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- 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 +1 -0
- package/src/index.tsx +7 -2
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,7 +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.
|
|
19
|
+
import com.onekeyfe.reactnativelitecard.onekeyLite.PendingTag
|
|
20
20
|
import com.onekeyfe.reactnativelitecard.onekeyLite.entitys.CardState
|
|
21
21
|
import com.onekeyfe.reactnativelitecard.utils.NfcPermissionUtils
|
|
22
22
|
import com.onekeyfe.reactnativelitecard.utils.Utils
|
|
@@ -67,12 +67,11 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
private val mNFCConnectedChannel = Channel<
|
|
70
|
+
private val mNFCConnectedChannel = Channel<PendingTag?>(1)
|
|
71
71
|
private var lastIsoDep: IsoDep? = null
|
|
72
72
|
private val diagnosticSequence = AtomicInteger(0)
|
|
73
73
|
private val mNFCState = AtomicInteger(NFCState.Dead)
|
|
74
74
|
private val mShowDialogNumber = AtomicInteger(0)
|
|
75
|
-
private var mCurrentCardState: CardState? = null
|
|
76
75
|
|
|
77
76
|
private val mActivityEventListener = object : BaseActivityEventListener() {
|
|
78
77
|
override fun onNewIntent(intent: Intent) {
|
|
@@ -98,15 +97,14 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
OneKeyLog.info("LiteCard", "stage=tag_received wallTimeMs=${System.currentTimeMillis()}")
|
|
100
|
+
val pendingTag = PendingTag(isoDep)
|
|
101
|
+
if (mNFCConnectedChannel.trySend(pendingTag).isFailure) return
|
|
101
102
|
launch(Dispatchers.IO) {
|
|
102
|
-
mNFCConnectedChannel.trySend(isoDep)
|
|
103
103
|
try {
|
|
104
104
|
// 处理主动触发 NFC
|
|
105
105
|
delay(100)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// mNFCConnectedChannel.receive()
|
|
109
|
-
val startRequest = OneKeyLiteCard.initRequest(isoDep)
|
|
106
|
+
val startRequest = OneKeyLiteCard.tryDiscover(pendingTag)
|
|
107
|
+
if (startRequest != null && !pendingTag.isClaimed()) {
|
|
110
108
|
val dataMap = Arguments.createMap().apply {
|
|
111
109
|
putInt("code", -1)
|
|
112
110
|
putString("type", "OneKey_Lite")
|
|
@@ -118,6 +116,7 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
118
116
|
}
|
|
119
117
|
} catch (e: Exception) {
|
|
120
118
|
OneKeyLog.error("LiteCard", "stage=connection_failure type=${if (e is NFCExceptions) "nfc" else "other"}")
|
|
119
|
+
if (pendingTag.isClaimed()) return@launch
|
|
121
120
|
// 未知设备或连接失败
|
|
122
121
|
val dataMap = Arguments.createMap().apply {
|
|
123
122
|
putInt("code", -1)
|
|
@@ -138,7 +137,6 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
138
137
|
private fun releaseDevice() {
|
|
139
138
|
if (mShowDialogNumber.get() <= 0) return
|
|
140
139
|
|
|
141
|
-
mCurrentCardState = null
|
|
142
140
|
val decrementAndGet = mShowDialogNumber.decrementAndGet()
|
|
143
141
|
|
|
144
142
|
// 关闭连接结束 ui
|
|
@@ -175,11 +173,8 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
175
173
|
var receiveIsoDep: IsoDep? = null
|
|
176
174
|
|
|
177
175
|
if (tryReceiveResult.isSuccess) {
|
|
178
|
-
receiveIsoDep = tryReceiveResult.getOrNull()
|
|
176
|
+
receiveIsoDep = tryReceiveResult.getOrNull()?.claim()
|
|
179
177
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_source source=channel connected=${receiveIsoDep.isSafeConnected()}")
|
|
180
|
-
if (!receiveIsoDep.isSafeConnected()) {
|
|
181
|
-
receiveIsoDep = null
|
|
182
|
-
}
|
|
183
178
|
}
|
|
184
179
|
|
|
185
180
|
if (receiveIsoDep == null && lastIsoDep.isSafeConnected()) {
|
|
@@ -188,33 +183,18 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
188
183
|
}
|
|
189
184
|
if (receiveIsoDep == null) {
|
|
190
185
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_source source=wait")
|
|
191
|
-
receiveIsoDep = mNFCConnectedChannel.receive()
|
|
186
|
+
receiveIsoDep = mNFCConnectedChannel.receive()?.claim()
|
|
192
187
|
}
|
|
193
188
|
|
|
194
189
|
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=acquire_result available=${receiveIsoDep != null} connected=${receiveIsoDep.isSafeConnected()}")
|
|
195
190
|
lastIsoDep = receiveIsoDep
|
|
196
|
-
if (receiveIsoDep == null) {
|
|
197
|
-
// 取消连接
|
|
198
|
-
releaseDevice()
|
|
199
|
-
} else {
|
|
200
|
-
val initChannelRequest = OneKeyLiteCard.initRequest(receiveIsoDep)
|
|
201
|
-
|
|
202
|
-
mCurrentCardState = initChannelRequest
|
|
203
|
-
OneKeyLog.info("LiteCard", "acquireID=$acquireID stage=card_state isNewCard=${initChannelRequest.isNewCard} hasBackup=${initChannelRequest.hasBackup}")
|
|
204
|
-
|
|
205
|
-
// 展示连接结束 ui
|
|
206
|
-
emitOnNFCUIEvent(Arguments.createMap().also {
|
|
207
|
-
it.putInt("code", 2)
|
|
208
|
-
it.putString("message", "connected")
|
|
209
|
-
}.copy())
|
|
210
|
-
}
|
|
211
191
|
return receiveIsoDep
|
|
212
192
|
}
|
|
213
193
|
|
|
214
194
|
private suspend fun <T> handleOperation(
|
|
215
195
|
callback: Callback?,
|
|
216
196
|
operation: String,
|
|
217
|
-
execute: (
|
|
197
|
+
execute: (session: OneKeyLiteCard.CardSession) -> T
|
|
218
198
|
) {
|
|
219
199
|
val operationID = diagnosticSequence.incrementAndGet()
|
|
220
200
|
val started = SystemClock.elapsedRealtime()
|
|
@@ -224,15 +204,25 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
224
204
|
return
|
|
225
205
|
}
|
|
226
206
|
NfcPermissionUtils.checkPermission(topActivity) {
|
|
207
|
+
var cardState: CardState? = null
|
|
227
208
|
try {
|
|
228
209
|
OneKeyLog.debug("LiteCard","NFC permission check success")
|
|
229
210
|
val isoDep = acquireDevice() ?: return
|
|
230
|
-
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
|
+
}
|
|
231
221
|
OneKeyLog.info("LiteCard", "operation=$operation operationID=$operationID stage=callback code=0 elapsedMs=${SystemClock.elapsedRealtime() - started}")
|
|
232
|
-
callback?.invoke(null, executeResult,
|
|
222
|
+
callback?.invoke(null, executeResult, cardState.createArguments())
|
|
233
223
|
} catch (e: NFCExceptions) {
|
|
234
224
|
OneKeyLog.info("LiteCard", "operation=$operation operationID=$operationID stage=callback code=${e.code} elapsedMs=${SystemClock.elapsedRealtime() - started}")
|
|
235
|
-
callback?.invoke(e.createArguments(), null,
|
|
225
|
+
callback?.invoke(e.createArguments(), null, cardState.createArguments())
|
|
236
226
|
} finally {
|
|
237
227
|
releaseDevice()
|
|
238
228
|
}
|
|
@@ -265,9 +255,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
265
255
|
override fun getLiteInfo(callback: Callback?) {
|
|
266
256
|
launch {
|
|
267
257
|
OneKeyLog.debug("LiteCard","getLiteInfo")
|
|
268
|
-
handleOperation<Any>(callback, "getLiteInfo") {
|
|
258
|
+
handleOperation<Any>(callback, "getLiteInfo") { session ->
|
|
269
259
|
OneKeyLog.debug("LiteCard","getLiteInfo Obtain the device")
|
|
270
|
-
val cardInfo = OneKeyLiteCard.getCardInfo(
|
|
260
|
+
val cardInfo = OneKeyLiteCard.getCardInfo(session)
|
|
271
261
|
OneKeyLog.debug("LiteCard","getLiteInfo result obtained")
|
|
272
262
|
cardInfo.createArguments()
|
|
273
263
|
}
|
|
@@ -320,21 +310,15 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
320
310
|
return
|
|
321
311
|
}
|
|
322
312
|
launch {
|
|
323
|
-
handleOperation(callback, "setMnemonic") {
|
|
324
|
-
val serial = mCurrentCardState?.serialNum
|
|
325
|
-
if (expectedSerial != null && (serial.isNullOrEmpty() ||
|
|
326
|
-
serial == NfcConstant.NOT_MATCH_DEVICE || expectedSerial != serial)) {
|
|
327
|
-
OneKeyLog.info("LiteCard", "operation=setMnemonic stage=identity_mismatch")
|
|
328
|
-
throw NFCExceptions.DeviceMismatchException()
|
|
329
|
-
}
|
|
313
|
+
handleOperation(callback, "setMnemonic") { session ->
|
|
330
314
|
OneKeyLog.debug("LiteCard","setMnemonic Obtain the device")
|
|
331
315
|
val isSuccess =
|
|
332
316
|
OneKeyLiteCard.setMnemonic(
|
|
333
|
-
|
|
334
|
-
isoDep,
|
|
317
|
+
session,
|
|
335
318
|
mnemonic,
|
|
336
319
|
pwd,
|
|
337
|
-
overwrite
|
|
320
|
+
overwrite,
|
|
321
|
+
expectedSerial
|
|
338
322
|
)
|
|
339
323
|
if (!isSuccess) throw NFCExceptions.ExecFailureException()
|
|
340
324
|
OneKeyLog.debug("LiteCard","setMnemonic result success")
|
|
@@ -353,9 +337,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
353
337
|
}
|
|
354
338
|
launch {
|
|
355
339
|
OneKeyLog.debug("LiteCard","getMnemonicWithPin")
|
|
356
|
-
handleOperation(callback, "getMnemonicWithPin") {
|
|
340
|
+
handleOperation(callback, "getMnemonicWithPin") { session ->
|
|
357
341
|
OneKeyLog.debug("LiteCard","getMnemonicWithPin Obtain the device")
|
|
358
|
-
OneKeyLiteCard.getMnemonicWithPin(
|
|
342
|
+
OneKeyLiteCard.getMnemonicWithPin(session, pwd)
|
|
359
343
|
}
|
|
360
344
|
}
|
|
361
345
|
}
|
|
@@ -363,13 +347,14 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
363
347
|
override fun changePin(
|
|
364
348
|
oldPin: String,
|
|
365
349
|
newPin: String,
|
|
350
|
+
expectedSerial: String?,
|
|
366
351
|
callback: Callback?
|
|
367
352
|
) {
|
|
368
353
|
launch {
|
|
369
354
|
OneKeyLog.debug("LiteCard","changePin")
|
|
370
|
-
handleOperation(callback, "changePin") {
|
|
355
|
+
handleOperation(callback, "changePin") { session ->
|
|
371
356
|
OneKeyLog.debug("LiteCard","changePin Obtain the device")
|
|
372
|
-
OneKeyLiteCard.changPin(
|
|
357
|
+
OneKeyLiteCard.changPin(session, oldPin, newPin, expectedSerial)
|
|
373
358
|
}
|
|
374
359
|
}
|
|
375
360
|
}
|
|
@@ -377,9 +362,9 @@ class ReactNativeLiteCardModule(val reactContext: ReactApplicationContext) :
|
|
|
377
362
|
override fun reset(callback: Callback?) {
|
|
378
363
|
launch {
|
|
379
364
|
OneKeyLog.debug("LiteCard","reset")
|
|
380
|
-
handleOperation(callback, "reset") {
|
|
365
|
+
handleOperation(callback, "reset") { session ->
|
|
381
366
|
OneKeyLog.debug("LiteCard","reset Obtain the device")
|
|
382
|
-
val isSuccess = OneKeyLiteCard.reset(
|
|
367
|
+
val isSuccess = OneKeyLiteCard.reset(session)
|
|
383
368
|
if (!isSuccess) throw NFCExceptions.ExecFailureException()
|
|
384
369
|
OneKeyLog.debug("LiteCard","reset result success")
|
|
385
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(
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
#if !TARGET_OS_SIMULATOR
|
|
10
10
|
|
|
11
11
|
#import "GPChannelSDK.h"
|
|
12
|
+
#import "GPChannelSDKCore.h"
|
|
12
13
|
#import "OKNFCLiteDefine.h"
|
|
13
14
|
#import "NFCConfig.h"
|
|
14
15
|
|
|
@@ -19,6 +20,22 @@
|
|
|
19
20
|
#import "OKNFCBridge.h"
|
|
20
21
|
#import "LCLogger.h"
|
|
21
22
|
|
|
23
|
+
#if !TARGET_OS_SIMULATOR
|
|
24
|
+
static NSString *sourceIdForLog() {
|
|
25
|
+
NSString *source = [NSString stringWithUTF8String:(const char *)GPChannelSDKCoreVersionString];
|
|
26
|
+
if (source.length != 74 || ![source hasPrefix:@"source-v1:"]) return @"unavailable";
|
|
27
|
+
for (NSUInteger index = 10; index < source.length; ++index) {
|
|
28
|
+
unichar digit = [source characterAtIndex:index];
|
|
29
|
+
if (!((digit >= '0' && digit <= '9') || (digit >= 'a' && digit <= 'f'))) return @"unavailable";
|
|
30
|
+
}
|
|
31
|
+
NSMutableArray<NSString *> *groups = [NSMutableArray array];
|
|
32
|
+
for (NSUInteger offset = 10; offset < source.length; offset += 8) {
|
|
33
|
+
[groups addObject:[source substringWithRange:NSMakeRange(offset, 8)]];
|
|
34
|
+
}
|
|
35
|
+
return [@"source-v1:" stringByAppendingString:[groups componentsJoinedByString:@"-"]];
|
|
36
|
+
}
|
|
37
|
+
#endif
|
|
38
|
+
|
|
22
39
|
@implementation OKNFCBridge
|
|
23
40
|
|
|
24
41
|
#if !TARGET_OS_SIMULATOR
|
|
@@ -39,6 +56,11 @@
|
|
|
39
56
|
} else {
|
|
40
57
|
rv = JUB_GPC_BuildAPDU(cla, ins, p1, p2, c_data, &c_apdu);
|
|
41
58
|
}
|
|
59
|
+
@try {
|
|
60
|
+
[LCLogger info:[NSString stringWithFormat:@"stage=command_build nativeSourceId=%@ safe=%d sdkReturnCode=%lu outputPresent=%d", sourceIdForLog(), encrypt, (unsigned long)rv, c_apdu != nullptr]];
|
|
61
|
+
} @catch (NSException *exception) {
|
|
62
|
+
// Diagnostics must not replace the SDK result.
|
|
63
|
+
}
|
|
42
64
|
|
|
43
65
|
if (JUBR_OK != rv) {
|
|
44
66
|
return nil;
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
|
|
35
35
|
- (void)getMnemonicWithPin:(NSString *)pin complete:(GetMnemonicCallback)complete;
|
|
36
36
|
|
|
37
|
-
- (void)changePin:(NSString *)oldPin to:(NSString *)newPin complete:(ChangePinCallback)complete;
|
|
37
|
+
- (void)changePin:(NSString *)oldPin to:(NSString *)newPin expectedSerial:(NSString * _Nullable)expectedSerial complete:(ChangePinCallback)complete;
|
|
38
38
|
|
|
39
39
|
|
|
40
40
|
@end
|