198 lines
6.1 KiB
TypeScript
Executable File
198 lines
6.1 KiB
TypeScript
Executable File
import Services from '../services/service';
|
|
import { addSubscription } from './subscription.utils';
|
|
import QRCode from 'qrcode';
|
|
|
|
//Copy Address
|
|
export async function copyToClipboard(fullAddress: string) {
|
|
try {
|
|
await navigator.clipboard.writeText(fullAddress);
|
|
alert('Adresse copiée dans le presse-papiers !');
|
|
} catch (err) {
|
|
console.error('Failed to copy the address: ', err);
|
|
}
|
|
}
|
|
|
|
//Generate emojis list
|
|
export function generateEmojiList(): string[] {
|
|
const emojiRanges = [
|
|
[0x1f600, 0x1f64f],
|
|
[0x1f300, 0x1f5ff],
|
|
[0x1f680, 0x1f6ff],
|
|
[0x1f700, 0x1f77f],
|
|
];
|
|
|
|
const emojiList: string[] = [];
|
|
for (const range of emojiRanges) {
|
|
const [start, end] = range;
|
|
for (let i = start; i <= end && emojiList.length < 256; i++) {
|
|
emojiList.push(String.fromCodePoint(i));
|
|
}
|
|
if (emojiList.length >= 256) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return emojiList.slice(0, 256);
|
|
}
|
|
|
|
//Adress to emojis
|
|
export async function addressToEmoji(text: string): Promise<string> {
|
|
//Adress to Hash
|
|
const encoder = new TextEncoder();
|
|
const data = encoder.encode(text);
|
|
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
|
|
|
|
const hash = new Uint8Array(hashBuffer);
|
|
const bytes = hash.slice(-4);
|
|
|
|
//Hash slice to emojis
|
|
const emojiList = generateEmojiList();
|
|
const emojis = Array.from(bytes)
|
|
.map((byte) => emojiList[byte])
|
|
.join('');
|
|
return emojis;
|
|
}
|
|
|
|
//Get emojis from other device
|
|
async function emojisPairingRequest() {
|
|
try {
|
|
const container = document.getElementById('containerId');
|
|
|
|
const urlParams: URLSearchParams = new URLSearchParams(window.location.search);
|
|
const sp_adress: string | null = urlParams.get('sp_address');
|
|
|
|
if (!sp_adress) {
|
|
// console.error("No 'sp_adress' parameter found in the URL.");
|
|
return;
|
|
}
|
|
|
|
const emojis = await addressToEmoji(sp_adress);
|
|
const emojiDisplay = container?.querySelector('.pairing-request');
|
|
|
|
if (emojiDisplay) {
|
|
emojiDisplay.textContent = '(Request from: ' + emojis + ')';
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|
|
|
|
// Display address emojis and other device emojis
|
|
export async function displayEmojis(text: string) {
|
|
console.log('🚀 ~ Services ~ adressToEmoji');
|
|
try {
|
|
const container = document.getElementById('containerId');
|
|
const emojis = await addressToEmoji(text);
|
|
const emojiDisplay = container?.querySelector('.emoji-display');
|
|
|
|
if (emojiDisplay) {
|
|
emojiDisplay.textContent = emojis;
|
|
}
|
|
|
|
emojisPairingRequest();
|
|
|
|
initAddressInput();
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|
|
|
|
// Verify Other address
|
|
export function initAddressInput() {
|
|
const addressInput = document.getElementById('addressInput') as HTMLInputElement;
|
|
const emojiDisplay = document.getElementById('emoji-display-2');
|
|
const okButton = document.getElementById('okButton');
|
|
addSubscription(addressInput, 'input', async () => {
|
|
let address = addressInput.value;
|
|
|
|
// Vérifie si l'adresse est une URL
|
|
try {
|
|
const url = new URL(address);
|
|
// Si c'est une URL valide, extraire le paramètre sp_address
|
|
const urlParams = new URLSearchParams(url.search);
|
|
const extractedAddress = urlParams.get('sp_address') || ''; // Prend sp_address ou une chaîne vide
|
|
|
|
if (extractedAddress) {
|
|
address = extractedAddress;
|
|
addressInput.value = address; // Met à jour l'input pour afficher uniquement l'adresse extraite
|
|
}
|
|
} catch (e) {
|
|
// Si ce n'est pas une URL valide, on garde l'adresse originale
|
|
console.log("Ce n'est pas une URL valide, on garde l'adresse originale.");
|
|
}
|
|
if (address) {
|
|
const emojis = await addressToEmoji(address);
|
|
if (emojiDisplay) {
|
|
emojiDisplay.innerHTML = emojis;
|
|
}
|
|
if (okButton) {
|
|
okButton.style.display = 'inline-block';
|
|
}
|
|
} else {
|
|
if (emojiDisplay) {
|
|
emojiDisplay.innerHTML = '';
|
|
}
|
|
if (okButton) {
|
|
okButton.style.display = 'none';
|
|
}
|
|
}
|
|
});
|
|
|
|
if (okButton) {
|
|
addSubscription(okButton, 'click', () => {
|
|
onOkButtonClick();
|
|
});
|
|
}
|
|
}
|
|
|
|
async function onOkButtonClick() {
|
|
const service = await Services.getInstance();
|
|
const addressInput = (document.getElementById('addressInput') as HTMLInputElement).value;
|
|
try {
|
|
// Connect to target, if necessary
|
|
const sharedSecret = await service.getSecretForAddress(addressInput);
|
|
if (!sharedSecret) {
|
|
const member = {
|
|
sp_addresses: [addressInput],
|
|
}
|
|
const connectMemberResult = await service.connectMember([member]);
|
|
await service.handleApiReturn(connectMemberResult);
|
|
}
|
|
// Create the process
|
|
setTimeout(async () => {
|
|
const relayAddress = await service.getRelayAddresses(); // Get one (or more?) relay addresses
|
|
const createPairingProcessReturn = await service.createPairingProcess([addressInput], relayAddress[0], 1);
|
|
|
|
if (!createPairingProcessReturn.updated_process) {
|
|
throw new Error('createPairingProcess returned an empty new process'); // This should never happen
|
|
}
|
|
|
|
await service.handleApiReturn(createPairingProcessReturn);
|
|
}, 1000);
|
|
} catch (e) {
|
|
console.error(`onOkButtonClick error: ${e}`);
|
|
}
|
|
}
|
|
|
|
export async function generateQRCode(spAddress: string) {
|
|
try {
|
|
const container = document.getElementById('containerId');
|
|
const currentUrl = 'https://' + window.location.host;
|
|
const url = await QRCode.toDataURL(currentUrl + '?sp_address=' + spAddress);
|
|
const qrCode = container?.querySelector('.qr-code img');
|
|
qrCode?.setAttribute('src', url);
|
|
|
|
//Generate Address CopyBtn
|
|
const address = container?.querySelector('.sp-address-btn');
|
|
if (address) {
|
|
address.textContent = 'Copy address';
|
|
}
|
|
const copyBtn = document.getElementById('copyBtn');
|
|
if (copyBtn) {
|
|
addSubscription(copyBtn, 'click', () => copyToClipboard(currentUrl + '?sp_address=' + spAddress));
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
}
|
|
}
|