@onekeyfe/react-native-lite-card 0.1.2

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 (95) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +37 -0
  3. package/ReactNativeLiteCard.podspec +22 -0
  4. package/android/build.gradle +79 -0
  5. package/android/gradle.properties +5 -0
  6. package/android/src/main/AndroidManifest.xml +2 -0
  7. package/android/src/main/config/command.json +205 -0
  8. package/android/src/main/cpp/CMakeLists.txt +19 -0
  9. package/android/src/main/cpp/validation.c +227 -0
  10. package/android/src/main/cpp/validation.h +19 -0
  11. package/android/src/main/java/com/onekeyfe/reactnativelitecard/LoggerManager.kt +35 -0
  12. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardModule.kt +420 -0
  13. package/android/src/main/java/com/onekeyfe/reactnativelitecard/ReactNativeLiteCardPackage.kt +33 -0
  14. package/android/src/main/java/com/onekeyfe/reactnativelitecard/keys/KeysNativeProvider.kt +13 -0
  15. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/Exceptions.kt +58 -0
  16. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/NfcUtils.kt +142 -0
  17. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/broadcast/NfcStatusChangeBroadcastReceiver.kt +56 -0
  18. package/android/src/main/java/com/onekeyfe/reactnativelitecard/nfc/gpchannel/GPChannelNatives.kt +32 -0
  19. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/NfcConstant.kt +38 -0
  20. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/OnekeyLiteCard.kt +236 -0
  21. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/APDUParam.kt +17 -0
  22. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/CardInfo.java +84 -0
  23. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/CardResponse.java +37 -0
  24. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/CardState.kt +10 -0
  25. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/ParsedCertInfo.java +39 -0
  26. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SecureChanelParam.java +113 -0
  27. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/entitys/SendResponse.kt +26 -0
  28. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/CommandGenerator.kt +178 -0
  29. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/Connection.kt +439 -0
  30. package/android/src/main/java/com/onekeyfe/reactnativelitecard/onekeyLite/nfc/GPCAPDUGenerator.kt +56 -0
  31. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/EventUtils.kt +14 -0
  32. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/GpsUtil.kt +38 -0
  33. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/HexUtils.java +93 -0
  34. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/LogUtil.kt +11 -0
  35. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/MiUtil.kt +93 -0
  36. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/NfcPermissionUtils.kt +24 -0
  37. package/android/src/main/java/com/onekeyfe/reactnativelitecard/utils/Utils.java +437 -0
  38. package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
  39. package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
  40. package/ios/Classes/OKLiteCommand/OKLiteCommandModal.h +39 -0
  41. package/ios/Classes/OKLiteCommand/OKLiteCommandModal.m +143 -0
  42. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.h +28 -0
  43. package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +53 -0
  44. package/ios/Classes/OKNFCBridge.h +28 -0
  45. package/ios/Classes/OKNFCBridge.mm +221 -0
  46. package/ios/Classes/OKNFCLiteDefine.h +108 -0
  47. package/ios/Classes/OKNFTLite/OKLiteProtocol.h +37 -0
  48. package/ios/Classes/OKNFTLite/OKLiteV1.h +41 -0
  49. package/ios/Classes/OKNFTLite/OKLiteV1.m +609 -0
  50. package/ios/Classes/OKNFTLite/OKLiteV2.h +17 -0
  51. package/ios/Classes/OKNFTLite/OKLiteV2.m +132 -0
  52. package/ios/Classes/OKNFTLite/OKNFCManager.h +40 -0
  53. package/ios/Classes/OKNFTLite/OKNFCManager.m +330 -0
  54. package/ios/Classes/Utils/NFCConfig.h +19 -0
  55. package/ios/Classes/Utils/NFCConfig.m +18 -0
  56. package/ios/Classes/Utils/NSData+OKNFCHexData.h +17 -0
  57. package/ios/Classes/Utils/NSData+OKNFCHexData.m +24 -0
  58. package/ios/Classes/Utils/NSData+StringToData.h +12 -0
  59. package/ios/Classes/Utils/NSData+StringToData.m +25 -0
  60. package/ios/Classes/Utils/NSString+OKAdd.h +14 -0
  61. package/ios/Classes/Utils/NSString+OKAdd.m +40 -0
  62. package/ios/Classes/Utils/NSString+OKNFCHexStr.h +18 -0
  63. package/ios/Classes/Utils/NSString+OKNFCHexStr.m +38 -0
  64. package/ios/Classes/Utils/OKNFCUtility.h +18 -0
  65. package/ios/Classes/Utils/OKNFCUtility.m +22 -0
  66. package/ios/Classes/Utils/OKTools.h +17 -0
  67. package/ios/Classes/Utils/OKTools.m +21 -0
  68. package/ios/GPChannelSDKCore.xcframework/Info.plist +52 -0
  69. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  70. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +315 -0
  71. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +19 -0
  72. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Info.plist +0 -0
  73. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/Modules/module.modulemap +7 -0
  74. package/ios/GPChannelSDKCore.xcframework/ios-arm64/GPChannelSDKCore.framework/_CodeSignature/CodeResources +147 -0
  75. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
  76. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +315 -0
  77. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +19 -0
  78. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Info.plist +0 -0
  79. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/Modules/module.modulemap +7 -0
  80. package/ios/GPChannelSDKCore.xcframework/ios-arm64_x86_64-simulator/GPChannelSDKCore.framework/_CodeSignature/CodeResources +147 -0
  81. package/ios/ReactNativeLiteCard.h +5 -0
  82. package/ios/ReactNativeLiteCard.mm +222 -0
  83. package/lib/module/NativeReactNativeLiteCard.js +25 -0
  84. package/lib/module/NativeReactNativeLiteCard.js.map +1 -0
  85. package/lib/module/index.js +71 -0
  86. package/lib/module/index.js.map +1 -0
  87. package/lib/module/package.json +1 -0
  88. package/lib/typescript/package.json +1 -0
  89. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts +59 -0
  90. package/lib/typescript/src/NativeReactNativeLiteCard.d.ts.map +1 -0
  91. package/lib/typescript/src/index.d.ts +23 -0
  92. package/lib/typescript/src/index.d.ts.map +1 -0
  93. package/package.json +168 -0
  94. package/src/NativeReactNativeLiteCard.ts +112 -0
  95. package/src/index.tsx +94 -0
