From 51c066b8f5f1fc52a7fc92bfe1e9fd2338d62ebe Mon Sep 17 00:00:00 2001 From: James Sangalli Date: Wed, 11 Apr 2018 14:21:15 +0800 Subject: [PATCH 1/3] write base class for xml parser of asset definition --- Podfile | 1 + Podfile.lock | 10 +++- Trust.xcodeproj/project.pbxproj | 14 +++++ Trust/Foundation/AssetDefinitionXML.swift | 14 +++++ Trust/Foundation/XMLHandler.swift | 61 ++++++++++++++++++++++ Trust/Info.plist | 2 +- Trust/Market/UniversalLinkHandler.swift | 3 +- TrustTests/Foundation/XMLHandlerTest.swift | 22 ++++++++ 8 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 Trust/Foundation/AssetDefinitionXML.swift create mode 100644 Trust/Foundation/XMLHandler.swift create mode 100644 TrustTests/Foundation/XMLHandlerTest.swift diff --git a/Podfile b/Podfile index 91921452c..90e910f41 100644 --- a/Podfile +++ b/Podfile @@ -24,6 +24,7 @@ target 'AlphaWallet' do pod 'CryptoSwift', :git=>'https://github.com/krzyzanowskim/CryptoSwift', :branch=>'master' pod 'Fabric' pod 'Crashlytics' + pod "SwiftyXMLParser", :git => 'https://github.com/yahoojapan/SwiftyXMLParser.git' pod 'Kingfisher', '~> 4.0' pod 'TrustKeystore', :git=>'https://github.com/alpha-wallet/trust-keystore-snapshot', :branch=>'master' pod 'Branch' diff --git a/Podfile.lock b/Podfile.lock index bbc5d990f..cd0372309 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -43,6 +43,7 @@ PODS: - StatefulViewController (3.0) - SwiftLint (0.24.1) - SwiftyJSON (4.0.0) + - SwiftyXMLParser (4.1.0) - TrezorCrypto (0.0.4) - TrustKeystore (0.2.0): - BigInt @@ -74,6 +75,7 @@ DEPENDENCIES: - StatefulViewController - SwiftLint - SwiftyJSON + - SwiftyXMLParser (from `https://github.com/yahoojapan/SwiftyXMLParser.git`) - TrustKeystore (from `https://github.com/alpha-wallet/trust-keystore-snapshot`, branch `master`) EXTERNAL SOURCES: @@ -85,6 +87,8 @@ EXTERNAL SOURCES: QRCodeReaderViewController: :branch: master :git: https://github.com/yannickl/QRCodeReaderViewController.git + SwiftyXMLParser: + :git: https://github.com/yahoojapan/SwiftyXMLParser.git TrustKeystore: :branch: master :git: https://github.com/alpha-wallet/trust-keystore-snapshot @@ -99,6 +103,9 @@ CHECKOUT OPTIONS: QRCodeReaderViewController: :commit: 80bd79cbeede842949b229f81bc6328f53701c5a :git: https://github.com/yannickl/QRCodeReaderViewController.git + SwiftyXMLParser: + :commit: 1ddcad0a09efb85f2325d994c2169cd9ba116ae3 + :git: https://github.com/yahoojapan/SwiftyXMLParser.git TrustKeystore: :commit: 71cf8592d88c770081bd4e24ad1bbdd06bb63303 :git: https://github.com/alpha-wallet/trust-keystore-snapshot @@ -133,9 +140,10 @@ SPEC CHECKSUMS: StatefulViewController: 4803bf900d44de26074344998e10e041113b5931 SwiftLint: 2e4b89feed5909c42c3735bbd6745f4345c4b772 SwiftyJSON: 070dabdcb1beb81b247c65ffa3a79dbbfb3b48aa + SwiftyXMLParser: b5a79fb8a730903703d0a92d30752a0b05158967 TrezorCrypto: 7f87a2296cf13872b21565c29fd9f7882dd00803 TrustKeystore: 381590c561f58fe0948abbd9cb626cbc08d75ff8 -PODFILE CHECKSUM: 53f98ad2d779a0137c05fe727a9e97f20552dae3 +PODFILE CHECKSUM: 224c249f366bf912993e5027df97fb05425c3ccc COCOAPODS: 1.3.1 diff --git a/Trust.xcodeproj/project.pbxproj b/Trust.xcodeproj/project.pbxproj index 45a6985c5..d3fc79288 100644 --- a/Trust.xcodeproj/project.pbxproj +++ b/Trust.xcodeproj/project.pbxproj @@ -441,6 +441,9 @@ AA893ED5203C3E5400CDCED1 /* TokenBalance.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA893ED4203C3E5400CDCED1 /* TokenBalance.swift */; }; AAEB8DA2204BC7B700CB0B2C /* RedeemTicketsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAEB8DA1204BC7B700CB0B2C /* RedeemTicketsViewController.swift */; }; AAEF2CAB2050A68A0038BE0D /* SignatureHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAEF2CAA2050A68A0038BE0D /* SignatureHelper.swift */; }; + B1DB1A95207DBF6D00CA2B77 /* XMLHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DB1A94207DBF6D00CA2B77 /* XMLHandler.swift */; }; + B1DB1A97207DCECF00CA2B77 /* AssetDefinitionXML.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DB1A96207DCECF00CA2B77 /* AssetDefinitionXML.swift */; }; + B1DB1A99207DDF9600CA2B77 /* XMLHandlerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DB1A98207DDF9600CA2B77 /* XMLHandlerTest.swift */; }; B1DC375D203AEAE200C9756D /* MarketQueueHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DC375C203AEAE100C9756D /* MarketQueueHandler.swift */; }; B1DC375F203AEB4800C9756D /* MarketQueueHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B1DC375E203AEB4800C9756D /* MarketQueueHandlerTests.swift */; }; BB5D6A9E20232EE8000FC5AB /* CurrencyRate+Fee.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5D6A9D20232EE8000FC5AB /* CurrencyRate+Fee.swift */; }; @@ -949,6 +952,9 @@ AAEB8DA1204BC7B700CB0B2C /* RedeemTicketsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RedeemTicketsViewController.swift; sourceTree = ""; }; AAEF2CAA2050A68A0038BE0D /* SignatureHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignatureHelper.swift; sourceTree = ""; }; B13A87D3BA5167741E5D0801 /* Pods-Trust.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Trust.release.xcconfig"; path = "Pods/Target Support Files/Pods-Trust/Pods-Trust.release.xcconfig"; sourceTree = ""; }; + B1DB1A94207DBF6D00CA2B77 /* XMLHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLHandler.swift; sourceTree = ""; }; + B1DB1A96207DCECF00CA2B77 /* AssetDefinitionXML.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssetDefinitionXML.swift; sourceTree = ""; }; + B1DB1A98207DDF9600CA2B77 /* XMLHandlerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLHandlerTest.swift; sourceTree = ""; }; B1DC375C203AEAE100C9756D /* MarketQueueHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarketQueueHandler.swift; sourceTree = ""; }; B1DC375E203AEB4800C9756D /* MarketQueueHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarketQueueHandlerTests.swift; sourceTree = ""; }; B2CF9CDF557F98DECE6D0AF6 /* Pods_AlphaWalletUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AlphaWalletUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1699,6 +1705,7 @@ 61FC5ED01FCFBDEB00CCB12A /* EtherNumberFormatterTests.swift */, 73D2683A202E8411009777A1 /* DecimalNumberFormatterTest.swift */, 73ED85A82034C42D00593BF3 /* StringFormatterTest.swift */, + B1DB1A98207DDF9600CA2B77 /* XMLHandlerTest.swift */, ); path = Foundation; sourceTree = ""; @@ -1838,6 +1845,8 @@ 298542F81FBE9A0100CB5081 /* CryptoAddressValidator.swift */, 61FC5ECE1FCFBAE500CCB12A /* EtherNumberFormatter.swift */, 73ED85A420349BE400593BF3 /* StringFormatter.swift */, + B1DB1A94207DBF6D00CA2B77 /* XMLHandler.swift */, + B1DB1A96207DCECF00CA2B77 /* AssetDefinitionXML.swift */, ); path = Foundation; sourceTree = ""; @@ -3068,6 +3077,7 @@ "${BUILT_PRODUCTS_DIR}/SipHash/SipHash.framework", "${BUILT_PRODUCTS_DIR}/StatefulViewController/StatefulViewController.framework", "${BUILT_PRODUCTS_DIR}/SwiftyJSON/SwiftyJSON.framework", + "${BUILT_PRODUCTS_DIR}/SwiftyXMLParser/SwiftyXMLParser.framework", "${BUILT_PRODUCTS_DIR}/TrezorCrypto/TrezorCrypto.framework", "${BUILT_PRODUCTS_DIR}/TrustKeystore/TrustKeystore.framework", "${BUILT_PRODUCTS_DIR}/secp256k1_ios/secp256k1_ios.framework", @@ -3097,6 +3107,7 @@ "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SipHash.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/StatefulViewController.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftyJSON.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftyXMLParser.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TrezorCrypto.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TrustKeystore.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/secp256k1_ios.framework", @@ -3156,6 +3167,7 @@ 77872D232023F43B0032D687 /* TransactionsTracker.swift in Sources */, 29FA00CE201CA64E002F7DC5 /* DappCommand.swift in Sources */, 29FF12F61F74799D00AFD326 /* NSAttributedString.swift in Sources */, + B1DB1A97207DCECF00CA2B77 /* AssetDefinitionXML.swift in Sources */, AA26C62320412A4100318B9B /* UIViewInspectableEnhancements.swift in Sources */, 290B2B651F91A4880053C83E /* TransactionsFooterView.swift in Sources */, 293B8B431F70815900356286 /* BalanceTitleView.swift in Sources */, @@ -3383,6 +3395,7 @@ 29F1C83E1FEB5C91003780D8 /* EditTokensViewController.swift in Sources */, 296AF9A31F733AB30058AF78 /* WalletCoordinator.swift in Sources */, 442FCBBFCC5926B4D416E6D3 /* GetNameCoordinator.swift in Sources */, + B1DB1A95207DBF6D00CA2B77 /* XMLHandler.swift in Sources */, 442FCB9850FC6F2D28978199 /* GetERC20Symbol.swift in Sources */, 442FC5F70AF003F331F7C841 /* GetSymbolCoordinator.swift in Sources */, 442FCB2B0DD39EB2D9233A2F /* GetStormBirdBalance.swift in Sources */, @@ -3556,6 +3569,7 @@ 29BDF19D1FEE50E90023A45F /* GasPriceConfigurationTests.swift in Sources */, 295996141FAB09A200DB66A8 /* DepositCoordinatorTests.swift in Sources */, 778EAF7D1FF10AF400C8E2AB /* SettingsCoordinatorTests.swift in Sources */, + B1DB1A99207DDF9600CA2B77 /* XMLHandlerTest.swift in Sources */, 732E0F502022716700B39C1F /* LockEnterPasscodeCoordinatorTest.swift in Sources */, 29F114E91FA3EC9E00114A29 /* InCoordinatorTests.swift in Sources */, 2981F4731F8303E600CA6590 /* TransactionCoordinatorTests.swift in Sources */, diff --git a/Trust/Foundation/AssetDefinitionXML.swift b/Trust/Foundation/AssetDefinitionXML.swift new file mode 100644 index 000000000..acd1dbf14 --- /dev/null +++ b/Trust/Foundation/AssetDefinitionXML.swift @@ -0,0 +1,14 @@ +// +// AssetDefinitionXML.swift +// AlphaWallet +// +// Created by James Sangalli on 11/4/18. +// + +import Foundation + +class AssetDefinitionXML { + + public static let assetDefinition = "
0xa3347C8ED134444C25169872c90a6360D42b3691
https://482kdh4npg.execute-api.ap-southeast-1.amazonaws.com/dev/ https://app.awallet.io/ город City 城市 Ciudad Москва́ Moscow 莫斯科 Moscú Санкт-Петербу́рг Saint Petersburg 圣彼得堡 San Petersburgo сочи Sochi 索契 Sochi екатеринбург Ekaterinburg 叶卡捷琳堡 Ekaterinburg Саранск Saransk 萨兰斯克 Saransk казань Kazan 喀山 Kazan Нижний Новгород Nizhny Novgorod 下诺夫哥罗德 Nizhny Novgorod Ростов-на-Дону Rostov-on-Don 顿河畔罗斯托夫 Rostov-on-Don Самара Samara 翅果 Samara Волгоград Volgograd 伏尔加格勒 Volgogrado Калининград Kaliningrad 加里宁格勒 Kaliningrad место встречи Venue 场馆 Lugar Стадион Калининград Kaliningrad Stadium 加里寧格勒體育場 Estadio de Kaliningrado Екатеринбург Арена Volgograd Arena 伏爾加格勒體育場 Volgogrado Arena Казань Арена Ekaterinburg Arena 加里宁格勒体育场 Ekaterimburgo Arena Мордовия Арена Fisht Stadium 菲什特奧林匹克體育場 Estadio Fisht Ростов Арена Kazan Arena 喀山體育場 Kazan Arena Самара Арена Nizhny Novgorod Stadium 下諾夫哥羅德體育場 Estadio de Nizhni Novgorod Стадион Калининград Luzhniki Stadium 卢日尼基体育场 Estadio Luzhniki Стадион Лужники Samara Arena 薩馬拉體育場 Samara Arena Стадион Нижний Новгород Rostov Arena 羅斯托夫體育場 Rostov Arena Стадион Спартак Spartak Stadium 斯巴達克體育場 Estadio del Spartak Стадион Санкт-Петербург Saint Petersburg Stadium 十字架體育場 Estadio de San Petersburgo Стадион Фишт Mordovia Arena 莫爾多維亞體育場 Mordovia Arena время Time 时间 Tiempo Team A 甲队 Equipo A Team B 乙队 Equipo B Match 场次 Evento Cat 等级 Cat Number 票号 Número
" + +} diff --git a/Trust/Foundation/XMLHandler.swift b/Trust/Foundation/XMLHandler.swift new file mode 100644 index 000000000..59995dc0b --- /dev/null +++ b/Trust/Foundation/XMLHandler.swift @@ -0,0 +1,61 @@ +// +// XMLHandler.swift +// AlphaWallet +// +// Created by James Sangalli on 11/4/18. +// + +import Foundation +import SwiftyXMLParser + +struct FIFAInfo { + let locale: String + let team: String + let venue: String + let time: Int + let countryA: Int + let countryB: Int + let match: Int + let category: Int + let number: Int +} + +public class XMLHandler { + + let xml = try! XML.parse(AssetDefinitionXML.assetDefinition) + + func getFifaInfoForToken(tokenId: String, lang: String) -> FIFAInfo { + let locale = getLocale(attribute: tokenId.substring(to: 2), lang: lang) + let team = getTeam(attribute: tokenId.substring(to: 8), lang: lang) + let venue = getVenue(attribute: tokenId.substring(to: 6), lang: lang) + let time = Int(tokenId.substring(with: Range((5, 12))), radix: 16)! + let countryA = Int(tokenId.substring(with: Range(13, 19)), radix: 16)! + let countryB = Int(tokenId.substring(with: Range(20, 26)), radix: 16)! + let match = Int(tokenId.substring(with: Range(27, 29)), radix: 16)! + let category = Int(tokenId.substring(with: Range(30, 32)), radix: 16)! + let number = Int(tokenId.substring(from: 33), radix: 16)! + return FIFAInfo + ( + locale: locale, + team: team, + venue: venue, + time: time, + countryA: countryA, + countryB: countryB, + match: match, + category: category, + number: number + ) + } + + func getLocale(attribute: String, lang: String) -> String { + let localeNumber = Int(attribute, radix: 16)! + return xml["field"]["locality"]["mapping"]["entity"][localeNumber][lang] + } + + func getVenue(attribute: String, lang: String) -> String { + let venueNumber = Int(attribute, radix: 16)! + return xml["field"]["venue"]["mapping"]["entity"][venueNumber][lang] + } + +} diff --git a/Trust/Info.plist b/Trust/Info.plist index f80503c4e..3874e49ef 100644 --- a/Trust/Info.plist +++ b/Trust/Info.plist @@ -28,7 +28,7 @@ CFBundleVersion - 161 + 162 Fabric APIKey diff --git a/Trust/Market/UniversalLinkHandler.swift b/Trust/Market/UniversalLinkHandler.swift index e36b56968..216805aff 100644 --- a/Trust/Market/UniversalLinkHandler.swift +++ b/Trust/Market/UniversalLinkHandler.swift @@ -30,7 +30,8 @@ import BigInt public class UniversalLinkHandler { public let urlPrefix = "https://app.awallet.io/" - public static let paymentServer = "https://feemaster.eastasia.cloudapp.azure.com:8080/api/claimToken" //"http://stormbird.duckdns.org:8080/api/claimToken" + public static let paymentServer = "https://feemaster.eastasia.cloudapp.azure.com:8080/api/claimToken" + //"http://stormbird.duckdns.org:8080/api/claimToken" //message is with 32 bytes each of price and expiry and is shortened for link func createUniversalLink(signedOrder: SignedOrder) -> String { diff --git a/TrustTests/Foundation/XMLHandlerTest.swift b/TrustTests/Foundation/XMLHandlerTest.swift new file mode 100644 index 000000000..6f0fcae94 --- /dev/null +++ b/TrustTests/Foundation/XMLHandlerTest.swift @@ -0,0 +1,22 @@ +// +// XMLHandlerTest.swift +// AlphaWalletTests +// +// Created by James Sangalli on 11/4/18. +// + +import Foundation +import XCTest +@testable import Trust + +class XMLHandlerTest: XCTestCase { + + func testParser() { + let fifaDetails = XMLHandler().getFifaInfoForToken( + tokenId: "0x0a015af6d74042544e43484e01010001", + lang: "en" + ) + XCTAssertNotNil(fifaDetails) + } + +} From 1d6c76b58888fdc00aaaa15eba7a14b4e8624a42 Mon Sep 17 00:00:00 2001 From: James Sangalli Date: Wed, 11 Apr 2018 16:13:09 +0800 Subject: [PATCH 2/3] handles xml parsing for asset definition --- Podfile | 2 +- Podfile.lock | 2 +- .../xcschemes/AlphaWallet - Europe.xcscheme | 4 +- .../xcschemes/AlphaWallet.xcscheme | 4 +- .../xcschemes/AlphaWalletTests.xcscheme | 4 +- .../xcschemes/AlphaWalletUITests.xcscheme | 4 +- Trust/Foundation/AssetDefinitionXML.swift | 1 + Trust/Foundation/XMLHandler.swift | 38 ++++++++++--------- TrustTests/Foundation/XMLHandlerTest.swift | 6 +-- 9 files changed, 35 insertions(+), 30 deletions(-) diff --git a/Podfile b/Podfile index 90e910f41..e18491373 100644 --- a/Podfile +++ b/Podfile @@ -24,7 +24,7 @@ target 'AlphaWallet' do pod 'CryptoSwift', :git=>'https://github.com/krzyzanowskim/CryptoSwift', :branch=>'master' pod 'Fabric' pod 'Crashlytics' - pod "SwiftyXMLParser", :git => 'https://github.com/yahoojapan/SwiftyXMLParser.git' + pod 'SwiftyXMLParser', :git => 'https://github.com/yahoojapan/SwiftyXMLParser.git' pod 'Kingfisher', '~> 4.0' pod 'TrustKeystore', :git=>'https://github.com/alpha-wallet/trust-keystore-snapshot', :branch=>'master' pod 'Branch' diff --git a/Podfile.lock b/Podfile.lock index cd0372309..6a4a1854b 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -144,6 +144,6 @@ SPEC CHECKSUMS: TrezorCrypto: 7f87a2296cf13872b21565c29fd9f7882dd00803 TrustKeystore: 381590c561f58fe0948abbd9cb626cbc08d75ff8 -PODFILE CHECKSUM: 224c249f366bf912993e5027df97fb05425c3ccc +PODFILE CHECKSUM: caca540a69e277efe6ea84cc65721152f15ab2cd COCOAPODS: 1.3.1 diff --git a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet - Europe.xcscheme b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet - Europe.xcscheme index adedabef8..d50fe4a3b 100644 --- a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet - Europe.xcscheme +++ b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet - Europe.xcscheme @@ -15,7 +15,7 @@ @@ -49,7 +49,7 @@ diff --git a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet.xcscheme b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet.xcscheme index adedabef8..d50fe4a3b 100644 --- a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet.xcscheme +++ b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWallet.xcscheme @@ -15,7 +15,7 @@ @@ -49,7 +49,7 @@ diff --git a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletTests.xcscheme b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletTests.xcscheme index e4774e262..33854556d 100644 --- a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletTests.xcscheme +++ b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletTests.xcscheme @@ -15,7 +15,7 @@ @@ -190,7 +190,7 @@ diff --git a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletUITests.xcscheme b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletUITests.xcscheme index adedabef8..d50fe4a3b 100644 --- a/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletUITests.xcscheme +++ b/Trust.xcworkspace/xcshareddata/xcschemes/AlphaWalletUITests.xcscheme @@ -15,7 +15,7 @@ @@ -49,7 +49,7 @@ diff --git a/Trust/Foundation/AssetDefinitionXML.swift b/Trust/Foundation/AssetDefinitionXML.swift index acd1dbf14..132c90557 100644 --- a/Trust/Foundation/AssetDefinitionXML.swift +++ b/Trust/Foundation/AssetDefinitionXML.swift @@ -9,6 +9,7 @@ import Foundation class AssetDefinitionXML { + // swiftlint:disable:next line_length public static let assetDefinition = "
0xa3347C8ED134444C25169872c90a6360D42b3691
https://482kdh4npg.execute-api.ap-southeast-1.amazonaws.com/dev/ https://app.awallet.io/ город City 城市 Ciudad Москва́ Moscow 莫斯科 Moscú Санкт-Петербу́рг Saint Petersburg 圣彼得堡 San Petersburgo сочи Sochi 索契 Sochi екатеринбург Ekaterinburg 叶卡捷琳堡 Ekaterinburg Саранск Saransk 萨兰斯克 Saransk казань Kazan 喀山 Kazan Нижний Новгород Nizhny Novgorod 下诺夫哥罗德 Nizhny Novgorod Ростов-на-Дону Rostov-on-Don 顿河畔罗斯托夫 Rostov-on-Don Самара Samara 翅果 Samara Волгоград Volgograd 伏尔加格勒 Volgogrado Калининград Kaliningrad 加里宁格勒 Kaliningrad место встречи Venue 场馆 Lugar Стадион Калининград Kaliningrad Stadium 加里寧格勒體育場 Estadio de Kaliningrado Екатеринбург Арена Volgograd Arena 伏爾加格勒體育場 Volgogrado Arena Казань Арена Ekaterinburg Arena 加里宁格勒体育场 Ekaterimburgo Arena Мордовия Арена Fisht Stadium 菲什特奧林匹克體育場 Estadio Fisht Ростов Арена Kazan Arena 喀山體育場 Kazan Arena Самара Арена Nizhny Novgorod Stadium 下諾夫哥羅德體育場 Estadio de Nizhni Novgorod Стадион Калининград Luzhniki Stadium 卢日尼基体育场 Estadio Luzhniki Стадион Лужники Samara Arena 薩馬拉體育場 Samara Arena Стадион Нижний Новгород Rostov Arena 羅斯托夫體育場 Rostov Arena Стадион Спартак Spartak Stadium 斯巴達克體育場 Estadio del Spartak Стадион Санкт-Петербург Saint Petersburg Stadium 十字架體育場 Estadio de San Petersburgo Стадион Фишт Mordovia Arena 莫爾多維亞體育場 Mordovia Arena время Time 时间 Tiempo Team A 甲队 Equipo A Team B 乙队 Equipo B Match 场次 Evento Cat 等级 Cat Number 票号 Número
" } diff --git a/Trust/Foundation/XMLHandler.swift b/Trust/Foundation/XMLHandler.swift index 59995dc0b..1c7f9e841 100644 --- a/Trust/Foundation/XMLHandler.swift +++ b/Trust/Foundation/XMLHandler.swift @@ -10,7 +10,6 @@ import SwiftyXMLParser struct FIFAInfo { let locale: String - let team: String let venue: String let time: Int let countryA: Int @@ -20,24 +19,29 @@ struct FIFAInfo { let number: Int } +/** + langs: + 0 = ru + 1 = en + 2 = zh + 3 = es + */ + public class XMLHandler { let xml = try! XML.parse(AssetDefinitionXML.assetDefinition) - func getFifaInfoForToken(tokenId: String, lang: String) -> FIFAInfo { + func getFifaInfoForToken(tokenId: String, lang: Int) -> FIFAInfo { let locale = getLocale(attribute: tokenId.substring(to: 2), lang: lang) - let team = getTeam(attribute: tokenId.substring(to: 8), lang: lang) - let venue = getVenue(attribute: tokenId.substring(to: 6), lang: lang) - let time = Int(tokenId.substring(with: Range((5, 12))), radix: 16)! - let countryA = Int(tokenId.substring(with: Range(13, 19)), radix: 16)! - let countryB = Int(tokenId.substring(with: Range(20, 26)), radix: 16)! - let match = Int(tokenId.substring(with: Range(27, 29)), radix: 16)! - let category = Int(tokenId.substring(with: Range(30, 32)), radix: 16)! - let number = Int(tokenId.substring(from: 33), radix: 16)! - return FIFAInfo - ( + let venue = getVenue(attribute: tokenId.substring(with: Range(uncheckedBounds: (2, 4))), lang: lang) + let time = Int(tokenId.substring(with: Range(uncheckedBounds: (5, 12))), radix: 16)! + let countryA = Int(tokenId.substring(with: Range(uncheckedBounds: (13, 18))), radix: 16)! + let countryB = Int(tokenId.substring(with: Range(uncheckedBounds: (19, 24))), radix: 16)! + let match = Int(tokenId.substring(with: Range(uncheckedBounds: (25, 26))), radix: 16)! + let category = Int(tokenId.substring(with: Range(uncheckedBounds: (27, 29))), radix: 16)! + let number = Int(tokenId.substring(from: 30), radix: 16)! + return FIFAInfo( locale: locale, - team: team, venue: venue, time: time, countryA: countryA, @@ -48,14 +52,14 @@ public class XMLHandler { ) } - func getLocale(attribute: String, lang: String) -> String { + func getLocale(attribute: String, lang: Int) -> String { let localeNumber = Int(attribute, radix: 16)! - return xml["field"]["locality"]["mapping"]["entity"][localeNumber][lang] + return xml["asset"]["fields"]["field"][0][0]["mapping"]["entity"][localeNumber]["name"][lang].text! } - func getVenue(attribute: String, lang: String) -> String { + func getVenue(attribute: String, lang: Int) -> String { let venueNumber = Int(attribute, radix: 16)! - return xml["field"]["venue"]["mapping"]["entity"][venueNumber][lang] + return xml["asset"]["fields"]["field"][1][0]["mapping"]["entity"][venueNumber]["name"][lang].text! } } diff --git a/TrustTests/Foundation/XMLHandlerTest.swift b/TrustTests/Foundation/XMLHandlerTest.swift index 6f0fcae94..e72bbb1c5 100644 --- a/TrustTests/Foundation/XMLHandlerTest.swift +++ b/TrustTests/Foundation/XMLHandlerTest.swift @@ -10,11 +10,11 @@ import XCTest @testable import Trust class XMLHandlerTest: XCTestCase { - + func testParser() { let fifaDetails = XMLHandler().getFifaInfoForToken( - tokenId: "0x0a015af6d74042544e43484e01010001", - lang: "en" + tokenId: "0x0a015af6d74042544e43484e01010001".substring(from: 2), + lang: 1 ) XCTAssertNotNil(fifaDetails) } From 025fb3f1459cbb39edf2d682822871b60203e9a2 Mon Sep 17 00:00:00 2001 From: James Sangalli Date: Thu, 12 Apr 2018 10:33:19 +0800 Subject: [PATCH 3/3] parsing to ascii and taking correct values from hex string: --- Trust/Foundation/XMLHandler.swift | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Trust/Foundation/XMLHandler.swift b/Trust/Foundation/XMLHandler.swift index 1c7f9e841..0bcdc0d32 100644 --- a/Trust/Foundation/XMLHandler.swift +++ b/Trust/Foundation/XMLHandler.swift @@ -12,8 +12,8 @@ struct FIFAInfo { let locale: String let venue: String let time: Int - let countryA: Int - let countryB: Int + let countryA: String + let countryB: String let match: Int let category: Int let number: Int @@ -29,23 +29,24 @@ struct FIFAInfo { public class XMLHandler { - let xml = try! XML.parse(AssetDefinitionXML.assetDefinition) + private let xml = try! XML.parse(AssetDefinitionXML.assetDefinition) func getFifaInfoForToken(tokenId: String, lang: Int) -> FIFAInfo { let locale = getLocale(attribute: tokenId.substring(to: 2), lang: lang) let venue = getVenue(attribute: tokenId.substring(with: Range(uncheckedBounds: (2, 4))), lang: lang) let time = Int(tokenId.substring(with: Range(uncheckedBounds: (5, 12))), radix: 16)! - let countryA = Int(tokenId.substring(with: Range(uncheckedBounds: (13, 18))), radix: 16)! - let countryB = Int(tokenId.substring(with: Range(uncheckedBounds: (19, 24))), radix: 16)! - let match = Int(tokenId.substring(with: Range(uncheckedBounds: (25, 26))), radix: 16)! - let category = Int(tokenId.substring(with: Range(uncheckedBounds: (27, 29))), radix: 16)! - let number = Int(tokenId.substring(from: 30), radix: 16)! + //translatable to ascii + let countryA = tokenId.substring(with: Range(uncheckedBounds: (12, 18))).hexa2Bytes + let countryB = tokenId.substring(with: Range(uncheckedBounds: (18, 24))).hexa2Bytes + let match = Int(tokenId.substring(with: Range(uncheckedBounds: (24, 26))), radix: 16)! + let category = Int(tokenId.substring(with: Range(uncheckedBounds: (26, 28))), radix: 16)! + let number = Int(tokenId.substring(from: 28), radix: 16)! return FIFAInfo( locale: locale, venue: venue, time: time, - countryA: countryA, - countryB: countryB, + countryA: String(data: Data(bytes: countryA), encoding: .utf8)!, + countryB: String(data: Data(bytes:countryB), encoding: .utf8)!, match: match, category: category, number: number