Skip to content

Commit

Permalink
Merge pull request #1408 from WalletConnect/verify-bug-fix
Browse files Browse the repository at this point in the history
fix bug when url does not have scheme
  • Loading branch information
llbartekll authored Aug 24, 2024
2 parents c15347b + b184ab7 commit cf9c5cf
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 cf9c5cf

Please sign in to comment.