Sort PGP keys by email and id

This commit is contained in:
the-djmaze 2024-02-26 19:21:40 +01:00
parent 6141ca7936
commit 091895df38
2 changed files with 22 additions and 17 deletions

View file

@ -101,9 +101,13 @@ export const GnuPGUserStore = new class {
};
key.view = () => key.fetch(() => showScreenPopup(OpenPgpKeyPopupView, [key]));
return key;
};
this.publicKeys(oData.Result.public.map(key => initKey(key, 0)));
this.privateKeys(oData.Result.private.map(key => initKey(key, 1)));
},
collator = new Intl.Collator(undefined, {sensitivity: 'base'}),
sort = keys => keys.sort(
(a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.id, b.id)
);
this.publicKeys(sort(oData.Result.public.map(key => initKey(key, 0))));
this.privateKeys(sort(oData.Result.private.map(key => initKey(key, 1))));
console.log('gnupg ready');
}
}

View file

@ -39,6 +39,15 @@ const
}
},
collator = new Intl.Collator(undefined, {sensitivity: 'base'}),
sort = keys => keys.sort(
// (a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.fingerprint, b.fingerprint)
(a, b) => collator.compare(a.emails[0], b.emails[0]) || collator.compare(a.id, b.id)
),
dedup = keys => sort((keys || [])
.filter((v, i, a) => a.findIndex(entry => entry.fingerprint == v.fingerprint) === i)
),
/**
* OpenPGP.js v5 removed the localStorage (keyring)
* This should be compatible with the old OpenPGP.js v2
@ -114,10 +123,6 @@ export const OpenPGPUserStore = new class {
loadKeyrings() {
if (window.openpgp) {
const collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}),
dedup = keys => (keys || [])
.filter((v, i, a) => a.findIndex(entry => entry.fingerprint == v.fingerprint) === i)
.sort((a, b) => collator.compare(a.emails[0], b.emails[0]));
loadOpenPgpKeys(publicKeysItem).then(keys => {
this.publicKeys(dedup(keys));
console.log('openpgp.js public keys loaded');
@ -140,16 +145,12 @@ export const OpenPGPUserStore = new class {
window.openpgp && openpgp.readKey({armoredKey:armoredKey}).then(key => {
if (!key.err) {
key = new OpenPgpKeyModel(armoredKey, key);
if (key.key.isPrivate()) {
if (!this.privateKeys.find(entry => entry.fingerprint == key.fingerprint)) {
this.privateKeys.push(key);
storeOpenPgpKeys(this.privateKeys, privateKeysItem);
}
} else {
if (!this.publicKeys.find(entry => entry.fingerprint == key.fingerprint)) {
this.publicKeys.push(key);
storeOpenPgpKeys(this.publicKeys, publicKeysItem);
}
const isPrivate = key.key.isPrivate(),
keys = isPrivate ? this.privateKeys : this.publicKeys;
if (!keys.find(entry => entry.fingerprint == key.fingerprint)) {
keys.push(key);
keys(sort(keys));
storeOpenPgpKeys(keys, isPrivate ? privateKeysItem : publicKeysItem);
}
}
});