Skip to content

Commit

Permalink
Merge pull request #23 from ERussel/fix/hex-encoding
Browse files Browse the repository at this point in the history
Fix/hex encoding
  • Loading branch information
ERussel authored Jun 30, 2021
2 parents 770960c + 42f8bd9 commit 94f779d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 49 deletions.
41 changes: 22 additions & 19 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
PODS:
- IrohaCrypto (0.7.4):
- IrohaCrypto/BIP39 (= 0.7.4)
- IrohaCrypto/blake2 (= 0.7.4)
- IrohaCrypto/Common (= 0.7.4)
- IrohaCrypto/ed25519 (= 0.7.4)
- IrohaCrypto/Iroha (= 0.7.4)
- IrohaCrypto/Scrypt (= 0.7.4)
- IrohaCrypto/secp256k1 (= 0.7.4)
- IrohaCrypto/sr25519 (= 0.7.4)
- IrohaCrypto/BIP39 (0.7.4):
- IrohaCrypto (0.8.0):
- IrohaCrypto/BIP39 (= 0.8.0)
- IrohaCrypto/blake2 (= 0.8.0)
- IrohaCrypto/Common (= 0.8.0)
- IrohaCrypto/ed25519 (= 0.8.0)
- IrohaCrypto/Iroha (= 0.8.0)
- IrohaCrypto/Scrypt (= 0.8.0)
- IrohaCrypto/secp256k1 (= 0.8.0)
- IrohaCrypto/sr25519 (= 0.8.0)
- IrohaCrypto/ss58 (= 0.8.0)
- IrohaCrypto/BIP39 (0.8.0):
- IrohaCrypto/Common
- IrohaCrypto/blake2 (0.7.4)
- IrohaCrypto/Common (0.7.4)
- IrohaCrypto/ed25519 (0.7.4):
- IrohaCrypto/blake2 (0.8.0)
- IrohaCrypto/Common (0.8.0)
- IrohaCrypto/ed25519 (0.8.0):
- IrohaCrypto/Common
- IrohaCrypto/Iroha (0.7.4):
- IrohaCrypto/Iroha (0.8.0):
- IrohaCrypto/Common
- IrohaCrypto/Scrypt (0.7.4):
- IrohaCrypto/Scrypt (0.8.0):
- IrohaCrypto/Common
- scrypt.c (~> 0.1)
- IrohaCrypto/secp256k1 (0.7.4):
- IrohaCrypto/secp256k1 (0.8.0):
- IrohaCrypto/Common
- secp256k1.c (~> 0.1)
- IrohaCrypto/sr25519 (0.7.4):
- IrohaCrypto/sr25519 (0.8.0):
- IrohaCrypto/BIP39
- IrohaCrypto/blake2
- IrohaCrypto/Common
- IrohaCrypto/ss58 (0.8.0):
- IrohaCrypto/Common
- scrypt.c (0.1.1)
- secp256k1.c (0.1.2)

Expand All @@ -42,10 +45,10 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
IrohaCrypto: 753b4f9c2af9ddb97538e6b919d087f87112e708
IrohaCrypto: c2e54b873bc858daf7afbd414865582f63cacdea
scrypt.c: b42ae06183251329d2b2c620c226fb541a4a3592
secp256k1.c: db47b726585d80f027423682eb369729e61b3b20

PODFILE CHECKSUM: 090cdf3e5819729438de794e136ffaa6b24d3abc

COCOAPODS: 1.9.3
COCOAPODS: 1.10.1
9 changes: 8 additions & 1 deletion IrohaCrypto.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'IrohaCrypto'
s.version = '0.7.4'
s.version = '0.8.0'
s.summary = 'Provides object oriented wrappers for C/C++ crypto functions used by blockchains.'

s.homepage = 'https://github.com/soramitsu'
Expand Down Expand Up @@ -83,6 +83,13 @@ Pod::Spec.new do |s|
ed.preserve_paths = 'ed25519Imp/**/*.h'
end

s.subspec 'ss58' do |ss|
ss.dependency 'IrohaCrypto/blake2'
ss.dependency 'IrohaCrypto/Common'
ss.source_files = 'IrohaCrypto/Classes/ss58/**/*'
ss.public_header_files = 'IrohaCrypto/Classes/ss58/**/*.h'
end

s.pod_target_xcconfig = { 'CLANG_WARN_DOCUMENTATION_COMMENTS' => "NO" }

s.test_spec do |ts|
Expand Down
21 changes: 13 additions & 8 deletions IrohaCrypto/Classes/Common/NSData+Hex.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,31 @@ - (nullable instancetype)initWithHexString:(nonnull NSString*)hexString error:(N
return nil;
}

uint8_t *bytes = malloc(sizeof(uint8_t) * length / 2);

const char* cString = [hexString cStringUsingEncoding:NSASCIIStringEncoding];

for(int index = 0; index + 2 <= length; index += 2) {
sscanf(&cString[index], "%02" SCNx8, &bytes[index / 2]);
NSMutableData *resultData = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};

