From 7d7c21bba1666d62d49e58f3f25d902c8d5a1985 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Mon, 8 Sep 2025 21:49:47 +0800 Subject: [PATCH 1/4] Fix: .bytes -> [UInt8] --- Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift | 8 +++----- .../Encryption/EllipticCurveEncrypterSecp256k1.swift | 5 ++--- Sources/HDWalletKit/Mnemonic/Mnemonic.swift | 3 +-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift index 201bc32..7bd6e1b 100755 --- a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift +++ b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift @@ -46,13 +46,11 @@ public final class ECDSA { guard let ctx = secp256k1_context_create(UInt32(SECP256K1_CONTEXT_VERIFY)) else { return false } 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); diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift index 2f6f0e1..8a4cbbe 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] = Array(privateKey.bytes) var publickKey = secp256k1_pubkey() _ = SecpResult(secp256k1_ec_pubkey_create(context, &publickKey, privateKey)) return publickKey @@ -95,9 +95,8 @@ public class EllipticCurveEncrypterSecp256k1 { /// - Returns: public key structure or nil, if signature invalid public func publicKey(signature: inout secp256k1_ecdsa_recoverable_signature, hash: Data) -> secp256k1_pubkey? { 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)) + let status = SecpResult(secp256k1_ecdsa_recover(context, &outPubKey, &signature, [UInt8](hash.bytes))) 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)) } } From 4b9d2d366557a097c4d892da8f96e5f52203e2f7 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Mon, 8 Sep 2025 21:54:17 +0800 Subject: [PATCH 2/4] Fix: .bytes -> [UInt8] --- .../Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/EllipticCurveEncrypterSecp256k1.swift index 8a4cbbe..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: [UInt8] = Array(privateKey.bytes) + let privateKey = [UInt8](privateKey) var publickKey = secp256k1_pubkey() _ = SecpResult(secp256k1_ec_pubkey_create(context, &publickKey, privateKey)) return publickKey @@ -94,9 +94,10 @@ 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") var outPubKey = secp256k1_pubkey() - let status = SecpResult(secp256k1_ecdsa_recover(context, &outPubKey, &signature, [UInt8](hash.bytes))) + let status = SecpResult(secp256k1_ecdsa_recover(context, &outPubKey, &signature, hash)) return status == .success ? outPubKey : nil } From b3045d7ed7577c9b97c1194c473bf6f66903f84f Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Mon, 8 Sep 2025 23:28:31 +0800 Subject: [PATCH 3/4] Fix potential memory leak. --- Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift index 7bd6e1b..b534478 100755 --- a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift +++ b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift @@ -44,6 +44,7 @@ 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, [UInt8](sigData), sigData.count) @@ -53,7 +54,6 @@ public final class ECDSA { if (secp256k1_ecdsa_verify(ctx, &signature, [UInt8](message), &pubkey) != 1) { return false }; - secp256k1_context_destroy(ctx); return true } } From 3591c59ed2c5bf443495dd706338bc0fd2544068 Mon Sep 17 00:00:00 2001 From: Kai Luo Date: Tue, 9 Sep 2025 12:05:50 +0800 Subject: [PATCH 4/4] Remove unnecessary semicolon. --- Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift index b534478..8aeae38 100755 --- a/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift +++ b/Sources/HDWalletKit/Core/Crypto/Encryption/ECDSA.swift @@ -44,7 +44,7 @@ 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); } + defer { secp256k1_context_destroy(ctx) } var pubkey = secp256k1_pubkey() var signature = secp256k1_ecdsa_signature() secp256k1_ecdsa_signature_parse_der(ctx, &signature, [UInt8](sigData), sigData.count)