Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #4744: Add checksum validation for send token address. (#5275)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu authored Apr 22, 2022
1 parent 120c314 commit 768d6cb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion BraveWallet/Crypto/BuySendSwap/SendTokenView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct SendTokenView: View {
return sendAmount == 0
|| sendAmount > balance
|| sendTokenStore.sendAmount.isEmpty
|| sendTokenStore.addressError != nil
|| (sendTokenStore.addressError != nil && sendTokenStore.addressError != .missingChecksum)
}

var body: some View {
Expand Down
23 changes: 22 additions & 1 deletion BraveWallet/Crypto/Stores/SendTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class SendTokenStore: ObservableObject {
case sameAsFromAddress
case contractAddress
case notEthAddress
case missingChecksum
case invalidChecksum

var errorDescription: String? {
switch self {
Expand All @@ -51,6 +53,10 @@ public class SendTokenStore: ObservableObject {
return Strings.Wallet.sendWarningAddressIsContract
case .notEthAddress:
return Strings.Wallet.sendWarningAddressNotValid
case .missingChecksum:
return Strings.Wallet.sendWarningAddressMissingChecksumInfo
case .invalidChecksum:
return Strings.Wallet.sendWarningAddressInvalidChecksum
}
}
}
Expand Down Expand Up @@ -178,14 +184,29 @@ public class SendTokenStore: ObservableObject {
}
let normalizedSendAddress = sendAddress.lowercased()
if !sendAddress.isETHAddress {
// 1. check if send address is a valid eth address
addressError = .notEthAddress
} else if currentAccountAddress?.lowercased() == normalizedSendAddress {
// 2. check if send address is the same as the from address
addressError = .sameAsFromAddress
} else if (userAssets.first(where: { $0.contractAddress.lowercased() == normalizedSendAddress }) != nil)
|| (allTokens.first(where: { $0.contractAddress.lowercased() == normalizedSendAddress }) != nil) {
// 3. check if send address is a contract address
addressError = .contractAddress
} else {
addressError = nil
keyringService.checksumEthAddress(sendAddress) { [self] checksumAddress in
if sendAddress == checksumAddress {
// 4. check if send address is the same as the checksum address from the `KeyringService`
addressError = nil
} else if sendAddress.removingHexPrefix.lowercased() == sendAddress.removingHexPrefix || sendAddress.removingHexPrefix.uppercased() == sendAddress.removingHexPrefix {
// 5. check if send address has each of the alphabetic character as uppercase, or has each of
// the alphabeic character as lowercase
addressError = .missingChecksum
} else {
// 6. send address has mixed with uppercase and lowercase and does not match with the checksum address
addressError = .invalidChecksum
}
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions BraveWallet/WalletStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,20 @@ extension Strings {
value: "Not a valid ETH address",
comment: "A warning that appears below the send crypto address text field, when the input `To` address is not a valid ETH address."
)
public static let sendWarningAddressMissingChecksumInfo = NSLocalizedString(
"wallet.sendWarningAddressMissingChecksumInfo",
tableName: "BraveWallet",
bundle: .braveWallet,
value: "This address cannot be verified (missing checksum). Proceed?",
comment: "A warning that appears below the send crypto address text field, when the input `To` address is missing checksum information."
)
public static let sendWarningAddressInvalidChecksum = NSLocalizedString(
"wallet.sendWarningAddressInvalidChecksum",
tableName: "BraveWallet",
bundle: .braveWallet,
value: "Address did not pass verification (invalid checksum). Please try again, replacing lowercase letters with uppercase.",
comment: "A warning that appears below the send crypto address text field, when the input `To` address has invalid checksum."
)
public static let betaLabel = NSLocalizedString(
"wallet.betaLabel",
tableName: "BraveWallet",
Expand Down

0 comments on commit 768d6cb

Please sign in to comment.