@@ -0,0 +1,41 @@
1
+ //
2
+ // OKLiteV1.h
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import "OKLiteProtocol.h"
10
+ #import "OKNFCManager.h"
11
+ #import "OKNFCLiteDefine.h"
12
+ #import "OKNFCBridge.h"
13
+ #import "OKLiteCommandTool.h"
14
+
15
+
16
+ @interface OKLiteV1 : NSObject<OKLiteProtocol>
17
+ @property (nonatomic, copy) NSString *SN;
18
+ @property (nonatomic, assign) NSUInteger pinRTL;
19
+ @property (nonatomic, assign) OKNFCLiteStatus status;
20
+
21
+ @property (nonatomic, assign) OKNFCLiteVersion version;
22
+ @property (nonatomic, weak) id<OKNFCManagerDelegate> delegate;
23
+ @property (nonatomic, strong) OKLiteCommandTool *commandTool;
24
+
25
+
26
+ - (instancetype)initWithDelegate:(id<OKNFCManagerDelegate>)delegate;
27
+
28
+ - (BOOL)openSecureChannel;
29
+
30
+ - (BOOL)setPin:(NSString *)pin;
31
+
32
+ - (BOOL)setMnc:(NSString *)mnc;
33
+
34
+ - (BOOL)selectNFCAppWith:(OKLiteCommand)command;
35
+
36
+ - (BOOL)verifyCert;
37
+
38
+ - (id)cardInfo;
39
+
40
+ @end
41
+
@@ -0,0 +1,609 @@
1
+ //
2
+ // OKLiteV1.m
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import "OKLiteV1.h"
9
+ #import "OKNFCUtility.h"
10
+ #import "OKLiteCommandModal.h"
11
+
12
+ @interface OKLiteV1()
13
+
14
+ @property (nonatomic, assign) BOOL certVerified;
15
+ @property (nonatomic, assign) BOOL inSecureChannel;
16
+
17
+ @property (nonatomic, assign) OKNFCLiteApp selectNFCApp;
18
+ @end
19
+
20
+
21
+ @implementation OKLiteV1
22
+ - (instancetype)initWithDelegate:(id<OKNFCManagerDelegate>)delegate {
23
+ self = [super init];
24
+ if (!self) return self;
25
+ _delegate = delegate;
26
+ _version = OKNFCLiteVersionV1;
27
+ _commandTool = [[OKLiteCommandTool alloc] init];
28
+ _commandTool.delegate = delegate;
29
+ return self;
30
+ }
31
+
32
+ - (id<NFCISO7816Tag>)getTag {
33
+ id<NFCISO7816Tag> tag = [self.delegate getNFCsessionTag];
34
+ if (!tag) {
35
+ [self.delegate endNFCSessionWithError:YES];
36
+ return nil;
37
+ }
38
+ return tag;
39
+ }
40
+
41
+ - (id)cardInfo {
42
+ if (_SN && _pinRTL && _status) {
43
+ return @{
44
+ @"hasBackup":[NSNull null],
45
+ @"pinRetryCount":@(_pinRTL),
46
+ @"isNewCard":@(_status == OKNFCLiteStatusNewCard),
47
+ @"serialNum":_SN
48
+ };
49
+ }
50
+ return [NSNull null];
51
+ }
52
+
53
+ #pragma mark - getLiteInfo
54
+
55
+ - (void)getLiteInfo:(GetLiteInfoCallback)callback {
56
+
57
+ OKNFCLiteStatus status = OKNFCLiteStatusError;
58
+ self.pinRTL = 0;
59
+ NSUInteger PinStatus = OKNFC_PIN_ERROR;
60
+
61
+ if (![self selectNFCAppWith:OKLiteCommandGetCardSN]) {
62
+ [self.delegate endNFCSessionWithError:YES];
63
+ callback(self,status);
64
+ return;
65
+ }
66
+
67
+ NSString *liteSN = [self getSN];
68
+ if ([self.delegate getSessionType] == OKNFCLiteSessionTypeUpdateInfo) {
69
+ if (!liteSN.length || ![liteSN isEqualToString:self.SN]) {
70
+ [self.delegate endNFCSessionWithError:NO];
71
+ callback(self,status);
72
+ return;
73
+ }
74
+ }
75
+
76
+ self.SN = liteSN;
77
+ if (!self.SN.length || ![self verifyCert]) {
78
+ [self.delegate endNFCSessionWithError:YES];
79
+ callback(self,status);
80
+ return;
81
+ }
82
+
83
+ PinStatus = [self getPINStatus];
84
+ if (PinStatus == OKNFC_PIN_ERROR) {
85
+ [self.delegate endNFCSessionWithError:NO];
86
+ callback(self,status);
87
+ return;
88
+ }
89
+
90
+ status = PinStatus == OKNFC_PIN_UNSET ? OKNFCLiteStatusNewCard : OKNFCLiteStatusActivated;
91
+ self.status = status;
92
+ self.pinRTL = PinStatus == OKNFC_PIN_UNSET ? 10 : PinStatus;
93
+
94
+ [self.delegate endNFCSessionWithError:status == OKNFCLiteStatusError];
95
+ callback(self,status);
96
+ }
97
+
98
+ - (BOOL)syncLiteInfo {
99
+
100
+ OKNFCLiteStatus status = OKNFCLiteStatusError;
101
+ self.pinRTL = 0;
102
+ NSUInteger PinStatus = OKNFC_PIN_ERROR;
103
+
104
+ if (![self selectNFCAppWith:OKLiteCommandGetCardSN]) {
105
+ return NO;
106
+ }
107
+
108
+ NSString *liteSN = [self getSN];
109
+ self.SN = liteSN;
110
+ if (!self.SN.length || ![self verifyCert]) {
111
+ return NO;
112
+ }
113
+ PinStatus = [self getPINStatus];
114
+ status = PinStatus == OKNFC_PIN_UNSET ? OKNFCLiteStatusNewCard : OKNFCLiteStatusActivated;
115
+ self.status = status;
116
+ self.pinRTL = PinStatus == OKNFC_PIN_UNSET ? 10 : PinStatus;
117
+ return YES;
118
+ }
119
+
120
+ #pragma mark - setMnemonic
121
+
122
+ - (void)setMnemonic:(NSString *)mnemonic
123
+ withPin:(NSString *)pin
124
+ overwrite:(BOOL)overwrite
125
+ complete:(SetMnemonicCallback)callBack {
126
+ id<NFCISO7816Tag> tag = [self.delegate getNFCsessionTag];
127
+
128
+ OKNFCLiteSetMncStatus status = OKNFCLiteSetMncStatusError;
129
+
130
+ if (![self syncLiteInfo]) {
131
+ [self.delegate endNFCSessionWithError:YES];
132
+ callBack(self,status);
133
+ return;
134
+ }
135
+
136
+ if (self.status == OKNFCLiteStatusActivated && !overwrite) {
137
+ [self.delegate endNFCSessionWithError:YES];
138
+ callBack(self,status);
139
+ return;
140
+ }
141
+
142
+ if (self.status == OKNFCLiteStatusNewCard) {
143
+ [self openSecureChannel];
144
+ [self setPin:pin];
145
+ }
146
+
147
+ BOOL selectNFCAppSuccess = [self selectNFCAppWith:OKLiteCommandImportMnemonic];
148
+ BOOL openSecureChannelSuccess2 = [self openSecureChannel];
149
+
150
+ if (selectNFCAppSuccess && openSecureChannelSuccess2) {
151
+ OKNFCLitePINVerifyResult verifyResult = [self verifyPin:pin];
152
+ if (verifyResult == OKNFCLitePINVerifyResultPass) {
153
+ BOOL setMncSuccess = [self setMnc:mnemonic];
154
+ status = setMncSuccess ? OKNFCLiteSetMncStatusSuccess : OKNFCLiteSetMncStatusError;
155
+ } else if (verifyResult == OKNFCLitePINVerifyResultNotMatch) {
156
+ status = OKNFCLiteSetMncStatusPinNotMatch;
157
+ if (self.pinRTL <= 0) {
158
+ BOOL restSeccuss = [self resetSync];
159
+ status = restSeccuss ? OKNFCLiteSetMncStatusWiped : OKNFCLiteSetMncStatusError;
160
+ }
161
+ }
162
+ }
163
+ if (status == OKNFCLiteSetMncStatusSuccess) {
164
+ self.status = OKNFCLiteStatusActivated;
165
+ } else if (status == OKNFCLiteSetMncStatusWiped) {
166
+ self.status = OKNFCLiteStatusNewCard;
167
+ }
168
+ [self.delegate endNFCSessionWithError:status != OKNFCLiteSetMncStatusSuccess];
169
+ callBack(self,status);
170
+ }
171
+
172
+
173
+ #pragma mark - reset
174
+
175
+ - (void)reset:(ResetCallback)callBack {
176
+ if (![self selectNFCAppWith:OKLiteCommandWipeCard]) {
177
+ [self.delegate endNFCSessionWithError:YES];
178
+ callBack(self,false,nil);
179
+ return;
180
+ }
181
+
182
+ if (![self verifyCert] || ![self openSecureChannel]) {
183
+ [self.delegate endNFCSessionWithError:YES];
184
+ callBack(self,false,nil);
185
+ return;
186
+ }
187
+
188
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandWipeCard version:self.version];
189
+ modal.parseResp = true;
190
+ __weak typeof(self) weakSelf = self;
191
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
192
+ [weakSelf.delegate endNFCSessionWithError:sw1 != OKNFC_SW1_OK];
193
+ if (callBack) {
194
+ callBack(self,sw1 == OKNFC_SW1_OK,nil);
195
+ }
196
+ }];
197
+ }
198
+
199
+ - (BOOL)resetSync {
200
+ __block BOOL success = NO;
201
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
202
+
203
+ [self selectNFCAppWith:OKLiteCommandWipeCard];
204
+
205
+ if (!self.certVerified) {
206
+ self.SN = [self getSN];
207
+ [self verifyCert];
208
+ }
209
+ [self openSecureChannel];
210
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandWipeCard version:self.version];
211
+ modal.parseResp = true;
212
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
213
+ success = sw1 == OKNFC_SW1_OK;
214
+ dispatch_semaphore_signal(sema);
215
+ }];
216
+
217
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
218
+ return success;
219
+ }
220
+
221
+ #pragma mark - getMnemonic
222
+
223
+ - (void)getMnemonicWithPin:(NSString *)pin complete:(GetMnemonicCallback)callback {
224
+ OKNFCLiteGetMncStatus status = OKNFCLiteGetMncStatusError;
225
+ if (![self syncLiteInfo]) {
226
+ [self.delegate endNFCSessionWithError:YES];
227
+ callback(self,nil,status);
228
+ return;
229
+ }
230
+
231
+ if (self.status == OKNFCLiteStatusNewCard) {
232
+ [self.delegate endNFCSessionWithError:YES];
233
+ callback(self,nil,status);
234
+ return;
235
+ }
236
+
237
+ BOOL selectNFCAppSuccess = [self selectNFCAppWith:OKLiteCommandExportMnemonic];;
238
+ BOOL openSecureChannelSuccess2 = [self openSecureChannel];
239
+ if (!selectNFCAppSuccess || !openSecureChannelSuccess2) {
240
+ [self.delegate endNFCSessionWithError:YES];
241
+ callback(self,nil,status);
242
+ return;
243
+ }
244
+
245
+ NSString *mnc = nil;
246
+ OKNFCLitePINVerifyResult verifyResult = [self verifyPin:pin];
247
+ if (verifyResult == OKNFCLitePINVerifyResultPass) {
248
+ mnc = [self getMnc];
249
+ if (mnc) {
250
+ status = OKNFCLiteGetMncStatusSuccess;
251
+ }
252
+ } else if (verifyResult == OKNFCLitePINVerifyResultNotMatch) {
253
+ if (self.pinRTL <= 0) {
254
+ BOOL restSeccuss = [self resetSync];
255
+ status = restSeccuss ? OKNFCLiteGetMncStatusWiped : OKNFCLiteGetMncStatusError;
256
+ } else {
257
+ status = OKNFCLiteGetMncStatusPinNotMatch;
258
+ }
259
+ }
260
+ if (status == OKNFCLiteGetMncStatusWiped) {
261
+ self.status = OKNFCLiteStatusNewCard;
262
+ }
263
+ [self.delegate endNFCSessionWithError:status == OKNFCLiteGetMncStatusError || status == OKNFCLiteGetMncStatusPinNotMatch || status == OKNFCLiteGetMncStatusWiped];
264
+ callback(self,mnc,status);
265
+ }
266
+
267
+ #pragma mark - changePin
268
+
269
+ - (void)changePin:(NSString *)oldPin to:(NSString *)newPin complete:(ChangePinCallback)callback {
270
+ if (![self syncLiteInfo]) {
271
+ [self.delegate endNFCSessionWithError:YES];
272
+ callback(self,OKNFCLiteChangePinStatusError);
273
+ return;
274
+ }
275
+
276
+ if (self.status == OKNFCLiteStatusNewCard) {
277
+ [self.delegate endNFCSessionWithError:YES];
278
+ callback(self,OKNFCLiteChangePinStatusError);
279
+ return;
280
+ }
281
+
282
+ if (![self openSecureChannel]) {
283
+ [self.delegate endNFCSessionWithError:YES];
284
+ callback(self,OKNFCLiteChangePinStatusError);
285
+ return;
286
+ };
287
+
288
+ OKNFCLiteChangePinResult changePinResult = [self setNewPin:newPin withOldPin:oldPin];
289
+ if (changePinResult == OKNFCLiteChangePinResultError) {
290
+ if ([self syncLiteInfo]) {
291
+ callback(self,self.pinRTL == 10 ? OKNFCLiteChangePinStatusWiped:OKNFCLiteChangePinStatusPinNotMatch);
292
+ } else {
293
+ callback(self,OKNFCLiteChangePinStatusError);
294
+ }
295
+ [self.delegate endNFCSessionWithError:YES];
296
+ return;
297
+ } else if (changePinResult == OKNFCLiteChangePinResultWiped) {
298
+ [self.delegate endNFCSessionWithError:YES];
299
+ callback(self,OKNFCLiteChangePinStatusWiped);
300
+ return;
301
+ }
302
+
303
+ [self.delegate endNFCSessionWithError:NO];
304
+ callback(self,OKNFCLiteChangePinStatusSuccess);
305
+ }
306
+
307
+
308
+ #pragma mark - OKNFCLiteAppBackup / OKNFCLiteAppSecure
309
+
310
+ -(OKNFCLiteApp)getLiteAppWithCommand:(OKLiteCommand)command {
311
+ switch (command) {
312
+ case OKLiteCommandGetCardSN:
313
+ case OKLiteCommandGetPINStatus:
314
+ case OKLiteCommandPinRTL:
315
+ case OKLiteCommandSetPIN:
316
+ case OKLiteCommandChangePIN:
317
+ case OKLiteCommandWipeCard:
318
+ return self.version == OKNFCLiteVersionV1 ? OKNFCLiteAppSecure : OKNFCLiteAppBackup;
319
+ case OKLiteCommandGetBackupStatus:
320
+ case OKLiteCommandVerifyPIN:
321
+ case OKLiteCommandImportMnemonic:
322
+ case OKLiteCommandExportMnemonic:
323
+ return OKNFCLiteAppBackup;
324
+ default:
325
+ break;
326
+ }
327
+ return OKNFCLiteAppNONE;
328
+ }
329
+
330
+ - (BOOL)selectNFCAppWith:(OKLiteCommand)command {
331
+ OKNFCLiteApp app = [self getLiteAppWithCommand:command];
332
+
333
+ if(app != OKNFCLiteAppNONE) {
334
+ __block BOOL selectSuccess = NO;
335
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
336
+ OKLiteCommand command = OKLiteCommandSelectSecure;
337
+ if (app == OKNFCLiteAppBackup) {
338
+ command = OKLiteCommandSelectBackup;
339
+ }
340
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:command version:self.version];
341
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU]
342
+ modal:modal
343
+ completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
344
+ selectSuccess = sw1 == OKNFC_SW1_OK;
345
+ dispatch_semaphore_signal(sema);
346
+ }];
347
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
348
+ if (selectSuccess) {
349
+ self.selectNFCApp = app;
350
+ } else {
351
+ self.selectNFCApp = OKNFCLiteAppNONE;
352
+ }
353
+ return selectSuccess;
354
+ }
355
+ return true;
356
+
357
+ }
358
+
359
+ - (BOOL)openSecureChannel {
360
+ __block BOOL success = NO;
361
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
362
+ OKLiteCommandModal *modal1 = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandOpenChannel_1 version:self.version];
363
+ [self.commandTool sendCommandWithAPDU:[modal1 buildAPDU]
364
+ modal:modal1
365
+ completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
366
+ OKLiteCommandModal *modal2 = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandOpenChannel_2 version:self.version];
367
+ [self.commandTool sendCommandWithAPDU:[modal2 buildAPDU]
368
+ modal:modal2
369
+ completionHandler:^(NSData * _Nonnull responseData_2, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
370
+ success = [OKNFCBridge openChannel:responseData_2];
371
+ dispatch_semaphore_signal(sema);
372
+ }];
373
+ }];
374
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
375
+ return success;
376
+ }
377
+
378
+ #pragma mark - Commands
379
+
380
+ - (NSString *)getSN {
381
+ __block NSString *SN = nil;
382
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
383
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandGetCardSN version:self.version];
384
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString *parseRespon) {
385
+ if (sw1 != OKNFC_SW1_OK) {
386
+ dispatch_semaphore_signal(sema);
387
+ return;
388
+ }
389
+ SN = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
390
+ dispatch_semaphore_signal(sema);
391
+ }];
392
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
393
+ return SN;
394
+ }
395
+
396
+ - (OKNFCLitePINVerifyResult)verifyPin:(NSString *)pin {
397
+
398
+ static const u_int8_t AuthenticationLockCode = 0x69;
399
+ static const u_int8_t FailedVerificationCode = 0x63;
400
+ static const u_int8_t PinRTLBitMask = 0x0f;
401
+
402
+ __block OKNFCLitePINVerifyResult result = OKNFCLitePINVerifyResultError;
403
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
404
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandVerifyPIN version:self.version];
405
+ modal.parseResp = true;
406
+ [self.commandTool sendCommandWithAPDU:[modal verifyPIN:pin]
407
+ modal:modal
408
+ completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString *parseRespon) {
409
+ if (error) {
410
+ result = OKNFCLitePINVerifyResultError;
411
+ dispatch_semaphore_signal(sema);
412
+ return;
413
+ }
414
+ if (sw1 != OKNFC_SW1_OK) {
415
+ if (sw1 == FailedVerificationCode) {
416
+ self.pinRTL = sw2 & PinRTLBitMask;
417
+ } else if (sw1 == AuthenticationLockCode) {
418
+ self.pinRTL = 0;
419
+ }
420
+ result = OKNFCLitePINVerifyResultNotMatch;
421
+ dispatch_semaphore_signal(sema);
422
+ return;
423
+ }
424
+ self.pinRTL = 10;
425
+ result = OKNFCLitePINVerifyResultPass;
426
+ dispatch_semaphore_signal(sema);
427
+ }];
428
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
429
+ return result;
430
+ }
431
+
432
+ //// 获取证书 & 验证证书 & 初始化安全通道
433
+ - (BOOL)verifyCert {
434
+ if (self.certVerified) {
435
+ return true;
436
+ }
437
+
438
+ if (!self.SN.length) {
439
+ NSString *liteSN = [self getSN]
440
+ ;
441
+ self.SN = liteSN;
442
+ }
443
+
444
+ __block BOOL isVerificationPass = NO;
445
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
446
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandGetCardCert version:self.version];
447
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU]
448
+ modal:modal
449
+ completionHandler:^(NSData * _Nonnull certData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
450
+ isVerificationPass = YES;
451
+
452
+ if (sw1 != OKNFC_SW1_OK) {
453
+ NSLog(@"OKNFC: 获取证书失败");
454
+ isVerificationPass = NO;
455
+ dispatch_semaphore_signal(sema);
456
+ return;
457
+ }
458
+
459
+ if (![OKNFCBridge verifySN:self.SN withCert:certData]) {
460
+ NSLog(@"OKNFC: 验证证书失败");
461
+ isVerificationPass = NO;
462
+ }
463
+
464
+ if (![OKNFCBridge JUB_GPC_Initialize:certData]) {
465
+ NSLog(@"OKNFC: 初始化安全通道失败");
466
+ isVerificationPass = NO;
467
+ }
468
+
469
+ dispatch_semaphore_signal(sema);
470
+ }];
471
+
472
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
473
+ self.certVerified = isVerificationPass;
474
+
475
+ return isVerificationPass;
476
+ }
477
+
478
+ // 获取 PIN 状态,-1: 无pin,0~10: 剩余次数
479
+ - (NSInteger)getPINStatus {
480
+
481
+ __block NSInteger pinStatus = OKNFC_PIN_ERROR;
482
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
483
+
484
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandGetPINStatus version:self.version];
485
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU]
486
+ modal:modal
487
+ completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
488
+ if (sw1 != OKNFC_SW1_OK) {
489
+ NSLog(@"OKNFC: 获取 PIN 状态失败");
490
+ dispatch_semaphore_signal(sema);
491
+ return;
492
+ }
493
+ if (responseData.toHexString.intValue == 2) {
494
+ NSLog(@"OKNFC: 未设置 PIN");
495
+ pinStatus = OKNFC_PIN_UNSET;
496
+ dispatch_semaphore_signal(sema);
497
+ return;
498
+ } else {
499
+ OKLiteCommandModal *modal2 = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandPinRTL version:self.version];
500
+ [self.commandTool sendCommandWithAPDU:[modal2 buildAPDU] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
501
+ if (sw1 == OKNFC_SW1_OK) {
502
+ unsigned int outVal;
503
+ NSScanner* scanner = [NSScanner scannerWithString:responseData.toHexString];
504
+ [scanner scanHexInt:&outVal];
505
+ pinStatus = outVal;
506
+ }
507
+ dispatch_semaphore_signal(sema);
508
+ }];
509
+ }
510
+ }];
511
+
512
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
513
+ return pinStatus;
514
+ }
515
+
516
+ - (BOOL)setMnc:(NSString *)mnc {
517
+ __block BOOL success = NO;
518
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
519
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandImportMnemonic version:self.version];
520
+ modal.parseResp = true;
521
+ [self.commandTool sendCommandWithAPDU:[modal importMnemonic:mnc] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
522
+ if (sw1 != OKNFC_SW1_OK) {
523
+ dispatch_semaphore_signal(sema);
524
+ return;
525
+ }
526
+ success = YES;
527
+ dispatch_semaphore_signal(sema);
528
+ }];
529
+
530
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
531
+ return success;
532
+ }
533
+
534
+ - (NSString *)getMnc {
535
+ __weak typeof(self) weakSelf = self;
536
+ __block NSString *mnc = nil;
537
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
538
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandExportMnemonic version:self.version];
539
+ modal.parseResp = true;
540
+ [self.commandTool sendCommandWithAPDU:[modal buildAPDU] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
541
+ if (sw1 != OKNFC_SW1_OK) {
542
+ dispatch_semaphore_signal(sema);
543
+ return;
544
+ }
545
+ if (weakSelf.version == OKNFCLiteVersionV1) {
546
+ /// https://onekeyhq.atlassian.net/wiki/spaces/ONEKEY/pages/10551684/Lite
547
+ mnc = responseData.toHexString;
548
+ } else {
549
+ mnc = parseRespon;
550
+ }
551
+ dispatch_semaphore_signal(sema);
552
+ }];
553
+
554
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
555
+ return mnc;
556
+ }
557
+
558
+ - (BOOL)setPin:(NSString *)pin {
559
+
560
+ __block BOOL success = NO;
561
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
562
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandSetPIN version:self.version];
563
+ modal.parseResp = self.version == OKNFCLiteVersionV2;
564
+
565
+ [self.commandTool sendCommandWithAPDU:[modal setPIN:pin] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
566
+ if (sw1 != OKNFC_SW1_OK) {
567
+ dispatch_semaphore_signal(sema);
568
+ return;
569
+ }
570
+ success = YES;
571
+ NSLog(@"OKNFC: 成功设置 PIN = %@", pin);
572
+ dispatch_semaphore_signal(sema);
573
+ }];
574
+
575
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
576
+ return success;
577
+ }
578
+
579
+ - (OKNFCLiteChangePinResult)setNewPin:(NSString *)newPin withOldPin:(NSString *)oldPin {
580
+ static const u_int8_t AuthenticationLockCode = 0x69;
581
+ static const u_int8_t AuthenticationLockSw2Code = 0x83;
582
+ static const u_int8_t FailedVerificationCode = 0x63;
583
+ static const u_int8_t PinRTLBitMask = 0x0f;
584
+ __block OKNFCLiteChangePinResult result = OKNFCLiteChangePinResultError;
585
+ dispatch_semaphore_t sema = dispatch_semaphore_create(0);
586
+ OKLiteCommandModal *modal = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandChangePIN version:self.version];
587
+ modal.parseResp = true;
588
+ [self.commandTool sendCommandWithAPDU:[modal changePIN:oldPin newPin:newPin] modal:modal completionHandler:^(NSData * _Nonnull responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString * _Nonnull parseRespon) {
589
+ if (sw1 != OKNFC_SW1_OK) {
590
+ if (sw1 == FailedVerificationCode) {
591
+ self.pinRTL = sw2 & PinRTLBitMask;
592
+ } else if (sw1 == AuthenticationLockCode && sw2 == AuthenticationLockSw2Code) {
593
+ self.pinRTL = 0;
594
+ result = OKNFCLiteChangePinResultWiped;
595
+ }
596
+ dispatch_semaphore_signal(sema);
597
+ return;
598
+ }
599
+ result = OKNFCLiteChangePinResultPass;
600
+ NSLog(@"OKNFC: 成功修改 PIN = %@", newPin);
601
+ dispatch_semaphore_signal(sema);
602
+ }];
603
+
604
+ dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
605
+ return result;
606
+ }
607
+
608
+ @end
609
+
@@ -0,0 +1,17 @@
1
+ //
2
+ // OKLiteV2.h
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import "OKLiteV1.h"
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ @interface OKLiteV2 : OKLiteV1
14
+
15
+ @end
16
+
17
+ NS_ASSUME_NONNULL_END