Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: visibilitychange fetch [DO NOT MERGE] #5142

Draft
wants to merge 3 commits into
base: v2.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/core/src/constants/verify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const VERIFY_CONTEXT = "verify-api";

export const VERIFY_SERVER = "https://verify.walletconnect.com";
const VERIFY_SERVER_COM = "https://verify.walletconnect.com";
const VERIFY_SERVER_ORG = "https://verify.walletconnect.org";
export const VERIFY_SERVER = VERIFY_SERVER_ORG;

export const VERIFY_FALLBACK_SERVER = "https://verify.walletconnect.org";

export const TRUSTED_VERIFY_URLS = [VERIFY_SERVER, VERIFY_FALLBACK_SERVER];
export const TRUSTED_VERIFY_URLS = [VERIFY_SERVER_COM, VERIFY_SERVER_ORG];
6 changes: 3 additions & 3 deletions packages/core/src/controllers/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ export class Relayer extends IRelayer {
private initialized = false;
private connectionAttemptInProgress = false;

private relayUrl: string;
private projectId: string | undefined;
private bundleId: string | undefined;
public relayUrl: string;
public projectId: string | undefined;
public bundleId: string | undefined;
private connectionStatusPollingInterval = 20;
private staleConnectionErrors = ["socket hang up", "stalled", "interrupted"];
private hasExperiencedNetworkDisruption = false;
Expand Down
71 changes: 71 additions & 0 deletions packages/core/src/controllers/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
createExpiringPromise,
hashMessage,
isValidArray,
formatRelayRpcUrl,
} from "@walletconnect/utils";
import {
CORE_STORAGE_PREFIX,
Expand All @@ -27,8 +28,10 @@ import {
SUBSCRIBER_STORAGE_VERSION,
PENDING_SUB_RESOLUTION_TIMEOUT,
RELAYER_EVENTS,
RELAYER_SDK_VERSION,
} from "../constants";
import { SubscriberTopicMap } from "./topicmap";
import { getBigIntRpcId } from "@walletconnect/jsonrpc-utils";

export class Subscriber extends ISubscriber {
public subscriptions = new Map<string, SubscriberTypes.Active>();
Expand Down Expand Up @@ -61,6 +64,73 @@ export class Subscriber extends ISubscriber {
this.logger.trace(`Initialized`);
this.registerEventListeners();
this.clientId = await this.relayer.core.crypto.getClientId();
// window.addEventListener("focus", () => {
// // alert("=====onfocus===");
// // this.reset();
// this.batchFetchMessages(this.values);
// });
// window.addEventListener("pageshow", () => {
// // alert("=====pageshow===");
// // this.reset();
// this.batchFetchMessages(this.values);
// });
window.addEventListener("visibilitychange", async () => {
// alert("=====visibilitychange===");
// this.reset();
// this.batchFetchMessages(this.values);

const subscriptions = this.values;
const relay = subscriptions[0].relay;
const api = getRelayProtocolApi(relay.protocol);
const request = {
id: getBigIntRpcId().toString() as any,
jsonrpc: "2.0",
method: api.batchFetchMessages,
params: {
topics: subscriptions.map((s) => s.topic),
},
};

const auth = await this.relayer.core.crypto.signJWT(
(this.relayer as unknown as any).relayUrl,
);
const url = formatRelayRpcUrl({
sdkVersion: RELAYER_SDK_VERSION,
protocol: (this.relayer as unknown as any).protocol,
version: (this.relayer as unknown as any).version,
relayUrl: "http://192.168.0.159:3003/rpc",
projectId: (this.relayer as unknown as any).projectId,
auth,
useOnCloseEvent: true,
bundleId: (this.relayer as unknown as any).bundleId,
});
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(request),
// credentials: "omit",
// mode: "no-cors",
});
if (!res.ok) {
alert(res.status);
alert(await res.text());
}
// console.log(await res.text());
const response = await res.json();
if (response && response.result?.messages) {
// alert(response.result?.messages.length);
this.pendingBatchMessages = this.pendingBatchMessages.concat(response.result.messages);

if (this.pendingBatchMessages.length) {
await this.relayer.handleBatchMessageEvents(this.pendingBatchMessages);
this.pendingBatchMessages = [];
}
}

// await this.relayer.restartTransport();
});
}
};

Expand Down Expand Up @@ -389,6 +459,7 @@ export class Subscriber extends ISubscriber {
}

private async reset() {
// alert(this.cached.length);
if (this.cached.length) {
const numOfBatches = Math.ceil(this.cached.length / this.batchSubscribeTopicsLimit);
for (let i = 0; i < numOfBatches; i++) {
Expand Down
35 changes: 4 additions & 31 deletions packages/core/src/controllers/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import { isBrowser, isNode, isReactNative } from "@walletconnect/utils";
import { FIVE_SECONDS, ONE_SECOND, toMiliseconds } from "@walletconnect/time";

import {
TRUSTED_VERIFY_URLS,
VERIFY_CONTEXT,
VERIFY_FALLBACK_SERVER,
VERIFY_SERVER,
} from "../constants";
import { TRUSTED_VERIFY_URLS, VERIFY_CONTEXT, VERIFY_SERVER } from "../constants";

export class Verify extends IVerify {
public name = VERIFY_CONTEXT;
Expand Down Expand Up @@ -49,19 +44,7 @@
} catch (error) {
this.logger.info(`Verify iframe failed to load: ${this.verifyUrl}`);
this.logger.info(error);
}

if (this.initialized) return;

this.removeIframe();
this.verifyUrl = VERIFY_FALLBACK_SERVER;

try {
await this.createIframe();
} catch (error) {
this.logger.info(`Verify iframe failed to load: ${this.verifyUrl}`);
this.logger.info(error);
// if the fallback url fails to load as well, disable verify
// if the iframe fails to load, disable verify
this.verifyDisabled = true;
}
};
Expand All @@ -75,21 +58,11 @@
}
};

public resolve: IVerify["resolve"] = async (params) => {

Check warning on line 61 in packages/core/src/controllers/verify.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Async method 'resolve' has no 'await' expression
if (this.isDevEnv) return "";

const verifyUrl = this.getVerifyUrl(params?.verifyUrl);
let result;
try {
result = await this.fetchAttestation(params.attestationId, verifyUrl);
} catch (error) {
this.logger.info(
`failed to resolve attestation: ${params.attestationId} from url: ${verifyUrl}`,
);
this.logger.info(error);
result = await this.fetchAttestation(params.attestationId, VERIFY_FALLBACK_SERVER);
}
return result;
return this.fetchAttestation(params.attestationId, verifyUrl);
};

get context(): string {
Expand All @@ -99,7 +72,7 @@
private fetchAttestation = async (attestationId: string, url: string) => {
this.logger.info(`resolving attestation: ${attestationId} from url: ${url}`);
// set artificial timeout to prevent hanging
const timeout = this.startAbortTimer(ONE_SECOND * 2);
const timeout = this.startAbortTimer(ONE_SECOND * 5);
const result = await fetch(`${url}/attestation/${attestationId}`, {
signal: this.abortController.signal,
});
Expand Down
Loading