@onetokenfe/react-native-nfc-wallet-sdk 0.1.0

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 (50) hide show
  1. package/NfcWalletSdk.podspec +22 -0
  2. package/README.md +81 -0
  3. package/android/build.gradle +59 -0
  4. package/android/src/main/AndroidManifest.xml +4 -0
  5. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletEngine.kt +486 -0
  6. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletSdkModule.kt +268 -0
  7. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletSdkPackage.kt +14 -0
  8. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/nfc/NfcUtils.kt +24 -0
  9. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/utils/HexUtils.kt +24 -0
  10. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletApduClient.kt +495 -0
  11. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletConstants.kt +105 -0
  12. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcController.kt +262 -0
  13. package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcTagHolder.kt +74 -0
  14. package/ios/Classes/NfcWalletHexUtils.h +8 -0
  15. package/ios/Classes/NfcWalletHexUtils.m +31 -0
  16. package/ios/Classes/NfcWalletNFCManager.h +58 -0
  17. package/ios/Classes/NfcWalletNFCManager.m +244 -0
  18. package/ios/Classes/NfcWalletSession.h +52 -0
  19. package/ios/Classes/NfcWalletSession.m +1109 -0
  20. package/ios/NfcWalletSdk.h +5 -0
  21. package/ios/NfcWalletSdk.mm +369 -0
  22. package/lib/module/NativeNfcWalletSdk.js +8 -0
  23. package/lib/module/NativeNfcWalletSdk.js.map +1 -0
  24. package/lib/module/constants.js +5 -0
  25. package/lib/module/constants.js.map +1 -0
  26. package/lib/module/index.js +8 -0
  27. package/lib/module/index.js.map +1 -0
  28. package/lib/module/nfc.js +81 -0
  29. package/lib/module/nfc.js.map +1 -0
  30. package/lib/module/package.json +1 -0
  31. package/lib/module/walletOperations.js +68 -0
  32. package/lib/module/walletOperations.js.map +1 -0
  33. package/lib/typescript/package.json +1 -0
  34. package/lib/typescript/src/NativeNfcWalletSdk.d.ts +166 -0
  35. package/lib/typescript/src/NativeNfcWalletSdk.d.ts.map +1 -0
  36. package/lib/typescript/src/constants.d.ts +3 -0
  37. package/lib/typescript/src/constants.d.ts.map +1 -0
  38. package/lib/typescript/src/index.d.ts +7 -0
  39. package/lib/typescript/src/index.d.ts.map +1 -0
  40. package/lib/typescript/src/nfc.d.ts +43 -0
  41. package/lib/typescript/src/nfc.d.ts.map +1 -0
  42. package/lib/typescript/src/walletOperations.d.ts +22 -0
  43. package/lib/typescript/src/walletOperations.d.ts.map +1 -0
  44. package/package.json +90 -0
  45. package/react-native.config.js +10 -0
  46. package/src/NativeNfcWalletSdk.ts +205 -0
  47. package/src/constants.ts +2 -0
  48. package/src/index.tsx +33 -0
  49. package/src/nfc.ts +80 -0
  50. package/src/walletOperations.ts +152 -0
