diff --git a/Gruntfile.js b/Gruntfile.js index f9ed59621..9c8ae169e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -175,6 +175,16 @@ module.exports = function(grunt) { 'src/js/trezor-url.js', 'bower_components/trezor-connect/connect.js', 'node_modules/bezier-easing/dist/bezier-easing.min.js', + + 'bower_components/crypto-js/core.js', + 'bower_components/crypto-js/enc-base64.js', + 'bower_components/crypto-js/hmac.js', + 'bower_components/crypto-js/md5.js', + 'bower_components/crypto-js/sha1.js', + 'bower_components/crypto-js/evpkdf.js', + 'bower_components/crypto-js/cipher-core.js', + 'bower_components/crypto-js/aes.js', + 'node_modules/cordova-plugin-qrscanner/dist/cordova-plugin-qrscanner-lib.min.js' ], dest: 'www/js/app.js' diff --git a/bower.json b/bower.json index 08a82d9b8..a66685254 100644 --- a/bower.json +++ b/bower.json @@ -11,6 +11,7 @@ "angular-gettext": "2.2.1", "angular-moment": "0.10.1", "angular-qrcode": "bitpay/angular-qrcode#~6.3.0", + "crypto-js": "^3.1.9", "ionic": "https://github.com/ionic-team/ionic-v1.git", "moment": "2.10.3", "ng-lodash": "0.2.3", diff --git a/src/js/services/encryptionService.js b/src/js/services/encryptionService.js new file mode 100644 index 000000000..1d3b6ff4e --- /dev/null +++ b/src/js/services/encryptionService.js @@ -0,0 +1,86 @@ +'use strict'; + +angular.module('copayApp.services').factory('encryptionService', function($log) { + var root = {}; + + //lazy creation of cipher and decipher? + + // need a function to get the key + var password = 'password'; + + function _getGetOrCreateKey() { + + //crytpo.scrypt() + //crypto.createCipheriv() + }; + + function encryptUsingcrypto(str) { + var cipher = crypto.createCipher('aes256', password); + + cipher.on('readable', () => { + var data = cipher.read(); + if (data) { + encrypted += data.toString('hex'); + } + }); + + cipher.on('end', () => { + console.log('Encrypted 1: ' + encrypted); + //cb(); + }); + + //cipher.write(str); + cipher.write(str); + cipher.end(); + } + + function encryptUsingCryptoJS(str) { + var ciphertext = CryptoJS.AES.encrypt(str, password); + $log.debug('cipherText: ' + ciphertext); + } + + root.encrypt = function(str, cb) { + $log.debug('*** crypto exists: ' + !!crypto); + $log.debug('*** CryptoJS exists: ' + !!CryptoJS); + + encryptUsingCryptoJS('I am a secret.'); + +/* + // var ciphertext = CryptoJS.AES.encrypt(str, password); + var cipher = crypto.createCipher('aes256', password); + + cipher.on('readable', () => { + var data = cipher.read(); + if (data) { + encrypted += data.toString('hex'); + } + }); + + cipher.on('end', () => { + console.log('Encrypted: ' + encrypted); + //cb(); + }); + + //cipher.write(str); + cipher.write('I am secret'); + cipher.end(); + */ + + }; + + root.encryptedObjectFromString = function(str) { + try { + var parsed = JSON.parse(str); + } catch(e) { + return null; + } + + if (parsed.encryptionVersion) { + return parsed; + } else { + return null; + } + }; + + return root; +}); \ No newline at end of file diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index a2d85950b..acfe36cd4 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -1,6 +1,6 @@ 'use strict'; angular.module('copayApp.services') - .factory('storageService', function(appConfigService, logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, secureStorageService, $timeout) { + .factory('storageService', function(appConfigService, encryptionService, logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, secureStorageService, $timeout) { var root = {}; var storage; @@ -32,7 +32,7 @@ angular.module('copayApp.services') // This is only used in Copay, we used to encrypt profile // using device's UUID. - var decryptOnMobile = function(text, cb) { + var copayDecryptOnMobile = function(text, cb) { var json; try { json = JSON.parse(text); @@ -121,11 +121,11 @@ angular.module('copayApp.services') root.storeProfile = function(profile, cb) { var profileString = profile.toObj(); - if (platformInfo.isNW) { + //if (platformInfo.isNW) { storage.set('profile', profileString, cb); - } else { - secureStorageService.set('profile', profileString, cb); - } + //} else { + // secureStorageService.set('profile', profileString, cb); + //} }; /** @@ -150,7 +150,7 @@ angular.module('copayApp.services') return cb(null, null); } - decryptOnMobile(profileStr, function(decryptErr, decryptedStr) { + copayDecryptOnMobile(profileStr, function(decryptErr, decryptedStr) { if (decryptErr) return cb(decryptErr, null); var profile; try { @@ -205,6 +205,46 @@ angular.module('copayApp.services') * @param {getProfileCallback} cb */ root.getProfile = function(cb) { + $log.debug('getProfile()'); + storage.get('profile', function onProfileRetrieved(getErr, profileStr){ + if (getErr) { + $log.error(getErr); + return cb(getErr, null); + } + + if (!profileStr) { + $log.debug('No string loaded, returning nothing.'); + return cb(null, null); + } + + var encryptedProfile = encryptionService.encryptedObjectFromString(profileStr); + if (!encryptedProfile) { + + copayDecryptOnMobile(profileStr, function(decryptErr, decryptedStr) { + if (decryptErr) return cb(decryptErr, null); + var profile; + try { + profile = Profile.fromString(decryptedStr); + } catch (e) { + $log.debug('Could not read profile:', e); + return cb(new Error('Could not read profile.'), null); + } + + encryptedProfile = encryptionService.encrypt(profile); + $log.debug('encryptedProfile'); + + //cb(null, profile) + }); + } else { + $log.debug('profile was encrypted.'); + } + + + + }); + + /* + if (platformInfo.isNW) { storage.get('profile', function(getErr, getStr) { _onOldProfileRetrieved(getErr, getStr, cb); @@ -248,6 +288,7 @@ angular.module('copayApp.services') }); }); }); + */ }; root.setFeedbackInfo = function(feedbackValues, cb) {