From 38e6272dc617e06e3a618311c795bc268a688197 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Fri, 5 Oct 2018 13:48:39 +1000 Subject: [PATCH] Use async/await in addMockContact instead of nested promises --- libtextsecure/account_manager.js | 72 +++++++++++++------------------- 1 file changed, 30 insertions(+), 42 deletions(-) diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index e6c241d7a..4e3b05c39 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -62,53 +62,41 @@ }) ); }, - addMockContact(doSave) { + async addMockContact(doSave) { if (doSave === undefined) { doSave = true; } - libsignal.KeyHelper.generateIdentityKeyPair().then(keyPair => { - const pubKey = StringView.arrayBufferToHex(keyPair.pubKey); - const privKey = StringView.arrayBufferToHex(keyPair.privKey); - log.info('contact pubkey ' + pubKey); - log.info('contact privkey ' + privKey); - const signedKeyId = Math.floor((Math.random() * 1000) + 1); - const promises = [ - libsignal.KeyHelper.generateSignedPreKey(keyPair, signedKeyId) - .then((signedPreKey) => { - const contactSignedPreKey = { - publicKey: signedPreKey.keyPair.pubKey, - signature: signedPreKey.signature, - keyId: signedPreKey.keyId - }; - if (doSave) { - return textsecure.storage.protocol.storeContactSignedPreKey(pubKey, contactSignedPreKey); - } - else { - log.info('signed prekey: ' + StringView.arrayBufferToHex(contactSignedPreKey.publicKey)); - log.info('signature: ' + StringView.arrayBufferToHex(contactSignedPreKey.signature)); - return Promise.resolve(); - } - }), - ]; + const keyPair = await libsignal.KeyHelper.generateIdentityKeyPair(); + const pubKey = StringView.arrayBufferToHex(keyPair.pubKey); + const privKey = StringView.arrayBufferToHex(keyPair.privKey); + log.info('contact pubkey ' + pubKey); + log.info('contact privkey ' + privKey); + const signedKeyId = Math.floor((Math.random() * 1000) + 1); + + const signedPreKey = await libsignal.KeyHelper.generateSignedPreKey(keyPair, signedKeyId); + const contactSignedPreKey = { + publicKey: signedPreKey.keyPair.pubKey, + signature: signedPreKey.signature, + keyId: signedPreKey.keyId + }; + if (doSave) { + await textsecure.storage.protocol.storeContactSignedPreKey(pubKey, contactSignedPreKey); + } + else { + log.info('signed prekey: ' + StringView.arrayBufferToHex(contactSignedPreKey.publicKey)); + log.info('signature: ' + StringView.arrayBufferToHex(contactSignedPreKey.signature)); + } - for (let keyId = 0; keyId < 10; keyId += 1) { - promises.push( - libsignal.KeyHelper.generatePreKey(keyId) - .then((preKey) => { - if (doSave) { - return textsecure.storage.protocol.storeContactPreKey(pubKey, { publicKey: preKey.keyPair.pubKey, keyId: keyId }); - } - else { - log.info('signed prekey: ' + StringView.arrayBufferToHex(preKey.keyPair.pubKey)); - return Promise.resolve(); - } - }), - ) + for (let keyId = 0; keyId < 10; keyId += 1) { + const preKey = await libsignal.KeyHelper.generatePreKey(keyId); + if (doSave) { + await textsecure.storage.protocol.storeContactPreKey(pubKey, { publicKey: preKey.keyPair.pubKey, keyId: keyId }); } - return Promise.all(promises).then( - log.info("Added mock contact") - ); - }); + else { + log.info('signed prekey: ' + StringView.arrayBufferToHex(preKey.keyPair.pubKey)); + } + } + log.info("Added mock contact") }, registerSecondDevice(setProvisioningUrl, confirmNumber, progressCallback) { const createAccount = this.createAccount.bind(this);