<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">what about a choice of either.<div><br></div><div>the developer can decide what they want to do.</div><div><br></div><div>In DataManager, they can either use callbacks or promises, &nbsp;or both at the same time<br><div><br><div><div>On Nov 21, 2013, at 11:07 AM, Erik Jan de Wit &lt;<a href="mailto:edewit@redhat.com">edewit@redhat.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>Just wanted to talk about the api for cordova crypto, right now the api looks like this:</div><div><br></div><div><pre class="line-pre" style="font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; width: 744px; margin-top: 0px; margin-bottom: 0px; padding: 0px; background-color: rgb(255, 255, 255); "><div class="line" id="file-crypto-cordova-api-js-LC8"><span class="kd" style="font-weight: bold;">var</span> <span class="nx">agCrypto</span> <span class="o" style="font-weight: bold;">=</span> <span class="k" style="font-weight: bold;">new</span> <span class="nx">AeroGear</span><span class="p">.</span><span class="nx">Crypto</span><span class="p">();</span></div><div class="line" id="file-crypto-cordova-api-js-LC9"><span class="nx">agCrypto</span><span class="p">.</span><span class="nx">deriveKey</span><span class="p">(</span> <span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">rawPassword</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-cordova-api-js-LC10">    <span class="kd" style="font-weight: bold;">var</span> <span class="nx">options</span> <span class="o" style="font-weight: bold;">=</span> <span class="p">{</span></div><div class="line" id="file-crypto-cordova-api-js-LC11">            <span class="nx">IV</span><span class="o" style="font-weight: bold;">:</span> <span class="s2" style="color: rgb(221, 17, 68);">"69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37"</span><span class="p">,</span></div><div class="line" id="file-crypto-cordova-api-js-LC12">            <span class="nx">AAD</span><span class="o" style="font-weight: bold;">:</span> <span class="s2" style="color: rgb(221, 17, 68);">"feedfacedeadbeeffeedfacedeadbeefabaddad2"</span><span class="p">,</span></div><div class="line" id="file-crypto-cordova-api-js-LC13">            <span class="nx">key</span><span class="o" style="font-weight: bold;">:</span> <span class="nx">rawPassword</span><span class="p">,</span></div><div class="line" id="file-crypto-cordova-api-js-LC14">            <span class="nx">data</span><span class="o" style="font-weight: bold;">:</span> <span class="s2" style="color: rgb(221, 17, 68);">"My Bonnie lies over the ocean, my Bonnie lies over the sea"</span></div><div class="line" id="file-crypto-cordova-api-js-LC15">        <span class="p">};</span></div><div class="line" id="file-crypto-cordova-api-js-LC16">    <span class="nx">agCrypto</span><span class="p">.</span><span class="nx">encrypt</span><span class="p">(</span> <span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">cipherText</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-cordova-api-js-LC17">        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">cipherText</span><span class="p">)</span></div><div class="line" id="file-crypto-cordova-api-js-LC18">    <span class="p">},</span> <span class="nx">options</span> <span class="p">);</span> </div><div class="line" id="file-crypto-cordova-api-js-LC19"><span class="p">},</span> <span class="nx">errorHandler</span><span class="p">,</span> <span class="s1" style="color: rgb(221, 17, 68);">'myPassword'</span> <span class="p">);</span></div></pre><div><br></div></div><div>So this calls the deriveKey function and within that function it calls the encrypt now this nesting is going to get worse when we add getRandomValue on top of this so I suggest we change it a bit to the style we also use in the data manager:</div><div><br></div><div><pre class="line-pre" style="font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; width: 744px; margin-top: 0px; margin-bottom: 0px; padding: 0px; background-color: rgb(255, 255, 255); "><div class="line" id="file-object-crypto-js-LC1"><span class="nx">agCrypto</span><span class="p">.</span><span class="nx">getRandomValue</span><span class="p">({</span></div><div class="line" id="file-object-crypto-js-LC2">    <span class="nx">success</span><span class="o" style="font-weight: bold;">:</span> <span class="kd" style="font-weight: bold;">function</span> <span class="p">(</span><span class="nx">generatedIV</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-object-crypto-js-LC3">        <span class="nx">agCrypto</span><span class="p">.</span><span class="nx">deriveKey</span><span class="p">({</span></div><div class="line" id="file-object-crypto-js-LC4">            <span class="nx">password</span><span class="o" style="font-weight: bold;">:</span> <span class="s1" style="color: rgb(221, 17, 68);">'my password'</span><span class="p">,</span></div><div class="line" id="file-object-crypto-js-LC5">            <span class="nx">success</span><span class="o" style="font-weight: bold;">:</span> <span class="kd" style="font-weight: bold;">function</span> <span class="p">(</span><span class="nx">rawPassword</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-object-crypto-js-LC6">                <span class="nx">agCrypto</span><span class="p">.</span><span class="nx">encrypt</span><span class="p">({</span></div><div class="line" id="file-object-crypto-js-LC7">                    <span class="nx">options</span><span class="o" style="font-weight: bold;">:</span> <span class="p">{</span></div><div class="line" id="file-object-crypto-js-LC8">                        <span class="nx">IV</span><span class="o" style="font-weight: bold;">:</span> <span class="nx">generatedIV</span><span class="p">,</span></div><div class="line" id="file-object-crypto-js-LC9">                        <span class="nx">key</span><span class="o" style="font-weight: bold;">:</span> <span class="nx">rawPassword</span><span class="p">,</span></div><div class="line" id="file-object-crypto-js-LC10">                        <span class="nx">data</span><span class="o" style="font-weight: bold;">:</span> <span class="s2" style="color: rgb(221, 17, 68);">"My Bonnie lies over the ocean, my Bonnie lies over the sea"</span></div><div class="line" id="file-object-crypto-js-LC11">                    <span class="p">},</span></div><div class="line" id="file-object-crypto-js-LC12">                    <span class="nx">success</span><span class="o" style="font-weight: bold;">:</span> <span class="kd" style="font-weight: bold;">function</span> <span class="p">(</span><span class="nx">cipherText</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-object-crypto-js-LC13">                        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">cipherText</span><span class="p">);</span></div><div class="line" id="file-object-crypto-js-LC14">                    <span class="p">}</span></div><div class="line" id="file-object-crypto-js-LC15">                <span class="p">});</span></div><div class="line" id="file-object-crypto-js-LC16">            <span class="p">}</span></div><div class="line" id="file-object-crypto-js-LC17">        <span class="p">});</span></div><div class="line" id="file-object-crypto-js-LC18">    <span class="p">}</span></div><div class="line" id="file-object-crypto-js-LC19"><span class="p">});</span></div></pre><div><br></div></div><div>Although this style is more readable and the nesting could be broken up by introducing references to functions it's still not very nice and it's getting more away from the original javascript api. One last solution we could use be to use promises, because we already have a dependency to the jquery promises we could reuse them here and get rid of the nesting like so:</div><div><br></div><div><pre class="line-pre" style="font-family: Consolas, 'Liberation Mono', Courier, monospace; font-size: 12px; line-height: 16px; width: 744px; margin-top: 0px; margin-bottom: 0px; padding: 0px; background-color: rgb(255, 255, 255); "><div class="line" id="file-crypto-promise-js-LC1"><span class="nx">agCrypto</span><span class="p">.</span><span class="nx">deriveKey</span><span class="p">(</span><span class="s1" style="color: rgb(221, 17, 68);">'my password'</span><span class="p">).</span><span class="nx">then</span><span class="p">(</span><span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">rawPassword</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-promise-js-LC2">        <span class="k" style="font-weight: bold;">return</span> <span class="nx">agCrypto</span><span class="p">.</span><span class="nx">getRandomValue</span><span class="p">();</span></div><div class="line" id="file-crypto-promise-js-LC3"><span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">generatedIV</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-promise-js-LC4">        <span class="k" style="font-weight: bold;">return</span> <span class="nx">agCrypto</span><span class="p">.</span><span class="nx">encrypt</span><span class="p">({</span></div><div class="line" id="file-crypto-promise-js-LC5">                <span class="nx">IV</span><span class="o" style="font-weight: bold;">:</span> <span class="nx">generatedIV</span><span class="p">,</span></div><div class="line" id="file-crypto-promise-js-LC6">                <span class="nx">key</span><span class="o" style="font-weight: bold;">:</span> <span class="nx">rawPassword</span><span class="p">,</span></div><div class="line" id="file-crypto-promise-js-LC7">                <span class="nx">data</span><span class="o" style="font-weight: bold;">:</span> <span class="s2" style="color: rgb(221, 17, 68);">"My Bonnie lies over the ocean, my Bonnie lies over the sea"</span></div><div class="line" id="file-crypto-promise-js-LC8">        <span class="p">});</span></div><div class="line" id="file-crypto-promise-js-LC9"><span class="p">}).</span><span class="nx">then</span><span class="p">(</span><span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">cipherText</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-promise-js-LC10">        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">cipherText</span><span class="p">);</span></div><div class="line" id="file-crypto-promise-js-LC11"><span class="p">},</span> <span class="kd" style="font-weight: bold;">function</span><span class="p">(</span><span class="nx">error</span><span class="p">)</span> <span class="p">{</span></div><div class="line" id="file-crypto-promise-js-LC12">  <span class="c1" style="color: rgb(153, 153, 136); font-style: italic;">// there was some error.</span></div><div class="line" id="file-crypto-promise-js-LC13"><span class="p">});</span></div></pre><div><br></div></div><div>Now in my opinion this is the most readable of them all, but the trick is now it looks nothing like the original javascript api, but maybe we need to let go of that in favour of readability. Which version do you prefer and what do you think?</div><div><br></div><div>Cheers,</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Erik Jan</div></div>_______________________________________________<br>aerogear-dev mailing list<br><a href="mailto:aerogear-dev@lists.jboss.org">aerogear-dev@lists.jboss.org</a><br>https://lists.jboss.org/mailman/listinfo/aerogear-dev</blockquote></div><br></div></div></body></html>