Skip to content

Let's spell stripped correctly #747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions Sources/Web3Core/Utility/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 stipped = publicKey
if stipped.count == 65 {
if stipped[0] != 4 {
guard (publicKey[0] == 2 || publicKey[0] == 3),
let decompressedKey = SECP256K1.combineSerializedPublicKeys(keys: [publicKey], outputCompressed: false) else {
return nil
}
stipped = stipped[1...64]
publicKey = decompressedKey
}
if stipped.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 = stipped.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()
Expand All @@ -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()
Expand Down