Skip to content

Commit 4ec728f

Browse files
chore: refactoring of func publicToAddressData and docs
1 parent 04b3cfe commit 4ec728f

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

Sources/Web3Core/Utility/Utilities.swift

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,39 @@ import BigInt
1010

1111
public struct Utilities {
1212

13-
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
14-
/// or raw concat(X, Y) (64 bytes) format.
13+
/// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes)
14+
/// or uncompressed without prefix (64 bytes) format.
1515
///
16-
/// Returns 20 bytes of address data.
16+
/// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes)
17+
/// - Returns: 20 bytes of address data.
1718
static func publicToAddressData(_ publicKey: Data) -> Data? {
19+
var publicKey = publicKey
1820
if publicKey.count == 33 {
19-
guard let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {return nil}
20-
return publicToAddressData(decompressedKey)
21-
}
22-
var stripped = publicKey
23-
if stripped.count == 65 {
24-
if stripped[0] != 4 {
21+
guard (publicKey[0] == 2 || publicKey[0] == 3),
22+
let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {
2523
return nil
2624
}
27-
stripped = stripped[1...64]
25+
publicKey = decompressedKey
2826
}
29-
if stripped.count != 64 {
27+
28+
if publicKey.count == 65 {
29+
guard publicKey[0] == 4 else {
30+
return nil
31+
}
32+
publicKey = publicKey[1...64]
33+
} else if publicKey.count != 64 {
3034
return nil
3135
}
32-
let sha3 = stripped.sha3(.keccak256)
36+
let sha3 = publicKey.sha3(.keccak256)
3337
let addressData = sha3[12...31]
3438
return addressData
3539
}
3640

37-
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
38-
/// or raw concat(X, Y) (64 bytes) format.
41+
/// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes)
42+
/// or uncompressed without prefix (64 bytes) format.
3943
///
40-
/// Returns the EthereumAddress object.
44+
/// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes)
45+
/// - Returns: `EthereumAddress` object.
4146
public static func publicToAddress(_ publicKey: Data) -> EthereumAddress? {
4247
guard let addressData = publicToAddressData(publicKey) else {return nil}
4348
let address = addressData.toHexString().addHexPrefix().lowercased()
@@ -50,10 +55,11 @@ public struct Utilities {
5055
return publicKey
5156
}
5257

53-
/// Convert a public key to the corresponding EthereumAddress. Accepts public keys in compressed (33 bytes), non-compressed (65 bytes)
54-
/// or raw concat(X, Y) (64 bytes) format.
58+
/// Convert a public key to the corresponding ``EthereumAddress``. Accepts public keys in compressed (33 bytes), uncompressed (65 bytes)
59+
/// or uncompressed without prefix (64 bytes) format.
5560
///
56-
/// Returns a 0x prefixed hex string.
61+
/// - Parameter publicKey: compressed 33, non-compressed (65 bytes) or non-compressed without prefix (64 bytes)
62+
/// - Returns: `0x` prefixed hex string.
5763
public static func publicToAddressString(_ publicKey: Data) -> String? {
5864
guard let addressData = Utilities.publicToAddressData(publicKey) else {return nil}
5965
let address = addressData.toHexString().addHexPrefix().lowercased()

0 commit comments

Comments
 (0)