Skip to content
Permalink
Browse files

♻️ Add _Crypto.verifySignature

  • Loading branch information...
usatie committed Sep 19, 2018
1 parent 71539f6 commit 48c46adecb54a01cc44d582f29c0fbc18a187fb9
Showing with 19 additions and 23 deletions.
  1. +1 −1 BitcoinKit/BitcoinKitPrivate.h
  2. +17 −1 BitcoinKit/BitcoinKitPrivate.m
  3. +1 −21 Sources/BitcoinKit/Core/Crypto.swift
@@ -59,6 +59,6 @@ NS_ASSUME_NONNULL_BEGIN

@interface _Crypto : NSObject
+ (NSData *)signMessage:(NSData *)message withPrivateKey:(NSData *)privateKey;
+ (BOOL)verifySignature:(NSData *)signature message:(NSData *)message publicKey:(NSData *)publicKey;
@end

NS_ASSUME_NONNULL_END
@@ -216,12 +216,28 @@ + (NSData *)signMessage:(NSData *)message withPrivateKey:(NSData *)privateKey {
secp256k1_ecdsa_signature normalizedSignature;
secp256k1_ecdsa_sign(ctx, &signature, message.bytes, privateKey.bytes, NULL, NULL);
secp256k1_ecdsa_signature_normalize(ctx, &normalizedSignature, &signature);
size_t siglen = 128;
size_t siglen = 74;
NSMutableData *der = [NSMutableData dataWithLength:siglen];
secp256k1_ecdsa_signature_serialize_der(ctx, der.mutableBytes, &siglen, &normalizedSignature);
der.length = siglen;
secp256k1_context_destroy(ctx);
return der;
}

+ (BOOL)verifySignature:(NSData *)sigData message:(NSData *)message publicKey:(NSData *)pubkeyData {
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
secp256k1_ecdsa_signature signature;
secp256k1_pubkey pubkey;

secp256k1_ecdsa_signature_parse_der(ctx, &signature, sigData.bytes, sigData.length);
if (secp256k1_ec_pubkey_parse(ctx, &pubkey, pubkeyData.bytes, pubkeyData.length) != 1) {
return FALSE;
};

if (secp256k1_ecdsa_verify(ctx, &signature, message.bytes, &pubkey) != 1) {
return FALSE;
};
secp256k1_context_destroy(ctx);
return TRUE;
}
@end
@@ -29,7 +29,6 @@ import BitcoinKit.Private
#else
import BitcoinKitPrivate
#endif
import secp256k1

public struct Crypto {
public static func sha1(_ data: Data) -> Data {
@@ -66,26 +65,7 @@ public struct Crypto {

public static func verifySignature(_ signature: Data, message: Data, publicKey: Data) throws -> Bool {
#if BitcoinKitXcode
let ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY))!
defer { secp256k1_context_destroy(ctx) }

let signaturePointer = UnsafeMutablePointer<secp256k1_ecdsa_signature>.allocate(capacity: 1)
defer { signaturePointer.deallocate() }
guard signature.withUnsafeBytes({ secp256k1_ecdsa_signature_parse_der(ctx, signaturePointer, $0, signature.count) }) == 1 else {
print("signature : ", signature.hex)
throw CryptoError.signatureParseFailed
}

let pubkeyPointer = UnsafeMutablePointer<secp256k1_pubkey>.allocate(capacity: 1)
defer { pubkeyPointer.deallocate() }
guard publicKey.withUnsafeBytes({ secp256k1_ec_pubkey_parse(ctx, pubkeyPointer, $0, publicKey.count) }) == 1 else {
throw CryptoError.publicKeyParseFailed
}

guard message.withUnsafeBytes ({ secp256k1_ecdsa_verify(ctx, signaturePointer, $0, pubkeyPointer) }) == 1 else {
return false
}
return true
return _Crypto.verifySignature(signature, message: message, publicKey: publicKey)
#else
return try _Crypto.verifySignature(signature, message: message, publicKey: publicKey)
#endif

0 comments on commit 48c46ad

Please sign in to comment.
You can’t perform that action at this time.