MCPcopy Create free account
hub / github.com/Web3Auth/Auth / getHashQueryParams

Function getHashQueryParams

src/core/utils.ts:21–83  ·  view source on GitHub ↗
(replaceUrl = false)

Source from the content-addressed store, hash-verified

19};
20
21export function getHashQueryParams(replaceUrl = false): HashQueryParamResult {
22 const result: HashQueryParamResult = {};
23
24 const queryUrlParams = new URLSearchParams(window.location.search.slice(1));
25 queryUrlParams.forEach((value: string, key: string) => {
26 if (key !== "b64Params") {
27 result[key as keyof HashQueryParamResult] = value;
28 }
29 });
30 const queryResult = queryUrlParams.get("b64Params");
31 if (queryResult) {
32 try {
33 const queryParams = JSON.parse(decodeBase64Url(queryResult));
34 Object.keys(queryParams).forEach((key: string) => {
35 result[key as keyof HashQueryParamResult] = queryParams[key];
36 });
37 } catch (error) {
38 log.error(error);
39 }
40 }
41
42 const hashUrlParams = new URLSearchParams(window.location.hash.substring(1));
43 hashUrlParams.forEach((value: string, key: string) => {
44 if (key !== "b64Params") {
45 result[key as keyof HashQueryParamResult] = value;
46 }
47 });
48
49 const hashResult = hashUrlParams.get("b64Params");
50 if (hashResult) {
51 try {
52 const hashParams = JSON.parse(decodeBase64Url(hashResult));
53 Object.keys(hashParams).forEach((key: string) => {
54 result[key as keyof HashQueryParamResult] = hashParams[key];
55 });
56 } catch (error) {
57 log.error(error);
58 }
59 }
60
61 if (replaceUrl) {
62 const cleanUrl = new URL(window.location.origin + window.location.pathname);
63 // https://dapp.com/#b64Params=asacsdnvdfv&state=sldjvndfkjvn&dappValue=sdjvndf
64 // NB: `params.size !== 0` evaluates to true even if `.size` isn't implemented and returns `undefined`, like in Safari <17 and Chrome <113.
65 if (queryUrlParams.size !== 0) {
66 queryUrlParams.delete("error");
67 queryUrlParams.delete("state");
68 queryUrlParams.delete("b64Params");
69 queryUrlParams.delete("sessionNamespace");
70 cleanUrl.search = queryUrlParams.toString();
71 }
72 if (hashUrlParams.size !== 0) {
73 hashUrlParams.delete("error");
74 hashUrlParams.delete("state");
75 hashUrlParams.delete("b64Params");
76 hashUrlParams.delete("sessionNamespace");
77 cleanUrl.hash = hashUrlParams.toString();
78 }

Callers 1

initMethod · 0.90

Calls 2

getMethod · 0.80
toStringMethod · 0.65

Tested by

no test coverage detected