Skip to content

Commit

Permalink
fix bug when url does not have scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Aug 24, 2024
1 parent c15347b commit b184ab7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Sources/WalletConnectVerify/VerifyContextFactory.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@

import Foundation

class VerifyContextFactory {

private func ensureUrlHasScheme(_ urlString: String) -> String {
if urlString.hasPrefix("http://") || urlString.hasPrefix("https://") {
return urlString
} else {
return "https://" + urlString
}
}

public func createVerifyContext(origin: String?, domain: String, isScam: Bool?, isVerified: Bool?) -> VerifyContext {

guard isScam != true else {
Expand All @@ -19,7 +27,11 @@ class VerifyContextFactory {
)
}

if let origin, let originUrl = URL(string: origin), let domainUrl = URL(string: domain) {
// Ensure both origin and domain have a scheme
let originWithScheme = origin.map { ensureUrlHasScheme($0) }
let domainWithScheme = ensureUrlHasScheme(domain)

if let originWithScheme, let originUrl = URL(string: originWithScheme), let domainUrl = URL(string: domainWithScheme) {
return VerifyContext(
origin: origin,
validation: (originUrl.host == domainUrl.host) ? .valid : .invalid
Expand Down
10 changes: 10 additions & 0 deletions Tests/VerifyTests/VerifyContextFactoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class VerifyContextFactoryTests: XCTestCase {
XCTAssertEqual(contextWithFalseVerification.validation, .scam)
}

func testValidOriginAndDomainWithoutScheme() {
let context = factory.createVerifyContext(origin: "https://dev.lab.web3modal.com", domain: "dev.lab.web3modal.com", isScam: false, isVerified: nil)
XCTAssertEqual(context.validation, .valid)
}

func testInvalidOriginAndDomainWithoutScheme() {
let context = factory.createVerifyContext(origin: "https://dev.lab.web3modal.com", domain: "different.com", isScam: false, isVerified: nil)
XCTAssertEqual(context.validation, .invalid)
}

// tests for createVerifyContextForLinkMode

func testValidUniversalLink() {
Expand Down

0 comments on commit b184ab7

Please sign in to comment.