Skip to content

Commit

Permalink
Merge branch 'v2.0' into chore/2.11.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov authored Feb 6, 2024
2 parents cf165dc + 59a04c8 commit 7fe0a0f
Show file tree
Hide file tree
Showing 22 changed files with 577 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci_sign_client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
uses: docker/setup-buildx-action@v3

- name: Cache Docker layers
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
Expand Down
17 changes: 14 additions & 3 deletions packages/core/src/controllers/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
isJsonRpcResult,
isJsonRpcError,
} from "@walletconnect/jsonrpc-utils";
import { FIVE_MINUTES, THIRTY_DAYS } from "@walletconnect/time";
import { FIVE_MINUTES, THIRTY_DAYS, toMiliseconds } from "@walletconnect/time";
import EventEmitter from "events";
import {
PAIRING_CONTEXT,
Expand Down Expand Up @@ -99,6 +99,7 @@ export class Pairing implements IPairing {
topic,
symKey,
relay,
expiryTimestamp: expiry,
});
await this.pairings.set(topic, pairing);
await this.core.relayer.subscribe(topic);
Expand All @@ -110,7 +111,7 @@ export class Pairing implements IPairing {
public pair: IPairing["pair"] = async (params) => {
this.isInitialized();
this.isValidPair(params);
const { topic, symKey, relay } = parseUri(params.uri);
const { topic, symKey, relay, expiryTimestamp } = parseUri(params.uri);
let existingPairing;
if (this.pairings.keys.includes(topic)) {
existingPairing = this.pairings.get(topic);
Expand All @@ -121,7 +122,7 @@ export class Pairing implements IPairing {
}
}

const expiry = calcExpiry(FIVE_MINUTES);
const expiry = expiryTimestamp || calcExpiry(FIVE_MINUTES);
const pairing = { topic, relay, expiry, active: false };
await this.pairings.set(topic, pairing);
this.core.expirer.set(topic, expiry);
Expand Down Expand Up @@ -397,6 +398,16 @@ export class Pairing implements IPairing {
const { message } = getInternalError("MISSING_OR_INVALID", `pair() uri#symKey`);
throw new Error(message);
}
if (uri?.expiryTimestamp) {
const expiration = toMiliseconds(uri?.expiryTimestamp);
if (expiration < Date.now()) {
const { message } = getInternalError(
"EXPIRED",
`pair() URI has expired. Please try again with a new connection URI.`,
);
throw new Error(message);
}
}
};

private isValidPing = async (params: { topic: string }) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/controllers/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Publisher extends IPublisher {
public name = PUBLISHER_CONTEXT;
public queue = new Map<string, PublisherTypes.Params>();

private publishTimeout = toMiliseconds(TEN_SECONDS);
private publishTimeout = toMiliseconds(TEN_SECONDS * 2);
private needsTransportRestart = false;

constructor(public relayer: IRelayer, public logger: Logger) {
Expand Down Expand Up @@ -50,7 +50,7 @@ export class Publisher extends IPublisher {
const publish = await createExpiringPromise(
this.rpcPublish(topic, message, ttl, relay, prompt, tag, id),
this.publishTimeout,
"Failed to publish payload, please try again.",
`Failed to publish payload, please try again. id:${id} tag:${tag}`,
);
await publish;
this.removeRequestFromQueue(id);
Expand Down
26 changes: 25 additions & 1 deletion packages/core/src/controllers/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export class Relayer extends IRelayer {
private connectionStatusPollingInterval = 20;
private staleConnectionErrors = ["socket hang up", "socket stalled"];
private hasExperiencedNetworkDisruption = false;
private requestsInFlight = new Map<
number,
{
promise: Promise<any>;
request: RequestArguments<RelayJsonRpc.SubscribeParams>;
}
>();

constructor(opts: RelayerOptions) {
super(opts);
Expand Down Expand Up @@ -172,13 +179,22 @@ export class Relayer extends IRelayer {

public request = async (request: RequestArguments<RelayJsonRpc.SubscribeParams>) => {
this.logger.debug(`Publishing Request Payload`);
const id = request.id as number;
const requestPromise = this.provider.request(request);
this.requestsInFlight.set(id, {
promise: requestPromise,
request,
});
try {
await this.toEstablishConnection();
return await this.provider.request(request);
const result = await requestPromise;
return result;
} catch (e) {
this.logger.debug(`Failed to Publish Request`);
this.logger.error(e as any);
throw e;
} finally {
this.requestsInFlight.delete(id);
}
};

Expand All @@ -204,6 +220,14 @@ export class Relayer extends IRelayer {
}

public async transportClose() {
// wait for all requests to finish before closing the transport
if (this.requestsInFlight.size > 0) {
this.logger.debug("Waiting for all in-flight requests to finish before closing transport...");
this.requestsInFlight.forEach(async (value) => {
await value.promise;
});
}

this.transportExplicitlyClosed = true;
/**
* if there was a network disruption like restart of network driver, the socket is most likely stalled and we can't rely on it
Expand Down
1 change: 1 addition & 0 deletions packages/sign-client/src/constants/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SIGN_CLIENT_EVENTS: Record<SignClientTypes.Event, SignClientTypes.E
session_request_sent: "session_request_sent",
session_event: "session_event",
proposal_expire: "proposal_expire",
session_request_expire: "session_request_expire",
};

export const SIGN_CLIENT_STORAGE_OPTIONS = {
Expand Down
Loading

0 comments on commit 7fe0a0f

Please sign in to comment.