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
|
message.payload.msg
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 'init':
|
case 'generatePrivateKey':
|
||||||
await initialize();
|
sendResponse(generatePrivateKey());
|
||||||
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);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Options page
|
// 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
|
// Options
|
||||||
async function clearData() {
|
async function clearData() {
|
||||||
let ignoreInstallHook = await storage.get({ ignoreInstallHook: false });
|
let ignoreInstallHook = await storage.get({ ignoreInstallHook: false });
|
||||||
@@ -190,11 +157,6 @@ async function getPubKey() {
|
|||||||
return pubKey;
|
return pubKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getProfileNames() {
|
|
||||||
let profiles = await get('profiles');
|
|
||||||
return profiles.map(p => p.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function currentProfile() {
|
async function currentProfile() {
|
||||||
let index = await getProfileIndex();
|
let index = await getProfileIndex();
|
||||||
let profiles = await get('profiles');
|
let profiles = await get('profiles');
|
||||||
@@ -216,22 +178,6 @@ async function newProfile() {
|
|||||||
return profiles.length - 1;
|
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) {
|
async function signEvent_(event) {
|
||||||
event = { ...event };
|
event = { ...event };
|
||||||
let privKey = await getPrivKey();
|
let privKey = await getPrivKey();
|
||||||
@@ -284,16 +230,6 @@ async function get(item) {
|
|||||||
return (await storage.get(item))[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) {
|
async function getProfile(index) {
|
||||||
let profiles = await get('profiles');
|
let profiles = await get('profiles');
|
||||||
return profiles[index];
|
return profiles[index];
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
import Alpine from 'alpinejs';
|
import Alpine from 'alpinejs';
|
||||||
import { getProfileNames } from './utils';
|
import {
|
||||||
|
clearData,
|
||||||
|
deleteProfile,
|
||||||
|
getProfileIndex,
|
||||||
|
getProfileNames,
|
||||||
|
initialize,
|
||||||
|
newProfile,
|
||||||
|
saveProfileName,
|
||||||
|
} from './utils';
|
||||||
|
|
||||||
const RECOMMENDED_RELAYS = [
|
const RECOMMENDED_RELAYS = [
|
||||||
new URL('wss://relay.damus.io'),
|
new URL('wss://relay.damus.io'),
|
||||||
@@ -27,7 +35,7 @@ Alpine.data('options', () => ({
|
|||||||
|
|
||||||
async init(watch = true) {
|
async init(watch = true) {
|
||||||
log('Initialize backend.');
|
log('Initialize backend.');
|
||||||
await browser.runtime.sendMessage({ kind: 'init' });
|
await initialize();
|
||||||
|
|
||||||
if (watch) {
|
if (watch) {
|
||||||
this.$watch('profileIndex', async () => {
|
this.$watch('profileIndex', async () => {
|
||||||
@@ -41,13 +49,13 @@ Alpine.data('options', () => ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
log('Setting active index.');
|
await this.getProfileNames();
|
||||||
await this.getActiveIndex();
|
await this.getProfileIndex();
|
||||||
await this.refreshProfile();
|
await this.refreshProfile();
|
||||||
},
|
},
|
||||||
|
|
||||||
async refreshProfile() {
|
async refreshProfile() {
|
||||||
await this.loadProfileNames();
|
await this.getProfileNames();
|
||||||
await this.getProfileName();
|
await this.getProfileName();
|
||||||
await this.getNsec();
|
await this.getNsec();
|
||||||
await this.getNpub();
|
await this.getNpub();
|
||||||
@@ -57,7 +65,7 @@ Alpine.data('options', () => ({
|
|||||||
|
|
||||||
// Profile functions
|
// Profile functions
|
||||||
|
|
||||||
async loadProfileNames() {
|
async getProfileNames() {
|
||||||
this.profileNames = await getProfileNames();
|
this.profileNames = await getProfileNames();
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -68,25 +76,18 @@ Alpine.data('options', () => ({
|
|||||||
this.pristineProfileName = name;
|
this.pristineProfileName = name;
|
||||||
},
|
},
|
||||||
|
|
||||||
async getActiveIndex() {
|
async getProfileIndex() {
|
||||||
this.profileIndex = await browser.runtime.sendMessage({
|
this.profileIndex = await getProfileIndex();
|
||||||
kind: 'getProfileIndex',
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async newProfile() {
|
async newProfile() {
|
||||||
let newIndex = await browser.runtime.sendMessage({
|
let newIndex = await newProfile();
|
||||||
kind: 'newProfile',
|
await this.getProfileNames();
|
||||||
});
|
|
||||||
await this.loadProfileNames();
|
|
||||||
this.profileIndex = newIndex;
|
this.profileIndex = newIndex;
|
||||||
},
|
},
|
||||||
|
|
||||||
async deleteProfile() {
|
async deleteProfile() {
|
||||||
await browser.runtime.sendMessage({
|
await deleteProfile(this.profileIndex);
|
||||||
kind: 'deleteProfile',
|
|
||||||
payload: this.profileIndex,
|
|
||||||
});
|
|
||||||
await this.init(false);
|
await this.init(false);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -99,11 +100,8 @@ Alpine.data('options', () => ({
|
|||||||
kind: 'savePrivateKey',
|
kind: 'savePrivateKey',
|
||||||
payload: [this.profileIndex, this.privKey],
|
payload: [this.profileIndex, this.privKey],
|
||||||
});
|
});
|
||||||
await browser.runtime.sendMessage({
|
await saveProfileName(this.profileIndex, this.profileName);
|
||||||
kind: 'saveProfileName',
|
await this.getProfileNames();
|
||||||
payload: [this.profileIndex, this.profileName],
|
|
||||||
});
|
|
||||||
await this.loadProfileNames();
|
|
||||||
await this.refreshProfile();
|
await this.refreshProfile();
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -172,7 +170,7 @@ Alpine.data('options', () => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
async clearData() {
|
async clearData() {
|
||||||
await browser.runtime.sendMessage({ kind: 'clearData' });
|
await clearData();
|
||||||
await this.init(false);
|
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: [] });
|
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() {
|
export async function getProfileIndex() {
|
||||||
@@ -20,3 +30,66 @@ export async function getProfileIndex() {
|
|||||||
export async function setProfileIndex(profileIndex) {
|
export async function setProfileIndex(profileIndex) {
|
||||||
await storage.set({ 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