2023-07-23 15:06:57 +02:00
|
|
|
function base32_nopad_encode(slice) {
|
|
|
|
const encoder = new base32.Encoder({ type: "rfc4648", lc: true });
|
|
|
|
const code = encoder.write(slice).finalize();
|
|
|
|
return code.replaceAll('=', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
function base64_decode(str_b64) {
|
|
|
|
const raw_str = atob(str_b64);
|
|
|
|
const length = raw_str.length;
|
|
|
|
var b = [];
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
b.push(raw_str.charCodeAt(i));
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2023-07-25 18:52:01 +02:00
|
|
|
class Account {
|
2023-07-25 21:46:03 +02:00
|
|
|
constructor(local_part, separator, domain, key) {
|
2023-07-23 15:06:57 +02:00
|
|
|
this.local_part = local_part;
|
|
|
|
this.domain = domain;
|
|
|
|
this.separator = separator;
|
2023-07-25 21:46:03 +02:00
|
|
|
if (Array.isArray(key)) {
|
|
|
|
this.key = key;
|
|
|
|
} else {
|
|
|
|
this.key = base64_decode(key);
|
|
|
|
}
|
2023-07-23 15:06:57 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 18:50:04 +02:00
|
|
|
getName() {
|
|
|
|
return `${this.local_part}@${this.domain}`;
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:06:57 +02:00
|
|
|
genSubAddr(sub_addr_name) {
|
|
|
|
var hasher = sha256.hmac.create(this.key);
|
|
|
|
hasher.update(this.local_part);
|
|
|
|
hasher.update(this.separator);
|
|
|
|
hasher.update(sub_addr_name);
|
|
|
|
const hash = hasher.array();
|
|
|
|
const offset = hash[hash.length - 1] & 0xf;
|
|
|
|
const reduced_hash = hash.slice(offset, offset + 5);
|
|
|
|
const code = base32_nopad_encode(reduced_hash);
|
|
|
|
return `${this.local_part}${this.separator}${sub_addr_name}${this.separator}${code}@${this.domain}`
|
|
|
|
}
|
2023-07-25 21:46:03 +02:00
|
|
|
|
|
|
|
register() {
|
|
|
|
localStorage.setItem(this.getName(), JSON.stringify(this));
|
|
|
|
}
|
2023-07-23 15:06:57 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:46:03 +02:00
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
|
// Functions to open and close a modal
|
2023-07-25 22:25:40 +02:00
|
|
|
function openModal(el) {
|
2023-07-25 23:10:32 +02:00
|
|
|
if (el.id === 'modal-del-account') {
|
|
|
|
document.querySelector('#del-account-name').innerHTML = getSelectedAccountName();
|
|
|
|
}
|
2023-07-25 22:25:40 +02:00
|
|
|
el.classList.add('is-active');
|
2023-07-25 21:46:03 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 22:25:40 +02:00
|
|
|
function openNewAccountModal(el) {
|
|
|
|
const new_account_modal = document.querySelector('#modal-add-account');
|
|
|
|
openModal(new_account_modal);
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeModal(el) {
|
|
|
|
if (!(el.id === 'modal-add-account' && localStorage.length === 0)) {
|
|
|
|
el.classList.remove('is-active');
|
2023-07-25 21:46:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:25:40 +02:00
|
|
|
function closeAllModals() {
|
|
|
|
(document.querySelectorAll('.modal') || []).forEach((modal) => {
|
|
|
|
closeModal(modal);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-25 23:10:32 +02:00
|
|
|
// Function to get the name of the currently selected account
|
|
|
|
function getSelectedAccountName() {
|
|
|
|
return document.querySelector('#account-name').value;
|
|
|
|
}
|
|
|
|
|
2023-07-25 22:26:29 +02:00
|
|
|
// Function to get an account by its name
|
|
|
|
function getAccountByName(name) {
|
|
|
|
const account_string = localStorage.getItem(name);
|
|
|
|
const account_raw = JSON.parse(account_string);
|
|
|
|
return new Account(
|
|
|
|
account_raw.local_part,
|
|
|
|
account_raw.separator,
|
|
|
|
account_raw.domain,
|
|
|
|
account_raw.key,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:46:03 +02:00
|
|
|
// Function to synchronize the account list
|
|
|
|
function syncAccountList() {
|
|
|
|
var acc_list = document.querySelector('#account-name');
|
|
|
|
while (acc_list.lastElementChild) {
|
|
|
|
acc_list.removeChild(acc_list.lastElementChild);
|
|
|
|
}
|
2023-07-25 22:26:29 +02:00
|
|
|
var account_names = [];
|
2023-07-25 21:46:03 +02:00
|
|
|
for (var i = 0, len = localStorage.length; i < len; ++i) {
|
2023-07-25 22:26:29 +02:00
|
|
|
account_names.push(localStorage.key(i));
|
|
|
|
}
|
|
|
|
account_names.sort();
|
|
|
|
for (const name of account_names) {
|
|
|
|
const account = getAccountByName(name);
|
2023-07-25 21:46:03 +02:00
|
|
|
const new_elem = new Option(account.getName(), account.getName());
|
|
|
|
acc_list.appendChild(new_elem);
|
|
|
|
}
|
|
|
|
if (localStorage.length === 0) {
|
2023-07-25 22:25:40 +02:00
|
|
|
openNewAccountModal();
|
2023-07-25 21:46:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
syncAccountList();
|
|
|
|
|
|
|
|
// Add a click event on buttons to open a specific modal
|
2023-07-25 22:25:40 +02:00
|
|
|
(document.querySelectorAll('.js-modal-trigger') || []).forEach((trigger) => {
|
|
|
|
const modal = trigger.dataset.target;
|
|
|
|
const target = document.getElementById(modal);
|
|
|
|
|
|
|
|
trigger.addEventListener('click', () => {
|
|
|
|
openModal(target);
|
2023-07-25 21:46:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a click event on various child elements to close the parent modal
|
2023-07-25 22:25:40 +02:00
|
|
|
(document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button-close') || []).forEach((close) => {
|
|
|
|
const target = close.closest('.modal');
|
|
|
|
|
|
|
|
close.addEventListener('click', () => {
|
|
|
|
closeModal(target);
|
2023-07-25 21:46:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add a keyboard event to close all modals
|
|
|
|
document.addEventListener('keydown', (event) => {
|
|
|
|
if (event.code === 'Escape') {
|
2023-07-25 22:25:40 +02:00
|
|
|
closeAllModals();
|
2023-07-25 21:46:03 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-07-25 23:10:32 +02:00
|
|
|
// Add a click event on the delete account button to display the confirmation message
|
|
|
|
document.querySelector('#btn-del-account-confirm').addEventListener('click', (event) => {
|
|
|
|
const account_name = getSelectedAccountName();
|
|
|
|
localStorage.removeItem(account_name);
|
|
|
|
console.log(`Account ${account_name} deleted.`);
|
|
|
|
syncAccountList();
|
|
|
|
closeAllModals();
|
|
|
|
});
|
|
|
|
|
2023-07-25 21:46:03 +02:00
|
|
|
// Add a click event on the new account button to register the new account
|
|
|
|
document.querySelector('#btn-new-account').addEventListener('click', (event) => {
|
|
|
|
console.log('Adding new account…');
|
|
|
|
const new_account = new Account(
|
|
|
|
document.querySelector('#new-addr-local-part').value,
|
|
|
|
document.querySelector('#new-addr-separator').value,
|
|
|
|
document.querySelector('#new-addr-domain').value,
|
|
|
|
document.querySelector('#new-addr-key').value,
|
|
|
|
);
|
|
|
|
console.log(new_account);
|
|
|
|
new_account.register();
|
|
|
|
console.log(`Account ${new_account.getName()} added.`);
|
2023-07-25 23:04:03 +02:00
|
|
|
['#new-addr-local-part', '#new-addr-domain', '#new-addr-key'].forEach((selector) => {
|
|
|
|
document.querySelector(selector).value = '';
|
|
|
|
});
|
|
|
|
document.querySelector('#new-addr-separator').value = '+';
|
2023-07-25 22:25:40 +02:00
|
|
|
syncAccountList();
|
|
|
|
closeAllModals();
|
2023-07-25 21:46:03 +02:00
|
|
|
});
|
2023-07-23 15:06:57 +02:00
|
|
|
|
2023-07-25 21:46:03 +02:00
|
|
|
// Add a click event on the new address button to generate it
|
|
|
|
document.querySelector('#btn-generate').addEventListener('click', (event) => {
|
|
|
|
event.preventDefault();
|
2023-07-25 22:27:14 +02:00
|
|
|
const account = getAccountByName(document.querySelector('#account-name').value);
|
|
|
|
const new_address = account.genSubAddr(document.querySelector('#sub-addr-name').value);
|
|
|
|
document.querySelector('#generated-addr').value = new_address;
|
|
|
|
console.log(`New sub-address for account ${account.getName()}: ${new_address}`);
|
2023-07-25 19:01:38 +02:00
|
|
|
});
|
2023-07-25 23:11:03 +02:00
|
|
|
|
|
|
|
// Add a change event on the main form to remove previously generated address
|
|
|
|
['#account-name', '#sub-addr-name'].forEach((selector) => {
|
|
|
|
document.querySelector(selector).addEventListener('change', () => {
|
|
|
|
document.querySelector('#generated-addr').value = '';
|
|
|
|
});
|
|
|
|
});
|
2023-07-25 19:01:38 +02:00
|
|
|
});
|
|
|
|
|
2023-07-23 15:06:57 +02:00
|
|
|
window.addEventListener('load', () => {
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
navigator.serviceWorker.register('/sw.js');
|
|
|
|
}
|
|
|
|
});
|