diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift index 201bc32..8aeae38 100755 --- a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift +++ b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift @@ -44,18 +44,16 @@ public final class ECDSA { public func verifySignature(_ sigData: Data, message: Data, publicKeyData: Data) throws -> Bool { guard let ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY)) else { return false } + defer { secp256k1_context_destroy(ctx) } var pubkey = secp256k1_pubkey() var signature = secp256k1_ecdsa_signature() - secp256k1_ecdsa_signature_parse_der(ctx, &signature, sigData.bytes, sigData.count) - - if (secp256k1_ec_pubkey_parse(ctx, &pubkey, publicKeyData.bytes, publicKeyData.count) != 1) { + secp256k1_ecdsa_signature_parse_der(ctx, &signature, [UInt8](sigData), sigData.count) + if (secp256k1_ec_pubkey_parse(ctx, &pubkey, [UInt8](publicKeyData), publicKeyData.count) != 1) { return false }; - - if (secp256k1_ecdsa_verify(ctx, &signature, message.bytes, &pubkey) != 1) { + if (secp256k1_ecdsa_verify(ctx, &signature, [UInt8](message), &pubkey) != 1) { return false }; - secp256k1_context_destroy(ctx); return true } } diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift index 2f6f0e1..c0c19b5 100644 --- a/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift +++ b/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift @@ -28,7 +28,7 @@ public class EllipticCurveEncrypterSecp256k1 { /// - privateKey: private key bytes /// - Returns: public key structure public func createPublicKey(privateKey: Data) -> secp256k1_pubkey { - let privateKey = privateKey.bytes + let privateKey = [UInt8](privateKey) var publickKey = secp256k1_pubkey() _ = SecpResult(secp256k1_ec_pubkey_create(context, &publickKey, privateKey)) return publickKey @@ -94,8 +94,8 @@ public class EllipticCurveEncrypterSecp256k1 { /// - hash: 32-byte (256-bit) hash of a message /// - Returns: public key structure or nil, if signature invalid public func publicKey(signature: inout secp256k1_ecdsa_recoverable_signature, hash: Data) -> secp256k1_pubkey? { + let hash = [UInt8](hash) precondition(hash.count == 32, "Hash must be 32 bytes size") - let hash = hash.bytes var outPubKey = secp256k1_pubkey() let status = SecpResult(secp256k1_ecdsa_recover(context, &outPubKey, &signature, hash)) return status == .success ? outPubKey : nil diff --git a/Sources/HDWalletKit/Mnemonic/Mnemonic.swift b/Sources/HDWalletKit/Mnemonic/Mnemonic.swift index 932911e..dc50ea9 100755 --- a/Sources/HDWalletKit/Mnemonic/Mnemonic.swift +++ b/Sources/HDWalletKit/Mnemonic/Mnemonic.swift @@ -47,8 +47,7 @@ public final class Mnemonic { guard let salt = ("mnemonic" + passphrase).decomposedStringWithCompatibilityMapping.data(using: .utf8) else { fatalError("Nomalizing salt failed in \(self)") } - - return Crypto.PBKDF2SHA512(password: password.bytes, salt: salt.bytes) + return Crypto.PBKDF2SHA512(password: [UInt8](password), salt: [UInt8](salt)) } }