copay/js/plugins/InsightStorage.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

var request = require('request');
var cryptoUtil = require('../util/crypto');
var querystring = require('querystring');
var Identity = require('../models/Identity');
function InsightStorage(config) {
this.type = 'DB';
this.storeUrl = config.url || 'https://test-insight.bitpay.com:443/api/email';
this.request = config.request || request;
}
InsightStorage.prototype.init = function () {};
InsightStorage.prototype.setCredentials = function(email, password, opts) {
this.email = email;
this.password = password;
};
2014-10-28 11:20:43 -07:00
InsightStorage.prototype.createItem = function(name, value, callback) {
var self = this;
this.getItem(name, function(err, retrieved) {
if (err || !retrieved) {
return self.setItem(name, value, callback);
} else {
return callback('EEXISTS');
}
});
};
InsightStorage.prototype.getItem = function(name, callback) {
2014-10-28 08:01:09 -07:00
var key = cryptoUtil.kdf(this.password + this.email);
var secret = cryptoUtil.kdf(key, this.password);
var encodedEmail = encodeURIComponent(this.email);
var retrieveUrl = this.storeUrl + '/retrieve/' + encodedEmail;
this.request.get(retrieveUrl + '?' + querystring.encode({secret: secret, key: name}),
function(err, response, body) {
if (err) {
return callback('Connection error');
}
2014-10-31 07:24:16 -07:00
if (response.statusCode === 403) {
return callback('PNOTFOUND: Profile not found');
}
if (response.statusCode !== 200) {
return callback('Connection error');
}
return callback(null, body);
}
);
};
InsightStorage.prototype.setItem = function(name, value, callback) {
2014-10-28 08:01:09 -07:00
var key = cryptoUtil.kdf(this.password + this.email);
var secret = cryptoUtil.kdf(key, this.password);
var registerUrl = this.storeUrl + '/register';
this.request.post({
url: registerUrl,
body: querystring.encode({
key: name,
email: this.email,
secret: secret,
record: value
})
}, function(err, response, body) {
if (err) {
return callback('Connection error');
}
2014-10-31 07:24:16 -07:00
if (response.statusCode === 409) {
return callback('BADCREDENTIALS: Invalid username or password');
2014-10-31 07:24:16 -07:00
}
if (response.statusCode !== 200) {
return callback('Unable to store data on insight');
}
return callback();
});
};
InsightStorage.prototype.removeItem = function(name, callback) {
this.setItem(name, '', callback);
};
InsightStorage.prototype.clear = function(callback) {
// NOOP
callback();
};
InsightStorage.prototype.allKeys = function(callback) {
// TODO: compatibility with localStorage
return callback(null);
};
InsightStorage.prototype.getFirst = function(prefix, opts, callback) {
// TODO: compatibility with localStorage
return callback(null, true, true);
};
module.exports = InsightStorage;