@onetokenfe/react-native-nfc-wallet-sdk 0.1.3 → 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,20 +5,33 @@
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;
14
+ /** Applet SELECT once per tag connection. Re-SELECT clears PIN session (SW=6982). */
15
+ @property (nonatomic, assign) BOOL appletSelected;
12
16
  @end
13
17
 
14
18
  @implementation NfcWalletSession
15
19
 
16
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
17
28
  {
18
29
  self = [super init];
19
30
  if (self) {
20
31
  _tag = tag;
32
+ _commandQueue = commandQueue ?: dispatch_get_main_queue();
21
33
  _apduLogBlock = apduLogBlock;
34
+ _appletSelected = NO;
22
35
  }
23
36
  return self;
24
37
  }
@@ -979,6 +992,11 @@ static const uint8_t kCla = 0x80;
979
992
 
980
993
  - (BOOL)selectApplet:(NSError **)error
981
994
  {
995
+ // Match Android withWalletSession: SELECT once. Java Card deselect() clears PIN
996
+ // (PinService.onDeselect) — re-SELECT after VERIFY_PIN causes GET_PUBKEY SW=6982.
997
+ if (self.appletSelected) {
998
+ return YES;
999
+ }
982
1000
  NSData *aid = [NSData dataWithBytes:kWalletAppletAid length:sizeof(kWalletAppletAid)];
983
1001
  NFCISO7816APDU *apdu = [[NFCISO7816APDU alloc] initWithInstructionClass:0x00
984
1002
  instructionCode:0xA4
@@ -986,7 +1004,11 @@ static const uint8_t kCla = 0x80;
986
1004
  p2Parameter:0x00
987
1005
  data:aid
988
1006
  expectedResponseLength:256];
989
- return [self sendAPDU:apdu error:error] != nil;
1007
+ if ([self sendAPDU:apdu error:error] == nil) {
1008
+ return NO;
1009
+ }
1010
+ self.appletSelected = YES;
1011
+ return YES;
990
1012
  }
991
1013
 
992
1014
  - (NSDictionary *)simpleCommand:(uint8_t)ins data:(NSData *_Nullable)data
@@ -1025,39 +1047,66 @@ static const uint8_t kCla = 0x80;
1025
1047
  - (NSData *_Nullable)sendAPDU:(NFCISO7816APDU *)apdu error:(NSError **)error
1026
1048
  {
1027
1049
  if (self.apduLogBlock) {
1028
- 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
+ });
1029
1056
  }
1030
1057
  __block NSData *response = nil;
1031
1058
  __block NSError *localError = nil;
1032
1059
  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
1033
- [self.tag sendCommandAPDU:apdu
1034
- completionHandler:^(NSData *_Nullable responseData, uint8_t sw1, uint8_t sw2, NSError *_Nullable err) {
1035
- if (err) {
1036
- localError = err;
1037
- } else {
1038
- uint16_t sw = ((uint16_t)sw1 << 8) | sw2;
1039
- if (sw != 0x9000) {
1040
- localError = [NSError errorWithDomain:@"NfcWalletApdu"
1041
- code:sw
1042
- 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;
1043
1068
  } else {
1044
- 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
+ }
1045
1080
  }
1046
- }
1047
- dispatch_semaphore_signal(sem);
1048
- }];
1049
- 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
+ }
1050
1091
  if (localError) {
1051
1092
  if (error) {
1052
1093
  *error = localError;
1053
1094
  }
1054
1095
  if (self.apduLogBlock) {
1055
- 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
+ });
1056
1101
  }
1057
1102
  return nil;
1058
1103
  }
1059
1104
  if (self.apduLogBlock) {
1060
- 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
+ });
1061
1110
  }
1062
1111
  return response;
1063
1112
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onetokenfe/react-native-nfc-wallet-sdk",
3
- "version": "0.1.3",
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",