diff --git a/app-template/package-template.json b/app-template/package-template.json index 660063183..4183c33e0 100644 --- a/app-template/package-template.json +++ b/app-template/package-template.json @@ -77,6 +77,7 @@ "grunt-exec": "^1.0.0", "grunt-nw-builder": "^2.0.3", "grunt-sass": "^1.2.0", + "keytar": "^4.2.1", "load-grunt-tasks": "^3.5.0", "shelljs": "^0.3.0", "android-versions": "^1.2.1", diff --git a/src/js/services/desktopSecureStorage.js b/src/js/services/desktopSecureStorage.js deleted file mode 100644 index 8831363b4..000000000 --- a/src/js/services/desktopSecureStorage.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -angular.module('copayApp.services') - .factory('desktopSecureStorageService', function (platformInfo, $timeout, $log, lodash) { - var isNW = platformInfo.isNW; - var root = {}; - var serviceName = 'Bitcoin.com'; - if (!isNW) - $log.debug('This is not an NW.js app, keytar not available'); - else - var keytar = require('keytar'); - root.get = function (k, cb) { - return keytar.getPassword(serviceName, k).then(function(result) { - return cb(null, result); - }); - }; - - /** - * Same as setItem, but fails if an item already exists - */ - root.create = function (name, value, callback) { - root.get(name, - function (err, data) { - if (data) { - return callback('EEXISTS'); - } else { - return root.set(name, value, callback); - } - }); - }; - - root.set = function (k, v, cb) { - if (lodash.isObject(v)) { - v = JSON.stringify(v); - } - if (v && !lodash.isString(v)) { - v = v.toString(); - } - - keytar.deletePassword(serviceName, k).then(function (result) { - keytar.setPassword(serviceName, k, v).then(function (val) { - console.log(val); - }).catch(function (err) { - console.log(err); - }).finally(function () { - return cb(); - }); - }); - }; - - root.remove = function (k, cb) { - keytar.deletePassword(serviceName, k).then(function(result) { - return cb(); - }); - }; - - return root; - }); diff --git a/src/js/services/desktopSecureStorageService.js b/src/js/services/desktopSecureStorageService.js index 6e148da2c..4469459c2 100644 --- a/src/js/services/desktopSecureStorageService.js +++ b/src/js/services/desktopSecureStorageService.js @@ -1,6 +1,49 @@ 'use strict'; -angular.module('copayApp.services').factory('desktopSecureStorageService', function($log) { - // Placeholder - return {}; +angular.module('copayApp.services').factory('desktopSecureStorageService', function($log, appConfigService, platformInfo, lodash) { + var root = {}; + var storage = null; + var serviceName = appConfigService.packageNameId; + + if (platformInfo.isNW) { + storage = require('keytar'); + } + + root.get = function(key, cb) { + if (!platformInfo.isNW) { + cb(new Error('desktopSecureStorageService is only available on NW.js desktop.')); + return; + } + + storage.getPassword(serviceName, key).then(function(result) { + return cb(null, result); // XX SP: result is null if no value is found as it should + }).catch(function (error) { + cb(new Error(error)); + }); + }; + + root.set = function(key, value, cb) { + if (!platformInfo.isNW) { + cb(new Error('desktopSecureStorageService is only available on NW.js desktop.')); + return; + } + + if (lodash.isObject(value)) { + value = JSON.stringify(value); + } + if (value && !lodash.isString(value)) { + value = value.toString(); + } + + storage.deletePassword(serviceName, key).then(function (result) { + storage.setPassword(serviceName, key, value).then(function (value) { + cb(); + }).catch(function (error) { + console.log(error); + cb(new Error(error)); + }) + }); + }; + + return root; }); \ No newline at end of file diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index dac88169f..3b7fc2b0f 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -528,7 +528,7 @@ angular.module('copayApp.services') var walletId = client.credentials.walletId - if (!root.profile.addWallet(JSON.parse(client.export()))) + if (root.profile && !root.profile.addWallet(JSON.parse(client.export()))) return cb(gettextCatalog.getString("Wallet already in {{appName}}", { appName: appConfigService.nameCase }));