mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 22:55:00 +00:00
Refactor wallet-related modules
This commit is contained in:
@@ -11,6 +11,7 @@ import MtProtoKitDynamic
|
||||
import TelegramPresentationData
|
||||
import TelegramUIPreferences
|
||||
import AccountContext
|
||||
import WalletUrl
|
||||
|
||||
public enum ParsedInternalPeerUrlParameter {
|
||||
case botStart(String)
|
||||
@@ -435,42 +436,3 @@ public func resolveInstantViewUrl(account: Account, url: String) -> Signal<Resol
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct ParsedWalletUrl {
|
||||
public let address: String
|
||||
public let amount: Int64?
|
||||
public let comment: String?
|
||||
}
|
||||
|
||||
private let invalidWalletAddressCharacters = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=").inverted
|
||||
private func isValidWalletAddress(_ address: String) -> Bool {
|
||||
if address.count != 48 || address.rangeOfCharacter(from: invalidWalletAddressCharacters) != nil {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public func parseWalletUrl(_ url: URL) -> ParsedWalletUrl? {
|
||||
guard url.scheme == "ton" && url.host == "transfer" else {
|
||||
return nil
|
||||
}
|
||||
var address: String?
|
||||
let path = url.path.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
||||
if isValidWalletAddress(path) {
|
||||
address = path
|
||||
}
|
||||
var amount: Int64?
|
||||
var comment: String?
|
||||
if let query = url.query, let components = URLComponents(string: "/?" + query), let queryItems = components.queryItems {
|
||||
for queryItem in queryItems {
|
||||
if let value = queryItem.value {
|
||||
if queryItem.name == "amount", !value.isEmpty, let amountValue = Int64(value) {
|
||||
amount = amountValue
|
||||
} else if queryItem.name == "text", !value.isEmpty {
|
||||
comment = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return address.flatMap { ParsedWalletUrl(address: $0, amount: amount, comment: comment) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user