mirror of
https://github.com/RoboSats/robosats.git
synced 2025-09-05 10:24:13 +00:00
Load early
This commit is contained in:
@ -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<Uint8Array>;
|
||||
}
|
||||
|
||||
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<Uint8Array> => {
|
||||
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 (
|
||||
<StrictMode>
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
@ -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/`;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user