From d1f70f607026bafec315c8d50717f38e17c5fef0 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Tue, 1 Oct 2024 10:46:03 +0300 Subject: [PATCH] fix: enables pairing via base64 encoded uri --- packages/core/test/pairing.spec.ts | 13 ++++++++++++- packages/utils/src/validators.ts | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/core/test/pairing.spec.ts b/packages/core/test/pairing.spec.ts index 12ffc3651..f6e9dc11d 100644 --- a/packages/core/test/pairing.spec.ts +++ b/packages/core/test/pairing.spec.ts @@ -2,7 +2,7 @@ import { expect, describe, it, beforeEach, afterEach } from "vitest"; import { ICore } from "@walletconnect/types"; import { Core, CORE_PROTOCOL, CORE_VERSION, PAIRING_EVENTS, SUBSCRIBER_EVENTS } from "../src"; import { TEST_CORE_OPTIONS, disconnectSocket, waitForEvent } from "./shared"; -import { generateRandomBytes32, parseUri } from "@walletconnect/utils"; +import { generateRandomBytes32, parseUri, toBase64 } from "@walletconnect/utils"; const createCoreClients: () => Promise<{ coreA: ICore; coreB: ICore }> = async () => { const coreA = new Core(TEST_CORE_OPTIONS); @@ -53,6 +53,17 @@ describe("Pairing", () => { expect(coreA.pairing.getPairings()[0].active).toBe(false); expect(coreB.pairing.getPairings()[0].active).toBe(false); }); + it("can pair via base64 provided URI", async () => { + const { uri } = await coreA.pairing.create(); + const encodedUri = toBase64(uri, true); + await coreB.pairing.pair({ uri: encodedUri }); + + expect(coreA.pairing.pairings.keys.length).toBe(1); + expect(coreB.pairing.pairings.keys.length).toBe(1); + expect(coreA.pairing.pairings.keys).to.deep.equal(coreB.pairing.pairings.keys); + expect(coreA.pairing.getPairings()[0].active).toBe(false); + expect(coreB.pairing.getPairings()[0].active).toBe(false); + }); it("can pair via provided android deeplink URI", async () => { const { uri } = await coreA.pairing.create(); diff --git a/packages/utils/src/validators.ts b/packages/utils/src/validators.ts index f33a6d38d..d961a22bf 100644 --- a/packages/utils/src/validators.ts +++ b/packages/utils/src/validators.ts @@ -7,7 +7,7 @@ import { getAccountsChains, } from "./namespaces"; import { getSdkError, getInternalError } from "./errors"; -import { hasOverlap } from "./misc"; +import { fromBase64, hasOverlap } from "./misc"; import { getChainsFromNamespace } from "./caip"; export type ErrorObject = { message: string; code: number } | null; @@ -91,14 +91,23 @@ export function isValidAccountId(value: any) { } export function isValidUrl(value: any) { - if (isValidString(value, false)) { + function validateUrl(blob: string) { try { - const url = new URL(value); + const url = new URL(blob); return typeof url !== "undefined"; } catch (e) { return false; } } + try { + if (isValidString(value, false)) { + const isValid = validateUrl(value); + if (isValid) return true; + + const decoded = fromBase64(value); + return validateUrl(decoded); + } + } catch (e) {} return false; }