for (int i = 0; i < ([hexString length] / 2); i++) {
byte_chars[0] = [hexString characterAtIndex:i * 2];
byte_chars[1] = [hexString characterAtIndex:i * 2 + 1];
whole_byte = strtol(byte_chars, NULL, 16);
[resultData appendBytes:&whole_byte length:1];
}

return [self initWithBytesNoCopy:bytes length:length / 2];
return resultData;
}

- (nonnull NSString*)toHexString {
unsigned char hex_str[]= "0123456789abcdef";
NSUInteger length = [self length];

char *cString = malloc(sizeof(char) * 2 * length + 1);

uint8_t *bytes = (uint8_t*)[self bytes];

for (int index = 0; index < length; index++) {
sprintf(&cString[2 * index], "%02x", bytes[index]);
cString[index * 2 + 0] = hex_str[(bytes[index] >> 4) & 0x0F];
cString[index * 2 + 1] = hex_str[(bytes[index]) & 0x0F];
}

cString[2*length] = '\0';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ typedef NS_ENUM(NSUInteger, SNAddressFactoryError) {

@protocol SS58AddressFactoryProtocol

- (nullable NSString*)addressFromPublicKey:(id<IRPublicKeyProtocol> _Nonnull)publicKey
type:(UInt8)type
- (nullable NSString*)addressFromAccountId:(NSData* _Nonnull)accountId
type:(UInt16)type
error:(NSError*_Nullable*_Nullable)error;

- (nullable NSData*)accountIdFromAddress:(nonnull NSString*)address
type:(UInt8)type
type:(UInt16)type
error:(NSError*_Nullable*_Nullable)error;

- (nullable NSNumber*)typeFromAddress:(nonnull NSString*)address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

@implementation SS58AddressFactory

- (nullable NSString*)addressFromPublicKey:(id<IRPublicKeyProtocol> _Nonnull)publicKey
type:(UInt8)type
- (nullable NSString*)addressFromAccountId:(NSData* _Nonnull)accountId
type:(UInt16)type
error:(NSError*_Nullable*_Nullable)error {

UInt16 ident = type & 0b0011111111111111;
Expand All @@ -35,8 +35,6 @@ - (nullable NSString*)addressFromPublicKey:(id<IRPublicKeyProtocol> _Nonnull)pub
[addressData appendBytes:&second length:1];
}

NSData *accountId = publicKey.rawData;

if ([accountId length] != ACCOUNT_ID_LENGTH) {
accountId = [accountId blake2b:ACCOUNT_ID_LENGTH error:error];

Expand All @@ -62,20 +60,20 @@ - (nullable NSString*)addressFromPublicKey:(id<IRPublicKeyProtocol> _Nonnull)pub
return [addressData toBase58];
}

- (UInt8) decodeTypeFromData:(NSData *)addressData {
- (UInt16) decodeTypeFromData:(NSData *)addressData {
UInt8 prefix = ((uint8_t*)addressData.bytes)[0];
if (prefix < 64) {
return prefix;
return (UInt16)prefix;
} else {
UInt8 second = ((uint8_t*)addressData.bytes)[1];
UInt8 lower = prefix << 2 | second >> 6;
UInt8 upper = second & 0b00111111;
UInt16 lower = prefix << 2 | second >> 6;
UInt16 upper = second & 0b00111111;
return lower | (upper << 8);
}
}

- (nullable NSData*)accountIdFromAddress:(nonnull NSString*)address
type:(UInt8)type
type:(UInt16)type
error:(NSError*_Nullable*_Nullable)error {
NSData *ss58Data = [[NSData alloc] initWithBase58String:address];

Expand Down
8 changes: 8 additions & 0 deletions Tests/Common/IRHexTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ - (void)testHexToData {
}
}

- (void)testUppercaseHexToData {
for (int index; index < MESSAGES_COUNT; index++) {
NSData *data = [[NSData alloc] initWithHexString:[HEX_MESSAGES[index] uppercaseString] error:nil];
NSData *expected = [[NSData alloc] initWithBase64EncodedString:MESSAGES[index] options:0];
XCTAssertEqualObjects(data, expected);
}
}

@end
18 changes: 9 additions & 9 deletions Tests/SR25519/SS58AddressFactoryTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
};

static UInt8 const TYPES[] = {
SNAddressTypeKusamaMain,
SNAddressTypeKusamaMain,
SNAddressTypeKusamaMain,
SNAddressTypeKusamaMain,
SNAddressTypeSoraMain,
SNAddressTypeSoraMain
2,
69,
2,
2,
69,
69
};

@interface SS58AddressFactoryTests : XCTestCase
Expand Down Expand Up @@ -67,7 +67,7 @@ - (void)testSS58AddressEncoding {
return;
}

NSString *address = [factory addressFromPublicKey:publicKey
NSString *address = [factory addressFromAccountId:publicKey.rawData
type:TYPES[i]
error:&error];

Expand Down Expand Up @@ -148,8 +148,8 @@ - (void)testAddressFromSecp256k1 {
return;
}

NSString *address = [factory addressFromPublicKey:keypair.publicKey
type:SNAddressTypeKusamaMain
NSString *address = [factory addressFromAccountId:keypair.publicKey.rawData
type:2
error:&error];

if (error != nil) {
Expand Down

0 comments on commit 94f779d

Please sign in to comment.