From 023e71cdd50322f96d24350d4f62d9cde1ae78bd Mon Sep 17 00:00:00 2001 From: James Sangalli Date: Mon, 14 May 2018 11:21:37 +0800 Subject: [PATCH] handle new ticket format --- Trust/Foundation/AssetDefinitionXML.swift | 2 +- Trust/Foundation/XMLHandler.swift | 13 +++++++------ .../Coordinators/UniversalLinkCoordinator.swift | 3 ++- Trust/Tokens/Helpers/TicketAdaptor.swift | 4 +++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Trust/Foundation/AssetDefinitionXML.swift b/Trust/Foundation/AssetDefinitionXML.swift index cdff771b0..9968512d1 100644 --- a/Trust/Foundation/AssetDefinitionXML.swift +++ b/Trust/Foundation/AssetDefinitionXML.swift @@ -11,7 +11,7 @@ import Foundation class AssetDefinitionXML { private static let xmlInputStream = InputStream(fileAtPath: "./contracts/AssetDefinition.xml") // swiftlint:disable:this - public static let assetDefinition = "\n" + + public static let assetDefinition = "\n" + "\n" + "\n" + "\n" + diff --git a/Trust/Foundation/XMLHandler.swift b/Trust/Foundation/XMLHandler.swift index 76c95a79b..4f471e953 100644 --- a/Trust/Foundation/XMLHandler.swift +++ b/Trust/Foundation/XMLHandler.swift @@ -22,13 +22,14 @@ public class XMLHandler { let tokenHex = MarketQueueHandler.bytesToHexa(tokenBytes32.serialize().bytes) let location = getLocality(attribute: tokenHex.substring(to: 2), lang: lang) let venue = getVenue(attribute: tokenHex.substring(with: Range(uncheckedBounds: (2, 4))), lang: lang) - let time = Int(tokenHex.substring(with: Range(uncheckedBounds: (4, 12))), radix: 16)! + let time = Int(tokenHex.substring(with: Range(uncheckedBounds: (4, 12))), radix: 16) + ?? Int(Date.tomorrow.timeIntervalSince1970.rounded(to: 0)) //translatable to ascii let countryA = tokenHex.substring(with: Range(uncheckedBounds: (12, 18))).hexa2Bytes let countryB = tokenHex.substring(with: Range(uncheckedBounds: (18, 24))).hexa2Bytes - let match = Int(tokenHex.substring(with: Range(uncheckedBounds: (24, 26))), radix: 16)! - let category = Int(tokenHex.substring(with: Range(uncheckedBounds: (26, 28))), radix: 16)! - let number = Int(tokenHex.substring(with: Range(uncheckedBounds: (28, 32))), radix: 16)! + let match = Int(tokenHex.substring(with: Range(uncheckedBounds: (24, 26))), radix: 16) ?? 0 + let category = Int(tokenHex.substring(with: Range(uncheckedBounds: (26, 28))), radix: 16) ?? 0 + let number = Int(tokenHex.substring(with: Range(uncheckedBounds: (28, 32))), radix: 16) ?? 0 return Ticket( id: MarketQueueHandler.bytesToHexa(tokenId.serialize().array), @@ -78,7 +79,7 @@ public class XMLHandler { } func getLocality(attribute: String, lang: Int) -> String { - //TODO find out why - 1 + //entity keys start at 1 but xml finder starts at 0, hence -1 let locality = Int(attribute, radix: 16)! - 1 if let parsedLocality = xml["asset"]["fields"]["field"][0][0]["mapping"]["entity"][locality]["name"][lang].text { return parsedLocality @@ -87,7 +88,7 @@ public class XMLHandler { } func getVenue(attribute: String, lang: Int) -> String { - let venueNumber = Int(attribute, radix: 16)! + let venueNumber = Int(attribute, radix: 16)! - 1 if let parsedVenue = xml["asset"]["fields"]["field"][1][0]["mapping"]["entity"][venueNumber]["name"][lang].text { return parsedVenue } diff --git a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift index bdae361e1..5f69ee25e 100644 --- a/Trust/Market/Coordinators/UniversalLinkCoordinator.swift +++ b/Trust/Market/Coordinators/UniversalLinkCoordinator.swift @@ -221,7 +221,8 @@ class UniversalLinkCoordinator: Coordinator { var tickets = [Ticket]() let xmlHandler = XMLHandler() for i in 0...bytes32Tickets.count - 1 { - if let tokenId = BigUInt(bytes32Tickets[i], radix: 16) { + let ticket = bytes32Tickets[i] + if let tokenId = BigUInt(ticket, radix: 16) { let ticket = xmlHandler.getFifaInfoForTicket(tokenId: tokenId, index: UInt16(i)) tickets.append(ticket) } diff --git a/Trust/Tokens/Helpers/TicketAdaptor.swift b/Trust/Tokens/Helpers/TicketAdaptor.swift index 6cd9aecbc..182cbb828 100644 --- a/Trust/Tokens/Helpers/TicketAdaptor.swift +++ b/Trust/Tokens/Helpers/TicketAdaptor.swift @@ -25,7 +25,9 @@ class TicketAdaptor { if id == "0x0000000000000000000000000000000000000000000000000000000000000000" { // if balance is 0, then skip continue } - let ticket = getTicket(for: BigUInt(id.substring(from: 2), radix: 16)!, index: UInt16(index), in: token) + //TODO shouldn't have to do this when parser is dynamic + let ticketData = id.substring(from: 2) + let ticket = getTicket(for: BigUInt(ticketData.substring(from: 32), radix: 16)!, index: UInt16(index), in: token) tickets.append(ticket) }