Improve ipfs: URL rewriting to be more forgiving

pull/2951/head
Hwee-Boon Yar 3 years ago
parent 47c7482ebc
commit 2aa89786ec
  1. 18
      AlphaWallet.xcodeproj/project.pbxproj
  2. 10
      AlphaWallet/Extensions/URL.swift
  3. 13
      AlphaWalletTests/Extensions/URLTests.swift

@ -471,6 +471,7 @@
5E7C797BE2C8DB7EF6F217B3 /* OnboardingPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7103135DCCCAB96EE5FC /* OnboardingPage.swift */; };
5E7C797F3278A290BE47BFA2 /* TokenIdOrEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C78302634885C93D9B6FA /* TokenIdOrEvent.swift */; };
5E7C798A5D213DD92F24CBFB /* AssetImplicitAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C78FAB9070B10A476DB29 /* AssetImplicitAttributes.swift */; };
5E7C799DED280839CE80FCD3 /* URLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7DCF28B3F0342113E20E /* URLTests.swift */; };
5E7C79BFFFB6A5FE833489C0 /* ActivitiesCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C72E648B7C34EDA193BDA /* ActivitiesCoordinator.swift */; };
5E7C79D78AA5E774119BE49B /* TextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C7CFDE7DEA8C06C4100AF /* TextField.swift */; };
5E7C79DE18FC5CB46E75753A /* AssetAttributeMapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E7C788F7461AC159215BFA7 /* AssetAttributeMapping.swift */; };
@ -858,7 +859,7 @@
C601B18D24407F4300C763D9 /* SettingTableViewCellViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C601B18C24407F4300C763D9 /* SettingTableViewCellViewModel.swift */; };
C6959E32246C1D720095DE91 /* SeedPhraseSuggestionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = C6959E31246C1D720095DE91 /* SeedPhraseSuggestionViewCell.swift */; };
C80DC77E22AB7FAD009CFA1C /* XDAI-bridge.tsml in Resources */ = {isa = PBXBuildFile; fileRef = C80DC77D22AB7FAD009CFA1C /* XDAI-bridge.tsml */; };
C8630C92268AD9640049B138 /* BuildFile in Frameworks */ = {isa = PBXBuildFile; productRef = C8630C91268AD9640049B138 /* SwiftPackageProductDependency */; };
C8630C92268AD9640049B138 /* MailchimpSDK in Frameworks */ = {isa = PBXBuildFile; productRef = C8630C91268AD9640049B138 /* MailchimpSDK */; };
C868C5292053BDE00059672B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C868C5282053BDE00059672B /* LaunchScreen.storyboard */; };
C868C536205409160059672B /* redeemInfo.html in Resources */ = {isa = PBXBuildFile; fileRef = C868C534205409160059672B /* redeemInfo.html */; };
C876FF79204A79D300B7D0EA /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = C876FF6B204A79D300B7D0EA /* README.md */; };
@ -1500,6 +1501,7 @@
5E7C7DC3AEBE4049927B7625 /* EnableServersHeaderView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EnableServersHeaderView.swift; sourceTree = "<group>"; };
5E7C7DC4B06C1A623788EEED /* WalletConnectSessionCellViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletConnectSessionCellViewModel.swift; sourceTree = "<group>"; };
5E7C7DCB0BDDD30D10130AE7 /* GetIsERC875Encode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GetIsERC875Encode.swift; sourceTree = "<group>"; };
5E7C7DCF28B3F0342113E20E /* URLTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLTests.swift; sourceTree = "<group>"; };
5E7C7DD409C330DA4033F504 /* Keystore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Keystore.swift; sourceTree = "<group>"; };
5E7C7DD9C564F2C7DE435894 /* ConfirmSignMessageTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfirmSignMessageTableViewCell.swift; sourceTree = "<group>"; };
5E7C7DDCCEDE8A14A9C49E4A /* TransactionRowViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TransactionRowViewModel.swift; sourceTree = "<group>"; };
@ -1817,7 +1819,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C8630C92268AD9640049B138 /* BuildFile in Frameworks */,
C8630C92268AD9640049B138 /* MailchimpSDK in Frameworks */,
2A669F5585899E890EF26C57 /* Pods_AlphaWallet.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -3693,6 +3695,7 @@
children = (
5E7C76D132F4BEA5CE4FFD0A /* StringExtensionTests.swift */,
5E7C7F4F209C6EE3828E18EC /* IntExtensionsTests.swift */,
5E7C7DCF28B3F0342113E20E /* URLTests.swift */,
);
path = Extensions;
sourceTree = "<group>";
@ -4347,7 +4350,7 @@
);
name = AlphaWallet;
packageProductDependencies = (
C8630C91268AD9640049B138 /* SwiftPackageProductDependency */,
C8630C91268AD9640049B138 /* MailchimpSDK */,
);
productName = etherwallet;
productReference = 2912CCF51F6A830700C6CBE3 /* AlphaWallet.app */;
@ -4476,7 +4479,7 @@
);
mainGroup = 2912CCEC1F6A830700C6CBE3;
packageReferences = (
C8630C90268AD9640049B138 /* RemoteSwiftPackageReference */,
C8630C90268AD9640049B138 /* XCRemoteSwiftPackageReference "Mailchimp-SDK-iOS" */,
);
productRefGroup = 2912CCF61F6A830700C6CBE3 /* Products */;
projectDirPath = "";
@ -5651,6 +5654,7 @@
5E7C7B89694C62A14CBE8105 /* FakeEventsDataStore.swift in Sources */,
5E7C7E5C76318C0E802F377F /* TokenScriptFilterParserTests.swift in Sources */,
5E7C7ADB4D80243679028A6D /* IntExtensionsTests.swift in Sources */,
5E7C799DED280839CE80FCD3 /* URLTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -6182,7 +6186,7 @@
/* End XCConfigurationList section */
/* Begin XCRemoteSwiftPackageReference section */
C8630C90268AD9640049B138 /* RemoteSwiftPackageReference */ = {
C8630C90268AD9640049B138 /* XCRemoteSwiftPackageReference "Mailchimp-SDK-iOS" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/mailchimp/Mailchimp-SDK-iOS";
requirement = {
@ -6193,9 +6197,9 @@
/* End XCRemoteSwiftPackageReference section */
/* Begin XCSwiftPackageProductDependency section */
C8630C91268AD9640049B138 /* SwiftPackageProductDependency */ = {
C8630C91268AD9640049B138 /* MailchimpSDK */ = {
isa = XCSwiftPackageProductDependency;
package = C8630C90268AD9640049B138 /* RemoteSwiftPackageReference */;
package = C8630C90268AD9640049B138 /* XCRemoteSwiftPackageReference "Mailchimp-SDK-iOS" */;
productName = MailchimpSDK;
};
/* End XCSwiftPackageProductDependency section */

@ -5,7 +5,15 @@ import Foundation
extension URL {
var rewrittenIfIpfs: URL {
if scheme == "ipfs" {
return URL(string: absoluteString.replacingOccurrences(of: "ipfs://", with: "https://ipfs.io/")) ?? self
//We can't use `URLComponents` or `pathComponents` here
let components = absoluteString.replacingOccurrences(of: "ipfs://", with: "").split(separator: "/")
if components.count == 1 {
//Matches doge NFT
return URL(string: absoluteString.replacingOccurrences(of: "ipfs://", with: "https://ipfs.io/ipfs/")) ?? self
} else {
//Matches Alchemy NFT
return URL(string: absoluteString.replacingOccurrences(of: "ipfs://", with: "https://ipfs.io/")) ?? self
}
} else {
return self
}

@ -0,0 +1,13 @@
// Copyright © 2021 Stormbird PTE. LTD.
import XCTest
@testable import AlphaWallet
class URLTests: XCTestCase {
func testRewrittenIfIpfs() {
XCTAssertEqual(URL(string: "ipfs://ipfs/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")?.rewrittenIfIpfs.absoluteString, "https://ipfs.io/ipfs/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")
XCTAssertEqual(URL(string: "ipfs://QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")?.rewrittenIfIpfs.absoluteString, "https://ipfs.io/ipfs/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")
XCTAssertEqual(URL(string: "ipfs://something/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")?.rewrittenIfIpfs.absoluteString, "https://ipfs.io/something/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")
XCTAssertEqual(URL(string: "ipfs://something/something/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")?.rewrittenIfIpfs.absoluteString, "https://ipfs.io/something/something/QmbZzG343A7JGmHGwnrv3wimHYkB98azcBH1ojzWmVeDty")
}
}
Loading…
Cancel
Save