Moved keys functionality into options now, except for the encryption code, which will stay in the background script.
This commit is contained in:
@@ -35,19 +35,8 @@ browser.runtime.onMessage.addListener(
|
||||
message.payload.msg
|
||||
);
|
||||
break;
|
||||
case 'init':
|
||||
await initialize();
|
||||
break;
|
||||
case 'setProfileIndex':
|
||||
await setProfileIndex(message.payload);
|
||||
break;
|
||||
case 'getProfileIndex':
|
||||
let profileIndex = await getProfileIndex();
|
||||
sendResponse(profileIndex);
|
||||
break;
|
||||
case 'getProfileNames':
|
||||
let profileNames = await getProfileNames();
|
||||
sendResponse(profileNames);
|
||||
case 'generatePrivateKey':
|
||||
sendResponse(generatePrivateKey());
|
||||
break;
|
||||
|
||||
// Options page
|
||||
@@ -122,28 +111,6 @@ browser.runtime.onMessage.addListener(
|
||||
}
|
||||
);
|
||||
|
||||
// General
|
||||
|
||||
async function initialize() {
|
||||
await getOrSetDefault('profileIndex', 0);
|
||||
await getOrSetDefault('profiles', [
|
||||
{
|
||||
name: 'Default',
|
||||
privKey: generatePrivateKey(),
|
||||
hosts: [],
|
||||
relays: [],
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
async function setProfileIndex(profileIndex) {
|
||||
await storage.set({ profileIndex });
|
||||
}
|
||||
|
||||
async function getProfileIndex() {
|
||||
return await get('profileIndex');
|
||||
}
|
||||
|
||||
// Options
|
||||
async function clearData() {
|
||||
let ignoreInstallHook = await storage.get({ ignoreInstallHook: false });
|
||||
@@ -190,11 +157,6 @@ async function getPubKey() {
|
||||
return pubKey;
|
||||
}
|
||||
|
||||
async function getProfileNames() {
|
||||
let profiles = await get('profiles');
|
||||
return profiles.map(p => p.name);
|
||||
}
|
||||
|
||||
async function currentProfile() {
|
||||
let index = await getProfileIndex();
|
||||
let profiles = await get('profiles');
|
||||
@@ -216,22 +178,6 @@ async function newProfile() {
|
||||
return profiles.length - 1;
|
||||
}
|
||||
|
||||
async function deleteProfile(index) {
|
||||
let profiles = await get('profiles');
|
||||
profiles.splice(index, 1);
|
||||
if (profiles.length == 0) {
|
||||
await clearData(); // If we have deleted all of the profiles, let's just start fresh with all new data
|
||||
await initialize();
|
||||
} else {
|
||||
// If the index deleted was the active profile, change the active profile to the next one
|
||||
let profileIndex =
|
||||
this.profileIndex === index
|
||||
? Math.max(index - 1, 0)
|
||||
: this.profileIndex;
|
||||
await storage.set({ profiles, profileIndex });
|
||||
}
|
||||
}
|
||||
|
||||
async function signEvent_(event) {
|
||||
event = { ...event };
|
||||
let privKey = await getPrivKey();
|
||||
@@ -284,16 +230,6 @@ async function get(item) {
|
||||
return (await storage.get(item))[item];
|
||||
}
|
||||
|
||||
async function getOrSetDefault(key, def) {
|
||||
let val = (await storage.get(key))[key];
|
||||
if (val == null || val == undefined) {
|
||||
await storage.set({ [key]: def });
|
||||
return def;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
async function getProfile(index) {
|
||||
let profiles = await get('profiles');
|
||||
return profiles[index];
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
import Alpine from 'alpinejs';
|
||||
import { getProfileNames } from './utils';
|
||||
import {
|
||||
clearData,
|
||||
deleteProfile,
|
||||
getProfileIndex,
|
||||
getProfileNames,
|
||||
initialize,
|
||||
newProfile,
|
||||
saveProfileName,
|
||||
} from './utils';
|
||||
|
||||
const RECOMMENDED_RELAYS = [
|
||||
new URL('wss://relay.damus.io'),
|
||||
@@ -27,7 +35,7 @@ Alpine.data('options', () => ({
|
||||
|
||||
async init(watch = true) {
|
||||
log('Initialize backend.');
|
||||
await browser.runtime.sendMessage({ kind: 'init' });
|
||||
await initialize();
|
||||
|
||||
if (watch) {
|
||||
this.$watch('profileIndex', async () => {
|
||||
@@ -41,13 +49,13 @@ Alpine.data('options', () => ({
|
||||
});
|
||||
}
|
||||
|
||||
log('Setting active index.');
|
||||
await this.getActiveIndex();
|
||||
await this.getProfileNames();
|
||||
await this.getProfileIndex();
|
||||
await this.refreshProfile();
|
||||
},
|
||||
|
||||
async refreshProfile() {
|
||||
await this.loadProfileNames();
|
||||
await this.getProfileNames();
|
||||
await this.getProfileName();
|
||||
await this.getNsec();
|
||||
await this.getNpub();
|
||||
@@ -57,7 +65,7 @@ Alpine.data('options', () => ({
|
||||
|
||||
// Profile functions
|
||||
|
||||
async loadProfileNames() {
|
||||
async getProfileNames() {
|
||||
this.profileNames = await getProfileNames();
|
||||
},
|
||||
|
||||
@@ -68,25 +76,18 @@ Alpine.data('options', () => ({
|
||||
this.pristineProfileName = name;
|
||||
},
|
||||
|
||||
async getActiveIndex() {
|
||||
this.profileIndex = await browser.runtime.sendMessage({
|
||||
kind: 'getProfileIndex',
|
||||
});
|
||||
async getProfileIndex() {
|
||||
this.profileIndex = await getProfileIndex();
|
||||
},
|
||||
|
||||
async newProfile() {
|
||||
let newIndex = await browser.runtime.sendMessage({
|
||||
kind: 'newProfile',
|
||||
});
|
||||
await this.loadProfileNames();
|
||||
let newIndex = await newProfile();
|
||||
await this.getProfileNames();
|
||||
this.profileIndex = newIndex;
|
||||
},
|
||||
|
||||
async deleteProfile() {
|
||||
await browser.runtime.sendMessage({
|
||||
kind: 'deleteProfile',
|
||||
payload: this.profileIndex,
|
||||
});
|
||||
await deleteProfile(this.profileIndex);
|
||||
await this.init(false);
|
||||
},
|
||||
|
||||
@@ -99,11 +100,8 @@ Alpine.data('options', () => ({
|
||||
kind: 'savePrivateKey',
|
||||
payload: [this.profileIndex, this.privKey],
|
||||
});
|
||||
await browser.runtime.sendMessage({
|
||||
kind: 'saveProfileName',
|
||||
payload: [this.profileIndex, this.profileName],
|
||||
});
|
||||
await this.loadProfileNames();
|
||||
await saveProfileName(this.profileIndex, this.profileName);
|
||||
await this.getProfileNames();
|
||||
await this.refreshProfile();
|
||||
},
|
||||
|
||||
@@ -172,7 +170,7 @@ Alpine.data('options', () => ({
|
||||
},
|
||||
|
||||
async clearData() {
|
||||
await browser.runtime.sendMessage({ kind: 'clearData' });
|
||||
await clearData();
|
||||
await this.init(false);
|
||||
},
|
||||
|
||||
|
||||
@@ -7,9 +7,19 @@ export async function bglog(msg, module = null) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function getProfileNames() {
|
||||
export async function getProfiles() {
|
||||
let profiles = await storage.get({ profiles: [] });
|
||||
return profiles.profiles.map(p => p.name);
|
||||
return profiles.profiles;
|
||||
}
|
||||
|
||||
async function getProfile(index) {
|
||||
let profiles = await getProfiles();
|
||||
return profiles[index];
|
||||
}
|
||||
|
||||
export async function getProfileNames() {
|
||||
let profiles = await getProfiles();
|
||||
return profiles.map(p => p.name);
|
||||
}
|
||||
|
||||
export async function getProfileIndex() {
|
||||
@@ -20,3 +30,66 @@ export async function getProfileIndex() {
|
||||
export async function setProfileIndex(profileIndex) {
|
||||
await storage.set({ profileIndex });
|
||||
}
|
||||
|
||||
export async function deleteProfile(index) {
|
||||
let profiles = await getProfiles();
|
||||
let profileIndex = await getProfileIndex();
|
||||
profiles.splice(index, 1);
|
||||
if (profiles.length == 0) {
|
||||
await clearData(); // If we have deleted all of the profiles, let's just start fresh with all new data
|
||||
await initialize();
|
||||
} else {
|
||||
// If the index deleted was the active profile, change the active profile to the next one
|
||||
let newIndex =
|
||||
profileIndex === index ? Math.max(index - 1, 0) : this.profileIndex;
|
||||
await storage.set({ profiles, profileIndex: newIndex });
|
||||
}
|
||||
}
|
||||
|
||||
export async function clearData() {
|
||||
let ignoreInstallHook = await storage.get({ ignoreInstallHook: false });
|
||||
await storage.clear();
|
||||
await storage.set(ignoreInstallHook);
|
||||
}
|
||||
|
||||
async function generatePrivateKey() {
|
||||
return await browser.runtime.sendMessage({ kind: 'generatePrivateKey' });
|
||||
}
|
||||
|
||||
export async function initialize() {
|
||||
await getOrSetDefault('profileIndex', 0);
|
||||
await getOrSetDefault('profiles', [await generateProfile()]);
|
||||
}
|
||||
|
||||
export async function generateProfile() {
|
||||
return {
|
||||
name: 'Default',
|
||||
privKey: await generatePrivateKey(),
|
||||
hosts: [],
|
||||
relays: [],
|
||||
};
|
||||
}
|
||||
|
||||
async function getOrSetDefault(key, def) {
|
||||
let val = (await storage.get(key))[key];
|
||||
if (val == null || val == undefined) {
|
||||
await storage.set({ [key]: def });
|
||||
return def;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
export async function saveProfileName(index, profileName) {
|
||||
let profiles = await getProfiles();
|
||||
profiles[index].name = profileName;
|
||||
await storage.set({ profiles });
|
||||
}
|
||||
|
||||
export async function newProfile() {
|
||||
let profiles = await getProfiles();
|
||||
const newProfile = await generateProfile('New Profile');
|
||||
profiles.push(newProfile);
|
||||
await storage.set({ profiles });
|
||||
return profiles.length - 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user