@onekeyfe/react-native-lite-card 1.0.0
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.
- package/LICENSE +20 -0
- package/android/build.gradle +102 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/assets/config/command.json +205 -0
- package/android/src/main/cpp/CMakeLists.txt +15 -0
- package/android/src/main/cpp/validation.c +227 -0
- package/android/src/main/cpp/validation.h +19 -0
- package/android/src/main/java/so/onekey/app/wallet/LoggerManager.kt +35 -0
- package/android/src/main/java/so/onekey/app/wallet/OKLiteManager.kt +399 -0
- package/android/src/main/java/so/onekey/app/wallet/OKLitePackage.kt +16 -0
- package/android/src/main/java/so/onekey/app/wallet/keys/KeysNativeProvider.kt +13 -0
- package/android/src/main/java/so/onekey/app/wallet/nfc/Exceptions.kt +58 -0
- package/android/src/main/java/so/onekey/app/wallet/nfc/NfcUtils.kt +151 -0
- package/android/src/main/java/so/onekey/app/wallet/nfc/broadcast/NfcStatusChangeBroadcastReceiver.kt +56 -0
- package/android/src/main/java/so/onekey/app/wallet/nfc/gpchannel/GPChannelNatives.kt +32 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/NfcConstant.kt +38 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/OnekeyLiteCard.kt +241 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/APDUParam.kt +17 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/CardInfo.java +84 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/CardResponse.java +37 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/CardState.kt +10 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/ParsedCertInfo.java +39 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/SecureChanelParam.java +113 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/entitys/SendResponse.kt +26 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/nfc/CommandGenerator.kt +178 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/nfc/Connection.kt +430 -0
- package/android/src/main/java/so/onekey/app/wallet/onekeyLite/nfc/GPCAPDUGenerator.kt +56 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/EventUtils.kt +14 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/GpsUtil.kt +38 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/HexUtils.java +93 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/LogUtil.kt +11 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/MiUtil.kt +93 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/NfcPermissionUtils.kt +24 -0
- package/android/src/main/java/so/onekey/app/wallet/utils/Utils.java +433 -0
- package/android/src/main/jniLibs/arm64-v8a/libgpchannelNDK.so +0 -0
- package/android/src/main/jniLibs/armeabi-v7a/libgpchannelNDK.so +0 -0
- package/ios/Classes/OKLiteCommand/OKLiteCommandModal.h +39 -0
- package/ios/Classes/OKLiteCommand/OKLiteCommandModal.m +143 -0
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.h +28 -0
- package/ios/Classes/OKLiteCommand/OKLiteCommandTool.m +53 -0
- package/ios/Classes/OKLiteManager.h +17 -0
- package/ios/Classes/OKLiteManager.m +222 -0
- package/ios/Classes/OKNFCBridge.h +28 -0
- package/ios/Classes/OKNFCBridge.mm +221 -0
- package/ios/Classes/OKNFCLiteDefine.h +108 -0
- package/ios/Classes/OKNFTLite/OKLiteProtocol.h +37 -0
- package/ios/Classes/OKNFTLite/OKLiteV1.h +41 -0
- package/ios/Classes/OKNFTLite/OKLiteV1.m +602 -0
- package/ios/Classes/OKNFTLite/OKLiteV2.h +17 -0
- package/ios/Classes/OKNFTLite/OKLiteV2.m +132 -0
- package/ios/Classes/OKNFTLite/OKNFCManager.h +40 -0
- package/ios/Classes/OKNFTLite/OKNFCManager.m +330 -0
- package/ios/Classes/Utils/NFCConfig.h +19 -0
- package/ios/Classes/Utils/NFCConfig.m +18 -0
- package/ios/Classes/Utils/NSData+OKNFCHexData.h +17 -0
- package/ios/Classes/Utils/NSData+OKNFCHexData.m +24 -0
- package/ios/Classes/Utils/NSData+StringToData.h +12 -0
- package/ios/Classes/Utils/NSData+StringToData.m +25 -0
- package/ios/Classes/Utils/NSString+OKAdd.h +14 -0
- package/ios/Classes/Utils/NSString+OKAdd.m +40 -0
- package/ios/Classes/Utils/NSString+OKNFCHexStr.h +18 -0
- package/ios/Classes/Utils/NSString+OKNFCHexStr.m +38 -0
- package/ios/Classes/Utils/OKNFCUtility.h +18 -0
- package/ios/Classes/Utils/OKNFCUtility.m +22 -0
- package/ios/Classes/Utils/OKTools.h +17 -0
- package/ios/Classes/Utils/OKTools.m +21 -0
- package/ios/GPChannelSDKCore.framework/GPChannelSDKCore +0 -0
- package/ios/GPChannelSDKCore.framework/Headers/GPChannelSDK.h +315 -0
- package/ios/GPChannelSDKCore.framework/Headers/GPChannelSDKCore.h +19 -0
- package/ios/GPChannelSDKCore.framework/Info.plist +0 -0
- package/ios/GPChannelSDKCore.framework/Modules/module.modulemap +7 -0
- package/ios/GPChannelSDKCore.framework/_CodeSignature/CodeResources +147 -0
- package/keys/keys.c +51 -0
- package/keys/keys.h +4 -0
- package/lib/commonjs/index.js +114 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/types.js +27 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/index.js +108 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/types.js +21 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/src/index.d.ts +35 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/types.d.ts +31 -0
- package/lib/typescript/src/types.d.ts.map +1 -0
- package/package.json +166 -0
- package/react-native-lite-card.podspec +42 -0
- package/src/index.ts +154 -0
- package/src/types.ts +37 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//
|
|
2
|
+
// OKLiteV2.m
|
|
3
|
+
// OneKeyWallet
|
|
4
|
+
//
|
|
5
|
+
// Created by linleiqin on 2023/6/27.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import "OKLiteV2.h"
|
|
9
|
+
#import "OKLiteCommandTool.h"
|
|
10
|
+
#import "OKNFCUtility.h"
|
|
11
|
+
|
|
12
|
+
@interface OKLiteV2 ()
|
|
13
|
+
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
@implementation OKLiteV2
|
|
17
|
+
|
|
18
|
+
- (instancetype)initWithDelegate:(id<OKNFCManagerDelegate>)delegate {
|
|
19
|
+
self = [super initWithDelegate:delegate];
|
|
20
|
+
if (!self) return self;
|
|
21
|
+
self.version = OKNFCLiteVersionV2;
|
|
22
|
+
return self;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#pragma mark - setMnemonic
|
|
26
|
+
|
|
27
|
+
- (void)setMnemonic:(NSString *)mnemonic
|
|
28
|
+
withPin:(NSString *)pin
|
|
29
|
+
complete:(SetMnemonicCallback)callBack {
|
|
30
|
+
|
|
31
|
+
OKNFCLiteSetMncStatus status = OKNFCLiteSetMncStatusError;
|
|
32
|
+
BOOL selectNFCAppSuccess = [self selectNFCAppWith:OKLiteCommandImportMnemonic];
|
|
33
|
+
[self openSecureChannel];
|
|
34
|
+
|
|
35
|
+
if (self.status == OKNFCLiteStatusActivated) {
|
|
36
|
+
[self.delegate endNFCSessionWithError:YES];
|
|
37
|
+
callBack(self,status);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (self.status == OKNFCLiteStatusNewCard) {
|
|
42
|
+
[self setPin:pin];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if (selectNFCAppSuccess) {
|
|
47
|
+
OKNFCLitePINVerifyResult verifyResult = [self verifyPin:pin];
|
|
48
|
+
if (verifyResult == OKNFCLitePINVerifyResultPass) {
|
|
49
|
+
BOOL setMncSuccess = [self setMnc:mnemonic];
|
|
50
|
+
status = setMncSuccess ? OKNFCLiteSetMncStatusSuccess : OKNFCLiteSetMncStatusError;
|
|
51
|
+
} else if (verifyResult == OKNFCLitePINVerifyResultNotMatch) {
|
|
52
|
+
status = OKNFCLiteSetMncStatusPinNotMatch;
|
|
53
|
+
if (self.pinRTL <= 0) {
|
|
54
|
+
BOOL restSeccuss = [self resetSync];
|
|
55
|
+
status = restSeccuss ? OKNFCLiteSetMncStatusWiped : OKNFCLiteSetMncStatusError;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (status == OKNFCLiteSetMncStatusSuccess) {
|
|
60
|
+
self.status = OKNFCLiteStatusActivated;
|
|
61
|
+
} else if (status == OKNFCLiteSetMncStatusWiped) {
|
|
62
|
+
self.status = OKNFCLiteStatusNewCard;
|
|
63
|
+
}
|
|
64
|
+
[self.delegate endNFCSessionWithError:status != OKNFCLiteSetMncStatusSuccess];
|
|
65
|
+
callBack(self,status);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
- (void)setMnemonicOverWrite:(NSString *)mnemonic
|
|
70
|
+
withPin:(NSString *)pin
|
|
71
|
+
complete:(SetMnemonicCallback)callBack {
|
|
72
|
+
OKNFCLiteSetMncStatus status = OKNFCLiteSetMncStatusError;
|
|
73
|
+
|
|
74
|
+
BOOL selectNFCAppSuccess = [self selectNFCAppWith:OKLiteCommandImportMnemonic];
|
|
75
|
+
BOOL openSecureChannelSuccess = [self openSecureChannel];
|
|
76
|
+
if (selectNFCAppSuccess && openSecureChannelSuccess) {
|
|
77
|
+
if (self.status == OKNFCLiteStatusNewCard) {
|
|
78
|
+
[self setPin:pin];
|
|
79
|
+
} else {
|
|
80
|
+
OKNFCLitePINVerifyResult oldPinVerifyResult = [self verifyPin:pin];
|
|
81
|
+
if (oldPinVerifyResult == OKNFCLitePINVerifyResultPass) {
|
|
82
|
+
[self setPin:pin];
|
|
83
|
+
} else if (oldPinVerifyResult == OKNFCLitePINVerifyResultNotMatch) {
|
|
84
|
+
status = OKNFCLiteSetMncStatusPinNotMatch;
|
|
85
|
+
if (self.pinRTL <= 0) {
|
|
86
|
+
BOOL restSeccuss = [self resetSync];
|
|
87
|
+
status = restSeccuss ? OKNFCLiteSetMncStatusWiped : OKNFCLiteSetMncStatusError;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
OKNFCLitePINVerifyResult verifyResult = [self verifyPin:pin];
|
|
93
|
+
if (verifyResult == OKNFCLitePINVerifyResultPass) {
|
|
94
|
+
BOOL setMncSuccess = [self setMnc:mnemonic];
|
|
95
|
+
status = setMncSuccess ? OKNFCLiteSetMncStatusSuccess : OKNFCLiteSetMncStatusError;
|
|
96
|
+
} else if (verifyResult == OKNFCLitePINVerifyResultNotMatch) {
|
|
97
|
+
status = OKNFCLiteSetMncStatusPinNotMatch;
|
|
98
|
+
if (self.pinRTL <= 0) {
|
|
99
|
+
BOOL restSeccuss = [self resetSync];
|
|
100
|
+
status = restSeccuss ? OKNFCLiteSetMncStatusWiped : OKNFCLiteSetMncStatusError;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (status == OKNFCLiteSetMncStatusSuccess) {
|
|
105
|
+
self.status = OKNFCLiteStatusActivated;
|
|
106
|
+
} else if (status == OKNFCLiteSetMncStatusWiped) {
|
|
107
|
+
self.status = OKNFCLiteStatusNewCard;
|
|
108
|
+
}
|
|
109
|
+
[self.delegate endNFCSessionWithError:status != OKNFCLiteSetMncStatusSuccess];
|
|
110
|
+
callBack(self,status);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
- (void)setMnemonic:(NSString *)mnemonic
|
|
115
|
+
withPin:(NSString *)pin
|
|
116
|
+
overwrite:(BOOL)overwrite
|
|
117
|
+
complete:(SetMnemonicCallback)callBack {
|
|
118
|
+
|
|
119
|
+
if (![self syncLiteInfo]) {
|
|
120
|
+
[self.delegate endNFCSessionWithError:YES];
|
|
121
|
+
callBack(self,OKNFCLiteSetMncStatusError);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!overwrite) {
|
|
126
|
+
[self setMnemonic:mnemonic withPin:pin complete:callBack];
|
|
127
|
+
} else {
|
|
128
|
+
[self setMnemonicOverWrite:mnemonic withPin:pin complete:callBack];
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// OKNFCManager.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
|
+
#define OKLITEFOLDERNAME @"OK_DEVICES_INFO_LITE"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@protocol OKNFCManagerDelegate <NSObject>
|
|
16
|
+
|
|
17
|
+
- (id<NFCISO7816Tag>)getNFCsessionTag;
|
|
18
|
+
|
|
19
|
+
- (OKNFCLiteSessionType)getSessionType;
|
|
20
|
+
|
|
21
|
+
- (void)endNFCSessionWithError:(BOOL)isError;
|
|
22
|
+
|
|
23
|
+
@end
|
|
24
|
+
|
|
25
|
+
@interface OKNFCManager : NSObject
|
|
26
|
+
|
|
27
|
+
@property (nonatomic, assign) OKNFCLiteSessionType sessionType;
|
|
28
|
+
|
|
29
|
+
- (void)getLiteInfo:(GetLiteInfoCallback)callBack;
|
|
30
|
+
|
|
31
|
+
- (void)reset:(ResetCallback)callBack;
|
|
32
|
+
|
|
33
|
+
- (void)setMnemonic:(NSString *)mnemonic withPin:(NSString *)pin overwrite:(BOOL)overwrite complete:(SetMnemonicCallback)complete;
|
|
34
|
+
|
|
35
|
+
- (void)getMnemonicWithPin:(NSString *)pin complete:(GetMnemonicCallback)complete;
|
|
36
|
+
|
|
37
|
+
- (void)changePin:(NSString *)oldPin to:(NSString *)newPin complete:(ChangePinCallback)complete;
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@end
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
//
|
|
2
|
+
// OKNFCManager.m
|
|
3
|
+
// OneKeyWallet
|
|
4
|
+
//
|
|
5
|
+
// Created by linleiqin on 2023/6/27.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import "OKNFCManager.h"
|
|
9
|
+
#import "OKNFCBridge.h"
|
|
10
|
+
#import "OKNFCUtility.h"
|
|
11
|
+
#import <CoreNFC/CoreNFC.h>
|
|
12
|
+
//#import "OKNFCHintViewController.h"
|
|
13
|
+
//#import "OKMnemonic.h"
|
|
14
|
+
#import "NSString+OKAdd.h"
|
|
15
|
+
#import "OKTools.h"
|
|
16
|
+
#import "OKLiteV1.h"
|
|
17
|
+
#import "OKLiteV2.h"
|
|
18
|
+
#import "OKLiteProtocol.h"
|
|
19
|
+
#import "OKLiteCommandModal.h"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
#define kGetLiteInfoBlock @"kGetLiteInfoBlock"
|
|
24
|
+
#define kSetMnemonicBlock @"kSetMnemonicBlock"
|
|
25
|
+
#define kGetMnemonicBlock @"kGetMnemonicBlock"
|
|
26
|
+
#define kChangePinBlock @"kChangePinBlock"
|
|
27
|
+
#define kResetBlock @"kResetBlock"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@interface OKNFCManager() <NFCTagReaderSessionDelegate,OKNFCManagerDelegate>
|
|
33
|
+
@property (nonatomic, strong) NFCTagReaderSession *session;
|
|
34
|
+
@property (nonatomic, copy) NSString *pin;
|
|
35
|
+
@property (nonatomic, copy) NSString *neoPin;
|
|
36
|
+
@property (nonatomic, copy) NSString *exportMnemonic;
|
|
37
|
+
@property (nonatomic, assign) BOOL certVerified;
|
|
38
|
+
@property (nonatomic, assign) OKNFCLiteApp selectNFCApp;
|
|
39
|
+
@property (nonatomic, strong) OKLiteV1 *lite;
|
|
40
|
+
|
|
41
|
+
@property (nonatomic, strong) NSMutableDictionary *completionBlocks;
|
|
42
|
+
|
|
43
|
+
@end
|
|
44
|
+
|
|
45
|
+
@implementation OKNFCManager
|
|
46
|
+
|
|
47
|
+
#pragma mark - OKNFCManagerDelegate
|
|
48
|
+
|
|
49
|
+
- (id<NFCISO7816Tag>)getNFCsessionTag {
|
|
50
|
+
id<NFCISO7816Tag> tag = [self.session.connectedTag asNFCISO7816Tag];
|
|
51
|
+
return tag;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
- (OKNFCLiteSessionType)getSessionType {
|
|
55
|
+
return self.sessionType;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
- (void)endNFCSessionWithError:(BOOL)isError {
|
|
60
|
+
self.session.alertMessage = @"";
|
|
61
|
+
if (isError) {
|
|
62
|
+
[self.session invalidateSessionWithErrorMessage:OKTools.isChineseLan ? @"读取失败,请重试":@"Connect fail, please try again."];
|
|
63
|
+
} else {
|
|
64
|
+
[self.session invalidateSession];
|
|
65
|
+
}
|
|
66
|
+
self.session = nil;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
-(NSMutableDictionary *)completionBlocks {
|
|
71
|
+
if (!_completionBlocks) {
|
|
72
|
+
_completionBlocks = [NSMutableDictionary dictionary];
|
|
73
|
+
}
|
|
74
|
+
return _completionBlocks;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
- (void)beginNewNFCSession {
|
|
78
|
+
self.session = [[NFCTagReaderSession alloc] initWithPollingOption:NFCPollingISO14443 delegate:self queue:dispatch_get_global_queue(2, 0)];
|
|
79
|
+
[self.session beginSession];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#pragma mark - NFCTagReaderSessionDelegate
|
|
83
|
+
|
|
84
|
+
- (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<__kindof id<NFCTag>> *)tags {
|
|
85
|
+
NSLog(@"OKNFC tagReaderSession didDetectTags %@", tags);
|
|
86
|
+
|
|
87
|
+
id<NFCISO7816Tag> tag = [tags.firstObject asNFCISO7816Tag];
|
|
88
|
+
if (!tag) { return; }
|
|
89
|
+
|
|
90
|
+
[session connectToTag:tag completionHandler:^(NSError * _Nullable error) {
|
|
91
|
+
if (error) {
|
|
92
|
+
NSString *errMsg = [NSString stringWithFormat:@"OKNFC connectToTag %@", error];
|
|
93
|
+
NSLog(@"%@", errMsg);
|
|
94
|
+
// [kTools debugTipMessage:errMsg];
|
|
95
|
+
[self endNFCSessionWithError:YES];
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
[self nfcSessionComplete:session];
|
|
99
|
+
}];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
- (void)tagReaderSession:(NFCTagReaderSession *)session didInvalidateWithError:(NSError *)error {
|
|
103
|
+
NSLog(@"OKNFC tagReaderSession didInvalidateWithError %@", error);
|
|
104
|
+
if (error.code == 200 || error.code == 6) {
|
|
105
|
+
switch (self.sessionType) {
|
|
106
|
+
case OKNFCLiteSessionTypeGetInfo:
|
|
107
|
+
case OKNFCLiteSessionTypeUpdateInfo:{
|
|
108
|
+
GetLiteInfoCallback callback = [_completionBlocks objectForKey:kGetLiteInfoBlock];
|
|
109
|
+
if(callback) {
|
|
110
|
+
callback(nil,OKNFCLiteStatusError);
|
|
111
|
+
}
|
|
112
|
+
} break;
|
|
113
|
+
case OKNFCLiteSessionTypeReset: {
|
|
114
|
+
ResetCallback callback = [_completionBlocks objectForKey:kResetBlock];
|
|
115
|
+
if (callback) {
|
|
116
|
+
callback(self.lite, NO,error);
|
|
117
|
+
}
|
|
118
|
+
} break;
|
|
119
|
+
case OKNFCLiteSessionTypeSetMnemonic:
|
|
120
|
+
case OKNFCLiteSessionTypeSetMnemonicForce: {
|
|
121
|
+
SetMnemonicCallback callback = [_completionBlocks objectForKey:kSetMnemonicBlock];
|
|
122
|
+
if (callback) {
|
|
123
|
+
callback(self.lite,OKNFCLiteSetMncStatusCancel);
|
|
124
|
+
}
|
|
125
|
+
} break;
|
|
126
|
+
case OKNFCLiteSessionTypeGetMnemonic: {
|
|
127
|
+
GetMnemonicCallback callback = [_completionBlocks objectForKey:kGetMnemonicBlock];
|
|
128
|
+
if (callback) {
|
|
129
|
+
callback(self.lite,nil,OKNFCLiteGetMncStatusCancel);
|
|
130
|
+
}
|
|
131
|
+
} break;
|
|
132
|
+
case OKNFCLiteSessionTypeChangePin: {
|
|
133
|
+
ChangePinCallback callback = [_completionBlocks objectForKey:kChangePinBlock];
|
|
134
|
+
if (callback) {
|
|
135
|
+
callback(self.lite,OKNFCLiteChangePinStatusCancel);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
} break;
|
|
139
|
+
default:
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
[session invalidateSession];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
- (void)tagReaderSessionDidBecomeActive:(NFCTagReaderSession *)session {
|
|
147
|
+
NSLog(@"OKNFC tagReaderSessionDidBecomeActive %@", session);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#pragma mark - Tasks
|
|
151
|
+
|
|
152
|
+
- (void)nfcSessionComplete:(NFCTagReaderSession *)session {
|
|
153
|
+
if (![self checkLiteVersion]) {
|
|
154
|
+
[self endNFCSessionWithError:YES];
|
|
155
|
+
}
|
|
156
|
+
self.selectNFCApp = OKNFCLiteAppNONE;
|
|
157
|
+
switch (self.sessionType) {
|
|
158
|
+
case OKNFCLiteSessionTypeGetInfo:
|
|
159
|
+
case OKNFCLiteSessionTypeUpdateInfo:{
|
|
160
|
+
[self _getLiteInfo];
|
|
161
|
+
} break;
|
|
162
|
+
case OKNFCLiteSessionTypeReset: {
|
|
163
|
+
[self _reset];
|
|
164
|
+
} break;
|
|
165
|
+
case OKNFCLiteSessionTypeSetMnemonic: {
|
|
166
|
+
[self _setMnemonic:NO];
|
|
167
|
+
} break;
|
|
168
|
+
case OKNFCLiteSessionTypeSetMnemonicForce: {
|
|
169
|
+
[self _setMnemonic:YES];
|
|
170
|
+
} break;
|
|
171
|
+
case OKNFCLiteSessionTypeGetMnemonic: {
|
|
172
|
+
[self _getMnemonic];
|
|
173
|
+
} break;
|
|
174
|
+
case OKNFCLiteSessionTypeChangePin: {
|
|
175
|
+
[self _changePin];
|
|
176
|
+
} break;
|
|
177
|
+
default:
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
self.sessionType = OKNFCLiteSessionTypeNone;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
#pragma mark - getLiteInfo
|
|
185
|
+
|
|
186
|
+
- (void)getLiteInfo:(GetLiteInfoCallback)callBack {
|
|
187
|
+
if (self.lite.SN.length > 0) {
|
|
188
|
+
self.sessionType = OKNFCLiteSessionTypeUpdateInfo;
|
|
189
|
+
} else {
|
|
190
|
+
self.sessionType = OKNFCLiteSessionTypeGetInfo;
|
|
191
|
+
}
|
|
192
|
+
[self.completionBlocks setObject:callBack forKey:kGetLiteInfoBlock];
|
|
193
|
+
[self beginNewNFCSession];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
- (void)getLiteInfo {
|
|
198
|
+
if (self.lite.SN.length > 0) {
|
|
199
|
+
self.sessionType = OKNFCLiteSessionTypeUpdateInfo;
|
|
200
|
+
} else {
|
|
201
|
+
self.sessionType = OKNFCLiteSessionTypeGetInfo;
|
|
202
|
+
}
|
|
203
|
+
[self beginNewNFCSession];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
- (void)_getLiteInfo {
|
|
207
|
+
GetLiteInfoCallback callback = [_completionBlocks objectForKey:kGetLiteInfoBlock];
|
|
208
|
+
[self.lite getLiteInfo:callback];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
- (BOOL)syncLiteInfo {
|
|
212
|
+
return [self.lite syncLiteInfo];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#pragma mark - setMnemonic
|
|
216
|
+
|
|
217
|
+
- (void)setMnemonic:(NSString *)mnemonic
|
|
218
|
+
withPin:(NSString *)pin
|
|
219
|
+
overwrite:(BOOL)overwrite
|
|
220
|
+
complete:(SetMnemonicCallback)complete {
|
|
221
|
+
|
|
222
|
+
if (pin.length != OKNFC_PIN_LENGTH) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
self.pin = pin;
|
|
226
|
+
self.exportMnemonic = mnemonic;
|
|
227
|
+
if(overwrite) {
|
|
228
|
+
// 写入强制覆盖
|
|
229
|
+
self.sessionType = OKNFCLiteSessionTypeSetMnemonicForce;
|
|
230
|
+
} else {
|
|
231
|
+
self.sessionType = OKNFCLiteSessionTypeSetMnemonic;
|
|
232
|
+
}
|
|
233
|
+
[self.completionBlocks setObject:complete forKey:kSetMnemonicBlock];
|
|
234
|
+
[self beginNewNFCSession];
|
|
235
|
+
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
- (void)_setMnemonic:(BOOL)force {
|
|
239
|
+
SetMnemonicCallback callback = [_completionBlocks objectForKey:kSetMnemonicBlock];
|
|
240
|
+
[self.lite setMnemonic:self.exportMnemonic withPin:self.pin overwrite:force complete:callback];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
#pragma mark - getMnemonic
|
|
244
|
+
|
|
245
|
+
- (void)getMnemonicWithPin:(NSString *)pin complete:(GetMnemonicCallback)complete {
|
|
246
|
+
if (pin.length != OKNFC_PIN_LENGTH) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
self.pin = pin;
|
|
250
|
+
self.sessionType = OKNFCLiteSessionTypeGetMnemonic;
|
|
251
|
+
[self.completionBlocks setObject:complete forKey:kGetMnemonicBlock];
|
|
252
|
+
[self beginNewNFCSession];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
- (void)_getMnemonic {
|
|
256
|
+
GetMnemonicCallback callback = [_completionBlocks objectForKey:kGetMnemonicBlock];
|
|
257
|
+
[self.lite getMnemonicWithPin:self.pin complete:callback];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#pragma mark - changePin
|
|
261
|
+
|
|
262
|
+
- (void)changePin:(NSString *)oldPin to:(NSString *)newPin complete:(ChangePinCallback)complete {
|
|
263
|
+
self.pin = oldPin;
|
|
264
|
+
self.neoPin = newPin;
|
|
265
|
+
self.sessionType = OKNFCLiteSessionTypeChangePin;
|
|
266
|
+
[self.completionBlocks setObject:complete forKey:kChangePinBlock];
|
|
267
|
+
[self beginNewNFCSession];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
- (void)_changePin {
|
|
271
|
+
ChangePinCallback callback = [_completionBlocks objectForKey:kChangePinBlock];
|
|
272
|
+
[self.lite changePin:self.pin to:self.neoPin complete:callback];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
#pragma mark - reset
|
|
276
|
+
|
|
277
|
+
- (void)reset:(ResetCallback)callBack {
|
|
278
|
+
self.sessionType = OKNFCLiteSessionTypeReset;
|
|
279
|
+
[self.completionBlocks setObject:callBack forKey:kResetBlock];
|
|
280
|
+
[self beginNewNFCSession];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
- (void)_reset {
|
|
284
|
+
ResetCallback callback = [_completionBlocks objectForKey:kResetBlock];
|
|
285
|
+
[self.lite reset:callback];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
- (BOOL)_resetSync {
|
|
289
|
+
return [self.lite resetSync];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
#pragma mark - CheckLiteVersion
|
|
294
|
+
|
|
295
|
+
-(BOOL)checkLiteVersion {
|
|
296
|
+
id<NFCISO7816Tag> tag = [self.session.connectedTag asNFCISO7816Tag];
|
|
297
|
+
if (!tag) { return false; }
|
|
298
|
+
|
|
299
|
+
__block BOOL success = NO;
|
|
300
|
+
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
|
|
301
|
+
|
|
302
|
+
OKLiteCommandModal *modalV1 = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandSelectBackup version:OKNFCLiteVersionV1];
|
|
303
|
+
[tag sendCommandAPDU:[modalV1 buildAPDU] completionHandler:^(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError *error) {
|
|
304
|
+
[OKNFCUtility logAPDU:@"检查是否LiteV1" response:responseData sw1:sw1 sw2:sw2 error:error];
|
|
305
|
+
success = sw1 == OKNFC_SW1_OK;
|
|
306
|
+
|
|
307
|
+
dispatch_semaphore_signal(sema);
|
|
308
|
+
}];
|
|
309
|
+
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
|
310
|
+
|
|
311
|
+
if(success) {
|
|
312
|
+
self.lite = [[OKLiteV1 alloc] initWithDelegate:self];
|
|
313
|
+
} else {
|
|
314
|
+
OKLiteCommandModal *modalV2 = [[OKLiteCommandModal alloc] initWithCommand:OKLiteCommandSelectBackup version:OKNFCLiteVersionV2];
|
|
315
|
+
[tag sendCommandAPDU:[modalV2 buildAPDU] completionHandler:^(NSData *responseData, uint8_t sw1, uint8_t sw2, NSError *error) {
|
|
316
|
+
[OKNFCUtility logAPDU:@"检查是否LiteV2" response:responseData sw1:sw1 sw2:sw2 error:error];
|
|
317
|
+
success = sw1 == OKNFC_SW1_OK;
|
|
318
|
+
dispatch_semaphore_signal(sema);
|
|
319
|
+
}];
|
|
320
|
+
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
|
|
321
|
+
if (success) {
|
|
322
|
+
self.lite = [[OKLiteV2 alloc] initWithDelegate:self];
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
return success;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
@end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NFCConfig.h
|
|
3
|
+
// OneKeyWallet
|
|
4
|
+
//
|
|
5
|
+
// Created by linleiqin on 2022/2/22.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#import <Foundation/Foundation.h>
|
|
9
|
+
|
|
10
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
11
|
+
|
|
12
|
+
@interface NFCConfig : NSObject
|
|
13
|
+
|
|
14
|
+
+ (NSDictionary *)env;
|
|
15
|
+
+ (NSString *)envFor: (NSString *)key;
|
|
16
|
+
|
|
17
|
+
@end
|
|
18
|
+
|
|
19
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#import "NFCConfig.h"
|
|
2
|
+
#import "keys.h"
|
|
3
|
+
|
|
4
|
+
@implementation NFCConfig
|
|
5
|
+
|
|
6
|
+
+ (NSDictionary *)env {
|
|
7
|
+
char *result = getInitParams();
|
|
8
|
+
NSData *data = [NSData dataWithBytesNoCopy:result length:strlen(result) freeWhenDone:YES];
|
|
9
|
+
NSDictionary *value = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
+ (NSString *)envFor: (NSString *)key {
|
|
14
|
+
NSString *value = (NSString *)[self.env objectForKey:key];
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSData+OKNFCHexData.h
|
|
3
|
+
//
|
|
4
|
+
// Created by linleiqin on 2023/6/20.
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
#import <Foundation/Foundation.h>
|
|
8
|
+
|
|
9
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
10
|
+
|
|
11
|
+
@interface NSData (OKNFCHexData)
|
|
12
|
+
|
|
13
|
+
- (NSString *)toHexString;
|
|
14
|
+
|
|
15
|
+
@end
|
|
16
|
+
|
|
17
|
+
NS_ASSUME_NONNULL_END
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSData+OKNFCHexData.m
|
|
3
|
+
//
|
|
4
|
+
// Created by linleiqin on 2023/6/20.
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
#import "NSData+OKNFCHexData.h"
|
|
8
|
+
|
|
9
|
+
@implementation NSData (OKNFCHexData)
|
|
10
|
+
- (NSString *)toHexString {
|
|
11
|
+
const unsigned char *dataBuffer = (const unsigned char *)[self bytes];
|
|
12
|
+
|
|
13
|
+
if (!dataBuffer)
|
|
14
|
+
return [NSString string];
|
|
15
|
+
|
|
16
|
+
NSUInteger dataLength = [self length];
|
|
17
|
+
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
|
|
18
|
+
|
|
19
|
+
for (int i = 0; i < dataLength; ++i)
|
|
20
|
+
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
|
|
21
|
+
return [NSString stringWithString:hexString];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
@end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSData+StringToData.h
|
|
3
|
+
// OneKey
|
|
4
|
+
//
|
|
5
|
+
// Created by xiaoliang on 2020/11/17.
|
|
6
|
+
// Copyright © 2020 OneKey. All rights reserved..
|
|
7
|
+
//
|
|
8
|
+
#import <Foundation/Foundation.h>
|
|
9
|
+
|
|
10
|
+
@interface NSData (StringToData)
|
|
11
|
+
+ (NSString*)hexStringForData:(NSData*)data;
|
|
12
|
+
@end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSData+StringToData.m
|
|
3
|
+
// OneKey
|
|
4
|
+
//
|
|
5
|
+
// Created by xiaoliang on 2020/11/17.
|
|
6
|
+
// Copyright © 2020 OneKey. All rights reserved..
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
#import "NSData+StringToData.h"
|
|
10
|
+
|
|
11
|
+
@implementation NSData (StringToData)
|
|
12
|
+
+ (NSString*)hexStringForData:(NSData*)data
|
|
13
|
+
{
|
|
14
|
+
if (data == nil) {
|
|
15
|
+
return nil;
|
|
16
|
+
}
|
|
17
|
+
NSMutableString* hexString = [NSMutableString string];
|
|
18
|
+
const unsigned char *p = [data bytes];
|
|
19
|
+
for (int i=0; i < [data length]; i++) {
|
|
20
|
+
[hexString appendFormat:@"%02x", *p++];
|
|
21
|
+
}
|
|
22
|
+
return hexString;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSString+BXAdd.m
|
|
3
|
+
// Electron-Cash
|
|
4
|
+
//
|
|
5
|
+
// Created by xiaoliang on 2020/9/28.
|
|
6
|
+
// Copyright © 2020 OneKey. All rights reserved..
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
#import "NSString+OKAdd.h"
|
|
10
|
+
#import <CommonCrypto/CommonDigest.h>
|
|
11
|
+
#import "NSData+StringToData.h"
|
|
12
|
+
|
|
13
|
+
@implementation NSString (OKAdd)
|
|
14
|
+
|
|
15
|
+
- (NSString *)SHA256
|
|
16
|
+
{
|
|
17
|
+
const char *s = [self cStringUsingEncoding:NSASCIIStringEncoding];
|
|
18
|
+
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
|
|
19
|
+
|
|
20
|
+
uint8_t digest[CC_SHA256_DIGEST_LENGTH] = {0};
|
|
21
|
+
CC_SHA256(keyData.bytes, (CC_LONG)keyData.length, digest);
|
|
22
|
+
NSData *out = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
|
|
23
|
+
NSString *hash = [NSData hexStringForData:out];
|
|
24
|
+
return hash;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (BOOL)ok_match:(NSString *)regex {
|
|
28
|
+
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
|
|
29
|
+
BOOL isMatch = [pred evaluateWithObject:self];
|
|
30
|
+
return isMatch;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
+ (NSString *)generateRequestId {
|
|
34
|
+
long long milliseconds = (long long)([[NSDate date] timeIntervalSince1970] * 1000.0);
|
|
35
|
+
int r = arc4random_uniform(1000000);
|
|
36
|
+
return [NSString stringWithFormat:@"%lld:%d", milliseconds, r];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NSString+OKNFCHexStr.h
|
|
3
|
+
//
|
|
4
|
+
// Created by linleiqin on 2023/6/20.
|
|
5
|
+
//
|
|
6
|
+
|
|
7
|
+
#import <Foundation/Foundation.h>
|
|
8
|
+
|
|
9
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
10
|
+
|
|
11
|
+
@interface NSString (OKNFCHexStr)
|
|
12
|
+
|
|
13
|
+
- (NSData *)dataFromHexString;
|
|
14
|
+
- (NSString *)hexString;
|
|
15
|
+
|
|
16
|
+
@end
|
|
17
|
+
|
|
18
|
+
NS_ASSUME_NONNULL_END
|