@onetokenfe/react-native-nfc-wallet-sdk 0.1.2 → 0.1.3

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.
@@ -2,6 +2,8 @@
2
2
 
3
3
  @interface NfcWalletNFCManager () <NFCTagReaderSessionDelegate>
4
4
  @property (nonatomic, strong, nullable) NFCTagReaderSession *session;
5
+ @property (nonatomic, strong) dispatch_queue_t nfcDelegateQueue;
6
+ @property (nonatomic, strong) dispatch_queue_t nfcWorkerQueue;
5
7
  @property (nonatomic, assign) NfcWalletOperationType operationType;
6
8
  @property (nonatomic, copy, nullable) NfcWalletOperationCompletion completion;
7
9
  @property (nonatomic, copy, nullable) NSString *pin;
@@ -16,10 +18,24 @@
16
18
  @property (nonatomic, copy, nullable) NSString *challengeHex;
17
19
  @property (nonatomic, assign) NSInteger backupCount;
18
20
  @property (nonatomic, assign) BOOL operationFinished;
21
+ @property (nonatomic, assign) BOOL operationInProgress;
19
22
  @end
20
23
 
21
24
  @implementation NfcWalletNFCManager
22
25
 
26
+ - (instancetype)init
27
+ {
28
+ self = [super init];
29
+ if (self) {
30
+ // Must not run APDU semaphore waits on the CoreNFC session/delegate queue —
31
+ // sendCommandAPDU completions arrive on that queue and would deadlock (Lite BackupCard
32
+ // avoids this with a dedicated worker queue; wallet SDK previously blocked in connectToTag).
33
+ _nfcDelegateQueue = dispatch_queue_create("com.ziancube.nfcwallet.nfc.delegate", DISPATCH_QUEUE_SERIAL);
34
+ _nfcWorkerQueue = dispatch_queue_create("com.ziancube.nfcwallet.nfc.worker", DISPATCH_QUEUE_SERIAL);
35
+ }
36
+ return self;
37
+ }
38
+
23
39
  - (void)cancelSession
