[aerogear-dev] cordova javascript api

Lucas Holmquist lholmqui at redhat.com
Thu Nov 21 11:14:26 EST 2013


what about a choice of either.

the developer can decide what they want to do.

In DataManager, they can either use callbacks or promises,  or both at the same time

On Nov 21, 2013, at 11:07 AM, Erik Jan de Wit <edewit at redhat.com> wrote:

> Hi,
> 
> Just wanted to talk about the api for cordova crypto, right now the api looks like this:
> 
> var agCrypto = new AeroGear.Crypto();
> agCrypto.deriveKey( function(rawPassword) {
>     var options = {
>             IV: "69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37",
>             AAD: "feedfacedeadbeeffeedfacedeadbeefabaddad2",
>             key: rawPassword,
>             data: "My Bonnie lies over the ocean, my Bonnie lies over the sea"
>         };
>     agCrypto.encrypt( function(cipherText) {
>         console.log(cipherText)
>     }, options ); 
> }, errorHandler, 'myPassword' );
> 
> 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:
> 
> agCrypto.getRandomValue({
>     success: function (generatedIV) {
>         agCrypto.deriveKey({
>             password: 'my password',
>             success: function (rawPassword) {
>                 agCrypto.encrypt({
>                     options: {
>                         IV: generatedIV,
>                         key: rawPassword,
>                         data: "My Bonnie lies over the ocean, my Bonnie lies over the sea"
>                     },
>                     success: function (cipherText) {
>                         console.log(cipherText);
>                     }
>                 });
>             }
>         });
>     }
> });
> 
> 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:
> 
> agCrypto.deriveKey('my password').then(function(rawPassword) {
> 	return agCrypto.getRandomValue();
> }).then(function(generatedIV) {
> 	return agCrypto.encrypt({
> 		IV: generatedIV,
> 		key: rawPassword,
> 		data: "My Bonnie lies over the ocean, my Bonnie lies over the sea"
> 	});
> }).then(function(cipherText) {
> 	console.log(cipherText);
> }, function(error) {
>   // there was some error.
> });
> 
> 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?
> 
> Cheers,
> 	Erik Jan
> _______________________________________________
> aerogear-dev mailing list
> aerogear-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/aerogear-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/aerogear-dev/attachments/20131121/a108d0a0/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1393 bytes
Desc: not available
Url : http://lists.jboss.org/pipermail/aerogear-dev/attachments/20131121/a108d0a0/attachment.bin 


More information about the aerogear-dev mailing list