-
Notifications
You must be signed in to change notification settings - Fork 181
Feat/to checksum address #228
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
Feat/to checksum address #228
Conversation
func toChecksumAddress() -> String { | ||
let lowerCaseAddress = self.stripHexPrefix().lowercased() | ||
let arr = Array(lowerCaseAddress) | ||
let keccaf = Array(lowerCaseAddress.web3.keccak256.web3.hexString.stripHexPrefix()) | ||
var result = "0x" | ||
for i in 0 ... lowerCaseAddress.count - 1 { | ||
if let val = Int(String(keccaf[i]), radix: 16), val >= 8 { | ||
result.append(arr[i].uppercased()) | ||
} else { | ||
result.append(arr[i]) | ||
} | ||
} | ||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an method in EthereumAddress
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
public extension String { | ||
func stripHexPrefix() -> String { | ||
if hasPrefix("0x") { | ||
let indexStart = index(startIndex, offsetBy: 2) | ||
return String(self[indexStart...]) | ||
} | ||
return self | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have an extension on string noHexPrefix
, why did you add this one here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my bad,removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dhruv500
to checksum address func added in string extension