diff --git a/src/util.js b/src/util.js
index bb3db37d..2ff15677 100644
--- a/src/util.js
+++ b/src/util.js
@@ -652,11 +652,23 @@ export default {
* Format user id for internal use.
*/
formatUserId: function(id) {
- // name and email address can be empty but must be of the correct type
- if ((id.name && !util.isString(id.name)) || (id.email && !util.isEmailAddress(id.email))) {
+ // name, email address and comment can be empty but must be of the correct type
+ if ((id.name && !util.isString(id.name)) ||
+ (id.email && !util.isEmailAddress(id.email)) ||
+ (id.comment && !util.isString(id.comment))) {
throw new Error('Invalid user id format');
}
- return new rfc2822.Address(id.name, id.email, id.comment).format();
+ const components = [];
+ if (id.name) {
+ components.push(id.name);
+ }
+ if (id.comment) {
+ components.push(`(${id.comment})`);
+ }
+ if (id.email) {
+ components.push(`<${id.email}>`);
+ }
+ return components.join(' ');
},
/**
diff --git a/test/general/key.js b/test/general/key.js
index 781c245e..fbb3c10a 100644
--- a/test/general/key.js
+++ b/test/general/key.js
@@ -1737,7 +1737,49 @@ function versionSpecificTests() {
return openpgp.generateKey(opt).then(function(key) {
key = key.key;
expect(key.users.length).to.equal(1);
- expect(key.users[0].userId.userid).to.equal('test (test comment)');
+ expect(key.users[0].userId.userid).to.equal('test (test comment) ');
+ expect(key.users[0].userId.name).to.equal(userId.name);
+ expect(key.users[0].userId.email).to.equal(userId.email);
+ expect(key.users[0].userId.comment).to.equal(userId.comment);
+ });
+ });
+
+ it('Generate key - single userid (all missing)', function() {
+ const userId = { name: '', email: '', comment: '' };
+ const opt = {numBits: 512, userIds: userId, passphrase: '123'};
+ if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
+ return openpgp.generateKey(opt).then(function(key) {
+ key = key.key;
+ expect(key.users.length).to.equal(1);
+ expect(key.users[0].userId.userid).to.equal('');
+ expect(key.users[0].userId.name).to.equal(userId.name);
+ expect(key.users[0].userId.email).to.equal(userId.email);
+ expect(key.users[0].userId.comment).to.equal(userId.comment);
+ });
+ });
+
+ it('Generate key - single userid (missing email)', function() {
+ const userId = { name: 'test', email: '', comment: 'test comment' };
+ const opt = {numBits: 512, userIds: userId, passphrase: '123'};
+ if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
+ return openpgp.generateKey(opt).then(function(key) {
+ key = key.key;
+ expect(key.users.length).to.equal(1);
+ expect(key.users[0].userId.userid).to.equal('test (test comment)');
+ expect(key.users[0].userId.name).to.equal(userId.name);
+ expect(key.users[0].userId.email).to.equal(userId.email);
+ expect(key.users[0].userId.comment).to.equal(userId.comment);
+ });
+ });
+
+ it('Generate key - single userid (missing comment)', function() {
+ const userId = { name: 'test', email: 'a@b.com', comment: '' };
+ const opt = {numBits: 512, userIds: userId, passphrase: '123'};
+ if (openpgp.util.getWebCryptoAll()) { opt.numBits = 2048; } // webkit webcrypto accepts minimum 2048 bit keys
+ return openpgp.generateKey(opt).then(function(key) {
+ key = key.key;
+ expect(key.users.length).to.equal(1);
+ expect(key.users[0].userId.userid).to.equal('test ');
expect(key.users[0].userId.name).to.equal(userId.name);
expect(key.users[0].userId.email).to.equal(userId.email);
expect(key.users[0].userId.comment).to.equal(userId.comment);