@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,39 @@
1
+ //
2
+ // OKLiteCommandModal.h
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import <CoreNFC/CoreNFC.h>
10
+ #import "OKNFCLiteDefine.h"
11
+
12
+ NS_ASSUME_NONNULL_BEGIN
13
+
14
+ typedef NFCISO7816APDU* _Nonnull (^BuildStrBlock)(void);
15
+
16
+
17
+ @interface OKLiteCommandModal : NSObject
18
+
19
+ @property (nonatomic,copy) NSString *desc;
20
+ @property(nonatomic,assign)OKLiteCommand command;
21
+
22
+ //@property (nonatomic,assign) OKNFCLiteApp app;
23
+ @property (nonatomic,assign) BOOL parseResp;
24
+
25
+ - (instancetype)initWithCommand:(OKLiteCommand)command version:(OKNFCLiteVersion)version;
26
+
27
+ - (NFCISO7816APDU *)buildAPDU;
28
+
29
+ - (NFCISO7816APDU *)setPIN:(NSString *)pin;
30
+
31
+ - (NFCISO7816APDU *)changePIN:(NSString *)oldPin newPin:(NSString *)newPin;
32
+
33
+ - (NFCISO7816APDU *)verifyPIN:(NSString *)pin;
34
+
35
+ - (NFCISO7816APDU *)importMnemonic:(NSString *)mnemonic;
36
+
37
+ @end
38
+
39
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,143 @@
1
+ //
2
+ // OKLiteCommandModal.m
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import "OKLiteCommandModal.h"
9
+ #import "OKNFCBridge.h"
10
+ #import "OKLiteCommandTool.h"
11
+ #import "NFCConfig.h"
12
+
13
+ @interface OKLiteCommandModal ()
14
+
15
+ @property(nonatomic,assign)OKNFCLiteVersion version;
16
+
17
+ @end
18
+
19
+ @implementation OKLiteCommandModal
20
+
21
+ - (instancetype)initWithCommand:(OKLiteCommand)command version:(OKNFCLiteVersion)version {
22
+ self = [super init];
23
+ if (!self) return self;
24
+ _command = command;
25
+ _version = version;
26
+ // _app = [OKLiteCommandTool getLiteAppWithCommand:command version:version];
27
+ _parseResp = NO;
28
+ [self initDesc];
29
+ return self;
30
+ }
31
+
32
+ - (void)initDesc {
33
+ switch (_command) {
34
+ case OKLiteCommandSelectSecure:
35
+ self.desc = @"设置:主安全域";
36
+ break;
37
+ case OKLiteCommandSelectBackup:
38
+ self.desc = @"设置:备份应用";
39
+ break;
40
+ case OKLiteCommandGetCardSN:
41
+ self.desc = @"获取SN";
42
+ break;
43
+ case OKLiteCommandGetCardCert:
44
+ self.desc = @"获取设备证书";
45
+ break;
46
+ case OKLiteCommandGetBackupStatus:
47
+ self.desc = @"是否存在备份";
48
+ break;
49
+ case OKLiteCommandGetPINStatus:
50
+ self.desc = @"是否设置 PIN";
51
+ break;
52
+ case OKLiteCommandExportMnemonic:
53
+ self.desc = @"导出数据";
54
+ break;
55
+ case OKLiteCommandImportMnemonic:
56
+ self.desc = @"备份数据";
57
+ break;
58
+ case OKLiteCommandPinRTL:
59
+ self.desc = @"获取密码的重试次数";
60
+ break;
61
+ case OKLiteCommandWipeCard:
62
+ self.desc = @"重置卡片";
63
+ break;
64
+ case OKLiteCommandOpenChannel_1:
65
+ self.desc = @"开启安全通道:verify_certificate";
66
+ break;
67
+ case OKLiteCommandOpenChannel_2:
68
+ self.desc = @"开启安全通道:verify_auth_data";
69
+ break;
70
+ case OKLiteCommandSetPIN:
71
+ self.desc = @"设置Pin";
72
+ break;
73
+ case OKLiteCommandChangePIN:
74
+ self.desc = @"修改 Pin";
75
+ break;
76
+ case OKLiteCommandVerifyPIN:
77
+ self.desc = @"验证 Pin";
78
+ break;
79
+ }
80
+ }
81
+
82
+ - (NFCISO7816APDU *)buildAPDU {
83
+ switch (_command) {
84
+ case OKLiteCommandSelectSecure:
85
+ return [OKNFCBridge buildAPDUWithStr:@"0x00a4040008A000000151000000" encrypt:NO];
86
+ case OKLiteCommandSelectBackup:
87
+ if (_version == OKNFCLiteVersionV2) {
88
+ return [OKNFCBridge buildAPDUWithStr:@"0x00a404000e6f6e656b65792e6261636b757001" encrypt:NO];
89
+ } else {
90
+ return [OKNFCBridge buildAPDUWithStr:@"0x00a4040008D156000132834001" encrypt:NO];
91
+ }
92
+ case OKLiteCommandGetCardSN:
93
+ return [OKNFCBridge buildAPDUWithStr:@"0x80CB800005DFFF028101" encrypt:NO];
94
+ case OKLiteCommandGetCardCert:
95
+ return [OKNFCBridge buildAPDUWithStr:@"0x80CABF2106A60483021518" encrypt:NO];
96
+ case OKLiteCommandGetBackupStatus:
97
+ return [OKNFCBridge buildAPDUWithStr:@"0x806a0000" encrypt:NO];
98
+ case OKLiteCommandGetPINStatus:
99
+ return [OKNFCBridge buildAPDUWithStr:@"0x80CB800005DFFF028105" encrypt:NO];
100
+ case OKLiteCommandExportMnemonic:
101
+ if (_version == OKNFCLiteVersionV2) {
102
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0x4b p1:0x00 p2:0x00 data:@"" encrypt:YES];
103
+ } else {
104
+ return [OKNFCBridge buildAPDUWithStr:@"0x804B0000" encrypt:NO];
105
+ }
106
+ case OKLiteCommandPinRTL:
107
+ return [OKNFCBridge buildAPDUWithStr:@"0x80cb800005dfff028102" encrypt:NO];
108
+ case OKLiteCommandWipeCard:
109
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0xcb p1:0x80 p2:0x00 data:@"dffe028205" encrypt:YES];
110
+ case OKLiteCommandOpenChannel_1:
111
+ return [OKNFCBridge buildAPDUWithStr:[@"0x802a1810XX" stringByAppendingString:[NFCConfig envFor:@"crt"]] encrypt:NO];
112
+ case OKLiteCommandOpenChannel_2:
113
+ return [OKNFCBridge buildAPDUWithStr:[@"0x80821815XX" stringByAppendingString:[OKNFCBridge mutualAuthData]] encrypt:NO];
114
+ default:
115
+ return nil;
116
+ }
117
+ }
118
+
119
+ - (NFCISO7816APDU *)setPIN:(NSString *)pin {
120
+ NSString *dataStr = [NSString stringWithFormat:@"DFFE0B8204080006%@", pin.hexString];
121
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0xcb p1:0x80 p2:0x00 data:dataStr encrypt:YES];
122
+ }
123
+
124
+ - (NFCISO7816APDU *)changePIN:(NSString *)oldPin newPin:(NSString *)newPin {
125
+ NSString *dataStr = [NSString stringWithFormat:@"DFFE0B82040e06%@06%@", oldPin.hexString, newPin.hexString];
126
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0xcb p1:0x80 p2:0x00 data:dataStr encrypt:YES];
127
+ }
128
+
129
+ - (NFCISO7816APDU *)verifyPIN:(NSString *)pin {
130
+ NSString *hexPin = [@"06" stringByAppendingString: [pin hexString]];
131
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0x20 p1:0x00 p2:0x00 data:hexPin encrypt:YES];
132
+ }
133
+
134
+ - (NFCISO7816APDU *)importMnemonic:(NSString *)mnemonic {
135
+ /// https://onekeyhq.atlassian.net/wiki/spaces/ONEKEY/pages/10551684/Lite
136
+ if (_version == OKNFCLiteVersionV2) {
137
+ return [OKNFCBridge buildAPDUWith_cla:0x80 ins:0x3B p1:0x00 p2:0x00 data:mnemonic encrypt:YES];
138
+ } else {
139
+ return [OKNFCBridge buildAPDUWithStr:[@"0x803B0000XX" stringByAppendingString:mnemonic] encrypt:NO];
140
+ }
141
+ }
142
+
143
+ @end
@@ -0,0 +1,28 @@
1
+ //
2
+ // OKLiteCommandTool.h
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import "OKNFCLiteDefine.h"
10
+ #import <CoreNFC/CoreNFC.h>
11
+ #import "OKLiteCommandModal.h"
12
+ #import "OKNFCManager.h"
13
+
14
+ NS_ASSUME_NONNULL_BEGIN
15
+
16
+ @interface OKLiteCommandTool : NSObject
17
+
18
+ @property (nonatomic, weak) id<OKNFCManagerDelegate> delegate;
19
+
20
+ - (void)sendCommandWithAPDU:(NFCISO7816APDU *)apdu
21
+ modal:(OKLiteCommandModal *)modal
22
+ completionHandler:(void(^)(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString *parseRespon))completionHandler;
23
+
24
+ //+ (OKNFCLiteApp)getLiteAppWithCommand:(OKLiteCommand)command version:(OKNFCLiteVersion)version;
25
+
26
+ @end
27
+
28
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,53 @@
1
+ //
2
+ // OKLiteCommandTool.m
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import "OKLiteCommandTool.h"
9
+ #import "OKNFCBridge.h"
10
+ #import "OKNFCUtility.h"
11
+
12
+ @implementation OKLiteCommandTool
13
+
14
+ - (void)sendCommandWithAPDU:(NFCISO7816APDU *)apdu
15
+ modal:(OKLiteCommandModal *)modal
16
+ completionHandler:(void(^)(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError * _Nullable error, NSString *parseRespon))completionHandler {
17
+
18
+ id<NFCISO7816Tag> tag = [self.delegate getNFCsessionTag];
19
+ [tag sendCommandAPDU:apdu completionHandler:^(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError *error) {
20
+ NSString *parseRespon = nil;
21
+ if(modal.parseResp && sw1 == OKNFC_SW1_OK) {
22
+ parseRespon = [OKNFCBridge parseSafeAPDUResponse:responseData sw1:sw1 sw2:sw2];
23
+ }
24
+
25
+ [OKNFCUtility logAPDU:modal.desc response:responseData sw1:sw1 sw2:sw2 error:error];
26
+ if (completionHandler) {
27
+ completionHandler(responseData,sw1,sw2,error,parseRespon);
28
+ }
29
+ }];
30
+ }
31
+
32
+ //
33
+ //+ (OKNFCLiteApp)getLiteAppWithCommand:(OKLiteCommand)command version:(OKNFCLiteVersion)version {
34
+ // switch (command) {
35
+ // case OKLiteCommandGetCardSN:
36
+ // case OKLiteCommandGetPINStatus:
37
+ // case OKLiteCommandPinRTL:
38
+ // case OKLiteCommandSetPIN:
39
+ // case OKLiteCommandChangePIN:
40
+ // case OKLiteCommandWipeCard:
41
+ // return version == OKNFCLiteVersionV1 ? OKNFCLiteAppSecure : OKNFCLiteAppBackup;
42
+ // case OKLiteCommandGetBackupStatus:
43
+ // case OKLiteCommandVerifyPIN:
44
+ // case OKLiteCommandImportMnemonic:
45
+ // case OKLiteCommandExportMnemonic:
46
+ // return OKNFCLiteAppBackup;
47
+ // default:
48
+ // return OKNFCLiteAppNONE;
49
+ // }
50
+ //}
51
+
52
+
53
+ @end
@@ -0,0 +1,28 @@
1
+ //
2
+ // Bridge.h
3
+ // NFCTagReader
4
+ //
5
+ // Created by zj on 2021/3/16.
6
+ // Copyright © 2021 Apple. All rights reserved.
7
+ //
8
+
9
+ #import <Foundation/Foundation.h>
10
+ #import <CoreNFC/CoreNFC.h>
11
+
12
+
13
+ NS_ASSUME_NONNULL_BEGIN
14
+
15
+ @interface OKNFCBridge : NSObject
16
+ + (NFCISO7816APDU *)buildAPDUWith_cla:(unsigned long)cla ins:(unsigned long)ins p1:(unsigned long)p1 p2:(unsigned long)p2 data:(NSString *)data encrypt:(BOOL)encrypt;
17
+ + (NSString *)mutualAuthData;
18
+ + (BOOL)openChannel:(NSData *)authRes;
19
+ + (BOOL)JUB_GPC_Initialize:(NSData *)cert;
20
+ + (NSString *)parseSafeAPDUResponse:(NSData *)data sw1:(uint8_t)sw1 sw2:(uint8_t)sw2;
21
+ + (BOOL)parseAPDUResponse:(NSData *)data;
22
+ + (BOOL)verifySN:(NSString *)SNData withCert:(NSData *)cert;
23
+ + (NFCISO7816APDU *)buildAPDUWithStr:(NSString *)str encrypt:(BOOL)encrypt;
24
+ + (void)closeSecureChannel;
25
+
26
+ @end
27
+
28
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,221 @@
1
+ //
2
+ // Bridge.m
3
+ // NFCTagReader
4
+ //
5
+ // Created by zj on 2021/3/16.
6
+ // Copyright © 2021 Apple. All rights reserved.
7
+ //
8
+
9
+ #if !TARGET_OS_SIMULATOR
10
+
11
+ #import <GPChannelSDKCore/GPChannelSDKCore.h>
12
+ #import "OKNFCLiteDefine.h"
13
+ #import "NFCConfig.h"
14
+
15
+ #endif
16
+
17
+
18
+
19
+ #import "OKNFCBridge.h"
20
+
21
+ @implementation OKNFCBridge
22
+
23
+ #if !TARGET_OS_SIMULATOR
24
+
25
+ + (NFCISO7816APDU *)buildAPDUWith_cla:(unsigned long)cla
26
+ ins:(unsigned long)ins
27
+ p1:(unsigned long)p1
28
+ p2:(unsigned long)p2
29
+ data:(NSString *)data
30
+ encrypt:(BOOL)encrypt {
31
+ JUB_RV rv = JUBR_ERROR;
32
+ JUB_CHAR_CPTR c_data = [data UTF8String];
33
+
34
+ JUB_CHAR_PTR c_apdu = NULL;
35
+
36
+ if (encrypt) {
37
+ rv = JUB_GPC_BuildSafeAPDU(cla, ins, p1, p2, c_data, &c_apdu);
38
+ } else {
39
+ rv = JUB_GPC_BuildAPDU(cla, ins, p1, p2, c_data, &c_apdu);
40
+ }
41
+
42
+ if (JUBR_OK != rv) {
43
+ return nil;
44
+ }
45
+
46
+ NSString *hexStr = [NSString stringWithCString:c_apdu encoding:NSUTF8StringEncoding];
47
+ NSData *APDUData = [hexStr dataFromHexString];
48
+ NFCISO7816APDU *apdu = [[NFCISO7816APDU alloc] initWithData:APDUData];
49
+ return apdu;
50
+ }
51
+
52
+ + (NSString *)parseSafeAPDUResponse:(NSData *)data sw1:(uint8_t)sw1 sw2:(uint8_t)sw2 {
53
+
54
+ JUB_RV rv = JUBR_ERROR;
55
+
56
+ NSMutableData *mutableRaw = [[NSMutableData alloc] initWithData:data];
57
+ [mutableRaw appendBytes:&sw1 length:sizeof(sw1)];
58
+ [mutableRaw appendBytes:&sw2 length:sizeof(sw2)];
59
+
60
+ JUB_CHAR_CPTR c_data = [mutableRaw.toHexString UTF8String];
61
+
62
+ JUB_UINT16 wRet = 0;
63
+ JUB_CHAR_PTR pDecResp = nullptr;
64
+ rv = JUB_GPC_ParseSafeAPDUResponse(c_data ,&wRet, &pDecResp);
65
+ if (JUBR_OK != rv) {
66
+ return nil;
67
+ }
68
+
69
+ NSString *resp = [NSString stringWithCString:pDecResp encoding:NSUTF8StringEncoding];
70
+ JUB_FreeMemory(pDecResp);
71
+
72
+ return resp;
73
+ }
74
+
75
+ + (BOOL)parseAPDUResponse:(NSData *)data {
76
+
77
+ JUB_RV rv = JUBR_ERROR;
78
+ JUB_ULONG tag = 0;
79
+ JUB_CHAR_PTR value;
80
+ JUB_CHAR_CPTR c_data = [[data toHexString] UTF8String];
81
+
82
+ rv = JUB_GPC_TLVDecode(c_data, &tag, &value);
83
+ if (JUBR_OK != rv) {
84
+ return NO;
85
+ }
86
+
87
+ JUB_UINT16 wRet = 0;
88
+ JUB_CHAR_PTR pDecResp = nullptr;
89
+ rv = JUB_GPC_ParseAPDUResponse(c_data,&wRet, &pDecResp);
90
+ if (JUBR_OK != rv) {
91
+ return NO;
92
+ }
93
+
94
+ JUB_FreeMemory(pDecResp);
95
+
96
+ return YES;
97
+ }
98
+
99
+ + (NFCISO7816APDU *)buildAPDUWithStr:(NSString *)str encrypt:(BOOL)encrypt {
100
+ if (![str hasPrefix:@"0x"]) {
101
+ NSAssert(0, @"must starts with 0x");
102
+ return nil;
103
+ }
104
+
105
+ // drop str[10:12], since coreNFC will cal it for us.
106
+ NSString *meta = [str substringWithRange:NSMakeRange(0, 10)];
107
+ NSString *data = @"";
108
+ if (str.length > 12) {
109
+ data = [str substringFromIndex:12];
110
+ }
111
+
112
+ unsigned long long outVal;
113
+ NSScanner* scanner = [NSScanner scannerWithString:meta];
114
+ [scanner scanHexLongLong:&outVal];
115
+
116
+ unsigned long cla = outVal >> 24 & 0xff;
117
+ unsigned long ins = outVal >> 16 & 0xff;
118
+ unsigned long p1 = outVal >> 8 & 0xff;
119
+ unsigned long p2 = outVal & 0xff;
120
+
121
+ return [OKNFCBridge buildAPDUWith_cla:cla ins:ins p1:p1 p2:p2 data:data encrypt:encrypt];
122
+ }
123
+
124
+
125
+ + (NSString *)mutualAuthData {
126
+ JUB_CHAR_PTR mutualAuthData;
127
+ JUB_RV rv = JUB_GPC_BuildMutualAuthData(&mutualAuthData);
128
+
129
+ if (JUBR_OK != rv) {
130
+ return nil;
131
+ }
132
+
133
+ return [NSString stringWithFormat:@"%s", mutualAuthData];
134
+ }
135
+
136
+ + (BOOL)openChannel:(NSData *)authRes {
137
+ JUB_CHAR_CPTR authResp = [[authRes toHexString] UTF8String];
138
+ JUB_RV rv;
139
+
140
+ rv = JUB_GPC_OpenSecureChannel(authResp);
141
+ if (JUBR_OK != rv) {
142
+ NSLog(@"error: JUB_GPC_OpenSecureChannel");
143
+ return NO;
144
+ }
145
+ NSLog(@"OKNFC: openChannel success.");
146
+ return YES;
147
+ }
148
+
149
+ + (BOOL)JUB_GPC_Initialize:(NSData *)cert {
150
+ JUB_RV rv = JUBR_ERROR;
151
+ JUB_ULONG tag = 0;
152
+ JUB_CHAR_PTR value;
153
+ JUB_CHAR_CPTR c_data = [[cert toHexString] UTF8String];
154
+
155
+ rv = JUB_GPC_TLVDecode(c_data, &tag, &value);
156
+ if (JUBR_OK != rv) {
157
+ return NO;
158
+ }
159
+
160
+ JUB_CHAR_PTR sn = nullptr;
161
+ JUB_CHAR_PTR subjectID = nullptr;
162
+ rv = JUB_GPC_ParseCertificate(value, &sn, &subjectID);
163
+ if (JUBR_OK != rv) {
164
+ return NO;
165
+ }
166
+
167
+ NSLog(@"OKNFC: subjectID: %s",subjectID);
168
+ GPC_SCP11_SHAREDINFO shareInfo;
169
+ shareInfo.scpID = (char *)"1107";
170
+ shareInfo.keyUsage = (char *)"3C";
171
+ shareInfo.keyType = (char *)"88";
172
+ shareInfo.keyLength = (char *)"10";
173
+ shareInfo.hostID = (char *)"8080808080808080";
174
+ shareInfo.cardGroupID = subjectID;
175
+
176
+ // initParams.json -> sk
177
+ char *sk = (char *)[[NFCConfig envFor:@"sk"] UTF8String];
178
+
179
+ rv = JUB_GPC_Initialize(shareInfo, [NFCConfig envFor:@"crt"].UTF8String, sk);
180
+
181
+ if (JUBR_OK != rv) {
182
+ return NO;
183
+ }
184
+
185
+ NSLog(@"OKNFC: JUB_GPC_Initialize OK.");
186
+ return YES;
187
+ }
188
+
189
+ + (void)closeSecureChannel {
190
+ JUB_GPC_Finalize();
191
+ }
192
+
193
+ + (BOOL)verifySN:(NSString *)cardSN withCert:(NSData *)cert {
194
+ JUB_RV rv = JUBR_ERROR;
195
+ JUB_ULONG tag = 0;
196
+ JUB_CHAR_PTR value;
197
+ JUB_CHAR_CPTR c_data = [[cert toHexString] UTF8String];
198
+
199
+ rv = JUB_GPC_TLVDecode(c_data, &tag, &value);
200
+ if (JUBR_OK != rv) {
201
+ return NO;
202
+ }
203
+
204
+ JUB_CHAR_PTR sn = nullptr;
205
+ JUB_CHAR_PTR subjectID = nullptr;
206
+ rv = JUB_GPC_ParseCertificate(value, &sn, &subjectID);
207
+ if (JUBR_OK != rv) {
208
+ return NO;
209
+ }
210
+
211
+ NSString *certSN = [NSString stringWithCString:sn encoding:NSUTF8StringEncoding];
212
+
213
+ BOOL identical = [certSN isEqualToString:cardSN];
214
+
215
+ NSLog(@"OKNFC: certSN: %@; cardSN: %@; identical: %@",certSN, cardSN, identical ? @"YES✅" : @"NO❌");
216
+ return identical;
217
+ }
218
+
219
+ #endif
220
+
221
+ @end
@@ -0,0 +1,108 @@
1
+ //
2
+ // OKNFCLiteDefine.m
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import "NSString+OKNFCHexStr.h"
10
+ #import "NSData+OKNFCHexData.h"
11
+
12
+ typedef NS_ENUM(NSInteger, OKLiteCommand) {
13
+ OKLiteCommandSelectSecure = 0,
14
+ OKLiteCommandSelectBackup,
15
+ OKLiteCommandGetCardSN,
16
+ OKLiteCommandGetCardCert,
17
+ OKLiteCommandGetBackupStatus,
18
+ OKLiteCommandGetPINStatus,
19
+ OKLiteCommandSetPIN,
20
+ OKLiteCommandChangePIN,
21
+ OKLiteCommandVerifyPIN,
22
+ OKLiteCommandImportMnemonic,
23
+ OKLiteCommandExportMnemonic,
24
+ OKLiteCommandWipeCard,
25
+ OKLiteCommandPinRTL,
26
+ OKLiteCommandOpenChannel_1,
27
+ OKLiteCommandOpenChannel_2,
28
+ };
29
+
30
+ typedef NS_ENUM(NSInteger, OKNFCLiteSessionType) {
31
+ OKNFCLiteSessionTypeNone = 0,
32
+ OKNFCLiteSessionTypeGetInfo,
33
+ OKNFCLiteSessionTypeSetMnemonic,
34
+ OKNFCLiteSessionTypeSetMnemonicForce,
35
+ OKNFCLiteSessionTypeGetMnemonic,
36
+ OKNFCLiteSessionTypeReset,
37
+ OKNFCLiteSessionTypeUpdateInfo,
38
+ OKNFCLiteSessionTypeChangePin,
39
+ };
40
+
41
+ typedef NS_ENUM(NSInteger, OKNFCLiteStatus) {
42
+ OKNFCLiteStatusError = 1,
43
+ OKNFCLiteStatusNewCard = 2,
44
+ OKNFCLiteStatusActivated = 3,
45
+ OKNFCLiteStatusSNNotMatch,
46
+ };
47
+
48
+ typedef NS_ENUM(NSInteger, OKNFCLiteSetMncStatus) {
49
+ OKNFCLiteSetMncStatusError = -1,
50
+ OKNFCLiteSetMncStatusSuccess = 0,
51
+ OKNFCLiteSetMncStatusSNNotMatch,
52
+ OKNFCLiteSetMncStatusPinNotMatch,
53
+ OKNFCLiteSetMncStatusWiped,
54
+ OKNFCLiteSetMncStatusCancel
55
+ };
56
+
57
+ typedef NS_ENUM(NSInteger, OKNFCLiteGetMncStatus) {
58
+ OKNFCLiteGetMncStatusError = -1,
59
+ OKNFCLiteGetMncStatusSuccess = 0,
60
+ OKNFCLiteGetMncStatusSNNotMatch,
61
+ OKNFCLiteGetMncStatusPinNotMatch,
62
+ OKNFCLiteGetMncStatusWiped,
63
+ OKNFCLiteGetMncStatusCancel,
64
+ };
65
+
66
+ typedef NS_ENUM(NSInteger, OKNFCLiteChangePinStatus) {
67
+ OKNFCLiteChangePinStatusError = -1,
68
+ OKNFCLiteChangePinStatusSuccess = 0,
69
+ OKNFCLiteChangePinStatusSNNotMatch,
70
+ OKNFCLiteChangePinStatusPinNotMatch,
71
+ OKNFCLiteChangePinStatusWiped,
72
+ OKNFCLiteChangePinStatusCancel,
73
+ };
74
+
75
+ @class OKLiteV1;
76
+ typedef void (^GetLiteInfoCallback)(OKLiteV1 *lite, OKNFCLiteStatus status);
77
+ typedef void (^ResetCallback)(OKLiteV1 *lite, BOOL isSuccess,NSError *error);
78
+ typedef void (^SetMnemonicCallback)(OKLiteV1 *lite, OKNFCLiteSetMncStatus status);
79
+ typedef void (^GetMnemonicCallback)(OKLiteV1 *lite, NSString*mnemonic, OKNFCLiteGetMncStatus status);
80
+ typedef void (^ChangePinCallback)(OKLiteV1 *lite, OKNFCLiteChangePinStatus status);
81
+
82
+ static const NSInteger OKNFC_PIN_LENGTH = 6;
83
+ static const NSInteger OKNFC_PIN_UNSET = -1;
84
+ static const NSInteger OKNFC_PIN_ERROR = -2;
85
+
86
+ typedef NS_ENUM(NSInteger, OKNFCLiteApp) {
87
+ OKNFCLiteAppNONE = -1,
88
+ OKNFCLiteAppSecure = 0, // 主安全域
89
+ OKNFCLiteAppBackup = 1, // 备份
90
+ };
91
+
92
+ typedef NS_ENUM(NSInteger, OKNFCLitePINVerifyResult) {
93
+ OKNFCLitePINVerifyResultError = -1,
94
+ OKNFCLitePINVerifyResultPass = 0,
95
+ OKNFCLitePINVerifyResultNotMatch,
96
+ };
97
+
98
+ typedef NS_ENUM(NSInteger, OKNFCLiteChangePinResult) {
99
+ OKNFCLiteChangePinResultError = -1,
100
+ OKNFCLiteChangePinResultPass = 0,
101
+ OKNFCLiteChangePinResultWiped,
102
+ };
103
+
104
+ typedef NS_ENUM(NSInteger, OKNFCLiteVersion) {
105
+ OKNFCLiteVersionV1 = 0,
106
+ OKNFCLiteVersionV2 = 1,
107
+ };
108
+
@@ -0,0 +1,37 @@
1
+ //
2
+ // OKLiteProtocol.h
3
+ // OneKeyWallet
4
+ //
5
+ // Created by linleiqin on 2023/6/27.
6
+ //
7
+
8
+ #import <Foundation/Foundation.h>
9
+ #import "OKNFCLiteDefine.h"
10
+ #import <CoreNFC/CoreNFC.h>
11
+
12
+ NS_ASSUME_NONNULL_BEGIN
13
+
14
+ @protocol OKLiteProtocol <NSObject>
15
+
16
+ - (void)getLiteInfo:(GetLiteInfoCallback)callBack;
17
+
18
+ - (BOOL)syncLiteInfo;
19
+
20
+ - (void)setMnemonic:(NSString *)mnemonic
21
+ withPin:(NSString *)pin
22
+ overwrite:(BOOL)overwrite
23
+ complete:(SetMnemonicCallback)complete;
24
+
25
+ - (void)reset:(ResetCallback)callBack;
26
+
27
+ - (BOOL)resetSync;
28
+
29
+ - (void)getMnemonicWithPin:(NSString *)pin complete:(GetMnemonicCallback)complete;
30
+
31
+ - (void)changePin:(NSString *)oldPin to:(NSString *)newPin complete:(ChangePinCallback)complete;
32
+
33
+ - (OKNFCLitePINVerifyResult)verifyPin:(NSString *)pin;
34
+
35
+ @end
36
+
37
+ NS_ASSUME_NONNULL_END