Merge pull request #349 from James-Sangalli/fixFailedParsingOnOldERC875Tokens

guard against new and old tickets in ethereum
pull/353/head
James Sangalli 7 years ago committed by GitHub
commit a5bf00f0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      Trust/Foundation/XMLHandler.swift
  2. 6
      Trust/Tokens/Helpers/TicketAdaptor.swift

@ -14,8 +14,7 @@ public class XMLHandler {
private let xml = try! XML.parse(AssetDefinitionXML.assetDefinition) private let xml = try! XML.parse(AssetDefinitionXML.assetDefinition)
private func formatDateToMoscow(_ timestamp: Int) -> Date private func formatDateToMoscow(_ timestamp: Int) -> Date {
{
let formatter = DateFormatter() let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 10800) formatter.timeZone = TimeZone(secondsFromGMT: 10800)
formatter.dateFormat = "dd/MM/yyyy hh:mm:ss a" formatter.dateFormat = "dd/MM/yyyy hh:mm:ss a"
@ -27,6 +26,20 @@ public class XMLHandler {
return unFormattedDate return unFormattedDate
} }
//TODO remove once parser is properly dynamic
public static func parseTicket(ticket: String) -> String
{
let no0xTicket = ticket.substring(from: 2)
let firstHalfOfTicket = no0xTicket.substring(to: 32)
let bigUIntFirstHalf = BigUInt(firstHalfOfTicket, radix: 16)
if bigUIntFirstHalf == 0 {
return no0xTicket
}
//if first 16 bytes are not empty then cut it in half
//else return with padded 0's
return no0xTicket.substring(from: 32)
}
func getFifaInfoForTicket(tokenId tokenBytes32: BigUInt, index: UInt16) -> Ticket { func getFifaInfoForTicket(tokenId tokenBytes32: BigUInt, index: UInt16) -> Ticket {
//check if leading or trailing zeros //check if leading or trailing zeros
let tokenId = tokenBytes32 let tokenId = tokenBytes32
@ -99,14 +112,17 @@ public class XMLHandler {
func getLocality(attribute: String, lang: Int) -> String { func getLocality(attribute: String, lang: Int) -> String {
//entity keys start at 1 but xml finder starts at 0, hence -1 //entity keys start at 1 but xml finder starts at 0, hence -1
let locality = Int(attribute, radix: 16)! - 1 if let locality = Int(attribute, radix: 16) {
if let parsedLocality = xml["asset"]["fields"]["field"][0][0]["mapping"]["entity"][locality]["name"][lang].text { guard locality != 0 else { return "N/A" }
return parsedLocality if let parsedLocality = xml["asset"]["fields"]["field"][0][0]["mapping"]["entity"][locality - 1]["name"][lang].text {
return parsedLocality
}
} }
return "N/A" return "N/A"
} }
func getCategory(_ cat: Int, lang: Int) -> String { func getCategory(_ cat: Int, lang: Int) -> String {
guard cat != 0 else { return "N/A" }
if let category = xml["asset"]["fields"]["field"][6][0]["mapping"]["entity"][cat - 1]["name"][lang].text { if let category = xml["asset"]["fields"]["field"][6][0]["mapping"]["entity"][cat - 1]["name"][lang].text {
return category return category
} }
@ -114,9 +130,11 @@ public class XMLHandler {
} }
func getVenue(attribute: String, lang: Int) -> String { func getVenue(attribute: String, lang: Int) -> String {
let venueNumber = Int(attribute, radix: 16)! - 1 if let venueNumber = Int(attribute, radix: 16) {
if let parsedVenue = xml["asset"]["fields"]["field"][1][0]["mapping"]["entity"][venueNumber]["name"][lang].text { guard venueNumber != 0 else { return "N/A" }
return parsedVenue if let parsedVenue = xml["asset"]["fields"]["field"][1][0]["mapping"]["entity"][venueNumber - 1]["name"][lang].text {
return parsedVenue
}
} }
return "N/A" return "N/A"
} }

@ -22,12 +22,12 @@ class TicketAdaptor {
for (index, item) in balance.enumerated() { for (index, item) in balance.enumerated() {
//id is the value of the bytes32 ticket //id is the value of the bytes32 ticket
let id = item.balance let id = item.balance
if id == "0x0000000000000000000000000000000000000000000000000000000000000000" { // if balance is 0, then skip if id == Constants.nullTicket { // if balance is 0, then skip
continue continue
} }
//TODO shouldn't have to do this when parser is dynamic //TODO shouldn't have to do this when parser is dynamic
let ticketData = id.substring(from: 2) let ticketData = XMLHandler.parseTicket(ticket: id)
let ticket = getTicket(for: BigUInt(ticketData.substring(from: 32), radix: 16)!, index: UInt16(index), in: token) let ticket = getTicket(for: BigUInt(ticketData, radix: 16)!, index: UInt16(index), in: token)
tickets.append(ticket) tickets.append(ticket)
} }

Loading…
Cancel
Save