desktopSecureStorageService update

This commit is contained in:
Sebastiaan Pasma 2018-06-01 13:54:46 +02:00
commit 7c5a61a88b
4 changed files with 48 additions and 62 deletions

View file

@ -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",

View file

@ -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;
});

View file

@ -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;
});

View file

@ -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
}));