diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index dd945f40..79d0aac7 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -7,7 +7,6 @@ import TorConnectionBadge from './components/TorConnection'; import { I18nextProvider } from 'react-i18next'; import i18n from './i18n/Web'; -import * as CryptoJS from 'crypto-js'; import { systemClient } from './services/System'; import ErrorBoundary from './components/ErrorBoundary'; @@ -16,64 +15,7 @@ import { GarageContextProvider } from './contexts/GarageContext'; import { FederationContextProvider } from './contexts/FederationContext'; import NotificationSwitchBadge from './components/NotificationSwitch'; -interface SubtleCrypto { - digest(algorithm: string, data: ArrayBuffer | Uint8Array | string): Promise; -} - -interface Crypto { - getRandomValues(arr: Uint8Array): Uint8Array; - subtle: SubtleCrypto; -} - const App = (): React.JSX.Element => { - // Necesary for OpenPGP JS - const getWebCrypto = (): { subtle: SubtleCrypto } => { - return { - subtle: { - digest: ( - algorithm: string, - data: ArrayBuffer | Uint8Array | string, - ): Promise => { - return new Promise((resolve, reject) => { - if (algorithm === 'SHA-256') { - let message: string; - if (data instanceof Uint8Array) { - message = new TextDecoder().decode(data); - } else if (data instanceof ArrayBuffer) { - message = new TextDecoder().decode(new Uint8Array(data)); - } else { - message = data; - } - - const hash = CryptoJS.SHA256(message).toString(); - const match = hash.match(/.{1,2}/g); - if (!match) { - return reject(new Error('Hash computation failed')); - } - const hashArray = new Uint8Array(match.map((byte) => parseInt(byte, 16))); - resolve(hashArray); - } else { - reject(new Error('Algorithm not supported')); - } - }); - }, - }, - }; - }; - - if (typeof window !== 'undefined' && !window?.crypto?.subtle) { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (window as any).crypto = { - getRandomValues: (arr: Uint8Array): Uint8Array => { - for (let i = 0; i < arr.length; i++) { - arr[i] = Math.floor(Math.random() * 256); - } - return arr; - }, - subtle: getWebCrypto().subtle, - } as Crypto; - } - const [client] = window.RobosatsSettings.split('-'); return ( diff --git a/frontend/src/index.js b/frontend/src/index.js index f48559d9..c97771be 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -1 +1,49 @@ +import * as CryptoJS from 'crypto-js'; import App from './App'; + +// Create a polyfill for the WebCrypto API +const getWebCrypto = () => { + return { + subtle: { + digest: (algorithm, data) => { + return new Promise((resolve, reject) => { + if (algorithm === 'SHA-256') { + let message; + + if (data instanceof Uint8Array) { + message = new TextDecoder().decode(data); + } else if (data instanceof ArrayBuffer) { + message = new TextDecoder().decode(new Uint8Array(data)); + } else { + message = data; + } + + const hash = CryptoJS.SHA256(message).toString(); + const match = hash.match(/.{1,2}/g); + if (!match) { + return reject(new Error('Hash computation failed')); + } + + const hashArray = new Uint8Array(match.map((byte) => parseInt(byte, 16))); + resolve(hashArray); + } else { + reject(new Error('Algorithm not supported')); + } + }); + }, + }, + }; +}; + +// Override the global crypto object +if (typeof window !== 'undefined' && !window.crypto.getWebCrypto) { + window.crypto = { + getRandomValues: (arr) => { + for (let i = 0; i < arr.length; i++) { + arr[i] = Math.floor(Math.random() * 256); + } + return arr; + }, + subtle: getWebCrypto().subtle, + }; +} diff --git a/frontend/src/models/Coordinator.model.ts b/frontend/src/models/Coordinator.model.ts index 0b44ba68..d2b48c2f 100644 --- a/frontend/src/models/Coordinator.model.ts +++ b/frontend/src/models/Coordinator.model.ts @@ -307,9 +307,9 @@ export class Coordinator { getRelayUrl = (network: 'mainnet' | 'testnet', hostUrl: string, selfHosted: boolean): string => { const protocol = hostUrl.includes('https') ? 'wss' : 'ws'; if (selfHosted && this.shortAlias !== 'local') { - return `${protocol}://${hostUrl.replace(/^https?:\/\//, '')}/${network}/${this.shortAlias}/relay/`; + return `${protocol}://${hostUrl.replace(/^https?:\/\//, '')}/${network}/${this.shortAlias}/nostr/`; } else { - return `${protocol}://${this.url.replace(/^https?:\/\//, '')}/relay/`; + return `${protocol}://${this.url.replace(/^https?:\/\//, '')}/nostr/`; } }; } diff --git a/frontend/src/services/RoboPool/index.ts b/frontend/src/services/RoboPool/index.ts index 25f13bac..9a9e31e8 100644 --- a/frontend/src/services/RoboPool/index.ts +++ b/frontend/src/services/RoboPool/index.ts @@ -20,7 +20,7 @@ class RoboPool { if (settings.host) { const protocol = hostUrl.includes('https') ? 'wss' : 'ws'; - const hostNostr = `${protocol}://${settings.host.replace(/^https?:\/\//, '')}/relay/`; + const hostNostr = `${protocol}://${settings.host.replace(/^https?:\/\//, '')}/nostr/`; if (federationRelays.includes(hostNostr)) { this.relays.push(hostNostr); }