Use async function instead of explicit promises

This commit is contained in:
JC Brand 2018-10-17 23:58:11 +02:00
parent abd9786c0f
commit 94a1852b4f

View File

@ -729,17 +729,17 @@
}); });
}, },
generateBundle () { async generateBundle () {
/* The first thing that needs to happen if a client wants to /* The first thing that needs to happen if a client wants to
* start using OMEMO is they need to generate an IdentityKey * start using OMEMO is they need to generate an IdentityKey
* and a Device ID. The IdentityKey is a Curve25519 [6] * and a Device ID. The IdentityKey is a Curve25519 [6]
* public/private Key pair. The Device ID is a randomly * public/private Key pair. The Device ID is a randomly
* generated integer between 1 and 2^31 - 1. * generated integer between 1 and 2^31 - 1.
*/ */
const bundle = {}; const identity_keypair = await libsignal.KeyHelper.generateIdentityKeyPair();
return libsignal.KeyHelper.generateIdentityKeyPair()
.then(identity_keypair => { const bundle = {},
const identity_key = u.arrayBufferToBase64(identity_keypair.pubKey), identity_key = u.arrayBufferToBase64(identity_keypair.pubKey),
device_id = generateDeviceID(); device_id = generateDeviceID();
bundle['identity_key'] = identity_key; bundle['identity_key'] = identity_key;
@ -752,23 +752,21 @@
}, },
'identity_key': identity_key 'identity_key': identity_key
}); });
return libsignal.KeyHelper.generateSignedPreKey(identity_keypair, 0); const signed_prekey = await libsignal.KeyHelper.generateSignedPreKey(identity_keypair, 0);
}).then(signed_prekey => {
_converse.omemo_store.storeSignedPreKey(signed_prekey); _converse.omemo_store.storeSignedPreKey(signed_prekey);
bundle['signed_prekey'] = { bundle['signed_prekey'] = {
'id': signed_prekey.keyId, 'id': signed_prekey.keyId,
'public_key': u.arrayBufferToBase64(signed_prekey.keyPair.privKey), 'public_key': u.arrayBufferToBase64(signed_prekey.keyPair.privKey),
'signature': u.arrayBufferToBase64(signed_prekey.signature) 'signature': u.arrayBufferToBase64(signed_prekey.signature)
} }
return Promise.all(_.map(_.range(0, _converse.NUM_PREKEYS), id => libsignal.KeyHelper.generatePreKey(id))); const keys = await Promise.all(_.map(_.range(0, _converse.NUM_PREKEYS), id => libsignal.KeyHelper.generatePreKey(id)));
}).then(keys => {
_.forEach(keys, k => _converse.omemo_store.storePreKey(k.keyId, k.keyPair)); _.forEach(keys, k => _converse.omemo_store.storePreKey(k.keyId, k.keyPair));
const devicelist = _converse.devicelists.get(_converse.bare_jid), const devicelist = _converse.devicelists.get(_converse.bare_jid),
device = devicelist.devices.create({'id': bundle.device_id, 'jid': _converse.bare_jid}), device = devicelist.devices.create({'id': bundle.device_id, 'jid': _converse.bare_jid}),
marshalled_keys = _.map(keys, k => ({'id': k.keyId, 'key': u.arrayBufferToBase64(k.keyPair.pubKey)})); marshalled_keys = _.map(keys, k => ({'id': k.keyId, 'key': u.arrayBufferToBase64(k.keyPair.pubKey)}));
bundle['prekeys'] = marshalled_keys; bundle['prekeys'] = marshalled_keys;
device.save('bundle', bundle); device.save('bundle', bundle);
});
}, },
fetchSession () { fetchSession () {