Skip to content

Commit

Permalink
Validate amount before pay or withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
wuyuehyang committed Oct 18, 2022
1 parent cdc8cb0 commit 762fe11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Mixin/UserInterface/Windows/UrlWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class UrlWindow {
guard let assetId = query["asset"], let amount = query["amount"], let traceId = query["trace"], let addressId = query["address"] else {
return false
}
guard !assetId.isEmpty && UUID(uuidString: assetId) != nil && !traceId.isEmpty && UUID(uuidString: traceId) != nil && !addressId.isEmpty && UUID(uuidString: addressId) != nil && !amount.isEmpty else {
guard !assetId.isEmpty && UUID(uuidString: assetId) != nil && !traceId.isEmpty && UUID(uuidString: traceId) != nil && !addressId.isEmpty && UUID(uuidString: addressId) != nil && !amount.isEmpty && AmountFormatter.isValid(amount) else {
return false
}
var memo = query["memo"]
Expand Down Expand Up @@ -484,7 +484,7 @@ class UrlWindow {
showAutoHiddenHud(style: .error, text: R.string.localizable.invalid_payment_link())
return true
}
guard !recipientId.isEmpty && UUID(uuidString: recipientId) != nil && !assetId.isEmpty && UUID(uuidString: assetId) != nil && !amount.isEmpty && amount.isGenericNumber else {
guard !recipientId.isEmpty && UUID(uuidString: recipientId) != nil && !assetId.isEmpty && UUID(uuidString: assetId) != nil && !amount.isEmpty && amount.isGenericNumber && AmountFormatter.isValid(amount) else {
Logger.general.error(category: "PayURL", message: "Invalid URL: \(url)")
showAutoHiddenHud(style: .error, text: R.string.localizable.invalid_payment_link())
return true
Expand Down
11 changes: 11 additions & 0 deletions MixinServices/MixinServices/Foundation/AmountFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ public enum AmountFormatter {
}
}

public static func isValid(_ amount: String) -> Bool {
let parts = amount.components(separatedBy: ".")
if parts.count == 1 {
return true
} else if parts.count == 2 {
return parts[1].count <= 8
} else {
return false
}
}

}

0 comments on commit 762fe11

Please sign in to comment.