From 04b3cfef90db278e1c4f9dcc515b93842902f9bf Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 31 Jan 2023 15:35:08 +0100 Subject: [PATCH 1/2] Let's spell stripped correctly --- Sources/Web3Core/Utility/Utilities.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Web3Core/Utility/Utilities.swift b/Sources/Web3Core/Utility/Utilities.swift index 7b0557f6a..5a71cdd04 100644 --- a/Sources/Web3Core/Utility/Utilities.swift +++ b/Sources/Web3Core/Utility/Utilities.swift @@ -19,17 +19,17 @@ public struct Utilities { guard let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {return nil} return publicToAddressData(decompressedKey) } - var stipped = publicKey - if stipped.count == 65 { - if stipped[0] != 4 { + var stripped = publicKey + if stripped.count == 65 { + if stripped[0] != 4 { return nil } - stipped = stipped[1...64] + stripped = stripped[1...64] } - if stipped.count != 64 { + if stripped.count != 64 { return nil } - let sha3 = stipped.sha3(.keccak256) + let sha3 = stripped.sha3(.keccak256) let addressData = sha3[12...31] return addressData } From 4ec728f5a2d102b1b7409d1baa6323a01140f6bd Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu <36865532+JeneaVranceanu@users.noreply.github.com> Date: Wed, 1 Feb 2023 19:18:28 +0200 Subject: [PATCH 2/2] chore: refactoring of func publicToAddressData and docs --- Sources/Web3Core/Utility/Utilities.swift | 42 ++++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Sources/Web3Core/Utility/Utilities.swift b/Sources/Web3Core/Utility/Utilities.swift index 5a71cdd04..3d347e79a 100644 --- a/Sources/Web3Core/Utility/Utilities.swift +++ b/Sources/Web3Core/Utility/Utilities.swift @@ -10,34 +10,39 @@ import BigInt public struct Utilities { - /// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes) - /// or raw concat(X, Y) (64 bytes) format. + /// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes) + /// or uncompressed without prefix (64 bytes) format. /// - /// Returns 20 bytes of address data. + /// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes) + /// - Returns: 20 bytes of address data. static func publicToAddressData(_ publicKey: Data) -> Data? { + var publicKey = publicKey if publicKey.count == 33 { - guard let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {return nil} - return publicToAddressData(decompressedKey) - } - var stripped = publicKey - if stripped.count == 65 { - if stripped[0] != 4 { + guard (publicKey[0] == 2 || publicKey[0] == 3), + let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else { return nil } - stripped = stripped[1...64] + publicKey = decompressedKey } - if stripped.count != 64 { + + if publicKey.count == 65 { + guard publicKey[0] == 4 else { + return nil + } + publicKey = publicKey[1...64] + } else if publicKey.count != 64 { return nil } - let sha3 = stripped.sha3(.keccak256) + let sha3 = publicKey.sha3(.keccak256) let addressData = sha3[12...31] return addressData } - /// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes) - /// or raw concat(X, Y) (64 bytes) format. + /// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes) + /// or uncompressed without prefix (64 bytes) format. /// - /// Returns the EthereumAddress object. + /// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes) + /// - Returns: `EthereumAddress` object. public static func publicToAddress(_ publicKey: Data) -> EthereumAddress? { guard let addressData = publicToAddressData(publicKey) else {return nil} let address = addressData.toHexString().addHexPrefix().lowercased() @@ -50,10 +55,11 @@ public struct Utilities { return publicKey } - /// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes) - /// or raw concat(X, Y) (64 bytes) format. + /// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes) + /// or uncompressed without prefix (64 bytes) format. /// - /// Returns a 0x prefixed hex string. + /// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes) + /// - Returns: `0x` prefixed hex string. public static func publicToAddressString(_ publicKey: Data) -> String? { guard let addressData = Utilities.publicToAddressData(publicKey) else {return nil} let address = addressData.toHexString().addHexPrefix().lowercased()