diff --git a/IrohaCrypto.podspec b/IrohaCrypto.podspec index 613f060..11a93fb 100644 --- a/IrohaCrypto.podspec +++ b/IrohaCrypto.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'IrohaCrypto' - s.version = '0.8.2' + s.version = '0.9.0' s.summary = 'Provides object oriented wrappers for C/C++ crypto functions used by blockchains.' s.homepage = 'https://github.com/soramitsu' diff --git a/IrohaCrypto/Classes/secp256k1/SECPublicKey.h b/IrohaCrypto/Classes/secp256k1/SECPublicKey.h index b90c658..28d4664 100644 --- a/IrohaCrypto/Classes/secp256k1/SECPublicKey.h +++ b/IrohaCrypto/Classes/secp256k1/SECPublicKey.h @@ -14,6 +14,6 @@ + (NSUInteger)uncompressedLength; -- (nonnull NSData*)uncompressed; +- (nullable NSData*)uncompressed:(NSError*_Nullable*_Nullable)error; @end diff --git a/IrohaCrypto/Classes/secp256k1/SECPublicKey.m b/IrohaCrypto/Classes/secp256k1/SECPublicKey.m index d57fe05..4f8442c 100644 --- a/IrohaCrypto/Classes/secp256k1/SECPublicKey.m +++ b/IrohaCrypto/Classes/secp256k1/SECPublicKey.m @@ -7,6 +7,7 @@ #import "SECPublicKey.h" #import +#import "IRCryptoKey.h" @interface SECPublicKey() @@ -44,13 +45,26 @@ - (nullable instancetype)initWithRawData:(nonnull NSData*)data error:(NSError*_N return self; } -- (nonnull NSData*)uncompressed { +- (nullable NSData*)uncompressed:(NSError*_Nullable*_Nullable)error { secp256k1_pubkey rawPublicKey; secp256k1_context *context = secp256k1_context_create(SECP256K1_CONTEXT_NONE); int status = secp256k1_ec_pubkey_parse(context, &rawPublicKey, _rawData.bytes, [_rawData length]); + if (status == 0) { + secp256k1_context_destroy(context); + + if (error) { + NSString *message = [NSString stringWithFormat:@"Uncompressing unexpectedly failed"]; + *error = [NSError errorWithDomain:NSStringFromClass([self class]) + code:IRCryptoKeyErrorInvalidRawData + userInfo:@{NSLocalizedDescriptionKey: message}]; + } + + return nil; + } + size_t uncompressedLength = [SECPublicKey uncompressedLength]; unsigned char uncompresedPubkey[uncompressedLength]; @@ -60,6 +74,8 @@ - (nonnull NSData*)uncompressed { &rawPublicKey, SECP256K1_EC_UNCOMPRESSED); + secp256k1_context_destroy(context); + return [NSData dataWithBytes:uncompresedPubkey length:uncompressedLength]; } diff --git a/Tests/secp256k1/Secp256k1PublicKeyTests.m b/Tests/secp256k1/Secp256k1PublicKeyTests.m index 8f8f273..e5a5c1f 100644 --- a/Tests/secp256k1/Secp256k1PublicKeyTests.m +++ b/Tests/secp256k1/Secp256k1PublicKeyTests.m @@ -42,7 +42,13 @@ - (void)testPublicKeyUncompression { return; } - NSData *actualUncompressed = [publicKey uncompressed]; + NSData *actualUncompressed = [publicKey uncompressed:&error]; + + if (error) { + XCTFail(@"Did receive error: %@", [error localizedDescription]); + return; + } + NSData *expectedUncompressed = [[NSData alloc] initWithHexString:UNCOMPRESSED[index] error:&error]; if (error) {