Skip to content

Commit

Permalink
Merge branch 'fix/uri-validation' of github.com:WalletConnect/walletc…
Browse files Browse the repository at this point in the history
…onnect-monorepo into fix/uri-validation
  • Loading branch information
Gancho Radkov committed Nov 21, 2023
2 parents 93a99f3 + dbbb911 commit 085ae1b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
13 changes: 7 additions & 6 deletions packages/core/src/controllers/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ export class Pairing implements IPairing {
}
}

// avoid overwriting keychain pairing already exists
if (!this.core.crypto.keychain.has(topic)) {
await this.core.crypto.setSymKey(symKey, topic);
await this.core.relayer.subscribe(topic, { relay });
}

const expiry = calcExpiry(FIVE_MINUTES);
const pairing = { topic, relay, expiry, active: false };
await this.pairings.set(topic, pairing);
Expand All @@ -137,6 +131,13 @@ export class Pairing implements IPairing {
}

this.events.emit(PAIRING_EVENTS.create, pairing);

// avoid overwriting keychain pairing already exists
if (!this.core.crypto.keychain.has(topic)) {
await this.core.crypto.setSymKey(symKey, topic);
await this.core.relayer.subscribe(topic, { relay });
}

return pairing;
};

Expand Down
37 changes: 35 additions & 2 deletions packages/core/test/pairing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect, describe, it, beforeEach, afterEach } from "vitest";
import { ICore } from "@walletconnect/types";
import { Core, CORE_PROTOCOL, CORE_VERSION } from "../src";
import { Core, CORE_PROTOCOL, CORE_VERSION, PAIRING_EVENTS, SUBSCRIBER_EVENTS } from "../src";
import { TEST_CORE_OPTIONS, disconnectSocket, waitForEvent } from "./shared";
import { generateRandomBytes32 } from "@walletconnect/utils";
import { generateRandomBytes32, parseUri } from "@walletconnect/utils";

const createCoreClients: () => Promise<{ coreA: ICore; coreB: ICore }> = async () => {
const coreA = new Core(TEST_CORE_OPTIONS);
Expand Down Expand Up @@ -294,4 +294,37 @@ describe("Pairing", () => {
});
});
});
describe("events", () => {
it("should emit 'pairing_create' event", async () => {
let pairingCreatedEvent = false;
coreB.pairing.events.on(PAIRING_EVENTS.create, () => (pairingCreatedEvent = true));
const { uri } = await coreA.pairing.create();
coreB.pairing.pair({ uri });
await waitForEvent(() => pairingCreatedEvent);
});
it("should store pairing before subscribing to its topic", async () => {
let pairingCreatedEvent = false;
let pairingCreatedEventTime = 0;
let subscriptionCreatedEvent = false;
let subscriptionCreatedEventTime = 0;
const { uri } = await coreA.pairing.create();
const { topic } = parseUri(uri);
coreB.pairing.events.on(PAIRING_EVENTS.create, () => {
pairingCreatedEventTime = performance.now();
pairingCreatedEvent = true;
});

coreB.relayer.subscriber.events.on(SUBSCRIBER_EVENTS.created, () => {
subscriptionCreatedEventTime = performance.now();
subscriptionCreatedEvent = true;
});

coreB.pairing.pair({ uri });
await waitForEvent(() => pairingCreatedEvent);
await waitForEvent(() => subscriptionCreatedEvent);
expect(coreB.pairing.pairings.keys.length).toBe(1);
expect(coreB.pairing.pairings.values[0].topic).toEqual(topic);
expect(subscriptionCreatedEventTime).toBeGreaterThan(pairingCreatedEventTime);
});
});
});
1 change: 1 addition & 0 deletions packages/sign-client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ export class Engine extends IEngine {
optionalNamespaces: proposal.optionalNamespaces,
relays: proposal.relays,
proposer: proposal.proposer,
sessionProperties: proposal.sessionProperties,
},
proposal.id,
),
Expand Down

0 comments on commit 085ae1b

Please sign in to comment.