24
40
  {
25
41
  [self.session invalidateSession];
@@ -31,9 +47,9 @@
31
47
  newPin:(NSString *_Nullable)newPin
32
48
  hashHex:(NSString *)hashHex
33
49
  seedHex:(NSString *)seedHex
34
- derivationPath:(NSString *_Nullable)derivationPath
35
- pathsJson:(NSString *_Nullable)pathsJson
36
- expectedSerial:(NSString *_Nullable)expectedSerial
50
+ derivationPath:(NSString *_Nullable)derivationPath
51
+ pathsJson:(NSString *_Nullable)pathsJson
52
+ expectedSerial:(NSString *_Nullable)expectedSerial
37
53
  completion:(NfcWalletOperationCompletion)completion
38
54
  {
39
55
  #if TARGET_OS_SIMULATOR
@@ -58,7 +74,10 @@
58
74
  self.payloadHex = nil;
59
75
  self.backupCount = 2;
60
76
  self.operationFinished = NO;
61
- self.session = [[NFCTagReaderSession alloc] initWithPollingOption:NFCPollingISO14443 delegate:self queue:nil];
77
+ self.operationInProgress = NO;
78
+ self.session = [[NFCTagReaderSession alloc] initWithPollingOption:NFCPollingISO14443
79
+ delegate:self
80
+ queue:self.nfcDelegateQueue];
62
81
  self.session.alertMessage = NSLocalizedString(@"Hold near NFC wallet card", @"");
63
82
  [self.session beginSession];
64
83
  }
@@ -67,9 +86,9 @@
67
86
  pin:(NSString *)pin
68
87
  hashHex:(NSString *)hashHex
69
88
  seedHex:(NSString *)seedHex
70
- derivationPath:(NSString *_Nullable)derivationPath
71
- pathsJson:(NSString *_Nullable)pathsJson
72
- expectedSerial:(NSString *_Nullable)expectedSerial
89
+ derivationPath:(NSString *_Nullable)derivationPath
90
+ pathsJson:(NSString *_Nullable)pathsJson
91
+ expectedSerial:(NSString *_Nullable)expectedSerial
73
92
  completion:(NfcWalletOperationCompletion)completion
74
93
  {
75
94
  [self runOperation:type
@@ -118,14 +137,21 @@
118
137
 
119
138
  - (void)finishWithResult:(NSDictionary *_Nullable)result error:(NSError *_Nullable)error
120
139
  {
121
- NFCTagReaderSession *activeSession = self.session;
122
- NfcWalletOperationCompletion completion = self.completion;
123
- if (self.operationFinished) {
124
- return;
140
+ NFCTagReaderSession *activeSession = nil;
141
+ NfcWalletOperationCompletion completion = nil;
142
+
143
+ @synchronized(self) {
144
+ if (self.operationFinished) {
145
+ return;
146
+ }
147
+ self.operationFinished = YES;
148
+ self.operationInProgress = NO;
149
+ activeSession = self.session;
150
+ completion = self.completion;
151
+ self.completion = nil;
152
+ self.session = nil;
125
153
  }
126
- self.operationFinished = YES;
127
- self.completion = nil;
128
- self.session = nil;
154
+
129
155
  if (error) {
130
156
  [activeSession invalidateSessionWithErrorMessage:error.localizedDescription ?: @"Failed"];
131
157
  } else {
@@ -138,11 +164,96 @@
138
164
  }
139
165
  }
140
166
 
167
+ - (NSDictionary *)runCurrentOperationWithWallet:(NfcWalletSession *)wallet
168
+ {
169
+ switch (self.operationType) {
170
+ case NfcWalletOperationGetInfo:
171
+ return [wallet getWalletInfo];
172
+ case NfcWalletOperationSetPin:
173
+ return [wallet setPin:self.pin ?: @""];
174
+ case NfcWalletOperationChangePin:
175
+ return [wallet changePin:self.pin ?: @""
176
+ newPin:self.newPin ?: @""
177
+ expectedSerial:self.expectedSerial];
178
+ case NfcWalletOperationVerifyPin:
179
+ return [wallet verifyPin:self.pin ?: @"" expectedSerial:self.expectedSerial];
180
+ case NfcWalletOperationLogout:
181
+ return [wallet logout];
182
+ case NfcWalletOperationCreateWallet:
183
+ return [wallet createWalletWithExpectedSerial:self.expectedSerial];
184
+ case NfcWalletOperationCreateWalletWithSeed:
185
+ return [wallet createWalletWithSeedHex:self.seedHex ?: @""
186
+ expectedSerial:self.expectedSerial];
187
+ case NfcWalletOperationGetPublicKey:
188
+ return [wallet getPublicKeyWithPath:self.derivationPath curveP1:kNfcWalletP1PubkeySecp256k1];
189
+ case NfcWalletOperationSignHash:
190
+ return [wallet signHash:self.hashHex ?: @"" path:self.derivationPath];
191
+ case NfcWalletOperationWipe:
192
+ return [wallet wipeWallet];
193
+ case NfcWalletOperationWipeWithPin:
194
+ return [wallet wipeWalletWithPin:self.pin ?: @""
195
+ expectedSerial:self.expectedSerial ?: @""];
196
+ case NfcWalletOperationInitialize:
197
+ return [wallet initializeWallet:self.pin ?: @"" seedHex:self.seedHex];
198
+ case NfcWalletOperationInitializeWithPubkeys:
199
+ return [wallet initializeWalletWithPubkeys:self.pin ?: @""
200
+ pathsJson:self.pathsJson ?: @"[]"
201
+ seedHex:self.seedHex
202
+ expectedSerial:self.expectedSerial];
203
+ case NfcWalletOperationUnlockAndSign:
204
+ return [wallet unlockAndSignPin:self.pin ?: @"" hashHex:self.hashHex ?: @"" path:self.derivationPath];
205
+ case NfcWalletOperationBackupStep:
206
+ return [wallet runBackupStep:self.backupStep ?: @""
207
+ pin:self.pin
208
+ payloadHex:self.payloadHex
209
+ backupCount:self.backupCount];
210
+ case NfcWalletOperationAttestCard:
211
+ return [wallet attestCardWithChallengeHex:self.challengeHex ?: @""];
212
+ }
213
+ return [NSDictionary dictionary];
214
+ }
215
+
216
+ #pragma mark - NFCTagReaderSessionDelegate
217
+
141
218
  - (void)tagReaderSession:(NFCTagReaderSession *)session didInvalidateWithError:(NSError *)error
142
219
  {
143
- if (!self.operationFinished && self.completion) {
144
- [self finishWithResult:nil error:error];
220
+ NfcWalletOperationCompletion completion = nil;
221
+
222
+ @synchronized(self) {
223
+ // Ignore stale invalidation from a previous session.
224
+ if (session != self.session) {
225
+ return;
226
+ }
227
+ if (self.operationFinished) {
228
+ return;
229
+ }
230
+ // Worker is still running — let it finish and return a business-level result.
231
+ if (self.operationInProgress) {
232
+ return;
233
+ }
234
+ self.operationFinished = YES;
235
+ completion = self.completion;
236
+ self.completion = nil;
237
+ self.session = nil;
238
+ }
239
+
240
+ if (!completion) {
241
+ return;
145
242
  }
243
+
244
+ // User cancelled (NFCReaderErrorSessionInvalidatedByUser / system dismiss).
245
+ if (error.code == 200 || error.code == 6) {
246
+ dispatch_async(dispatch_get_main_queue(), ^{
247
+ completion(nil, [NSError errorWithDomain:@"NfcWallet"
248
+ code:2004
249
+ userInfo:@{NSLocalizedDescriptionKey : @"NFC wait cancelled"}]);
250
+ });
251
+ return;
252
+ }
253
+
254
+ dispatch_async(dispatch_get_main_queue(), ^{
255
+ completion(nil, error);
256
+ });
146
257
  }
147
258
 
148
259
  - (void)tagReaderSessionDidBecomeActive:(NFCTagReaderSession *)session
@@ -163,81 +274,38 @@
163
274
  }
164
275
  if (![tag respondsToSelector:@selector(asNFCISO7816Tag)]) {
165
276
  [self finishWithResult:nil
166
- error:[NSError errorWithDomain:@"NfcWallet" code:2004 userInfo:@{NSLocalizedDescriptionKey : @"Not ISO7816 tag"}]];
277
+ error:[NSError errorWithDomain:@"NfcWallet"
278
+ code:2004
279
+ userInfo:@{NSLocalizedDescriptionKey : @"Not ISO7816 tag"}]];
167
280
  return;
168
281
  }
169
282
  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;
283
+ if (!isoTag) {
284
+ [self finishWithResult:nil
285
+ error:[NSError errorWithDomain:@"NfcWallet"
286
+ code:2004
287
+ userInfo:@{NSLocalizedDescriptionKey : @"Not ISO7816 tag"}]];
288
+ return;
231
289
  }
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];
290
+
291
+ session.alertMessage = NSLocalizedString(@"Processing...", @"NFC processing prompt");
292
+ @synchronized(self) {
293
+ self.operationInProgress = YES;
240
294
  }
295
+ // Run APDU (semaphore) off the CoreNFC callback queue to avoid deadlock.
296
+ dispatch_async(self.nfcWorkerQueue, ^{
297
+ NfcWalletSession *wallet = [[NfcWalletSession alloc] initWithTag:isoTag apduLogBlock:self.apduLogBlock];
298
+ NSDictionary *result = [self runCurrentOperationWithWallet:wallet];
299
+ if (result[@"error"] != [NSNull null] && result[@"error"] != nil) {
300
+ NSDictionary *errObj = result[@"error"];
301
+ NSError *opError = [NSError errorWithDomain:@"NfcWallet"
302
+ code:[errObj[@"code"] integerValue]
303
+ userInfo:@{NSLocalizedDescriptionKey : errObj[@"message"] ?: @"Wallet error"}];
304
+ [self finishWithResult:result error:opError];
305
+ } else {
306
+ [self finishWithResult:result error:nil];
307
+ }
308
+ });
241
309
  }];
242
310
  }
243
311
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onetokenfe/react-native-nfc-wallet-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "React Native SDK for OneToken NFC Wallet card (instance 6F6E65746F6B656E02)",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",