@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.
- package/NfcWalletSdk.podspec +22 -0
- package/README.md +81 -0
- package/android/build.gradle +59 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletEngine.kt +486 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletSdkModule.kt +268 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/NfcWalletSdkPackage.kt +14 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/nfc/NfcUtils.kt +24 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/utils/HexUtils.kt +24 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletApduClient.kt +495 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletConstants.kt +105 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcController.kt +262 -0
- package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcTagHolder.kt +74 -0
- package/ios/Classes/NfcWalletHexUtils.h +8 -0
- package/ios/Classes/NfcWalletHexUtils.m +31 -0
- package/ios/Classes/NfcWalletNFCManager.h +58 -0
- package/ios/Classes/NfcWalletNFCManager.m +244 -0
- package/ios/Classes/NfcWalletSession.h +52 -0
- package/ios/Classes/NfcWalletSession.m +1109 -0
- package/ios/NfcWalletSdk.h +5 -0
- package/ios/NfcWalletSdk.mm +369 -0
- package/lib/module/NativeNfcWalletSdk.js +8 -0
- package/lib/module/NativeNfcWalletSdk.js.map +1 -0
- package/lib/module/constants.js +5 -0
- package/lib/module/constants.js.map +1 -0
- package/lib/module/index.js +8 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/nfc.js +81 -0
- package/lib/module/nfc.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/walletOperations.js +68 -0
- package/lib/module/walletOperations.js.map +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeNfcWalletSdk.d.ts +166 -0
- package/lib/typescript/src/NativeNfcWalletSdk.d.ts.map +1 -0
- package/lib/typescript/src/constants.d.ts +3 -0
- package/lib/typescript/src/constants.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +7 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/nfc.d.ts +43 -0
- package/lib/typescript/src/nfc.d.ts.map +1 -0
- package/lib/typescript/src/walletOperations.d.ts +22 -0
- package/lib/typescript/src/walletOperations.d.ts.map +1 -0
- package/package.json +90 -0
- package/react-native.config.js +10 -0
- package/src/NativeNfcWalletSdk.ts +205 -0
- package/src/constants.ts +2 -0
- package/src/index.tsx +33 -0
- package/src/nfc.ts +80 -0
- package/src/walletOperations.ts +152 -0
package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcController.kt
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
package com.ziancube.reactnativenfcwalletsdk.wallet
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.Intent
|
|
5
|
+
import android.nfc.NfcAdapter
|
|
6
|
+
import android.nfc.Tag
|
|
7
|
+
import android.nfc.tech.IsoDep
|
|
8
|
+
import android.os.Bundle
|
|
9
|
+
import android.os.Handler
|
|
10
|
+
import android.os.Looper
|
|
11
|
+
import android.util.Log
|
|
12
|
+
import java.util.concurrent.CancellationException
|
|
13
|
+
import java.util.concurrent.CountDownLatch
|
|
14
|
+
import java.util.concurrent.TimeUnit
|
|
15
|
+
import java.util.concurrent.atomic.AtomicReference
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Android NFC controller for wallet applet.
|
|
19
|
+
*
|
|
20
|
+
* Tag/IsoDep are stored in [WalletNfcTagHolder] (process-wide) so probe → Create
|
|
21
|
+
* Wallet works without remove/re-tap even if this controller is recreated.
|
|
22
|
+
* Reader mode stays on while a tag is held; RF is keep-alive pinged.
|
|
23
|
+
*/
|
|
24
|
+
class WalletNfcController(private val activity: Activity) {
|
|
25
|
+
private val nfcAdapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(activity)
|
|
26
|
+
private val mainHandler = Handler(Looper.getMainLooper())
|
|
27
|
+
private val logTag = "WalletNfcController"
|
|
28
|
+
|
|
29
|
+
private val pendingTag = AtomicReference<IsoDep?>(null)
|
|
30
|
+
@Volatile private var cancelled = false
|
|
31
|
+
@Volatile private var sessionBusy = false
|
|
32
|
+
|
|
33
|
+
private var keepAliveRunnable: Runnable? = null
|
|
34
|
+
|
|
35
|
+
// Omit FLAG_READER_NO_PLATFORM_SOUNDS so the system NFC tick plays on discover.
|
|
36
|
+
private val readerFlags =
|
|
37
|
+
NfcAdapter.FLAG_READER_NFC_A or
|
|
38
|
+
NfcAdapter.FLAG_READER_NFC_B or
|
|
39
|
+
NfcAdapter.FLAG_READER_SKIP_NDEF_CHECK
|
|
40
|
+
|
|
41
|
+
private val readerCallback =
|
|
42
|
+
NfcAdapter.ReaderCallback { tag: Tag ->
|
|
43
|
+
if (sessionBusy && WalletNfcTagHolder.isoDep?.isConnected == true) {
|
|
44
|
+
return@ReaderCallback
|
|
45
|
+
}
|
|
46
|
+
Log.d(logTag, "tag discovered (readerMode)")
|
|
47
|
+
WalletNfcTagHolder.remember(tag)
|
|
48
|
+
IsoDep.get(tag)?.let { pendingTag.set(it) }
|
|
49
|
+
startKeepAlive()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fun onResume() {
|
|
53
|
+
enableReaderMode()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fun onPause() {
|
|
57
|
+
// Do NOT disableReaderMode while a card Tag is held — on many OEMs that
|
|
58
|
+
// kills the RF link and the in-field card is not rediscovered until rematch.
|
|
59
|
+
if (WalletNfcTagHolder.tag == null) {
|
|
60
|
+
disableReaderMode()
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fun handleIntent(intent: Intent?) {
|
|
65
|
+
val tag: Tag? = intent?.getParcelableExtra(NfcAdapter.EXTRA_TAG)
|
|
66
|
+
if (tag != null) {
|
|
67
|
+
if (sessionBusy && WalletNfcTagHolder.isoDep?.isConnected == true) {
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
Log.d(logTag, "tag discovered (intent)")
|
|
71
|
+
WalletNfcTagHolder.remember(tag)
|
|
72
|
+
IsoDep.get(tag)?.let { pendingTag.set(it) }
|
|
73
|
+
startKeepAlive()
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fun cancelWait() {
|
|
78
|
+
cancelled = true
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fun releaseHeldTag() {
|
|
82
|
+
cancelled = true
|
|
83
|
+
stopKeepAlive()
|
|
84
|
+
WalletNfcTagHolder.clear()
|
|
85
|
+
pendingTag.set(null)
|
|
86
|
+
disableReaderMode()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
fun <T> withWalletSession(timeoutSeconds: Long = 180, block: (WalletApduClient) -> T): T {
|
|
90
|
+
cancelled = false
|
|
91
|
+
enableReaderMode()
|
|
92
|
+
val latch = CountDownLatch(1)
|
|
93
|
+
val holder = AtomicReference<T>()
|
|
94
|
+
val error = AtomicReference<Throwable?>(null)
|
|
95
|
+
|
|
96
|
+
Thread {
|
|
97
|
+
sessionBusy = true
|
|
98
|
+
try {
|
|
99
|
+
val iso = obtainConnectedTag(timeoutSeconds)
|
|
100
|
+
iso.timeout = 120_000
|
|
101
|
+
WalletNfcTagHolder.remember(iso)
|
|
102
|
+
val client =
|
|
103
|
+
WalletApduClient(
|
|
104
|
+
transceive = { apdu -> iso.transceive(apdu) },
|
|
105
|
+
)
|
|
106
|
+
client.selectApplet()
|
|
107
|
+
holder.set(block(client))
|
|
108
|
+
Log.d(logTag, "session ok, holding tag")
|
|
109
|
+
startKeepAlive()
|
|
110
|
+
} catch (t: Throwable) {
|
|
111
|
+
error.set(t)
|
|
112
|
+
if (t !is CancellationException) {
|
|
113
|
+
// Stale RF only — keep Tag so the next call can reconnectFresh.
|
|
114
|
+
Log.d(logTag, "session failed: ${t.message}")
|
|
115
|
+
}
|
|
116
|
+
} finally {
|
|
117
|
+
sessionBusy = false
|
|
118
|
+
latch.countDown()
|
|
119
|
+
}
|
|
120
|
+
}.start()
|
|
121
|
+
|
|
122
|
+
if (!latch.await(timeoutSeconds + 5, TimeUnit.SECONDS)) {
|
|
123
|
+
cancelled = true
|
|
124
|
+
throw WalletApduException(-1, "NFC operation timed out")
|
|
125
|
+
}
|
|
126
|
+
error.get()?.let { throw it }
|
|
127
|
+
return holder.get() ?: throw WalletApduException(-1, "NFC operation returned no result")
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private fun enableReaderMode() {
|
|
131
|
+
val adapter = nfcAdapter ?: return
|
|
132
|
+
val extras =
|
|
133
|
+
Bundle().apply {
|
|
134
|
+
putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 100)
|
|
135
|
+
}
|
|
136
|
+
activity.runOnUiThread {
|
|
137
|
+
runCatching {
|
|
138
|
+
adapter.enableReaderMode(activity, readerCallback, readerFlags, extras)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private fun disableReaderMode() {
|
|
144
|
+
val adapter = nfcAdapter ?: return
|
|
145
|
+
activity.runOnUiThread {
|
|
146
|
+
runCatching { adapter.disableReaderMode(activity) }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private fun bounceReaderMode() {
|
|
151
|
+
val adapter = nfcAdapter ?: return
|
|
152
|
+
Log.d(logTag, "bounceReaderMode")
|
|
153
|
+
val done = CountDownLatch(1)
|
|
154
|
+
activity.runOnUiThread {
|
|
155
|
+
runCatching { adapter.disableReaderMode(activity) }
|
|
156
|
+
mainHandler.postDelayed(
|
|
157
|
+
{
|
|
158
|
+
val extras =
|
|
159
|
+
Bundle().apply {
|
|
160
|
+
putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 50)
|
|
161
|
+
}
|
|
162
|
+
runCatching {
|
|
163
|
+
adapter.enableReaderMode(activity, readerCallback, readerFlags, extras)
|
|
164
|
+
}
|
|
165
|
+
done.countDown()
|
|
166
|
+
},
|
|
167
|
+
150,
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
runCatching { done.await(2, TimeUnit.SECONDS) }
|
|
171
|
+
Thread.sleep(250)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private fun startKeepAlive() {
|
|
175
|
+
stopKeepAlive()
|
|
176
|
+
val runnable =
|
|
177
|
+
object : Runnable {
|
|
178
|
+
override fun run() {
|
|
179
|
+
if (!sessionBusy && WalletNfcTagHolder.tag != null) {
|
|
180
|
+
val iso = WalletNfcTagHolder.tryReuseConnected()
|
|
181
|
+
if (iso == null) {
|
|
182
|
+
Log.d(logTag, "keepAlive: reconnect failed, will bounce on next op")
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
mainHandler.postDelayed(this, 2000)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
keepAliveRunnable = runnable
|
|
189
|
+
mainHandler.postDelayed(runnable, 2000)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private fun stopKeepAlive() {
|
|
193
|
+
keepAliveRunnable?.let { mainHandler.removeCallbacks(it) }
|
|
194
|
+
keepAliveRunnable = null
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private fun obtainConnectedTag(timeoutSeconds: Long): IsoDep {
|
|
198
|
+
WalletNfcTagHolder.tryReuseConnected()?.let {
|
|
199
|
+
Log.d(logTag, "obtain: reused held tag")
|
|
200
|
+
return it
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
pendingTag.getAndSet(null)?.let { early ->
|
|
204
|
+
try {
|
|
205
|
+
if (!early.isConnected) {
|
|
206
|
+
early.connect()
|
|
207
|
+
}
|
|
208
|
+
WalletNfcTagHolder.remember(early)
|
|
209
|
+
Log.d(logTag, "obtain: pending tag")
|
|
210
|
+
return early
|
|
211
|
+
} catch (_: Throwable) {
|
|
212
|
+
runCatching { early.close() }
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
bounceReaderMode()
|
|
217
|
+
WalletNfcTagHolder.tryReuseConnected()?.let {
|
|
218
|
+
Log.d(logTag, "obtain: reused after bounce")
|
|
219
|
+
return it
|
|
220
|
+
}
|
|
221
|
+
pendingTag.getAndSet(null)?.let { iso ->
|
|
222
|
+
try {
|
|
223
|
+
if (!iso.isConnected) {
|
|
224
|
+
iso.connect()
|
|
225
|
+
}
|
|
226
|
+
WalletNfcTagHolder.remember(iso)
|
|
227
|
+
Log.d(logTag, "obtain: pending after bounce")
|
|
228
|
+
return iso
|
|
229
|
+
} catch (_: Throwable) {
|
|
230
|
+
runCatching { iso.close() }
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
Log.d(logTag, "obtain: waiting for tag…")
|
|
235
|
+
val iso = waitForTag(timeoutSeconds)
|
|
236
|
+
if (!iso.isConnected) {
|
|
237
|
+
iso.connect()
|
|
238
|
+
}
|
|
239
|
+
WalletNfcTagHolder.remember(iso)
|
|
240
|
+
return iso
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private fun waitForTag(timeoutSeconds: Long): IsoDep {
|
|
244
|
+
val deadline = System.currentTimeMillis() + timeoutSeconds * 1000
|
|
245
|
+
var lastBounceAt = 0L
|
|
246
|
+
while (System.currentTimeMillis() < deadline) {
|
|
247
|
+
if (cancelled) {
|
|
248
|
+
throw CancellationException("NFC wait cancelled")
|
|
249
|
+
}
|
|
250
|
+
pendingTag.getAndSet(null)?.let { return it }
|
|
251
|
+
WalletNfcTagHolder.tryReuseConnected()?.let { return it }
|
|
252
|
+
if (System.currentTimeMillis() - lastBounceAt > 1500) {
|
|
253
|
+
bounceReaderMode()
|
|
254
|
+
lastBounceAt = System.currentTimeMillis()
|
|
255
|
+
pendingTag.getAndSet(null)?.let { return it }
|
|
256
|
+
WalletNfcTagHolder.tryReuseConnected()?.let { return it }
|
|
257
|
+
}
|
|
258
|
+
Thread.sleep(100)
|
|
259
|
+
}
|
|
260
|
+
throw WalletApduException(-1, "Tap NFC wallet card")
|
|
261
|
+
}
|
|
262
|
+
}
|
package/android/src/main/java/com/ziancube/reactnativenfcwalletsdk/wallet/WalletNfcTagHolder.kt
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
package com.ziancube.reactnativenfcwalletsdk.wallet
|
|
2
|
+
|
|
3
|
+
import android.nfc.Tag
|
|
4
|
+
import android.nfc.tech.IsoDep
|
|
5
|
+
import android.util.Log
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Process-wide NFC tag holder so IsoDep survives [WalletNfcController] recreation
|
|
9
|
+
* (ensureEngine / activity identity changes) between probe and Create Wallet.
|
|
10
|
+
*/
|
|
11
|
+
object WalletNfcTagHolder {
|
|
12
|
+
private const val TAG = "WalletNfcTagHolder"
|
|
13
|
+
|
|
14
|
+
@Volatile var tag: Tag? = null
|
|
15
|
+
private set
|
|
16
|
+
|
|
17
|
+
@Volatile var isoDep: IsoDep? = null
|
|
18
|
+
private set
|
|
19
|
+
|
|
20
|
+
fun remember(discovered: Tag) {
|
|
21
|
+
val iso = IsoDep.get(discovered) ?: return
|
|
22
|
+
tag = discovered
|
|
23
|
+
isoDep = iso
|
|
24
|
+
Log.d(TAG, "remember tag=${discovered.id?.joinToString("") { "%02X".format(it) }}")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fun remember(iso: IsoDep) {
|
|
28
|
+
isoDep = iso
|
|
29
|
+
// Tag is already stored when discovered via remember(Tag).
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fun clear() {
|
|
33
|
+
runCatching { isoDep?.close() }
|
|
34
|
+
isoDep = null
|
|
35
|
+
tag = null
|
|
36
|
+
Log.d(TAG, "cleared")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Fresh IsoDep from the stored Tag (Lite-style reconnect while card stays in field). */
|
|
40
|
+
fun connectFreshIsoDep(): IsoDep? {
|
|
41
|
+
val t = tag ?: return null
|
|
42
|
+
return try {
|
|
43
|
+
val iso = IsoDep.get(t) ?: return null
|
|
44
|
+
if (iso.isConnected) {
|
|
45
|
+
runCatching { iso.close() }
|
|
46
|
+
}
|
|
47
|
+
iso.connect()
|
|
48
|
+
isoDep = iso
|
|
49
|
+
Log.d(TAG, "connectFreshIsoDep ok")
|
|
50
|
+
iso
|
|
51
|
+
} catch (e: Throwable) {
|
|
52
|
+
Log.d(TAG, "connectFreshIsoDep failed: ${e.message}")
|
|
53
|
+
null
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fun tryReuseConnected(): IsoDep? {
|
|
58
|
+
val iso = isoDep
|
|
59
|
+
if (iso != null) {
|
|
60
|
+
try {
|
|
61
|
+
if (iso.isConnected) {
|
|
62
|
+
Log.d(TAG, "reuse connected IsoDep")
|
|
63
|
+
return iso
|
|
64
|
+
}
|
|
65
|
+
iso.connect()
|
|
66
|
+
Log.d(TAG, "reconnected existing IsoDep")
|
|
67
|
+
return iso
|
|
68
|
+
} catch (_: Throwable) {
|
|
69
|
+
// fall through to fresh from Tag
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return connectFreshIsoDep()
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#import "NfcWalletHexUtils.h"
|
|
2
|
+
|
|
3
|
+
NSString *NfcWalletHexFromData(NSData *data)
|
|
4
|
+
{
|
|
5
|
+
if (data.length == 0) {
|
|
6
|
+
return @"";
|
|
7
|
+
}
|
|
8
|
+
const unsigned char *bytes = data.bytes;
|
|
9
|
+
NSMutableString *hex = [NSMutableString stringWithCapacity:data.length * 2];
|
|
10
|
+
for (NSUInteger i = 0; i < data.length; i++) {
|
|
11
|
+
[hex appendFormat:@"%02X", bytes[i]];
|
|
12
|
+
}
|
|
13
|
+
return hex;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
NSData *_Nullable NfcWalletDataFromHex(NSString *hex)
|
|
17
|
+
{
|
|
18
|
+
NSString *clean = [[hex stringByReplacingOccurrencesOfString:@" " withString:@""] uppercaseString];
|
|
19
|
+
if (clean.length == 0 || clean.length % 2 != 0) {
|
|
20
|
+
return nil;
|
|
21
|
+
}
|
|
22
|
+
NSMutableData *data = [NSMutableData dataWithCapacity:clean.length / 2];
|
|
23
|
+
for (NSUInteger i = 0; i < clean.length; i += 2) {
|
|
24
|
+
unsigned int value = 0;
|
|
25
|
+
NSScanner *scanner = [NSScanner scannerWithString:[clean substringWithRange:NSMakeRange(i, 2)]];
|
|
26
|
+
[scanner scanHexInt:&value];
|
|
27
|
+
uint8_t byte = (uint8_t)value;
|
|
28
|
+
[data appendBytes:&byte length:1];
|
|
29
|
+
}
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
#import <Foundation/Foundation.h>
|
|
2
|
+
#import <CoreNFC/CoreNFC.h>
|
|
3
|
+
#import "NfcWalletSession.h"
|
|
4
|
+
|
|
5
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
6
|
+
|
|
7
|
+
typedef void (^NfcWalletOperationCompletion)(NSDictionary *_Nullable result, NSError *_Nullable error);
|
|
8
|
+
typedef void (^NfcWalletApduLogBlock)(NSString *message, BOOL isSent, BOOL isSuccess);
|
|
9
|
+
|
|
10
|
+
typedef NS_ENUM(NSInteger, NfcWalletOperationType) {
|
|
11
|
+
NfcWalletOperationGetInfo,
|
|
12
|
+
NfcWalletOperationSetPin,
|
|
13
|
+
NfcWalletOperationChangePin,
|
|
14
|
+
NfcWalletOperationVerifyPin,
|
|
15
|
+
NfcWalletOperationLogout,
|
|
16
|
+
NfcWalletOperationCreateWallet,
|
|
17
|
+
NfcWalletOperationCreateWalletWithSeed,
|
|
18
|
+
NfcWalletOperationGetPublicKey,
|
|
19
|
+
NfcWalletOperationSignHash,
|
|
20
|
+
NfcWalletOperationWipe,
|
|
21
|
+
NfcWalletOperationWipeWithPin,
|
|
22
|
+
NfcWalletOperationInitialize,
|
|
23
|
+
NfcWalletOperationInitializeWithPubkeys,
|
|
24
|
+
NfcWalletOperationUnlockAndSign,
|
|
25
|
+
NfcWalletOperationBackupStep,
|
|
26
|
+
NfcWalletOperationAttestCard,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
@interface NfcWalletNFCManager : NSObject
|
|
30
|
+
@property (nonatomic, copy, nullable) NfcWalletApduLogBlock apduLogBlock;
|
|
31
|
+
- (void)cancelSession;
|
|
32
|
+
- (void)runOperation:(NfcWalletOperationType)type
|
|
33
|
+
pin:(NSString *_Nullable)pin
|
|
34
|
+
hashHex:(NSString *_Nullable)hashHex
|
|
35
|
+
seedHex:(NSString *_Nullable)seedHex
|
|
36
|
+
derivationPath:(NSString *_Nullable)derivationPath
|
|
37
|
+
pathsJson:(NSString *_Nullable)pathsJson
|
|
38
|
+
expectedSerial:(NSString *_Nullable)expectedSerial
|
|
39
|
+
completion:(NfcWalletOperationCompletion)completion;
|
|
40
|
+
- (void)runOperation:(NfcWalletOperationType)type
|
|
41
|
+
pin:(NSString *_Nullable)pin
|
|
42
|
+
newPin:(NSString *_Nullable)newPin
|
|
43
|
+
hashHex:(NSString *_Nullable)hashHex
|
|
44
|
+
seedHex:(NSString *_Nullable)seedHex
|
|
45
|
+
derivationPath:(NSString *_Nullable)derivationPath
|
|
46
|
+
pathsJson:(NSString *_Nullable)pathsJson
|
|
47
|
+
expectedSerial:(NSString *_Nullable)expectedSerial
|
|
48
|
+
completion:(NfcWalletOperationCompletion)completion;
|
|
49
|
+
- (void)runBackupStep:(NSString *)step
|
|
50
|
+
pin:(NSString *_Nullable)pin
|
|
51
|
+
payloadHex:(NSString *_Nullable)payloadHex
|
|
52
|
+
backupCount:(NSInteger)backupCount
|
|
53
|
+
completion:(NfcWalletOperationCompletion)completion;
|
|
54
|
+
- (void)attestCardWithChallengeHex:(NSString *)challengeHex
|
|
55
|
+
completion:(NfcWalletOperationCompletion)completion;
|
|
56
|
+
@end
|
|
57
|
+
|
|
58
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
#import "NfcWalletNFCManager.h"
|
|
2
|
+
|
|
3
|
+
@interface NfcWalletNFCManager () <NFCTagReaderSessionDelegate>
|
|
4
|
+
@property (nonatomic, strong, nullable) NFCTagReaderSession *session;
|
|
5
|
+
@property (nonatomic, assign) NfcWalletOperationType operationType;
|
|
6
|
+
@property (nonatomic, copy, nullable) NfcWalletOperationCompletion completion;
|
|
7
|
+
@property (nonatomic, copy, nullable) NSString *pin;
|
|
8
|
+
@property (nonatomic, copy, nullable) NSString *newPin;
|
|
9
|
+
@property (nonatomic, copy, nullable) NSString *hashHex;
|
|
10
|
+
@property (nonatomic, copy, nullable) NSString *seedHex;
|
|
11
|
+
@property (nonatomic, copy, nullable) NSString *derivationPath;
|
|
12
|
+
@property (nonatomic, copy, nullable) NSString *pathsJson;
|
|
13
|
+
@property (nonatomic, copy, nullable) NSString *expectedSerial;
|
|
14
|
+
@property (nonatomic, copy, nullable) NSString *backupStep;
|
|
15
|
+
@property (nonatomic, copy, nullable) NSString *payloadHex;
|
|
16
|
+
@property (nonatomic, copy, nullable) NSString *challengeHex;
|
|
17
|
+
@property (nonatomic, assign) NSInteger backupCount;
|
|
18
|
+
@property (nonatomic, assign) BOOL operationFinished;
|
|
19
|
+
@end
|
|
20
|
+
|
|
21
|
+
@implementation NfcWalletNFCManager
|
|
22
|
+
|
|
23
|
+
- (void)cancelSession
|
|
24
|
+
{
|
|
25
|
+
[self.session invalidateSession];
|
|
26
|
+
self.session = nil;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
- (void)runOperation:(NfcWalletOperationType)type
|
|
30
|
+
pin:(NSString *)pin
|
|
31
|
+
newPin:(NSString *_Nullable)newPin
|
|
32
|
+
hashHex:(NSString *)hashHex
|
|
33
|
+
seedHex:(NSString *)seedHex
|
|
34
|
+
derivationPath:(NSString *_Nullable)derivationPath
|
|
35
|
+
pathsJson:(NSString *_Nullable)pathsJson
|
|
36
|
+
expectedSerial:(NSString *_Nullable)expectedSerial
|
|
37
|
+
completion:(NfcWalletOperationCompletion)completion
|
|
38
|
+
{
|
|
39
|
+
#if TARGET_OS_SIMULATOR
|
|
40
|
+
completion(nil, [NSError errorWithDomain:@"NfcWallet" code:2001 userInfo:@{NSLocalizedDescriptionKey : @"NFC unavailable on simulator"}]);
|
|
41
|
+
return;
|
|
42
|
+
#endif
|
|
43
|
+
if (![NFCTagReaderSession readingAvailable]) {
|
|
44
|
+
completion(nil, [NSError errorWithDomain:@"NfcWallet" code:2001 userInfo:@{NSLocalizedDescriptionKey : @"NFC unavailable"}]);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
[self cancelSession];
|
|
48
|
+
self.operationType = type;
|
|
49
|
+
self.completion = completion;
|
|
50
|
+
self.pin = pin;
|
|
51
|
+
self.newPin = newPin;
|
|
52
|
+
self.hashHex = hashHex;
|
|
53
|
+
self.seedHex = seedHex;
|
|
54
|
+
self.derivationPath = derivationPath;
|
|
55
|
+
self.pathsJson = pathsJson;
|
|
56
|
+
self.expectedSerial = expectedSerial;
|
|
57
|
+
self.backupStep = nil;
|
|
58
|
+
self.payloadHex = nil;
|
|
59
|
+
self.backupCount = 2;
|
|
60
|
+
self.operationFinished = NO;
|
|
61
|
+
self.session = [[NFCTagReaderSession alloc] initWithPollingOption:NFCPollingISO14443 delegate:self queue:nil];
|
|
62
|
+
self.session.alertMessage = NSLocalizedString(@"Hold near NFC wallet card", @"");
|
|
63
|
+
[self.session beginSession];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
- (void)runOperation:(NfcWalletOperationType)type
|
|
67
|
+
pin:(NSString *)pin
|
|
68
|
+
hashHex:(NSString *)hashHex
|
|
69
|
+
seedHex:(NSString *)seedHex
|
|
70
|
+
derivationPath:(NSString *_Nullable)derivationPath
|
|
71
|
+
pathsJson:(NSString *_Nullable)pathsJson
|
|
72
|
+
expectedSerial:(NSString *_Nullable)expectedSerial
|
|
73
|
+
completion:(NfcWalletOperationCompletion)completion
|
|
74
|
+
{
|
|
75
|
+
[self runOperation:type
|
|
76
|
+
pin:pin
|
|
77
|
+
newPin:nil
|
|
78
|
+
hashHex:hashHex
|
|
79
|
+
seedHex:seedHex
|
|
80
|
+
derivationPath:derivationPath
|
|
81
|
+
pathsJson:pathsJson
|
|
82
|
+
expectedSerial:expectedSerial
|
|
83
|
+
completion:completion];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
- (void)runBackupStep:(NSString *)step
|
|
87
|
+
pin:(NSString *)pin
|
|
88
|
+
payloadHex:(NSString *)payloadHex
|
|
89
|
+
backupCount:(NSInteger)backupCount
|
|
90
|
+
completion:(NfcWalletOperationCompletion)completion
|
|
91
|
+
{
|
|
92
|
+
self.backupStep = step;
|
|
93
|
+
self.payloadHex = payloadHex;
|
|
94
|
+
self.backupCount = backupCount > 0 ? backupCount : 2;
|
|
95
|
+
[self runOperation:NfcWalletOperationBackupStep
|
|
96
|
+
pin:pin
|
|
97
|
+
hashHex:nil
|
|
98
|
+
seedHex:nil
|
|
99
|
+
derivationPath:nil
|
|
100
|
+
pathsJson:nil
|
|
101
|
+
expectedSerial:nil
|
|
102
|
+
completion:completion];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
- (void)attestCardWithChallengeHex:(NSString *)challengeHex
|
|
106
|
+
completion:(NfcWalletOperationCompletion)completion
|
|
107
|
+
{
|
|
108
|
+
self.challengeHex = challengeHex;
|
|
109
|
+
[self runOperation:NfcWalletOperationAttestCard
|
|
110
|
+
pin:nil
|
|
111
|
+
hashHex:nil
|
|
112
|
+
seedHex:nil
|
|
113
|
+
derivationPath:nil
|
|
114
|
+
pathsJson:nil
|
|
115
|
+
expectedSerial:nil
|
|
116
|
+
completion:completion];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
- (void)finishWithResult:(NSDictionary *_Nullable)result error:(NSError *_Nullable)error
|
|
120
|
+
{
|
|
121
|
+
NFCTagReaderSession *activeSession = self.session;
|
|
122
|
+
NfcWalletOperationCompletion completion = self.completion;
|
|
123
|
+
if (self.operationFinished) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
self.operationFinished = YES;
|
|
127
|
+
self.completion = nil;
|
|
128
|
+
self.session = nil;
|
|
129
|
+
if (error) {
|
|
130
|
+
[activeSession invalidateSessionWithErrorMessage:error.localizedDescription ?: @"Failed"];
|
|
131
|
+
} else {
|
|
132
|
+
[activeSession invalidateSession];
|
|
133
|
+
}
|
|
134
|
+
if (completion) {
|
|
135
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
136
|
+
completion(result, error);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
- (void)tagReaderSession:(NFCTagReaderSession *)session didInvalidateWithError:(NSError *)error
|
|
142
|
+
{
|
|
143
|
+
if (!self.operationFinished && self.completion) {
|
|
144
|
+
[self finishWithResult:nil error:error];
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
- (void)tagReaderSessionDidBecomeActive:(NFCTagReaderSession *)session
|
|
149
|
+
{
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
- (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<__kindof id<NFCTag>> *)tags
|
|
153
|
+
{
|
|
154
|
+
id<NFCTag> tag = tags.firstObject;
|
|
155
|
+
if (!tag) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
[session connectToTag:tag
|
|
159
|
+
completionHandler:^(NSError *_Nullable error) {
|
|
160
|
+
if (error) {
|
|
161
|
+
[self finishWithResult:nil error:error];
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (![tag respondsToSelector:@selector(asNFCISO7816Tag)]) {
|
|
165
|
+
[self finishWithResult:nil
|
|
166
|
+
error:[NSError errorWithDomain:@"NfcWallet" code:2004 userInfo:@{NSLocalizedDescriptionKey : @"Not ISO7816 tag"}]];
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
id<NFCISO7816Tag> isoTag = [tag asNFCISO7816Tag];
|
|
170
|
+
NfcWalletSession *wallet = [[NfcWalletSession alloc] initWithTag:isoTag apduLogBlock:self.apduLogBlock];
|
|
171
|
+
NSDictionary *result = nil;
|
|
172
|
+
switch (self.operationType) {
|
|
173
|
+
case NfcWalletOperationGetInfo:
|
|
174
|
+
result = [wallet getWalletInfo];
|
|
175
|
+
break;
|
|
176
|
+
case NfcWalletOperationSetPin:
|
|
177
|
+
result = [wallet setPin:self.pin ?: @""];
|
|
178
|
+
break;
|
|
179
|
+
case NfcWalletOperationChangePin:
|
|
180
|
+
result = [wallet changePin:self.pin ?: @""
|
|
181
|
+
newPin:self.newPin ?: @""
|
|
182
|
+
expectedSerial:self.expectedSerial];
|
|
183
|
+
break;
|
|
184
|
+
case NfcWalletOperationVerifyPin:
|
|
185
|
+
result = [wallet verifyPin:self.pin ?: @"" expectedSerial:self.expectedSerial];
|
|
186
|
+
break;
|
|
187
|
+
case NfcWalletOperationLogout:
|
|
188
|
+
result = [wallet logout];
|
|
189
|
+
break;
|
|
190
|
+
case NfcWalletOperationCreateWallet:
|
|
191
|
+
result = [wallet createWalletWithExpectedSerial:self.expectedSerial];
|
|
192
|
+
break;
|
|
193
|
+
case NfcWalletOperationCreateWalletWithSeed:
|
|
194
|
+
result = [wallet createWalletWithSeedHex:self.seedHex ?: @""
|
|
195
|
+
expectedSerial:self.expectedSerial];
|
|
196
|
+
break;
|
|
197
|
+
case NfcWalletOperationGetPublicKey:
|
|
198
|
+
result = [wallet getPublicKeyWithPath:self.derivationPath curveP1:kNfcWalletP1PubkeySecp256k1];
|
|
199
|
+
break;
|
|
200
|
+
case NfcWalletOperationSignHash:
|
|
201
|
+
result = [wallet signHash:self.hashHex ?: @"" path:self.derivationPath];
|
|
202
|
+
break;
|
|
203
|
+
case NfcWalletOperationWipe:
|
|
204
|
+
result = [wallet wipeWallet];
|
|
205
|
+
break;
|
|
206
|
+
case NfcWalletOperationWipeWithPin:
|
|
207
|
+
result = [wallet wipeWalletWithPin:self.pin ?: @""
|
|
208
|
+
expectedSerial:self.expectedSerial ?: @""];
|
|
209
|
+
break;
|
|
210
|
+
case NfcWalletOperationInitialize:
|
|
211
|
+
result = [wallet initializeWallet:self.pin ?: @"" seedHex:self.seedHex];
|
|
212
|
+
break;
|
|
213
|
+
case NfcWalletOperationInitializeWithPubkeys:
|
|
214
|
+
result = [wallet initializeWalletWithPubkeys:self.pin ?: @""
|
|
215
|
+
pathsJson:self.pathsJson ?: @"[]"
|
|
216
|
+
seedHex:self.seedHex
|
|
217
|
+
expectedSerial:self.expectedSerial];
|
|
218
|
+
break;
|
|
219
|
+
case NfcWalletOperationUnlockAndSign:
|
|
220
|
+
result = [wallet unlockAndSignPin:self.pin ?: @"" hashHex:self.hashHex ?: @"" path:self.derivationPath];
|
|
221
|
+
break;
|
|
222
|
+
case NfcWalletOperationBackupStep:
|
|
223
|
+
result = [wallet runBackupStep:self.backupStep ?: @""
|
|
224
|
+
pin:self.pin
|
|
225
|
+
payloadHex:self.payloadHex
|
|
226
|
+
backupCount:self.backupCount];
|
|
227
|
+
break;
|
|
228
|
+
case NfcWalletOperationAttestCard:
|
|
229
|
+
result = [wallet attestCardWithChallengeHex:self.challengeHex ?: @""];
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
if (result[@"error"] != [NSNull null]) {
|
|
233
|
+
NSDictionary *errObj = result[@"error"];
|
|
234
|
+
NSError *opError = [NSError errorWithDomain:@"NfcWallet"
|
|
235
|
+
code:[errObj[@"code"] integerValue]
|
|
236
|
+
userInfo:@{NSLocalizedDescriptionKey : errObj[@"message"] ?: @"Wallet error"}];
|
|
237
|
+
[self finishWithResult:result error:opError];
|
|
238
|
+
} else {
|
|
239
|
+
[self finishWithResult:result error:nil];
|
|
240
|
+
}
|
|
241
|
+
}];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
@end
|