151 lines
3.8 KiB
TypeScript
151 lines
3.8 KiB
TypeScript
const openpgp = require('openpgp');
|
|
const keyring = new openpgp.Keyring();
|
|
import { KeyStore } from '@src/assets/js/cic-meta/auth';
|
|
|
|
interface MutableKeyStore extends KeyStore {
|
|
loadKeyring(): Promise<void>;
|
|
importKeyPair(publicKey: any, privateKey: any): Promise<void>;
|
|
importPublicKey(publicKey: any): Promise<void>;
|
|
importPrivateKey(privateKey: any): Promise<void>;
|
|
getPublicKeys(): Array<any>;
|
|
getTrustedKeys(): Array<any>;
|
|
getTrustedActiveKeys(): Array<any>;
|
|
getEncryptKeys(): Array<any>;
|
|
getPrivateKeys(): Array<any>;
|
|
getPrivateKey(): any;
|
|
isValidKey(key: any): boolean;
|
|
getFingerprint(): string;
|
|
getKeyId(key: any): string;
|
|
getPrivateKeyId(): string;
|
|
getKeysForId(keyId: string): Array<any>;
|
|
getPublicKeyForId(keyId: string): any;
|
|
getPrivateKeyForId(keyId: string): any;
|
|
getPublicKeyForSubkeyId(subkeyId: string): any;
|
|
getPublicKeysForAddress(address: string): Array<any>;
|
|
removeKeysForId(keyId: string): Array<any>;
|
|
removePublicKeyForId(keyId: string): any;
|
|
removePublicKey(publicKey: any): any;
|
|
clearKeysInKeyring(): void;
|
|
sign(plainText: string): Promise<any>;
|
|
}
|
|
|
|
class MutablePgpKeyStore implements MutableKeyStore{
|
|
|
|
async loadKeyring(): Promise<void> {
|
|
await keyring.load();
|
|
// clear any keys already in the keychain
|
|
// keyring.clear();
|
|
await keyring.store();
|
|
}
|
|
|
|
async importKeyPair(publicKey: any, privateKey: any): Promise<void> {
|
|
await keyring.publicKeys.importKey(publicKey);
|
|
await keyring.privateKeys.importKey(privateKey);
|
|
}
|
|
|
|
async importPublicKey(publicKey: any): Promise<void> {
|
|
await keyring.publicKeys.importKey(publicKey);
|
|
}
|
|
|
|
async importPrivateKey(privateKey: any): Promise<void> {
|
|
await keyring.privateKeys.importKey(privateKey);
|
|
}
|
|
|
|
getPublicKeys(): Array<any> {
|
|
return keyring.publicKeys.keys;
|
|
}
|
|
|
|
getTrustedKeys(): Array<any> {
|
|
return keyring.publicKeys.keys;
|
|
}
|
|
|
|
getTrustedActiveKeys(): Array<any> {
|
|
return keyring.publicKeys.keys;
|
|
}
|
|
|
|
getEncryptKeys(): Array<any> {
|
|
return [];
|
|
}
|
|
|
|
getPrivateKeys(): Array<any> {
|
|
return keyring.privateKeys.keys;
|
|
}
|
|
|
|
getPrivateKey(): any {
|
|
return keyring.privateKeys.keys[0];
|
|
}
|
|
|
|
isValidKey(key): boolean {
|
|
return typeof key === openpgp.Key;
|
|
}
|
|
|
|
getFingerprint(): string {
|
|
return keyring.privateKeys.keys[0].keyPacket.fingerprint;
|
|
}
|
|
|
|
getKeyId(key: any): string {
|
|
return key.getKeyId().toHex();
|
|
}
|
|
|
|
getPrivateKeyId(): string {
|
|
return keyring.privateKeys.keys[0].getKeyId().toHex();
|
|
}
|
|
|
|
getKeysForId(keyId: string): Array<any> {
|
|
return keyring.getKeysForId(keyId);
|
|
}
|
|
|
|
getPublicKeyForId(keyId): any {
|
|
return keyring.publicKeys.getForId(keyId);
|
|
}
|
|
|
|
getPrivateKeyForId(keyId): any {
|
|
return keyring.privateKeys.getForId(keyId);
|
|
}
|
|
|
|
getPublicKeyForSubkeyId(subkeyId): any {
|
|
return keyring.publicKeys.getForId(subkeyId, true);
|
|
}
|
|
|
|
getPublicKeysForAddress(address): Array<any> {
|
|
return keyring.publicKeys.getForAddress(address);
|
|
}
|
|
|
|
removeKeysForId(keyId): Array<any> {
|
|
return keyring.removeKeysForId(keyId);
|
|
}
|
|
|
|
removePublicKeyForId(keyId): any {
|
|
return keyring.publicKeys.removeForId(keyId);
|
|
}
|
|
|
|
removePublicKey(publicKey: any): any {
|
|
const keyId = publicKey.getKeyId().toHex();
|
|
return keyring.publicKeys.removeForId(keyId);
|
|
}
|
|
|
|
clearKeysInKeyring(): void {
|
|
keyring.clear();
|
|
}
|
|
|
|
async sign(plainText): Promise<any> {
|
|
const privateKey = this.getPrivateKey();
|
|
if (!privateKey.isDecrypted()) {
|
|
const password = window.prompt('password');
|
|
await privateKey.decrypt(password);
|
|
}
|
|
const opts = {
|
|
message: openpgp.message.fromText(plainText),
|
|
privateKeys: [privateKey],
|
|
detached: true,
|
|
};
|
|
const signatureObject = await openpgp.sign(opts);
|
|
return signatureObject.signature;
|
|
}
|
|
}
|
|
|
|
export {
|
|
MutablePgpKeyStore,
|
|
MutableKeyStore
|
|
};
|