@@ -0,0 +1,268 @@
1
+ package com.ziancube.reactnativenfcwalletsdk
2
+
3
+ import android.app.Activity
4
+ import android.content.Intent
5
+ import android.util.Log
6
+ import com.facebook.react.bridge.Arguments
7
+ import com.facebook.react.bridge.LifecycleEventListener
8
+ import com.facebook.react.bridge.Promise
9
+ import com.facebook.react.bridge.ReactApplicationContext
10
+ import com.facebook.react.bridge.WritableMap
11
+ import com.facebook.react.bridge.BaseActivityEventListener
12
+ import com.ziancube.reactnativenfcwalletsdk.nfc.NfcUtils
13
+ import com.ziancube.reactnativenfcwalletsdk.wallet.WalletInfoData
14
+ import com.ziancube.reactnativenfcwalletsdk.wallet.WalletNfcController
15
+ import kotlinx.coroutines.CoroutineScope
16
+ import kotlinx.coroutines.Dispatchers
17
+ import kotlinx.coroutines.SupervisorJob
18
+ import kotlinx.coroutines.cancel
19
+ import kotlinx.coroutines.launch
20
+ import kotlinx.coroutines.withContext
21
+
22
+ class NfcWalletSdkModule(private val reactContext: ReactApplicationContext) :
23
+ NativeNfcWalletSdkSpec(reactContext), LifecycleEventListener {
24
+
25
+ private val tag = "NfcWalletSdkModule"
26
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
27
+ private var controller: WalletNfcController? = null
28
+ private var engine: NfcWalletEngine? = null
29
+ private var boundActivity: Activity? = null
30
+
31
+ private val activityEventListener =
32
+ object : BaseActivityEventListener() {
33
+ override fun onNewIntent(intent: Intent) {
34
+ runCatching { controller?.handleIntent(intent) }
35
+ .onFailure { Log.e(tag, "handleIntent failed", it) }
36
+ }
37
+ }
38
+
39
+ override fun initialize() {
40
+ super.initialize()
41
+ ActivityHolder.init(reactContext.applicationContext as android.app.Application)
42
+ reactContext.addActivityEventListener(activityEventListener)
43
+ reactContext.addLifecycleEventListener(this)
44
+ ensureEngine()
45
+ }
46
+
47
+ override fun invalidate() {
48
+ controller?.releaseHeldTag()
49
+ reactContext.removeActivityEventListener(activityEventListener)
50
+ scope.cancel()
51
+ reactContext.removeLifecycleEventListener(this)
52
+ super.invalidate()
53
+ }
54
+
55
+ override fun onHostResume() {
56
+ controller?.onResume()
57
+ }
58
+
59
+ override fun onHostPause() {
60
+ controller?.onPause()
61
+ }
62
+
63
+ override fun onHostDestroy() {}
64
+
65
+ private fun ensureEngine(): NfcWalletEngine? {
66
+ // Keep the same controller across calls so held IsoDep survives probe →
67
+ // Create Wallet. Recreating here was dropping the tag and forcing rematch.
68
+ val existing = boundActivity
69
+ if (engine != null && controller != null && existing != null && !existing.isDestroyed) {
70
+ return engine
71
+ }
72
+ val activity =
73
+ ActivityHolder.getTopActivity()
74
+ ?: reactContext.currentActivity
75
+ ?: existing
76
+ if (activity == null || activity.isDestroyed) {
77
+ return null
78
+ }
79
+ boundActivity = activity
80
+ controller = WalletNfcController(activity)
81
+ engine = NfcWalletEngine(controller!!)
82
+ return engine
83
+ }
84
+
85
+ private fun WalletInfoData.toMap(): WritableMap =
86
+ Arguments.createMap().apply {
87
+ putString("serialNumber", serialNumber)
88
+ putInt("cardStatus", cardStatus)
89
+ putInt("pinRetryCount", pinRetryCount)
90
+ putBoolean("sessionActive", sessionActive)
91
+ putBoolean("hasKey", hasKey)
92
+ putBoolean("devFixedMasterSeed", devFixedMasterSeed)
93
+ putBoolean("backupDone", backupDone)
94
+ putDouble("signCount", signCount.toDouble())
95
+ putString("version", version)
96
+ cardSetPrimarySerial?.let { putString("cardSetPrimarySerial", it) }
97
+ ?: putNull("cardSetPrimarySerial")
98
+ putArray(
99
+ "cardSetMemberSerials",
100
+ Arguments.createArray().apply {
101
+ cardSetMemberSerials.forEach { pushString(it) }
102
+ },
103
+ )
104
+ }
105
+
106
+ private fun successMap(walletInfo: WalletInfoData? = null, data: String? = null): WritableMap =
107
+ Arguments.createMap().apply {
108
+ putNull("error")
109
+ putInt("resultCode", 0)
110
+ if (walletInfo != null) {
111
+ putMap("walletInfo", walletInfo.toMap())
112
+ } else {
113
+ putNull("walletInfo")
114
+ }
115
+ if (data != null) putString("data", data) else putNull("data")
116
+ }
117
+
118
+ private fun errorMap(code: Int, message: String?): WritableMap =
119
+ Arguments.createMap().apply {
120
+ putMap(
121
+ "error",
122
+ Arguments.createMap().apply {
123
+ putInt("code", code)
124
+ putString("message", message)
125
+ },
126
+ )
127
+ putInt("resultCode", code)
128
+ putNull("walletInfo")
129
+ putNull("data")
130
+ }
131
+
132
+ private fun runWallet(promise: Promise, block: (NfcWalletEngine) -> Any?) {
133
+ val walletEngine = ensureEngine()
134
+ if (walletEngine == null) {
135
+ promise.resolve(errorMap(2001, "No foreground activity"))
136
+ return
137
+ }
138
+ scope.launch {
139
+ try {
140
+ val result =
141
+ withContext(Dispatchers.IO) {
142
+ block(walletEngine)
143
+ }
144
+ when (result) {
145
+ is WalletInfoData -> promise.resolve(successMap(walletInfo = result, data = result.serialNumber))
146
+ is Pair<*, *> -> {
147
+ val data = result.first as? String
148
+ val walletInfo = result.second as? WalletInfoData
149
+ promise.resolve(successMap(walletInfo = walletInfo, data = data))
150
+ }
151
+ is String -> promise.resolve(successMap(data = result))
152
+ null -> promise.resolve(successMap())
153
+ else -> promise.resolve(successMap(data = result.toString()))
154
+ }
155
+ } catch (t: Throwable) {
156
+ Log.e(tag, "wallet op failed", t)
157
+ promise.resolve(errorMap(NfcWalletEngine.mapErrorCode(t), t.message))
158
+ }
159
+ }
160
+ }
161
+
162
+ override fun getWalletInfo(promise: Promise) {
163
+ runWallet(promise) { it.getWalletInfo() }
164
+ }
165
+
166
+ override fun setPin(pin: String, promise: Promise) {
167
+ runWallet(promise) { it.setPin(pin); null }
168
+ }
169
+
170
+ override fun changePin(
171
+ oldPin: String,
172
+ newPin: String,
173
+ expectedSerial: String?,
174
+ promise: Promise,
175
+ ) {
176
+ runWallet(promise) { it.changePin(oldPin, newPin, expectedSerial) }
177
+ }
178
+
179
+ override fun verifyPin(pin: String, expectedSerial: String?, promise: Promise) {
180
+ runWallet(promise) { it.verifyPin(pin, expectedSerial); null }
181
+ }
182
+
183
+ override fun logout(promise: Promise) {
184
+ runWallet(promise) { it.logout(); null }
185
+ }
186
+
187
+ override fun createWallet(expectedSerial: String?, promise: Promise) {
188
+ runWallet(promise) { it.createWallet(expectedSerial) }
189
+ }
190
+
191
+ override fun createWalletWithSeed(seedHex: String, expectedSerial: String?, promise: Promise) {
192
+ runWallet(promise) { it.createWalletWithSeed(seedHex, expectedSerial) }
193
+ }
194
+
195
+ override fun getPublicKey(path: String?, promise: Promise) {
196
+ runWallet(promise) { it.getPublicKeyHex(path) }
197
+ }
198
+
199
+ override fun signHash(hashHex: String, path: String?, promise: Promise) {
200
+ runWallet(promise) { it.signHashHex(hashHex, path) }
201
+ }
202
+
203
+ override fun wipeWallet(promise: Promise) {
204
+ runWallet(promise) { it.wipeWallet(); null }
205
+ }
206
+
207
+ override fun wipeWalletWithPin(
208
+ pin: String,
209
+ expectedSerial: String,
210
+ promise: Promise,
211
+ ) {
212
+ runWallet(promise) { it.wipeWalletWithPin(pin, expectedSerial) }
213
+ }
214
+
215
+ override fun initializeWallet(pin: String, seedHex: String?, promise: Promise) {
216
+ runWallet(promise) { it.initializeWallet(pin, seedHex) }
217
+ }
218
+
219
+ override fun initializeWalletWithPubkeys(
220
+ pin: String,
221
+ pathsJson: String,
222
+ seedHex: String?,
223
+ expectedSerial: String?,
224
+ promise: Promise,
225
+ ) {
226
+ runWallet(promise) {
227
+ val result =
228
+ it.initializeWalletWithPubkeys(pin, seedHex, pathsJson, expectedSerial)
229
+ Pair(result.pubkeysJson, result.walletInfo)
230
+ }
231
+ }
232
+
233
+ override fun unlockAndSign(pin: String, hashHex: String, path: String?, promise: Promise) {
234
+ runWallet(promise) { it.unlockAndSign(pin, hashHex, path) }
235
+ }
236
+
237
+ override fun runBackupStep(
238
+ step: String,
239
+ pin: String?,
240
+ payloadHex: String?,
241
+ backupCount: Double,
242
+ promise: Promise,
243
+ ) {
244
+ runWallet(promise) {
245
+ it.runBackupStep(step, pin, payloadHex, backupCount.toInt())
246
+ }
247
+ }
248
+
249
+ override fun attestCard(challengeHex: String, promise: Promise) {
250
+ runWallet(promise) {
251
+ val result = it.attestCard(challengeHex)
252
+ Pair(result.json, result.walletInfo)
253
+ }
254
+ }
255
+
256
+ override fun cancelNfcWait() {
257
+ controller?.cancelWait()
258
+ }
259
+
260
+ override fun checkNFCPermission(promise: Promise) {
261
+ val ok = NfcUtils.isNfcAvailable(reactContext) && NfcUtils.isNfcEnabled(reactContext)
262
+ if (ok) promise.resolve(true) else promise.reject("NFC_ERROR", "NFC not available or disabled")
263
+ }
264
+
265
+ override fun intoSetting() {
266
+ ActivityHolder.getTopActivity()?.let { NfcUtils.openNfcSettings(it) }
267
+ }
268
+ }
@@ -0,0 +1,14 @@
1
+ package com.ziancube.reactnativenfcwalletsdk
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+
8
+ class NfcWalletSdkPackage : ReactPackage {
9
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> =
10
+ listOf(NfcWalletSdkModule(reactContext))
11
+
12
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> =
13
+ emptyList()
14
+ }
@@ -0,0 +1,24 @@
1
+ package com.ziancube.reactnativenfcwalletsdk.nfc
2
+
3
+ import android.content.Context
4
+ import android.content.Intent
5
+ import android.nfc.NfcAdapter
6
+ import android.provider.Settings
7
+
8
+ object NfcUtils {
9
+ fun isNfcAvailable(context: Context): Boolean = NfcAdapter.getDefaultAdapter(context) != null
10
+
11
+ fun isNfcEnabled(context: Context): Boolean {
12
+ val adapter = NfcAdapter.getDefaultAdapter(context)
13
+ return adapter != null && adapter.isEnabled
14
+ }
15
+
16
+ fun openNfcSettings(context: Context): Boolean {
17
+ return try {
18
+ context.startActivity(Intent(Settings.ACTION_NFC_SETTINGS))
19
+ true
20
+ } catch (_: Exception) {
21
+ false
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,24 @@
1
+ package com.ziancube.reactnativenfcwalletsdk.utils
2
+
3
+ object HexUtils {
4
+ fun decode(hex: String): ByteArray {
5
+ val clean = hex.replace("\\s".toRegex(), "")
6
+ require(clean.length % 2 == 0) { "Invalid hex length" }
7
+ val out = ByteArray(clean.length / 2)
8
+ var i = 0
9
+ while (i < clean.length) {
10
+ out[i / 2] =
11
+ ((Character.digit(clean[i], 16) shl 4) + Character.digit(clean[i + 1], 16)).toByte()
12
+ i += 2
13
+ }
14
+ return out
15
+ }
16
+
17
+ fun encode(bytes: ByteArray): String {
18
+ val sb = StringBuilder(bytes.size * 2)
19
+ for (b in bytes) {
20
+ sb.append(String.format("%02X", b))
21
+ }
22
+ return sb.toString()
23
+ }
24
+ }