@onekeyfe/react-native-lite-card 6.7.0-4 → 6.7.0-40

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 (237) hide show
  1. package/README.md +12 -2
  2. package/ReactNativeLiteCard.podspec +31 -2
  3. package/android/build.gradle +8 -0
  4. package/android/consumer-rules.pro +4 -0
  5. package/android/src/main/cpp/CMakeLists.txt +6 -2
  6. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +60 -49
  7. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +7 -7
  8. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +117 -39
  9. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +6 -3
  10. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +24 -2
  11. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +46 -19
  12. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +2 -2
  13. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +38 -1
  14. package/ios/Classes/OKNFCBridge.mm +24 -1
  15. package/ios/Classes/OKNFTLite/OKLiteV1.m +20 -10
  16. package/ios/Classes/OKNFTLite/OKLiteV2.m +13 -5
  17. package/ios/Classes/OKNFTLite/OKNFCManager.h +2 -2
  18. package/ios/Classes/OKNFTLite/OKNFCManager.m +110 -18
  19. package/ios/ReactNativeLiteCard.mm +23 -11
  20. package/lib/module/index.js +4 -4
  21. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +4 -4
  22. package/lib/typescript/src/index.d.ts +2 -2
  23. package/package.json +8 -8
  24. package/sdk/CMakeLists.txt +121 -0
  25. package/sdk/README.md +190 -0
  26. package/sdk/android/implJni.cpp +216 -0
  27. package/sdk/deps/jsoncpp/LICENSE +55 -0
  28. package/sdk/deps/jsoncpp/include/CMakeLists.txt +2 -0
  29. package/sdk/deps/jsoncpp/include/json/allocator.h +98 -0
  30. package/sdk/deps/jsoncpp/include/json/assertions.h +54 -0
  31. package/sdk/deps/jsoncpp/include/json/autolink.h +25 -0
  32. package/sdk/deps/jsoncpp/include/json/config.h +187 -0
  33. package/sdk/deps/jsoncpp/include/json/features.h +61 -0
  34. package/sdk/deps/jsoncpp/include/json/forwards.h +37 -0
  35. package/sdk/deps/jsoncpp/include/json/json.h +15 -0
  36. package/sdk/deps/jsoncpp/include/json/reader.h +411 -0
  37. package/sdk/deps/jsoncpp/include/json/value.h +888 -0
  38. package/sdk/deps/jsoncpp/include/json/version.h +20 -0
  39. package/sdk/deps/jsoncpp/include/json/writer.h +357 -0
  40. package/sdk/deps/jsoncpp/src/lib_json/CMakeLists.txt +117 -0
  41. package/sdk/deps/jsoncpp/src/lib_json/json_reader.cpp +2060 -0
  42. package/sdk/deps/jsoncpp/src/lib_json/json_tool.h +114 -0
  43. package/sdk/deps/jsoncpp/src/lib_json/json_value.cpp +1661 -0
  44. package/sdk/deps/jsoncpp/src/lib_json/json_valueiterator.inl +167 -0
  45. package/sdk/deps/jsoncpp/src/lib_json/json_writer.cpp +1233 -0
  46. package/sdk/deps/jsoncpp/src/lib_json/version.h.in +20 -0
  47. package/sdk/deps/trezor-crypto/AUTHORS +2 -0
  48. package/sdk/deps/trezor-crypto/LICENSE +22 -0
  49. package/sdk/deps/trezor-crypto/include/TrezorCrypto/TrezorCrypto.h +29 -0
  50. package/sdk/deps/trezor-crypto/include/TrezorCrypto/address.h +44 -0
  51. package/sdk/deps/trezor-crypto/include/TrezorCrypto/aes.h +224 -0
  52. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base32.h +49 -0
  53. package/sdk/deps/trezor-crypto/include/TrezorCrypto/base58.h +58 -0
  54. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bignum.h +169 -0
  55. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip32.h +114 -0
  56. package/sdk/deps/trezor-crypto/include/TrezorCrypto/bip39.h +66 -0
  57. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake256.h +61 -0
  58. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2b.h +50 -0
  59. package/sdk/deps/trezor-crypto/include/TrezorCrypto/blake2s.h +49 -0
  60. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cash_addr.h +109 -0
  61. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-config.h +316 -0
  62. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-sync.h +289 -0
  63. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/ecrypt-types.h +53 -0
  64. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305/poly1305-donna.h +28 -0
  65. package/sdk/deps/trezor-crypto/include/TrezorCrypto/chacha20poly1305.h +27 -0
  66. package/sdk/deps/trezor-crypto/include/TrezorCrypto/cmac.h +44 -0
  67. package/sdk/deps/trezor-crypto/include/TrezorCrypto/curves.h +60 -0
  68. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ecdsa.h +112 -0
  69. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-32bit.h +87 -0
  70. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/curve25519-donna-helpers.h +30 -0
  71. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-blake2b.h +21 -0
  72. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-32bit-tables.h +25 -0
  73. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-basepoint-table.h +10 -0
  74. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-impl-base.h +110 -0
  75. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna-portable.h +32 -0
  76. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-donna.h +60 -0
  77. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-keccak.h +21 -0
  78. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/ed25519-sha3.h +21 -0
  79. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna/modm-donna-32bit.h +87 -0
  80. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519-donna.h +60 -0
  81. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ed25519.h +42 -0
  82. package/sdk/deps/trezor-crypto/include/TrezorCrypto/endian.h +132 -0
  83. package/sdk/deps/trezor-crypto/include/TrezorCrypto/groestl.h +104 -0
  84. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hasher.h +90 -0
  85. package/sdk/deps/trezor-crypto/include/TrezorCrypto/hmac.h +60 -0
  86. package/sdk/deps/trezor-crypto/include/TrezorCrypto/memzero.h +16 -0
  87. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/base58.h +42 -0
  88. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/monero.h +13 -0
  89. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/range_proof.h +30 -0
  90. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/serialize.h +15 -0
  91. package/sdk/deps/trezor-crypto/include/TrezorCrypto/monero/xmr.h +67 -0
  92. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nano.h +56 -0
  93. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nem.h +199 -0
  94. package/sdk/deps/trezor-crypto/include/TrezorCrypto/nist256p1.h +35 -0
  95. package/sdk/deps/trezor-crypto/include/TrezorCrypto/pbkdf2.h +64 -0
  96. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rand.h +44 -0
  97. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rc4.h +37 -0
  98. package/sdk/deps/trezor-crypto/include/TrezorCrypto/rfc6979.h +40 -0
  99. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripemd160.h +28 -0
  100. package/sdk/deps/trezor-crypto/include/TrezorCrypto/ripple/base58.h +45 -0
  101. package/sdk/deps/trezor-crypto/include/TrezorCrypto/schnorr.h +48 -0
  102. package/sdk/deps/trezor-crypto/include/TrezorCrypto/script.h +30 -0
  103. package/sdk/deps/trezor-crypto/include/TrezorCrypto/scrypt.h +68 -0
  104. package/sdk/deps/trezor-crypto/include/TrezorCrypto/secp256k1.h +59 -0
  105. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha2.h +125 -0
  106. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sha3.h +86 -0
  107. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/keypair.h +20 -0
  108. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10.h +85 -0
  109. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/ed25519_ref10_fe_25_5.h +43 -0
  110. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/constants.h +29 -0
  111. package/sdk/deps/trezor-crypto/include/TrezorCrypto/sodium/private/fe_25_5/fe.h +25 -0
  112. package/sdk/deps/trezor-crypto/src/address.c +87 -0
  113. package/sdk/deps/trezor-crypto/src/aes/aes_modes.c +959 -0
  114. package/sdk/deps/trezor-crypto/src/aes/aescrypt.c +307 -0
  115. package/sdk/deps/trezor-crypto/src/aes/aeskey.c +560 -0
  116. package/sdk/deps/trezor-crypto/src/aes/aesopt.h +784 -0
  117. package/sdk/deps/trezor-crypto/src/aes/aestab.c +418 -0
  118. package/sdk/deps/trezor-crypto/src/aes/aestab.h +173 -0
  119. package/sdk/deps/trezor-crypto/src/base32.c +233 -0
  120. package/sdk/deps/trezor-crypto/src/base58.c +298 -0
  121. package/sdk/deps/trezor-crypto/src/bignum.c +808 -0
  122. package/sdk/deps/trezor-crypto/src/bip32.c +852 -0
  123. package/sdk/deps/trezor-crypto/src/bip39.c +266 -0
  124. package/sdk/deps/trezor-crypto/src/bip39_english.h +2074 -0
  125. package/sdk/deps/trezor-crypto/src/blake256.c +235 -0
  126. package/sdk/deps/trezor-crypto/src/blake2_common.h +39 -0
  127. package/sdk/deps/trezor-crypto/src/blake2b.c +333 -0
  128. package/sdk/deps/trezor-crypto/src/blake2s.c +318 -0
  129. package/sdk/deps/trezor-crypto/src/cash_addr.c +195 -0
  130. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha20poly1305.c +60 -0
  131. package/sdk/deps/trezor-crypto/src/chacha20poly1305/chacha_merged.c +246 -0
  132. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-machine.h +49 -0
  133. package/sdk/deps/trezor-crypto/src/chacha20poly1305/ecrypt-portable.h +275 -0
  134. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna-32.h +219 -0
  135. package/sdk/deps/trezor-crypto/src/chacha20poly1305/poly1305-donna.c +179 -0
  136. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.c +48 -0
  137. package/sdk/deps/trezor-crypto/src/chacha20poly1305/rfc7539.h +10 -0
  138. package/sdk/deps/trezor-crypto/src/check_mem.h +30 -0
  139. package/sdk/deps/trezor-crypto/src/cmac.c +342 -0
  140. package/sdk/deps/trezor-crypto/src/curves.c +48 -0
  141. package/sdk/deps/trezor-crypto/src/ecdsa.c +1355 -0
  142. package/sdk/deps/trezor-crypto/src/ed25519-donna/README.md +183 -0
  143. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-32bit.c +681 -0
  144. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-helpers.c +66 -0
  145. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.c +67 -0
  146. package/sdk/deps/trezor-crypto/src/ed25519-donna/curve25519-donna-scalarmult-base.h +8 -0
  147. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-blake2b.c +8 -0
  148. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-32bit-tables.c +63 -0
  149. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-basepoint-table.c +261 -0
  150. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-donna-impl-base.c +722 -0
  151. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-blake2b.h +23 -0
  152. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-keccak.h +23 -0
  153. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom-sha3.h +23 -0
  154. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-hash-custom.h +23 -0
  155. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-keccak.c +8 -0
  156. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519-sha3.c +8 -0
  157. package/sdk/deps/trezor-crypto/src/ed25519-donna/ed25519.c +311 -0
  158. package/sdk/deps/trezor-crypto/src/ed25519-donna/modm-donna-32bit.c +517 -0
  159. package/sdk/deps/trezor-crypto/src/groestl.c +783 -0
  160. package/sdk/deps/trezor-crypto/src/groestl_internal.h +419 -0
  161. package/sdk/deps/trezor-crypto/src/hasher.c +186 -0
  162. package/sdk/deps/trezor-crypto/src/hmac.c +179 -0
  163. package/sdk/deps/trezor-crypto/src/memzero.c +6 -0
  164. package/sdk/deps/trezor-crypto/src/monero/base58.c +245 -0
  165. package/sdk/deps/trezor-crypto/src/monero/int-util.h +77 -0
  166. package/sdk/deps/trezor-crypto/src/monero/range_proof.c +115 -0
  167. package/sdk/deps/trezor-crypto/src/monero/serialize.c +54 -0
  168. package/sdk/deps/trezor-crypto/src/monero/xmr.c +136 -0
  169. package/sdk/deps/trezor-crypto/src/nano.c +151 -0
  170. package/sdk/deps/trezor-crypto/src/nem.c +569 -0
  171. package/sdk/deps/trezor-crypto/src/nist256p1.c +63 -0
  172. package/sdk/deps/trezor-crypto/src/nist256p1.table +1664 -0
  173. package/sdk/deps/trezor-crypto/src/options.h +43 -0
  174. package/sdk/deps/trezor-crypto/src/pbkdf2.c +177 -0
  175. package/sdk/deps/trezor-crypto/src/rand.c +51 -0
  176. package/sdk/deps/trezor-crypto/src/rc4.c +56 -0
  177. package/sdk/deps/trezor-crypto/src/ripemd160.c +343 -0
  178. package/sdk/deps/trezor-crypto/src/ripple/base58.c +76 -0
  179. package/sdk/deps/trezor-crypto/src/schnorr.c +208 -0
  180. package/sdk/deps/trezor-crypto/src/script.c +64 -0
  181. package/sdk/deps/trezor-crypto/src/scrypt.c +343 -0
  182. package/sdk/deps/trezor-crypto/src/secp256k1.c +160 -0
  183. package/sdk/deps/trezor-crypto/src/secp256k1.table +1664 -0
  184. package/sdk/deps/trezor-crypto/src/setup.py +38 -0
  185. package/sdk/deps/trezor-crypto/src/sha2.c +1314 -0
  186. package/sdk/deps/trezor-crypto/src/sha3.c +395 -0
  187. package/sdk/deps/trezor-crypto/src/shell.nix +6 -0
  188. package/sdk/deps/trezor-crypto/src/sodium/keypair.c +63 -0
  189. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10.c +481 -0
  190. package/sdk/deps/trezor-crypto/src/sodium/private/ed25519_ref10_fe_25_5.c +885 -0
  191. package/sdk/deps/trezor-crypto/src/sodium/private/fe_25_5/fe.c +237 -0
  192. package/sdk/gpc/include/scp03/scp03.hpp +80 -0
  193. package/sdk/gpc/include/scp11/scp11.hpp +492 -0
  194. package/sdk/gpc/include/scp11/scp11c.hpp +58 -0
  195. package/sdk/gpc/include/tag.h +68 -0
  196. package/sdk/gpc/include/tlv.hpp +94 -0
  197. package/sdk/gpc/include/tlvparse.hpp +24 -0
  198. package/sdk/gpc/src/scp03.cpp +243 -0
  199. package/sdk/gpc/src/scp11.cpp +798 -0
  200. package/sdk/gpc/src/scp11c.cpp +136 -0
  201. package/sdk/gpc/src/tlv.cpp +159 -0
  202. package/sdk/gpc/src/tlvparse.c +174 -0
  203. package/{framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers → sdk/include}/GPChannelSDK.h +0 -0
  204. package/sdk/include/GPChannelSDKCore.h +10 -0
  205. package/sdk/include/context/BaseContext.h +56 -0
  206. package/sdk/include/device/ApduBuilder.hpp +72 -0
  207. package/sdk/include/mSIGNA/stdutils/uchar_vector.h +36 -0
  208. package/sdk/include/utility/Apdu.hpp +172 -0
  209. package/sdk/include/utility/Debug.hpp +3 -0
  210. package/sdk/include/utility/util.h +104 -0
  211. package/sdk/scripts/generate-source-id.rb +22 -0
  212. package/sdk/source-id-manifest.txt +19 -0
  213. package/sdk/source-id.cmake +31 -0
  214. package/sdk/src/ApduBuilder.cpp +332 -0
  215. package/sdk/src/GPChannelSDK.cpp +258 -0
  216. package/sdk/src/random.c +25 -0
  217. package/sdk/src/version.c +3 -0
  218. package/sdk/tests/android/GPChannelProxy.java +55 -0
  219. package/sdk/tests/android_verify.py +124 -0
  220. package/sdk/tests/safety.cpp +757 -0
  221. package/sdk/tests/verify.py +284 -0
  222. package/src/NativeReactNativeLiteCard.ts +4 -2
  223. package/src/index.tsx +14 -3
  224. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  225. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  226. package/framework/GPChannelSDKCore.xcframework/Info.plist +0 -52
  227. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  228. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  229. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  230. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  231. package/framework/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
  232. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  233. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +0 -315
  234. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +0 -19
  235. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  236. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +0 -7
  237. package/framework/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +0 -147
