Skip to content

Commit

Permalink
Merge branch 'feature/pwn-626' into release/2.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Elizaveta Semenova committed Dec 29, 2023
2 parents 99ceccd + fafe4c6 commit 150a4f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
15 changes: 15 additions & 0 deletions p2p_wallet/Common/Extensions/Double+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,18 @@ extension Double {
"\(toString(maximumFractionDigits: maximumFractionDigits, roundingMode: roundingMode)) \(currency.code)"
}
}

extension Double {
// Check if lamport value is smaller than UInt64 max
func isLamportsBiggerThanUInt64(decimals: Int) -> Bool {
let value = (self * Double.pow(10, Double(decimals))).rounded()
let maxValue = Decimal(UInt64.max)

if Decimal(value) > maxValue {
debugPrint("\(value) overflows Int64 max \(maxValue)")
return true
}

return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ struct SendInputAmountField: UIViewRepresentable {

let number = Double(updatedText.replacingOccurrences(of: " ", with: ""))

if number?.isLamportsBiggerThanUInt64(decimals: countAfterDecimalPoint) == true {
return false
}

if (string.isEmpty || string.starts(with: "0"))
&& updatedText.starts(with: "0\(decimalSeparator)")
&& number == 0
Expand Down
11 changes: 10 additions & 1 deletion p2p_wallet/UI/SwiftUI/TextField/AmountTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,21 @@ final class AmountUITextField: UITextField, UITextFieldDelegate {
let updatedText = text
.replacingCharacters(in: textRange, with: string)
.replacingOccurrences(of: decimalSeparator == "." ? "," : ".", with: decimalSeparator)

if (updatedText.components(separatedBy: decimalSeparator).count - 1) > 1 {
return false
}
if updatedText.components(separatedBy: decimalSeparator).last?.count ?? 0 > maxFractionDigits.wrappedValue {

if updatedText.contains(decimalSeparator) && updatedText.components(separatedBy: decimalSeparator).last?
.count ?? 0 > maxFractionDigits.wrappedValue
{
return false
}

if Double(updatedText)?.isLamportsBiggerThanUInt64(decimals: maxFractionDigits.wrappedValue) == true {
return false
}

return isNotMoreThanMax(text: updatedText.amountFormat(
maxAfterComma: maxFractionDigits.wrappedValue,
decimalSeparator: decimalSeparator
Expand Down

0 comments on commit 150a4f9

Please sign in to comment.