Skip to content

Commit

Permalink
Merge pull request #5407 from WalletConnect/fix/encoded-uri
Browse files Browse the repository at this point in the history
fix: enables pairing via base64 encoded uri
  • Loading branch information
ganchoradkov authored Oct 1, 2024
2 parents d1b95c3 + d1f70f6 commit 7f29e12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 12 additions & 1 deletion packages/core/test/pairing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 12 additions & 3 deletions packages/utils/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 7f29e12

Please sign in to comment.