Skip to content

Commit c19ce15

Browse files
committed
Load early
1 parent 9382b60 commit c19ce15

File tree

4 files changed

+51
-61
lines changed

4 files changed

+51
-61
lines changed

frontend/src/App.tsx

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import TorConnectionBadge from './components/TorConnection';
77

88
import { I18nextProvider } from 'react-i18next';
99
import i18n from './i18n/Web';
10-
import * as CryptoJS from 'crypto-js';
1110

1211
import { systemClient } from './services/System';
1312
import ErrorBoundary from './components/ErrorBoundary';
@@ -16,64 +15,7 @@ import { GarageContextProvider } from './contexts/GarageContext';
1615
import { FederationContextProvider } from './contexts/FederationContext';
1716
import NotificationSwitchBadge from './components/NotificationSwitch';
1817

19-
interface SubtleCrypto {
20-
digest(algorithm: string, data: ArrayBuffer | Uint8Array | string): Promise<Uint8Array>;
21-
}
22-
23-
interface Crypto {
24-
getRandomValues(arr: Uint8Array): Uint8Array;
25-
subtle: SubtleCrypto;
26-
}
27-
2818
const App = (): React.JSX.Element => {
29-
// Necesary for OpenPGP JS
30-
const getWebCrypto = (): { subtle: SubtleCrypto } => {
31-
return {
32-
subtle: {
33-
digest: (
34-
algorithm: string,
35-
data: ArrayBuffer | Uint8Array | string,
36-
): Promise<Uint8Array> => {
37-
return new Promise((resolve, reject) => {
38-
if (algorithm === 'SHA-256') {
39-
let message: string;
40-
if (data instanceof Uint8Array) {
41-
message = new TextDecoder().decode(data);
42-
} else if (data instanceof ArrayBuffer) {
43-
message = new TextDecoder().decode(new Uint8Array(data));
44-
} else {
45-
message = data;
46-
}
47-
48-
const hash = CryptoJS.SHA256(message).toString();
49-
const match = hash.match(/.{1,2}/g);
50-
if (!match) {
51-
return reject(new Error('Hash computation failed'));
52-
}
53-
const hashArray = new Uint8Array(match.map((byte) => parseInt(byte, 16)));
54-
resolve(hashArray);
55-
} else {
56-
reject(new Error('Algorithm not supported'));
57-
}
58-
});
59-
},
60-
},
61-
};
62-
};
63-
64-
if (typeof window !== 'undefined' && !window?.crypto?.subtle) {
65-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
66-
(window as any).crypto = {
67-
getRandomValues: (arr: Uint8Array): Uint8Array => {
68-
for (let i = 0; i < arr.length; i++) {
69-
arr[i] = Math.floor(Math.random() * 256);
70-
}
71-
return arr;
72-
},
73-
subtle: getWebCrypto().subtle,
74-
} as Crypto;
75-
}
76-
7719
const [client] = window.RobosatsSettings.split('-');
7820
return (
7921
<StrictMode>

frontend/src/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
1+
import * as CryptoJS from 'crypto-js';
12
import App from './App';
3+
4+
// Create a polyfill for the WebCrypto API
5+
const getWebCrypto = () => {
6+
return {
7+
subtle: {
8+
digest: (algorithm, data) => {
9+
return new Promise((resolve, reject) => {
10+
if (algorithm === 'SHA-256') {
11+
let message;
12+
13+
if (data instanceof Uint8Array) {
14+
message = new TextDecoder().decode(data);
15+
} else if (data instanceof ArrayBuffer) {
16+
message = new TextDecoder().decode(new Uint8Array(data));
17+
} else {
18+
message = data;
19+
}
20+
21+
const hash = CryptoJS.SHA256(message).toString();
22+
const match = hash.match(/.{1,2}/g);
23+
if (!match) {
24+
return reject(new Error('Hash computation failed'));
25+
}
26+
27+
const hashArray = new Uint8Array(match.map((byte) => parseInt(byte, 16)));
28+
resolve(hashArray);
29+
} else {
30+
reject(new Error('Algorithm not supported'));
31+
}
32+
});
33+
},
34+
},
35+
};
36+
};
37+
38+
// Override the global crypto object
39+
if (typeof window !== 'undefined' && !window.crypto.getWebCrypto) {
40+
window.crypto = {
41+
getRandomValues: (arr) => {
42+
for (let i = 0; i < arr.length; i++) {
43+
arr[i] = Math.floor(Math.random() * 256);
44+
}
45+
return arr;
46+
},
47+
subtle: getWebCrypto().subtle,
48+
};
49+
}

frontend/src/models/Coordinator.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ export class Coordinator {
307307
getRelayUrl = (network: 'mainnet' | 'testnet', hostUrl: string, selfHosted: boolean): string => {
308308
const protocol = hostUrl.includes('https') ? 'wss' : 'ws';
309309
if (selfHosted && this.shortAlias !== 'local') {
310-
return `${protocol}://${hostUrl.replace(/^https?:\/\//, '')}/${network}/${this.shortAlias}/relay/`;
310+
return `${protocol}://${hostUrl.replace(/^https?:\/\//, '')}/${network}/${this.shortAlias}/nostr/`;
311311
} else {
312-
return `${protocol}://${this.url.replace(/^https?:\/\//, '')}/relay/`;
312+
return `${protocol}://${this.url.replace(/^https?:\/\//, '')}/nostr/`;
313313
}
314314
};
315315
}

frontend/src/services/RoboPool/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class RoboPool {
2020

2121
if (settings.host) {
2222
const protocol = hostUrl.includes('https') ? 'wss' : 'ws';
23-
const hostNostr = `${protocol}://${settings.host.replace(/^https?:\/\//, '')}/relay/`;
23+
const hostNostr = `${protocol}://${settings.host.replace(/^https?:\/\//, '')}/nostr/`;
2424
if (federationRelays.includes(hostNostr)) {
2525
this.relays.push(hostNostr);
2626
}

0 commit comments

Comments
 (0)