Skip to content

Commit

Permalink
Rename SubKey class
Browse files Browse the repository at this point in the history
  • Loading branch information
larabr committed May 18, 2021
1 parent 53dc7ad commit 60c5c07
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
18 changes: 9 additions & 9 deletions openpgp.d.ts
Expand Up @@ -22,7 +22,7 @@ export function reformatKey(options: { privateKey: Key; userIDs?: UserID|UserID[
export class Key {
constructor(packetlist: PacketList<AnyPacket>);
public primaryKey: PublicKeyPacket | SecretKeyPacket;
public subkeys: SubKey[];
public subkeys: Subkey[];
public users: User[];
public revocationSignatures: SignaturePacket[];
private keyPacket: PublicKeyPacket | SecretKeyPacket;
Expand All @@ -44,19 +44,19 @@ export class Key {
public isRevoked(signature: SignaturePacket, key?: AnyKeyPacket, date?: Date, config?: Config): Promise<boolean>;
public revoke(reason: { flag?: enums.reasonForRevocation; string?: string; }, date?: Date, config?: Config): Promise<Key>;
public getRevocationCertificate(date?: Date, config?: Config): Promise<Stream<string> | string | undefined>;
public getEncryptionKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<Key | SubKey>;
public getSigningKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<Key | SubKey>;
public getKeys(keyID?: KeyID): (Key | SubKey)[];
public getSubkeys(keyID?: KeyID): SubKey[];
public getEncryptionKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<Key | Subkey>;
public getSigningKey(keyID?: KeyID, date?: Date | null, userID?: UserID, config?: Config): Promise<Key | Subkey>;
public getKeys(keyID?: KeyID): (Key | Subkey)[];
public getSubkeys(keyID?: KeyID): Subkey[];
public isDecrypted(): boolean;
public getFingerprint(): string;
public getCreationTime(): Date;
public getAlgorithmInfo(): AlgorithmInfo;
public getKeyID(): KeyID;
public addSubkey(options: SubKeyOptions): Promise<Key>;
public addSubkey(options: SubkeyOptions): Promise<Key>;
}

export class SubKey {
export class Subkey {
constructor(subkeyPacket: SecretSubkeyPacket | PublicSubkeyPacket);
private keyPacket: SecretSubkeyPacket | PublicSubkeyPacket;
public bindingSignatures: SignaturePacket[];
Expand Down Expand Up @@ -618,11 +618,11 @@ interface KeyOptions {
rsaBits?: number;
keyExpirationTime?: number;
date?: Date;
subkeys?: SubKeyOptions[];
subkeys?: SubkeyOptions[];
config?: PartialConfig;
}

interface SubKeyOptions {
interface SubkeyOptions {
type?: 'ecc' | 'rsa';
curve?: EllipticCurveName;
rsaBits?: number;
Expand Down
32 changes: 16 additions & 16 deletions src/key/key.js
Expand Up @@ -26,7 +26,7 @@ import defaultConfig from '../config';
import enums from '../enums';
import util from '../util';
import User from './user';
import SubKey from './subkey';
import Subkey from './subkey';
import * as helper from './helper';

// A key revocation certificate can contain the following packets
Expand Down Expand Up @@ -94,7 +94,7 @@ class Key {
case enums.packet.publicSubkey:
case enums.packet.secretSubkey:
user = null;
subkey = new SubKey(packetlist[i]);
subkey = new Subkey(packetlist[i]);
this.subkeys.push(subkey);
break;
case enums.packet.signature:
Expand Down Expand Up @@ -191,7 +191,7 @@ class Key {
* Returns an array containing all public or private subkeys matching keyID;
* If no keyID is given, returns all subkeys.
* @param {type/keyID} [keyID] - key ID to look for
* @returns {Array<SubKey>} array of subkeys
* @returns {Array<Subkey>} array of subkeys
*/
getSubkeys(keyID = null) {
const subkeys = this.subkeys.filter(subkey => (
Expand All @@ -204,7 +204,7 @@ class Key {
* Returns an array containing all public or private keys matching keyID.
* If no keyID is given, returns all keys, starting with the primary key.
* @param {type/keyid~KeyID} [keyID] - key ID to look for
* @returns {Array<Key|SubKey>} array of keys
* @returns {Array<Key|Subkey>} array of keys
*/
getKeys(keyID = null) {
const keys = [];
Expand Down Expand Up @@ -298,7 +298,7 @@ class Key {
* @param {Date} [date] - Use the given date for verification instead of the current time
* @param {Object} userID, optional user ID
* @param {Object} [config] - Full configuration, defaults to openpgp.config
* @returns {Promise<Key|SubKey|null>} Key or null if no signing key has been found.
* @returns {Promise<Key|Subkey|null>} Key or null if no signing key has been found.
* @async
*/
async getSigningKey(keyID = null, date = new Date(), userID = {}, config = defaultConfig) {
Expand Down Expand Up @@ -351,7 +351,7 @@ class Key {
* @param {Date} date, optional
* @param {String} userID, optional
* @param {Object} [config] - Full configuration, defaults to openpgp.config
* @returns {Promise<Key|SubKey|null>} Key or null if no encryption key has been found.
* @returns {Promise<Key|Subkey|null>} Key or null if no encryption key has been found.
* @async
*/
async getEncryptionKey(keyID, date = new Date(), userID = {}, config = defaultConfig) {
Expand Down Expand Up @@ -397,7 +397,7 @@ class Key {
* @param {Date} date, optional
* @param {String} userID, optional
* @param {Object} [config] - Full configuration, defaults to openpgp.config
* @returns {Promise<Array<Key|SubKey>>} Array of decryption keys.
* @returns {Promise<Array<Key|Subkey>>} Array of decryption keys.
* @async
*/
async getDecryptionKeys(keyID, date = new Date(), userID = {}, config = defaultConfig) {
Expand Down Expand Up @@ -643,9 +643,9 @@ class Key {
if (this.isPublic() && key.isPrivate()) {
// check for equal subkey packets
const equal = (this.subkeys.length === key.subkeys.length) &&
(this.subkeys.every(destSubKey => {
return key.subkeys.some(srcSubKey => {
return destSubKey.hasSameFingerprintAs(srcSubKey);
(this.subkeys.every(destSubkey => {
return key.subkeys.some(srcSubkey => {
return destSubkey.hasSameFingerprintAs(srcSubkey);
});
}));
if (!equal) {
Expand Down Expand Up @@ -677,16 +677,16 @@ class Key {
}));
// TODO replace when Promise.some or Promise.any are implemented
// subkeys
await Promise.all(key.subkeys.map(async srcSubKey => {
await Promise.all(key.subkeys.map(async srcSubkey => {
let found = false;
await Promise.all(this.subkeys.map(async dstSubKey => {
if (dstSubKey.hasSameFingerprintAs(srcSubKey)) {
await dstSubKey.update(srcSubKey, this.keyPacket, config);
await Promise.all(this.subkeys.map(async dstSubkey => {
if (dstSubkey.hasSameFingerprintAs(srcSubkey)) {
await dstSubkey.update(srcSubkey, this.keyPacket, config);
found = true;
}
}));
if (!found) {
this.subkeys.push(srcSubKey);
this.subkeys.push(srcSubkey);
}
}));
}
Expand Down Expand Up @@ -901,7 +901,7 @@ class Key {

['getKeyID', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'hasSameFingerprintAs'].forEach(name => {
Key.prototype[name] =
SubKey.prototype[name];
Subkey.prototype[name];
});

export default Key;
Expand Down
32 changes: 16 additions & 16 deletions src/key/subkey.js
@@ -1,5 +1,5 @@
/**
* @module key/SubKey
* @module key/Subkey
* @private
*/

Expand All @@ -10,17 +10,17 @@ import defaultConfig from '../config';

/**
* Class that represents a subkey packet and the relevant signatures.
* @borrows PublicSubkeyPacket#getKeyID as SubKey#getKeyID
* @borrows PublicSubkeyPacket#getFingerprint as SubKey#getFingerprint
* @borrows PublicSubkeyPacket#hasSameFingerprintAs as SubKey#hasSameFingerprintAs
* @borrows PublicSubkeyPacket#getAlgorithmInfo as SubKey#getAlgorithmInfo
* @borrows PublicSubkeyPacket#getCreationTime as SubKey#getCreationTime
* @borrows PublicSubkeyPacket#isDecrypted as SubKey#isDecrypted
* @borrows PublicSubkeyPacket#getKeyID as Subkey#getKeyID
* @borrows PublicSubkeyPacket#getFingerprint as Subkey#getFingerprint
* @borrows PublicSubkeyPacket#hasSameFingerprintAs as Subkey#hasSameFingerprintAs
* @borrows PublicSubkeyPacket#getAlgorithmInfo as Subkey#getAlgorithmInfo
* @borrows PublicSubkeyPacket#getCreationTime as Subkey#getCreationTime
* @borrows PublicSubkeyPacket#isDecrypted as Subkey#isDecrypted
*/
class SubKey {
class Subkey {
constructor(subkeyPacket) {
if (!(this instanceof SubKey)) {
return new SubKey(subkeyPacket);
if (!(this instanceof Subkey)) {
return new Subkey(subkeyPacket);
}
this.keyPacket = subkeyPacket;
this.bindingSignatures = [];
Expand Down Expand Up @@ -113,7 +113,7 @@ class SubKey {

/**
* Update subkey with new components from specified subkey
* @param {SubKey} subkey - Source subkey to merge
* @param {Subkey} subkey - Source subkey to merge
* @param {SecretKeyPacket|
SecretSubkeyPacket} primaryKey primary key used for validation
* @param {Object} [config] - Full configuration, defaults to openpgp.config
Expand All @@ -122,7 +122,7 @@ class SubKey {
*/
async update(subkey, primaryKey, config = defaultConfig) {
if (!this.hasSameFingerprintAs(subkey)) {
throw new Error('SubKey update method: fingerprints of subkeys not equal');
throw new Error('Subkey update method: fingerprints of subkeys not equal');
}
// key packet
if (this.keyPacket.constructor.tag === enums.packet.publicSubkey &&
Expand Down Expand Up @@ -162,7 +162,7 @@ class SubKey {
* @param {String} reasonForRevocation.string optional, string explaining the reason for revocation
* @param {Date} date - optional, override the creationtime of the revocation signature
* @param {Object} [config] - Full configuration, defaults to openpgp.config
* @returns {Promise<SubKey>} New subkey with revocation signature.
* @returns {Promise<Subkey>} New subkey with revocation signature.
* @async
*/
async revoke(
Expand All @@ -175,7 +175,7 @@ class SubKey {
config = defaultConfig
) {
const dataToSign = { key: primaryKey, bind: this.keyPacket };
const subkey = new SubKey(this.keyPacket);
const subkey = new Subkey(this.keyPacket);
subkey.revocationSignatures.push(await helper.createSignaturePacket(dataToSign, null, primaryKey, {
signatureType: enums.signature.subkeyRevocation,
reasonForRevocationFlag: enums.write(enums.reasonForRevocation, reasonForRevocationFlag),
Expand All @@ -191,10 +191,10 @@ class SubKey {
}

['getKeyID', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'isDecrypted'].forEach(name => {
SubKey.prototype[name] =
Subkey.prototype[name] =
function() {
return this.keyPacket[name]();
};
});

export default SubKey;
export default Subkey;
2 changes: 1 addition & 1 deletion test/general/key.js
Expand Up @@ -2904,7 +2904,7 @@ module.exports = () => describe('Key', function() {
expect(expirationTime.toISOString()).to.be.equal('1970-01-01T00:22:18.000Z');
});

it('Method getExpirationTime V4 SubKey', async function() {
it('Method getExpirationTime V4 Subkey', async function() {
const [, pubKey] = await openpgp.readKeys({ armoredKeys: twoKeys });
expect(pubKey).to.exist;
expect(pubKey).to.be.an.instanceof(openpgp.Key);
Expand Down
4 changes: 2 additions & 2 deletions test/general/openpgp.js
Expand Up @@ -2935,8 +2935,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() {
privateKey: await openpgp.readKey({ armoredKey: priv_key_de }),
passphrase
});
return privKeyDE.subkeys[0].revoke(privKeyDE.primaryKey).then(async function(revSubKey) {
pubKeyDE.subkeys[0] = revSubKey;
return privKeyDE.subkeys[0].revoke(privKeyDE.primaryKey).then(async function(revSubkey) {
pubKeyDE.subkeys[0] = revSubkey;
return openpgp.encrypt({
message: await openpgp.createMessage({ text: plaintext }),
encryptionKeys: pubKeyDE,
Expand Down

0 comments on commit 60c5c07

Please sign in to comment.