@onetokenfe/react-native-nfc-wallet-sdk 0.1.4 → 0.1.5

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.
@@ -292,9 +292,13 @@
292
292
  @synchronized(self) {
293
293
  self.operationInProgress = YES;
294
294
  }
295
- // Run APDU (semaphore) off the CoreNFC callback queue to avoid deadlock.
295
+ // Run APDU waits off the CoreNFC callback queue to avoid deadlock, but each
296
+ // sendCommandAPDU must be issued on the session/delegate queue (see sendAPDU).
297
+ dispatch_queue_t commandQueue = self.nfcDelegateQueue;
296
298
  dispatch_async(self.nfcWorkerQueue, ^{
297
- NfcWalletSession *wallet = [[NfcWalletSession alloc] initWithTag:isoTag apduLogBlock:self.apduLogBlock];
299
+ NfcWalletSession *wallet = [[NfcWalletSession alloc] initWithTag:isoTag
300
+ commandQueue:commandQueue
301
+ apduLogBlock:self.apduLogBlock];
298
302
  NSDictionary *result = [self runCurrentOperationWithWallet:wallet];
299
303
  if (result[@"error"] != [NSNull null] && result[@"error"] != nil) {
300
304
  NSDictionary *errObj = result[@"error"];
@@ -11,6 +11,10 @@ static const uint8_t kNfcWalletP1PubkeyEd25519 = 0x01;
11
11
 
12
12
  @interface NfcWalletSession : NSObject
13
13
  - (instancetype)initWithTag:(id<NFCISO7816Tag>)tag apduLogBlock:(NfcWalletApduLogBlock _Nullable)apduLogBlock;
14
+ /** Prefer this: sendCommandAPDU must run on the NFCTagReaderSession queue. */
15
+ - (instancetype)initWithTag:(id<NFCISO7816Tag>)tag
16
+ commandQueue:(dispatch_queue_t)commandQueue
17
+ apduLogBlock:(NfcWalletApduLogBlock _Nullable)apduLogBlock;
14
18
  - (NSDictionary *)getWalletInfo;
15
19
  - (NSDictionary *)setPin:(NSString *)pin;
16
20
  - (NSDictionary *)setPin:(NSString *)pin expectedSerial:(NSString *_Nullable)expectedSerial;
@@ -5,9 +5,11 @@
5
5
 
6
6
  static const uint8_t kWalletAppletAid[] = {0x6F, 0x6E, 0x65, 0x74, 0x6F, 0x6B, 0x65, 0x6E, 0x02};
7
7
  static const uint8_t kCla = 0x80;
8
+ static const NSTimeInterval kNfcWalletApduTimeoutSeconds = 30;
8
9
 
9
10
  @interface NfcWalletSession ()
10
11
  @property (nonatomic, weak) id<NFCISO7816Tag> tag;
12
+ @property (nonatomic, strong) dispatch_queue_t commandQueue;
11
13
  @property (nonatomic, copy, nullable) NfcWalletApduLogBlock apduLogBlock;
12
14
  /** Applet SELECT once per tag connection. Re-SELECT clears PIN session (SW=6982). */
13
15
  @property (nonatomic, assign) BOOL appletSelected;
@@ -16,10 +18,18 @@ static const uint8_t kCla = 0x80;
16
18
  @implementation NfcWalletSession
17
19
 
18
20
  - (instancetype)initWithTag:(id<NFCISO7816Tag>)tag apduLogBlock:(NfcWalletApduLogBlock)apduLogBlock
21
+ {
22
+ return [self initWithTag:tag commandQueue:dispatch_get_main_queue() apduLogBlock:apduLogBlock];
23
+ }
24
+
25
+ - (instancetype)initWithTag:(id<NFCISO7816Tag>)tag
26
+ commandQueue:(dispatch_queue_t)commandQueue
27
+ apduLogBlock:(NfcWalletApduLogBlock)apduLogBlock
19
28
  {
20
29
  self = [super init];
21
30
  if (self) {
22
31
  _tag = tag;
32
+ _commandQueue = commandQueue ?: dispatch_get_main_queue();
23
33
  _apduLogBlock = apduLogBlock;
24
34
  _appletSelected = NO;
25
35
  }
@@ -1037,39 +1047,66 @@ static const uint8_t kCla = 0x80;
1037
1047
  - (NSData *_Nullable)sendAPDU:(NFCISO7816APDU *)apdu error:(NSError **)error
1038
1048
  {
1039
1049
  if (self.apduLogBlock) {
1040
- self.apduLogBlock(NfcWalletHexFromData(apdu.data), YES, YES);
1050
+ // Never block the NFC command path on RN bridge emission.
1051
+ NfcWalletApduLogBlock log = self.apduLogBlock;
1052
+ NSString *sent = NfcWalletHexFromData(apdu.data);
1053
+ dispatch_async(dispatch_get_main_queue(), ^{
1054
+ log(sent, YES, YES);
1055
+ });
1041
1056
  }
1042
1057
  __block NSData *response = nil;
1043
1058
  __block NSError *localError = nil;
1044
1059
  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
1045
- [self.tag sendCommandAPDU:apdu
1046
- completionHandler:^(NSData *_Nullable responseData, uint8_t sw1, uint8_t sw2, NSError *_Nullable err) {
1047
- if (err) {
1048
- localError = err;
1049
- } else {
1050
- uint16_t sw = ((uint16_t)sw1 << 8) | sw2;
1051
- if (sw != 0x9000) {
1052
- localError = [NSError errorWithDomain:@"NfcWalletApdu"
1053
- code:sw
1054
- userInfo:@{NSLocalizedDescriptionKey : [NSString stringWithFormat:@"SW=%04X", sw]}];
1060
+ // CoreNFC: issue sendCommandAPDU on the session/delegate queue. Calling it from the
1061
+ // worker queue can leave the completion pending until the tag is removed
1062
+ // ("Tag response error / no response").
1063
+ dispatch_async(self.commandQueue, ^{
1064
+ [self.tag sendCommandAPDU:apdu
1065
+ completionHandler:^(NSData *_Nullable responseData, uint8_t sw1, uint8_t sw2, NSError *_Nullable err) {
1066
+ if (err) {
1067
+ localError = err;
1055
1068
  } else {
1056
- response = responseData ?: [NSData data];
1069
+ uint16_t sw = ((uint16_t)sw1 << 8) | sw2;
1070
+ if (sw != 0x9000) {
1071
+ localError = [NSError errorWithDomain:@"NfcWalletApdu"
1072
+ code:sw
1073
+ userInfo:@{
1074
+ NSLocalizedDescriptionKey :
1075
+ [NSString stringWithFormat:@"SW=%04X", sw]
1076
+ }];
1077
+ } else {
1078
+ response = responseData ?: [NSData data];
1079
+ }
1057
1080
  }
1058
- }
1059
- dispatch_semaphore_signal(sem);
1060
- }];
1061
- dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
1081
+ dispatch_semaphore_signal(sem);
1082
+ }];
1083
+ });
1084
+ long waited = dispatch_semaphore_wait(
1085
+ sem, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kNfcWalletApduTimeoutSeconds * NSEC_PER_SEC)));
1086
+ if (waited != 0) {
1087
+ localError = [NSError errorWithDomain:@"NfcWallet"
1088
+ code:2003
1089
+ userInfo:@{NSLocalizedDescriptionKey : @"NFC APDU timeout"}];
1090
+ }
1062
1091
  if (localError) {
1063
1092
  if (error) {
1064
1093
  *error = localError;
1065
1094
  }
1066
1095
  if (self.apduLogBlock) {
1067
- self.apduLogBlock(localError.localizedDescription, NO, NO);
1096
+ NfcWalletApduLogBlock log = self.apduLogBlock;
1097
+ NSString *msg = localError.localizedDescription;
1098
+ dispatch_async(dispatch_get_main_queue(), ^{
1099
+ log(msg, NO, NO);
1100
+ });
1068
1101
  }
1069
1102
  return nil;
1070
1103
  }
1071
1104
  if (self.apduLogBlock) {
1072
- self.apduLogBlock(NfcWalletHexFromData(response), NO, YES);
1105
+ NfcWalletApduLogBlock log = self.apduLogBlock;
1106
+ NSString *recv = NfcWalletHexFromData(response);
1107
+ dispatch_async(dispatch_get_main_queue(), ^{
1108
+ log(recv, NO, YES);
1109
+ });
1073
1110
  }
1074
1111
  return response;
1075
1112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onetokenfe/react-native-nfc-wallet-sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
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",