From 921eaca5cda2dcc5820f969196e511a536557376 Mon Sep 17 00:00:00 2001 From: MukhinAlexey Date: Sat, 12 May 2018 23:50:50 +0300 Subject: [PATCH] Fix wrong pointer logic in keypair obj-c Signed-off-by: MukhinAlexey --- SwiftyIroha.xcodeproj/project.pbxproj | 8 +++++++- .../ObjC++/ModelCrypto/Keypair/IRKeypair.h | 2 +- .../ObjC++/ModelCrypto/Keypair/IRKeypair.mm | 16 +++++++++++++--- .../IRModelTransactionBuilder.mm | 8 ++++---- .../ObjC++/UnsignedQuery/IRUnsignedQuery.mm | 1 + .../UnsignedTransaction/IRUnsignedtransaction.mm | 1 + .../IrohaTransactionPreparation.swift | 4 ++-- SwiftyIrohaExample/ViewController.swift | 2 +- 8 files changed, 30 insertions(+), 12 deletions(-) diff --git a/SwiftyIroha.xcodeproj/project.pbxproj b/SwiftyIroha.xcodeproj/project.pbxproj index 5028edc43..fbf116518 100644 --- a/SwiftyIroha.xcodeproj/project.pbxproj +++ b/SwiftyIroha.xcodeproj/project.pbxproj @@ -706,7 +706,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0930; - LastUpgradeCheck = 0920; + LastUpgradeCheck = 0930; ORGANIZATIONNAME = Soramitsu; TargetAttributes = { 8A37473420A0E14F005D623F = { @@ -901,6 +901,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ADDRESS_SANITIZER_CONTAINER_OVERFLOW = YES; CLANG_ENABLE_OBJC_WEAK = YES; CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; @@ -922,6 +923,7 @@ buildSettings = { ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CLANG_ADDRESS_SANITIZER_CONTAINER_OVERFLOW = YES; CLANG_ENABLE_OBJC_WEAK = YES; CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; @@ -951,6 +953,7 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -958,6 +961,7 @@ CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; @@ -1021,6 +1025,7 @@ CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_DOCUMENTATION_COMMENTS = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -1028,6 +1033,7 @@ CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; diff --git a/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.h b/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.h index 53c5e7fa2..dfdcecfd3 100644 --- a/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.h +++ b/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.h @@ -19,7 +19,7 @@ @interface IRKeypair : NSObject { @private - struct KeypairImpl* keypairImpl; + struct KeypairImpl *keypairImpl; } @property NSString* privateKey; diff --git a/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.mm b/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.mm index 1699a63b2..9bd494ae7 100644 --- a/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.mm +++ b/SwiftyIroha/ObjC++/ModelCrypto/Keypair/IRKeypair.mm @@ -41,21 +41,31 @@ @implementation IRKeypair - (id)initWithPublicKey: (NSString*)publicKey - withPrivateKey: (NSString*)privateKey { + withPrivateKey: (NSString*)privateKey { self = [super init]; if (self) { - self.privateKey = privateKey; self.publicKey = publicKey; + self.privateKey = privateKey; + string publicKeyCpp = [self getStringCppFromStringObjC:publicKey]; + string privateKeyCpp = [self getStringCppFromStringObjC:privateKey]; + self->keypairImpl = new KeypairImpl(Keypair(PublicKey(Blob::fromHexString(publicKeyCpp)), + PrivateKey(Blob::fromHexString(privateKeyCpp)))); } return self; } -(void)setKeypair:(struct KeypairImpl*)keypair { - keypairImpl = keypair; + keypairImpl = new KeypairImpl(keypair->keypairCpp); } -(struct KeypairImpl)getKeypair { return *(keypairImpl); } +-(string)getStringCppFromStringObjC:(NSString*)stringObjC { + string stringCpp = string([stringObjC UTF8String], + [stringObjC lengthOfBytesUsingEncoding:NSUTF8StringEncoding]); + return stringCpp; +} + @end diff --git a/SwiftyIroha/ObjC++/ModelTransactionBuilder/IRModelTransactionBuilder.mm b/SwiftyIroha/ObjC++/ModelTransactionBuilder/IRModelTransactionBuilder.mm index 1893d3562..7d54f7231 100644 --- a/SwiftyIroha/ObjC++/ModelTransactionBuilder/IRModelTransactionBuilder.mm +++ b/SwiftyIroha/ObjC++/ModelTransactionBuilder/IRModelTransactionBuilder.mm @@ -114,13 +114,13 @@ -(IRModelTransactionBuilder*)createAccountWithAccountName:(NSString*)accountName withPublicKey:(IRPublicKey*)publicKey { string accountNameCpp = [self getStringCppFromStringObjC:accountName]; string domainIdCpp = [self getStringCppFromStringObjC:domainId]; - NSData *data = [self dataFromHexString:publicKey.value]; + string publicKeyCpp = [self getStringCppFromStringObjC:publicKey.value]; - PublicKey publicKeyCpp = PublicKey((char*)([data bytes])); transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createAccount(accountNameCpp, domainIdCpp, - publicKeyCpp); + PublicKey(Blob::fromHexString(publicKeyCpp))); + // NSLog(@"%@", data); return self; } @@ -287,7 +287,7 @@ -(string)getStringCppFromStringObjC:(NSString*)stringObjC { return stringCpp; } -- (NSData *)dataFromHexString:(NSString *) string { +- (NSData*)dataFromHexString:(NSString*) string { [string stringByReplacingOccurrencesOfString:@" " withString:@""]; NSMutableData* dataValue = [[NSMutableData alloc] init]; unsigned char byte; diff --git a/SwiftyIroha/ObjC++/UnsignedQuery/IRUnsignedQuery.mm b/SwiftyIroha/ObjC++/UnsignedQuery/IRUnsignedQuery.mm index 45bbdea79..9a561de9f 100644 --- a/SwiftyIroha/ObjC++/UnsignedQuery/IRUnsignedQuery.mm +++ b/SwiftyIroha/ObjC++/UnsignedQuery/IRUnsignedQuery.mm @@ -25,6 +25,7 @@ typedef UnsignedWrapper UnsignedQueryCpp; +// TODO: Redesign like in IRKeypair.mm struct UnsignedQueryImpl { UnsignedQueryCpp* unsignedQueryCpp; diff --git a/SwiftyIroha/ObjC++/UnsignedTransaction/IRUnsignedtransaction.mm b/SwiftyIroha/ObjC++/UnsignedTransaction/IRUnsignedtransaction.mm index 3dbc5e38a..ec36ec5e6 100644 --- a/SwiftyIroha/ObjC++/UnsignedTransaction/IRUnsignedtransaction.mm +++ b/SwiftyIroha/ObjC++/UnsignedTransaction/IRUnsignedtransaction.mm @@ -25,6 +25,7 @@ typedef UnsignedWrapper UnsignedTransactionCpp; +// TODO: Redesign like in IRKeypair.mm struct UnsignedTransactionImpl { UnsignedTransactionCpp* unsignedTransactionCpp; diff --git a/SwiftyIroha/Swift/Transaction/TransactionPreparation/IrohaTransactionPreparation.swift b/SwiftyIroha/Swift/Transaction/TransactionPreparation/IrohaTransactionPreparation.swift index 8d058cd73..87ec9c922 100644 --- a/SwiftyIroha/Swift/Transaction/TransactionPreparation/IrohaTransactionPreparation.swift +++ b/SwiftyIroha/Swift/Transaction/TransactionPreparation/IrohaTransactionPreparation.swift @@ -26,12 +26,12 @@ public class IrohaTransactionPreparation { } public func sign(_ unsignedTransaction: IrohaUnsignedTransaction, - with keypair: IrohaKeypair) -> Data { + with keypair: IrohaKeypair) -> Data { let objectForSigningObjC = unsignedTransaction.getObjectForSigning() // TODO: Implement better mapping let keypairObjC: IRKeypair = keypair.getKeypairObjC() - + let data = modelProto.signTransaction(objectForSigningObjC, with: keypairObjC) return data! } diff --git a/SwiftyIrohaExample/ViewController.swift b/SwiftyIrohaExample/ViewController.swift index ee5611da8..5e85a5aec 100644 --- a/SwiftyIrohaExample/ViewController.swift +++ b/SwiftyIrohaExample/ViewController.swift @@ -71,7 +71,7 @@ class ViewController: UIViewController { .creatorAccountId("admin@test") .createdTime(Date()) .transactionCounter(1) - .createAccount(withAccountName: "testuser", + .createAccount(withAccountName: "agggggg", withDomainId: "test", withPublicKey: keypair.getPublicKey()) .build()