Skip to content

Commit

Permalink
Merge pull request #62 from hyperledger/fix/format-of-publickey
Browse files Browse the repository at this point in the history
Fix problem with wrong public key format
  • Loading branch information
MukhinAlexey authored May 12, 2018
2 parents a8b3e71 + 8601254 commit 289ec65
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 49 deletions.
5 changes: 5 additions & 0 deletions SwiftyIroha/ObjC++/ModelCrypto/IRModelCrypto.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ - (KeypairObjC*) generateFromExistingKeypair:(IRKeypair*) keypair {
-(string)getStringCppWithString:(NSString*)stringObjC {
string stringCpp = string([stringObjC UTF8String],
[stringObjC lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
cout << stringCpp;
cout << "\n";
cout << stringCpp.length();
cout << "\n";
cout << "\n";
return stringCpp;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@

@interface IRPrivateKey : NSObject

@property NSString* value;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@

@property NSString* value;

- (id)init:(NSString*) value;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@

@implementation IRPublicKey

- (id)init:(NSString*) value {
self = [super init];
if (self) {
self.value = value;
}
return self;
}

@end
3 changes: 2 additions & 1 deletion SwiftyIroha/ObjC++/ModelProto/IRModelProto.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ -(NSData*)signTransaction:(IRUnsignedtransaction*)transactionForSigning
withKeypair:(KeypairObjC*)keypair {
UnsignedTransactionImpl unsignedTransactionImpl = [transactionForSigning getUnsignedTransaction];
KeypairImpl keypairImpl = [keypair getKeypair];
Blob blob = modelProtoImpl->transactionForSigningCpp.signAndAddSignature(*unsignedTransactionImpl.unsignedTransactionCpp, keypairImpl.keypairCpp);
Blob blob = modelProtoImpl->transactionForSigningCpp.signAndAddSignature(*unsignedTransactionImpl.unsignedTransactionCpp,
keypairImpl.keypairCpp);
return [[NSData alloc] initWithBytes:blob.blob().data()
length:blob.size()];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,21 @@ - (void)dealloc {
-(IRModelTransactionBuilder*)creatorAccountId:(NSString*)creatorAccountId {
string creatorAccountIdCpp = string([creatorAccountId UTF8String],
[creatorAccountId lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.creatorAccountId(creatorAccountIdCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.creatorAccountId(creatorAccountIdCpp);
return self;
}

-(IRModelTransactionBuilder*)createdTime:(NSDate*)creationTime {
UInt64 timestamp = (UInt64)([creationTime timeIntervalSince1970]) * 1000;
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createdTime(timestamp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.createdTime(timestamp);
return self;
}

-(IRModelTransactionBuilder*)transactionCounter:(int)transactionCounter {
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.txCounter(transactionCounter);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.txCounter(transactionCounter);
return self;
}

Expand All @@ -79,27 +82,30 @@ -(IRModelTransactionBuilder*)addAssetQuantityWithAccountId:(NSString*)accountId
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string assetIdCpp = [self getStringCppFromStringObjC:assetId];
string amountCpp = [self getStringCppFromStringObjC:amount];
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.addAssetQuantity(accountIdCpp, assetIdCpp, amountCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.addAssetQuantity(accountIdCpp, assetIdCpp, amountCpp);
return self;
}

-(IRModelTransactionBuilder*)addPeerWithAddress:(NSString*)address
withPublicKey:(IRPublicKey*)peerPublicKey {
string addressCpp = [self getStringCppFromStringObjC:address];
string publicKeyValueCpp = [self getStringCppFromStringObjC:peerPublicKey.value];
NSData *data = [self dataFromHexString:peerPublicKey.value];

PublicKey publicKeyCpp = PublicKey(publicKeyValueCpp);
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.addPeer(addressCpp, publicKeyCpp);
PublicKey publicKeyCpp = PublicKey((char*)([data bytes]));
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.addPeer(addressCpp, publicKeyCpp);
return self;
}

-(IRModelTransactionBuilder*)addSignatoryWithAccountId:(NSString*)accountId
withPublicKey:(IRPublicKey*)publicKey {
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string publicKeyValueCpp = [self getStringCppFromStringObjC:publicKey.value];
NSData *data = [self dataFromHexString:publicKey.value];

PublicKey publicKeyCpp = PublicKey(publicKeyValueCpp);
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.addSignatory(accountIdCpp, publicKeyCpp);
PublicKey publicKeyCpp = PublicKey((char*)([data bytes]));
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.addSignatory(accountIdCpp, publicKeyCpp);
return self;
}

Expand All @@ -108,21 +114,23 @@ -(IRModelTransactionBuilder*)createAccountWithAccountName:(NSString*)accountName
withPublicKey:(IRPublicKey*)publicKey {
string accountNameCpp = [self getStringCppFromStringObjC:accountName];
string domainIdCpp = [self getStringCppFromStringObjC:domainId];
string publicKeyValueCpp = [self getStringCppFromStringObjC:publicKey.value];
NSData *data = [self dataFromHexString:publicKey.value];

PublicKey publicKeyCpp = PublicKey(publicKeyValueCpp);
transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createAccount(accountNameCpp,
domainIdCpp,
publicKeyCpp);
PublicKey publicKeyCpp = PublicKey((char*)([data bytes]));
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.createAccount(accountNameCpp,
domainIdCpp,
publicKeyCpp);
return self;
}

-(IRModelTransactionBuilder*)setAccountQuorumWithAccountId:(NSString*)accountId
withQuorum:(int)quorum {
string accountIdCpp = [self getStringCppFromStringObjC:accountId];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.setAccountQuorum(accountIdCpp,
quorum);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.setAccountQuorum(accountIdCpp,
quorum);
return self;
}

Expand All @@ -131,8 +139,9 @@ -(IRModelTransactionBuilder*)createDomainWithDomainId:(NSString*)domainId
string domainIdCpp = [self getStringCppFromStringObjC:domainId];
string defaultRoleCpp = [self getStringCppFromStringObjC:defaultRole];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createDomain(domainIdCpp,
defaultRoleCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.createDomain(domainIdCpp,
defaultRoleCpp);
return self;
}

Expand All @@ -142,19 +151,21 @@ -(IRModelTransactionBuilder*)createAssetWithAssetName:(NSString*)assetName
string assetNameCpp = [self getStringCppFromStringObjC:assetName];
string domainIdCpp = [self getStringCppFromStringObjC:domainId];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createAsset(assetNameCpp,
domainIdCpp,
precision);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.createAsset(assetNameCpp,
domainIdCpp,
precision);
return self;
}

-(IRModelTransactionBuilder*)appendRoleToAccountId:(NSString*)accountId
withRoleName:(NSString*)roleName {
withRoleName:(NSString*)roleName {
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string roleNameCpp = [self getStringCppFromStringObjC:roleName];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.appendRole(accountIdCpp,
roleNameCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.appendRole(accountIdCpp,
roleNameCpp);
return self;
}

Expand All @@ -168,7 +179,9 @@ -(IRModelTransactionBuilder*)createRoleWithName:(NSString*)roleName
permissionsVectorCpp.push_back(permissionCpp);
}

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.createRole(roleNameCpp, permissionsVectorCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.createRole(roleNameCpp,
permissionsVectorCpp);
return self;
}

Expand All @@ -177,18 +190,20 @@ -(IRModelTransactionBuilder*)detachRoleWithAccountId:(NSString*)accountId
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string roleNameCpp = [self getStringCppFromStringObjC:roleName];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.detachRole(accountIdCpp,
roleNameCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.detachRole(accountIdCpp,
roleNameCpp);
return self;
}

-(IRModelTransactionBuilder*)grantPermissionToAccountId:(NSString*)accountId
withPermission:(NSString*)permission {
withPermission:(NSString*)permission {
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string permissionCpp = [self getStringCppFromStringObjC:permission];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.grantPermission(accountIdCpp,
permissionCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.grantPermission(accountIdCpp,
permissionCpp);
return self;
}

Expand All @@ -197,8 +212,9 @@ -(IRModelTransactionBuilder*)revokePermissionToAccountId:(NSString*)accountId
string accountIdCpp = [self getStringCppFromStringObjC:accountId];
string permissionCpp = [self getStringCppFromStringObjC:permission];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.revokePermission(accountIdCpp,
permissionCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.revokePermission(accountIdCpp,
permissionCpp);
return self;
}

Expand All @@ -209,9 +225,10 @@ -(IRModelTransactionBuilder*)setAccountDetailToAccountId:(NSString*)accountId
string keyCpp = [self getStringCppFromStringObjC:key];
string valueCpp = [self getStringCppFromStringObjC:value];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.setAccountDetail(accountIdCpp,
keyCpp,
valueCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.setAccountDetail(accountIdCpp,
keyCpp,
valueCpp);
return self;
}

Expand All @@ -222,9 +239,10 @@ -(IRModelTransactionBuilder*)subtractAssetQuantityFromAccountId:(NSString*)accou
string assetIdCpp = [self getStringCppFromStringObjC:assetId];
string amountCpp = [self getStringCppFromStringObjC:amount];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.subtractAssetQuantity(accountIdCpp,
assetIdCpp,
amountCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.subtractAssetQuantity(accountIdCpp,
assetIdCpp,
amountCpp);
return self;
}

Expand All @@ -239,11 +257,12 @@ -(IRModelTransactionBuilder*)transferAssetFromAccountId:(NSString*)sourceAccount
string descriptionCpp = [self getStringCppFromStringObjC:description];
string amountCpp = [self getStringCppFromStringObjC:amount];

transactionBuilderImpl->transactionBuilderCpp = transactionBuilderImpl->transactionBuilderCpp.transferAsset(sourceAccountIdCpp,
destinationAccountIdCpp,
assetIdCpp,
descriptionCpp,
amountCpp);
transactionBuilderImpl->transactionBuilderCpp =
transactionBuilderImpl->transactionBuilderCpp.transferAsset(sourceAccountIdCpp,
destinationAccountIdCpp,
assetIdCpp,
descriptionCpp,
amountCpp);
return self;
}

Expand All @@ -268,4 +287,24 @@ -(string)getStringCppFromStringObjC:(NSString*)stringObjC {
return stringCpp;
}

- (NSData *)dataFromHexString:(NSString *) string {
if ([string length] % 2 == 1){
string = [@"0" stringByAppendingString:string];
}
const char *chars = [string UTF8String];
int i = 0, len = (int)[string length];

NSMutableData *data = [NSMutableData dataWithCapacity:len / 2];
char byteChars[3] = {'\0','\0','\0'};
unsigned long wholeByte;

while (i < len) {
byteChars[0] = chars[i++];
byteChars[1] = chars[i++];
wholeByte = strtoul(byteChars, NULL, 16);
[data appendBytes:&wholeByte length:1];
}
return data;
}

@end
2 changes: 1 addition & 1 deletion SwiftyIroha/Swift/ModelCrypto/Keypair/IrohaKeypair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class IrohaKeypair {
fileprivate var privateKey: IrohaPrivateKey

public init(publicKey: IrohaPublicKey,
privateKey: IrohaPrivateKey) {
privateKey: IrohaPrivateKey) {
self.keypairObjC = IRKeypair(publicKey: publicKey.getValue(),
withPrivateKey: privateKey.getValue())
self.publicKey = publicKey
Expand Down
7 changes: 4 additions & 3 deletions SwiftyIrohaExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class ViewController: UIViewController {
// Creating transaction for iroha
let unsignedTransaction =
try irohaTransactionBuilder
.creatorAccountId(creatorAcountId)
.creatorAccountId("admin@test")
.createdTime(Date())
.transactionCounter(1)
.createDomain(withDomainId: "ru", withDefaultRole: "user")
.createAssets(withAssetName: "dollar", domainId: "ru", percision: 0.1)
.createAccount(withAccountName: "testuser",
withDomainId: "test",
withPublicKey: keypair.getPublicKey())
.build()

// Creating helper class for signing unsigned transaction
Expand Down

0 comments on commit 289ec65

Please sign in to comment.