@@ -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
- @Volatile
27
- private var mCardConnection: Connection? = null
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,68 +81,114 @@ object OneKeyLiteCard {
61
81
  NfcUtils.mNfcAdapter?.disableForegroundDispatch(activity)
62
82
  }
63
83
 
64
- // first init channel ,can filter not match device
65
- @Throws(NFCExceptions::class)
66
- suspend fun initRequest(isoDep: IsoDep?) = withContext(Dispatchers.IO) {
67
- if (isoDep == null) {
68
- throw NFCExceptions.ConnectionFailException()
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(isoDep: IsoDep, pin: String?): Boolean {
114
+ private fun setupNewPinRequest(session: CardSession, pin: String?): Boolean {
77
115
  if (pin.isNullOrEmpty()) {
78
116
  throw NFCExceptions.InputPasswordEmptyException()
79
117
  }
80
- return mCardConnection?.setupNewPin(pin) ?: false
118
+ return session.connection.setupNewPin(pin)
81
119
  }
82
120
 
83
121
  @Throws(NFCExceptions::class)
84
- fun changePinRequest(isoDep: IsoDep, oldPwd: String?, newPwd: String?): Int {
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 mCardConnection?.changePin(oldPwd, newPwd) ?: 0
126
+ return session.connection.changePin(oldPwd, newPwd)
89
127
  }
90
128
 
91
129
  @Throws(NFCExceptions::class)
92
- fun verifyPinBackupRequest(isoDep: IsoDep, verifyPin: String?): Int {
130
+ private fun verifyPinBackupRequest(session: CardSession, verifyPin: String?): Int {
93
131
  if (verifyPin.isNullOrEmpty()) {
94
132
  throw NFCExceptions.InputPasswordEmptyException()
95
133
  }
96
- return mCardConnection?.startVerifyPin(verifyPin) ?: 0
134
+ return session.connection.startVerifyPin(verifyPin)
97
135
  }
98
136
 
99
137
  @Throws(NFCExceptions::class)
100
- fun getCardInfo(isoDep: IsoDep): CardState {
101
- return mCardConnection?.getCardInfo() ?: throw NFCExceptions.ConnectionFailException()
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
- cardState: CardState?,
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 (cardState == null) throw NFCExceptions.ConnectionFailException()
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
- printLog(TAG, "--> setMnemonic: isNewCard:${cardState?.isNewCard}, hasBackup:${cardState?.hasBackup}")
177
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=card_state isNewCard=${cardState.isNewCard} hasBackup=${cardState.hasBackup} overwrite=$overwrite wallTimeMs=${System.currentTimeMillis()}")
115
178
 
116
179
  if (!overwrite) {
117
180
  // 不是覆写要验证是否已经已经存有备份
118
181
  if (!cardState.isNewCard || cardState.hasBackup) {
182
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=already_initialized")
119
183
  throw NFCExceptions.InitializedException()
120
184
  }
121
185
  }
122
186
 
123
- if (overwrite && mCardConnection?.getCardType() == AppleCardType.V2) {
187
+ if (overwrite && session.connection.getCardType() == AppleCardType.V2) {
124
188
  // Verify the password before clearing the card
125
- val verifyPin = verifyPinBackupRequest(isoDep, pwd)
189
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=overwrite_verify_begin")
190
+ val verifyPin = verifyPinBackupRequest(session, pwd)
191
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=overwrite_verify_result success=${verifyPin == NfcConstant.VERIFY_SUCCESS}")
126
192
  if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
127
193
  handlerVerifyPinError(cardState, verifyPin)
128
194
  }
@@ -133,12 +199,17 @@ object OneKeyLiteCard {
133
199
 
134
200
  if (cardState.isNewCard) {
135
201
  // 如果是新卡设置密码
136
- if (!setupNewPinRequest(isoDep, pwd)) {
202
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=set_pin_begin")
203
+ val setupSuccess = setupNewPinRequest(session, pwd)
204
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=set_pin_result success=$setupSuccess")
205
+ if (!setupSuccess) {
137
206
  throw NFCExceptions.InitPasswordException()
138
207
  }
139
208
  }
140
209
 
141
- val verifyPin = verifyPinBackupRequest(isoDep, pwd)
210
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=verify_pin_begin")
211
+ val verifyPin = verifyPinBackupRequest(session, pwd)
212
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=verify_pin_result success=${verifyPin == NfcConstant.VERIFY_SUCCESS}")
142
213
  if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
143
214
  if (overwrite) {
144
215
  handlerVerifyPinError(cardState, verifyPin)
@@ -147,24 +218,26 @@ object OneKeyLiteCard {
147
218
  }
148
219
  }
149
220
 
150
- if (mnemonic.isNullOrEmpty()) throw NFCExceptions.ExecFailureException()
151
- return mCardConnection?.backupData(mnemonic) == true
221
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_begin")
222
+ val backupSuccess = session.connection.backupData(mnemonic)
223
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_result success=$backupSuccess")
224
+ return backupSuccess
152
225
  }
153
226
 
154
227
  @Throws(NFCExceptions::class)
155
- fun getMnemonicWithPin(cardState: CardState?, isoDep: IsoDep, pwd: String): String {
156
- if (cardState == null) throw NFCExceptions.ConnectionFailException()
228
+ internal fun getMnemonicWithPin(session: CardSession, pwd: String): String {
229
+ val cardState = session.cardState
157
230
 
158
231
  if (cardState.isNewCard || !cardState.hasBackup) {
159
232
  throw NFCExceptions.NotInitializedException()
160
233
  }
161
234
 
162
- val verifyPin = verifyPinBackupRequest(isoDep, pwd)
235
+ val verifyPin = verifyPinBackupRequest(session, pwd)
163
236
  if (verifyPin != NfcConstant.VERIFY_SUCCESS) {
164
237
  handlerVerifyPinError(cardState, verifyPin)
165
238
  }
166
239
 
167
- val result = mCardConnection?.exportData()
240
+ val result = session.connection.exportData()
168
241
 
169
242
  if (result.isNullOrEmpty()) {
170
243
  throw NFCExceptions.NotInitializedException()
@@ -173,14 +246,19 @@ object OneKeyLiteCard {
173
246
  }
174
247
 
175
248
  @Throws(NFCExceptions::class)
176
- fun changPin(cardState: CardState?, isoDep: IsoDep, oldPwd: String, newPwd: String): Boolean {
177
- if (cardState == null) throw NFCExceptions.ConnectionFailException()
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
+ }
178
256
 
179
257
  if (cardState.isNewCard || !cardState.hasBackup) {
180
258
  throw NFCExceptions.NotInitializedException()
181
259
  }
182
260
 
183
- val result = changePinRequest(isoDep, oldPwd, newPwd)
261
+ val result = changePinRequest(session, oldPwd, newPwd)
184
262
  handlerVerifyPinError(cardState, result, true)
185
263
 
186
264
  return when (result) {
@@ -198,8 +276,8 @@ object OneKeyLiteCard {
198
276
  }
199
277
  }
200
278
 
201
- fun reset(isoDep: IsoDep): Boolean {
202
- return mCardConnection?.resetCard() == NfcConstant.RESET_PIN_SUCCESS
279
+ internal fun reset(session: CardSession): Boolean {
280
+ return session.connection.resetCard() == NfcConstant.RESET_PIN_SUCCESS
203
281
  }
204
282
 
205
283
  private fun handlerVerifyPinError(
@@ -9,11 +9,14 @@ data class SendResponse(
9
9
  val sw2: Byte,
10
10
  val data: String? = null,
11
11
  // Processed result
12
- var result: String? = null
12
+ var result: String? = null,
13
+ var parseSuccess: Boolean = true
13
14
  ) {
14
- fun isSuccess() = sw1 == 0x90.toByte() && sw2 == 0x00.toByte()
15
+ fun isSuccess() = parseSuccess && sw1 == 0x90.toByte() && sw2 == 0x00.toByte()
15
16
 
16
- fun isConnectFailure() = sw1 == 0x6F.toByte() && sw2 == 0x00.toByte()
17
+ fun isConnectFailure() =
18
+ (sw1 == 0x6F.toByte() && sw2 == 0x00.toByte()) ||
19
+ (sw1 == 0xFF.toByte() && sw2 == 0xFF.toByte())
17
20
 
18
21
  fun isEmptyData() = data.isNullOrEmpty()
19
22
 
@@ -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 =
@@ -97,7 +105,7 @@ class Command(
97
105
  )
98
106
  }
99
107
 
100
- private fun parseResponse(response: String): String {
108
+ private fun parseResponse(response: String): String? {
101
109
  return if (useSafeChannel || (useSafeChannelWhenOpen && hasOpenSafeChannel)) {
102
110
  nativeGPCParseSafeAPDUResponse(response)
103
111
  } else {
@@ -106,12 +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()}")
124
+ if (apdu.isEmpty()) {
125
+ return SendResponse("0xFFFF", 0xFF.toByte(), 0xFF.toByte(), parseSuccess = false)
126
+ }
112
127
  val result = Connection.send(connection.isoDep, apdu)
113
- val resultObject = CardResponse.objectFromData(parseResponse(result.rawResponse))
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)
114
133
  result.result = resultObject?.response ?: ""
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}")
115
136
  return result
116
137
  }
117
138
  }
@@ -165,6 +186,7 @@ class CommandGenerator {
165
186
 
166
187
  return Command(
167
188
  cardType = cardType,
189
+ type = type,
168
190
  area = area,
169
191
  command = command,
170
192
  ignoreSafeChannel = ignoreSafeChannel,
@@ -1,6 +1,7 @@
1
1
  package com.onekeyfe.reactnativelitecard.onekeyLite.nfc
2
2
 
3
3
  import android.nfc.TagLostException
4
+ import com.margelo.nitro.nativelogger.OneKeyLog
4
5
  import android.nfc.tech.IsoDep
5
6
  import org.haobtc.onekey.card.gpchannel.GPChannelNatives
6
7
  import org.haobtc.onekey.card.gpchannel.GPChannelNatives.nativeGPCFinalize
@@ -49,9 +50,11 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
49
50
 
50
51
  return SendResponse(byteArr2HexStr(response), sw1, sw2, byteArr2HexStr(resp))
51
52
  } catch (e: TagLostException) {
53
+ OneKeyLog.info("LiteCard", "stage=transport_failure type=tag_lost")
52
54
  // throw NFCExceptions.InterruptException()
53
55
  return SendResponse("0xFFFF", 0xFF.toByte(), 0xFF.toByte())
54
56
  } catch (e: IOException) {
57
+ OneKeyLog.info("LiteCard", "stage=transport_failure type=io")
55
58
  // e.printStackTrace()
56
59
  return SendResponse("0xFFFF", 0xFF.toByte(), 0xFF.toByte())
57
60
  }
@@ -63,6 +66,8 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
63
66
  private var hasOpenSafeChannel: Boolean = false
64
67
  private var hasSetupPin: Boolean = false
65
68
  private var hasBackup: Boolean = false
69
+ private var pinStatusValid: Boolean = false
70
+ private var backupStatusValid: Boolean = false
66
71
  private var mCommandArea = CommandArea.None
67
72
 
68
73
  init {
@@ -84,7 +89,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
84
89
  )
85
90
  val res = command.send(this)
86
91
  printLog(
87
- TAG, "<--- selectPrimarySafety end: ${res.getCode()} ${res.data} area:${mCommandArea}"
92
+ TAG, "<--- selectPrimarySafety end: ${res.getCode()} area:${mCommandArea}"
88
93
  )
89
94
 
90
95
  if (res.isSuccess() && !res.isEmptyData()) {
@@ -104,7 +109,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
104
109
  )
105
110
  val res = command.send(this)
106
111
 
107
- printLog(TAG, "<--- selectApplet end: ${res.getCode()} ${res.data} area:${mCommandArea}")
112
+ printLog(TAG, "<--- selectApplet end: ${res.getCode()} area:${mCommandArea}")
108
113
 
109
114
  if (res.isSuccess()) {
110
115
  mCommandArea = CommandArea.BackupApplet
@@ -117,16 +122,21 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
117
122
  printLog(TAG, "---> getDeviceCertificate begin")
118
123
  // #1.NFC:GET CERT.SD.ECKA 获取智能卡证书
119
124
  val apdu = buildGPCAPDU(APDUParam(0x80, 0xCA, 0xBF, 0x21, "A60483021518"))
125
+ if (apdu.isEmpty()) return null
120
126
  val rawCert = send(isoDep, apdu)
121
127
  printLog(
122
128
  TAG,
123
- "<--- getDeviceCertificate end: ${rawCert.getCode()} ${rawCert.data} area:${mCommandArea}"
129
+ "<--- getDeviceCertificate end: ${rawCert.getCode()} area:${mCommandArea}"
124
130
  )
131
+ if (!rawCert.isSuccess() || rawCert.isEmptyData()) {
132
+ return null
133
+ }
125
134
 
126
- val cert = GPChannelNatives.nativeGPCTLVDecode(rawCert.data)
127
- val certificate = CardResponse.objectFromData(cert).response
135
+ val cert = GPChannelNatives.nativeGPCTLVDecode(rawCert.data) ?: return null
136
+ val certificate = CardResponse.objectFromData(cert)?.response
137
+ if (certificate.isNullOrEmpty()) return null
128
138
 
129
- val certInfo = GPChannelNatives.nativeGPCParseCertificate(certificate)
139
+ val certInfo = GPChannelNatives.nativeGPCParseCertificate(certificate) ?: return null
130
140
  return ParsedCertInfo.objectFromData(certInfo)
131
141
  }
132
142
 
@@ -140,12 +150,18 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
140
150
 
141
151
  printLog(TAG, "0. ---> getDeviceCertificate begin")
142
152
  val certInfo = getDeviceCertificate() ?: return false
153
+ if (certInfo.subjectID.isNullOrEmpty() || certInfo.sn.isNullOrEmpty()) {
154
+ return false
155
+ }
156
+ if (mCardSerialNumber != NfcConstant.NOT_MATCH_DEVICE && certInfo.sn != mCardSerialNumber) {
157
+ return false
158
+ }
143
159
  printLog(TAG, "0. <--- getDeviceCertificate end")
144
160
 
145
161
  printLog(TAG, "1. ---> nativeGPCInitialize begin")
146
162
  val param = SecureChanelParam.objectFromData(
147
163
  KeysNativeProvider().getLiteSecureChannelInitParams(Utils.getApp())
148
- )
164
+ ) ?: return false
149
165
  param.cardGroupID = certInfo.subjectID
150
166
  printLog(TAG, "nativeGPCInitialize read param done")
151
167
  val initializeStatus = GPChannelNatives.nativeGPCInitialize(param.toString())
@@ -161,7 +177,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
161
177
  val securityRes = command.send(this)
162
178
  printLog(
163
179
  TAG,
164
- "2. <--- Perform security operation end: ${securityRes.getCode()} ${securityRes.data}"
180
+ "2. <--- Perform security operation end: ${securityRes.getCode()}"
165
181
  )
166
182
  if (!securityRes.isSuccess()) {
167
183
  return false
@@ -169,6 +185,9 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
169
185
 
170
186
  printLog(TAG, "3. ---> mutual authenticate begin")
171
187
  val authData = GPChannelNatives.nativeGPCBuildMutualAuthData()
188
+ if (authData.isNullOrEmpty()) {
189
+ return false
190
+ }
172
191
  printLog(TAG, "mutual authenticate data")
173
192
  val authCommand = mCommandGenerator.generalCommand(
174
193
  mCardType, CommandType.VERIFY_AUTH_DATA, hasOpenSafeChannel, authData
@@ -206,9 +225,11 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
206
225
  mCardType, CommandType.GET_BACKUP_STATUS, hasOpenSafeChannel
207
226
  )
208
227
  val res = command.send(this)
209
- printLog(TAG, "<--- getBackupStatus end: ${res.getCode()} ${res.data} area:${mCommandArea}")
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"}")
210
231
 
211
- if (!res.isSuccess()) {
232
+ if (!backupStatusValid) {
212
233
  return false
213
234
  }
214
235
 
@@ -221,9 +242,11 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
221
242
  mCardType, CommandType.GET_PIN_STATUS, hasOpenSafeChannel, "DFFF028105"
222
243
  )
223
244
  val res = command.send(this)
224
- printLog(TAG, "<--- getPinStatus end: ${res.getCode()} ${res.data} area:${mCommandArea}")
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"}")
225
248
 
226
- if (!res.isSuccess()) {
249
+ if (!pinStatusValid) {
227
250
  return false
228
251
  }
229
252
 
@@ -239,7 +262,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
239
262
  )
240
263
  val res = command.send(this)
241
264
 
242
- printLog(TAG, "<--- getSerialNumber end: ${res.getCode()} ${res.data} area:${mCommandArea}")
265
+ printLog(TAG, "<--- getSerialNumber end: ${res.getCode()} area:${mCommandArea}")
243
266
  if (!res.isSuccess()) {
244
267
  return NfcConstant.NOT_MATCH_DEVICE
245
268
  }
@@ -256,7 +279,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
256
279
  )
257
280
  val res = command.send(this)
258
281
 
259
- printLog(TAG, "<--- getRetryNum end: ${res.getCode()} ${res.data} area:${mCommandArea}")
282
+ printLog(TAG, "<--- getRetryNum end: ${res.getCode()} area:${mCommandArea}")
260
283
 
261
284
  if (res.isEmptyData() || !res.isSuccess()) {
262
285
  return NfcConstant.GET_RETRY_NUM_INTERRUPT_STATUS
@@ -277,12 +300,14 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
277
300
  }
278
301
  }
279
302
  } catch (e: NFCExceptions) {
280
- printLog(TAG, " init_channel NFCExceptions-->$e")
303
+ printLog(TAG, "stage=init_channel_failure type=nfc")
281
304
  }
282
305
 
283
306
  loadPinStatus()
307
+ if (!pinStatusValid) throw NFCExceptions.ConnectionFailException()
284
308
  if (mCardType == AppleCardType.V2) {
285
309
  loadBackupStatus()
310
+ if (!backupStatusValid) throw NFCExceptions.ConnectionFailException()
286
311
  } else {
287
312
  hasBackup = hasSetupPin
288
313
  }
@@ -328,7 +353,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
328
353
 
329
354
  printLog(TAG, "<--- changePin end: ${res.getCode()} area:${mCommandArea}")
330
355
 
331
- return if (res.isConnectFailure()) {
356
+ return if (res.isConnectFailure() || (!res.parseSuccess && res.sw1 == 0x90.toByte() && res.sw2 == 0x00.toByte())) {
332
357
  NfcConstant.INTERRUPT_STATUS
333
358
  } else if (res.isSuccess()) {
334
359
  printLog(TAG, "--- verify success")
@@ -358,7 +383,7 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
358
383
  val res = command.send(this)
359
384
  printLog(TAG, "<--- startVerifyPin end: ${res.getCode()} area:${mCommandArea}")
360
385
 
361
- return if (res.isConnectFailure()) {
386
+ return if (res.isConnectFailure() || (!res.parseSuccess && res.sw1 == 0x90.toByte() && res.sw2 == 0x00.toByte())) {
362
387
  NfcConstant.INTERRUPT_STATUS
363
388
  } else if (res.isSuccess()) {
364
389
  printLog(TAG, "--- verify success")
@@ -384,7 +409,9 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
384
409
  val command = mCommandGenerator.generalCommand(
385
410
  mCardType, CommandType.BACKUP_DATA, hasOpenSafeChannel, mnemonic
386
411
  )
412
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_command_begin wallTimeMs=${System.currentTimeMillis()}")
387
413
  val res = command.send(this)
414
+ OneKeyLog.info("LiteCard", "operation=setMnemonic stage=backup_command_result sw1=${res.sw1.toInt() and 0xFF} sw2=${res.sw2.toInt() and 0xFF} parseSuccess=${res.parseSuccess} success=${res.isSuccess()}")
388
415
  printLog(TAG, "<--- backupData end: ${res.getCode()} area:${mCommandArea}")
389
416
 
390
417
  if (!res.isSuccess()) {
@@ -428,9 +455,9 @@ class Connection(val isoDep: IsoDep, private val mCommandGenerator: CommandGener
428
455
  )
429
456
  val res = command.send(this)
430
457
 
431
- printLog(TAG, "<--- resetCard end: ${res.getCode()} ${res.data} area:${mCommandArea}")
458
+ printLog(TAG, "<--- resetCard end: ${res.getCode()} area:${mCommandArea}")
432
459
 
433
- return if (res.isConnectFailure()) {
460
+ return if (!res.isSuccess()) {
434
461
  NfcConstant.RESET_INTERRUPT_STATUS
435
462
  } else {
436
463
  NfcConstant.RESET_PIN_SUCCESS
@@ -11,7 +11,7 @@ object GPCAPDUGenerator {
11
11
  @JvmStatic
12
12
  fun buildGPCAPDU(param: APDUParam, safeChannel: Boolean = false): String {
13
13
  printLog(TAG, " --->> BuildGPCAPDU begin")
14
- return if (safeChannel) {
14
+ return (if (safeChannel) {
15
15
  GPChannelNatives.nativeGPCBuildSafeAPDU(
16
16
  param.cla,
17
17
  param.ins,
@@ -27,7 +27,7 @@ object GPCAPDUGenerator {
27
27
  param.p2,
28
28
  param.data
29
29
  )
30
- }.also {
30
+ } ?: "").also {
31
31
  printLog(TAG, " <<--- BuildGPCAPDU done: safeChannel:$safeChannel")
32
32
  }
33
33
  }
@@ -8,6 +8,28 @@
8
8
  #import "OKLiteCommandTool.h"
9
9
  #import "OKNFCBridge.h"
10
10
  #import "OKNFCUtility.h"
11
+ #import "LCLogger.h"
12
+
13
+ static NSString *OKLiteCommandName(OKLiteCommand command) {
14
+ switch (command) {
15
+ case OKLiteCommandSelectSecure: return @"select_secure";
16
+ case OKLiteCommandSelectBackup: return @"select_backup";
17
+ case OKLiteCommandGetCardSN: return @"get_serial";
18
+ case OKLiteCommandGetCardCert: return @"get_certificate";
19
+ case OKLiteCommandGetBackupStatus: return @"get_backup_status";
20
+ case OKLiteCommandGetPINStatus: return @"get_pin_status";
21
+ case OKLiteCommandSetPIN: return @"set_pin";
22
+ case OKLiteCommandChangePIN: return @"change_pin";
23
+ case OKLiteCommandVerifyPIN: return @"verify_pin";
24
+ case OKLiteCommandImportMnemonic: return @"backup_data";
25
+ case OKLiteCommandExportMnemonic: return @"export_data";
26
+ case OKLiteCommandWipeCard: return @"reset_card";
27
+ case OKLiteCommandPinRTL: return @"get_pin_retry_count";
28
+ case OKLiteCommandOpenChannel_1: return @"verify_certificate";
29
+ case OKLiteCommandOpenChannel_2: return @"verify_auth_data";
30
+ }
31
+ return @"unknown";
32
+ }
11
33
 
12
34
  @implementation OKLiteCommandTool
13
35
 
@@ -15,13 +37,28 @@
15
37
  modal:(OKLiteCommandModal *)modal
16
38
  completionHandler:(void(^)(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString *parseRespon))completionHandler {
17
39
 
40
+ static NSUInteger nextCommandID = 0;
41
+ NSUInteger commandID;
42
+ @synchronized (OKLiteCommandTool.class) { commandID = ++nextCommandID; }
43
+ NSString *command = OKLiteCommandName(modal.command);
44
+ [LCLogger info:[NSString stringWithFormat:@"commandID=%lu command=%@ stage=send buildSucceeded=%d parseRequested=%d", (unsigned long)commandID, command, apdu != nil, modal.parseResp]];
18
45
  id<NFCISO7816Tag> tag = [self.delegate getNFCsessionTag];
19
46
  [tag sendCommandAPDU:apdu completionHandler:^(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError *error) {
47
+ uint8_t rawSW1 = sw1;
48
+ uint8_t rawSW2 = sw2;
20
49
  NSString *parseRespon = nil;
21
- if(modal.parseResp && sw1 == OKNFC_SW1_OK) {
50
+ if (modal.parseResp) {
22
51
  parseRespon = [OKNFCBridge parseSafeAPDUResponse:responseData sw1:sw1 sw2:sw2];
52
+ if (sw1 == OKNFC_SW1_OK && parseRespon == nil) {
53
+ sw1 = 0x00;
54
+ }
23
55
  }
24
56
 
57
+ BOOL coreNFCError = [error.domain isEqualToString:NFCErrorDomain];
58
+ [LCLogger info:[NSString stringWithFormat:@"commandID=%lu command=%@ stage=response rawSW1=%u rawSW2=%u effectiveSW1=%u effectiveSW2=%u responseDataBytes=%lu parseRequested=%d parsePresent=%d transportError=%d coreNFCError=%d errorCode=%ld", (unsigned long)commandID, command, rawSW1, rawSW2, sw1, sw2, (unsigned long)responseData.length, modal.parseResp, parseRespon != nil, error != nil, coreNFCError, (long)error.code]];
59
+ if (modal.command == OKLiteCommandGetBackupStatus || modal.command == OKLiteCommandGetPINStatus) {
60
+ [LCLogger info:[NSString stringWithFormat:@"commandID=%lu command=%@ stage=state_read readSucceeded=%d", (unsigned long)commandID, command, error == nil && sw1 == OKNFC_SW1_OK && responseData.length > 0]];
61
+ }
25
62
  [OKNFCUtility logAPDU:modal.desc response:responseData sw1:sw1 sw2:sw2 error:error];
26
63
  if (completionHandler) {
27
64
  completionHandler(responseData,sw1,sw2,error,parseRespon);