@@ -10,34 +10,39 @@ import BigInt
10
10
11
11
public struct Utilities {
12
12
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.
15
15
///
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.
17
18
static func publicToAddressData( _ publicKey: Data ) -> Data ? {
19
+ var publicKey = publicKey
18
20
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 {
25
23
return nil
26
24
}
27
- stripped = stripped [ 1 ... 64 ]
25
+ publicKey = decompressedKey
28
26
}
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 {
30
34
return nil
31
35
}
32
- let sha3 = stripped . sha3 ( . keccak256)
36
+ let sha3 = publicKey . sha3 ( . keccak256)
33
37
let addressData = sha3 [ 12 ... 31 ]
34
38
return addressData
35
39
}
36
40
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.
39
43
///
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.
41
46
public static func publicToAddress( _ publicKey: Data ) -> EthereumAddress ? {
42
47
guard let addressData = publicToAddressData ( publicKey) else { return nil }
43
48
let address = addressData. toHexString ( ) . addHexPrefix ( ) . lowercased ( )
@@ -50,10 +55,11 @@ public struct Utilities {
50
55
return publicKey
51
56
}
52
57
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.
55
60
///
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.
57
63
public static func publicToAddressString( _ publicKey: Data ) -> String ? {
58
64
guard let addressData = Utilities . publicToAddressData ( publicKey) else { return nil }
59
65
let address = addressData. toHexString ( ) . addHexPrefix ( ) . lowercased ( )
0 commit comments