mirror of https://github.com/BTCPrivate/copay.git
Move insight storage to a plugin
This commit is contained in:
parent
5f2a508f40
commit
15d11f7e5e
|
@ -56,6 +56,11 @@ var defaultConfig = {
|
||||||
plugins: {
|
plugins: {
|
||||||
LocalStorage: true,
|
LocalStorage: true,
|
||||||
//GoogleDrive: true,
|
//GoogleDrive: true,
|
||||||
|
InsightStorage: true
|
||||||
|
},
|
||||||
|
|
||||||
|
InsightStorage: {
|
||||||
|
url: 'https://test-insight.bitpay.com:443/api/email'
|
||||||
},
|
},
|
||||||
|
|
||||||
GoogleDrive: {
|
GoogleDrive: {
|
||||||
|
|
|
@ -4,9 +4,6 @@ var preconditions = require('preconditions').singleton();
|
||||||
var _ = require('underscore');
|
var _ = require('underscore');
|
||||||
var log = require('../log');
|
var log = require('../log');
|
||||||
|
|
||||||
var querystring = require('querystring');
|
|
||||||
var request = require('request');
|
|
||||||
var cryptoUtil = require('../util/crypto');
|
|
||||||
var version = require('../../version').version;
|
var version = require('../../version').version;
|
||||||
var TxProposals = require('./TxProposals');
|
var TxProposals = require('./TxProposals');
|
||||||
var PublicKeyRing = require('./PublicKeyRing');
|
var PublicKeyRing = require('./PublicKeyRing');
|
||||||
|
@ -31,7 +28,6 @@ function Identity(password, opts) {
|
||||||
preconditions.checkArgument(opts);
|
preconditions.checkArgument(opts);
|
||||||
|
|
||||||
opts = _.extend({}, opts);
|
opts = _.extend({}, opts);
|
||||||
this.request = opts.request || request;
|
|
||||||
this.storage = Identity._getStorage(opts, password);
|
this.storage = Identity._getStorage(opts, password);
|
||||||
this.networkOpts = {
|
this.networkOpts = {
|
||||||
'livenet': opts.network.livenet,
|
'livenet': opts.network.livenet,
|
||||||
|
@ -42,6 +38,7 @@ function Identity(password, opts) {
|
||||||
'testnet': opts.network.testnet,
|
'testnet': opts.network.testnet,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.pluginManager = opts.pluginManager || {};
|
||||||
this.insightSaveOpts = opts.insightSave || {};
|
this.insightSaveOpts = opts.insightSave || {};
|
||||||
this.walletDefaults = opts.walletDefaults || {};
|
this.walletDefaults = opts.walletDefaults || {};
|
||||||
this.version = opts.version || version;
|
this.version = opts.version || version;
|
||||||
|
@ -158,15 +155,19 @@ Identity.create = function(email, password, opts, cb) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
iden.registerOnInsight(iden.insightSaveOpts, function(error) {
|
iden.pluginManager.get('remote-backup').store(
|
||||||
// Ignore error
|
iden,
|
||||||
|
iden.insightSaveOpts,
|
||||||
|
function(error) {
|
||||||
|
// FIXME: Ignoring this error may not be the best thing to do. But remote storage
|
||||||
|
// is not required for the user to use the wallet.
|
||||||
return cb(null, iden, w);
|
return cb(null, iden, w);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* validates Profile's email
|
* validates Profile's email
|
||||||
*
|
*
|
||||||
|
@ -196,7 +197,7 @@ Identity.open = function(email, password, opts, cb) {
|
||||||
Identity._openProfile(email, password, iden.storage, function(err, profile) {
|
Identity._openProfile(email, password, iden.storage, function(err, profile) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.message && err.message.indexOf('PNOTFOUND') !== -1) {
|
if (err.message && err.message.indexOf('PNOTFOUND') !== -1) {
|
||||||
return Identity.readFromInsight(email, password, opts, cb);
|
return opts.pluginManager.get('remote-backup').retrieve(email, password, opts, cb);
|
||||||
}
|
}
|
||||||
return cb(err);
|
return cb(err);
|
||||||
}
|
}
|
||||||
|
@ -491,7 +492,7 @@ Identity.prototype.createWallet = function(opts, cb) {
|
||||||
this.addWallet(w, function(err) {
|
this.addWallet(w, function(err) {
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
self.openWallets.push(w);
|
self.openWallets.push(w);
|
||||||
self.triggerInsightSave(self.insightSaveOpts, function(error) {
|
self.pluginManager.get('remote-backup').store(self, self.insightSaveOpts, function(error) {
|
||||||
// Ignore error
|
// Ignore error
|
||||||
w.netStart();
|
w.netStart();
|
||||||
return cb(null, w);
|
return cb(null, w);
|
||||||
|
@ -499,53 +500,6 @@ Identity.prototype.createWallet = function(opts, cb) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Identity.readFromInsight = function(email, password, opts, callback) {
|
|
||||||
var key = cryptoUtil.kdf(password, email);
|
|
||||||
var secret = cryptoUtil.kdf(key, password);
|
|
||||||
var useRequest = opts.request || request;
|
|
||||||
var encodedEmail = encodeURIComponent(email);
|
|
||||||
var retrieveUrl = opts.retrieveUrl || 'http://localhost:3001/api/email/retrieve/' + encodedEmail;
|
|
||||||
useRequest.get(retrieveUrl + '?' + querystring.encode({secret: secret}),
|
|
||||||
function(err, response, body) {
|
|
||||||
if (err) {
|
|
||||||
return callback('Connection error');
|
|
||||||
}
|
|
||||||
if (response.statusCode !== 200) {
|
|
||||||
return callback('Connection error');
|
|
||||||
}
|
|
||||||
var decryptedJson = cryptoUtil.decrypt(key, body);
|
|
||||||
if (!decryptedJson) {
|
|
||||||
return callback('Internal Error');
|
|
||||||
}
|
|
||||||
return Identity.importFromJson(decryptedJson, password, opts, callback);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
Identity.prototype.triggerInsightSave = Identity.prototype.registerOnInsight = function(opts, callback) {
|
|
||||||
var password = this.profile.password;
|
|
||||||
var key = cryptoUtil.kdf(password, this.profile.email);
|
|
||||||
var secret = cryptoUtil.kdf(key, password);
|
|
||||||
var exportData = this.exportAsJson
|
|
||||||
var record = cryptoUtil.encrypt(key, this.exportAsJson());
|
|
||||||
var registerUrl = opts.registerUrl || 'http://localhost:3001/api/email/register';
|
|
||||||
this.request.post({
|
|
||||||
url: registerUrl,
|
|
||||||
body: querystring.encode({
|
|
||||||
email: this.profile.email,
|
|
||||||
secret: secret,
|
|
||||||
record: record
|
|
||||||
})
|
|
||||||
}, function(err, response, body) {
|
|
||||||
if (err) {
|
|
||||||
return callback('Connection error');
|
|
||||||
}
|
|
||||||
if (response.statusCode !== 200) {
|
|
||||||
return callback('Unable to store data on insight');
|
|
||||||
}
|
|
||||||
return callback();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// add wallet (import)
|
// add wallet (import)
|
||||||
Identity.prototype.addWallet = function(wallet, cb) {
|
Identity.prototype.addWallet = function(wallet, cb) {
|
||||||
|
|
|
@ -30,6 +30,7 @@ var KIND_MULTIPLE = PluginManager.KIND_MULTIPLE = 2;
|
||||||
|
|
||||||
PluginManager.TYPE = {};
|
PluginManager.TYPE = {};
|
||||||
PluginManager.TYPE['DB'] = KIND_UNIQUE;
|
PluginManager.TYPE['DB'] = KIND_UNIQUE;
|
||||||
|
PluginManager.TYPE['remote-backup'] = KIND_UNIQUE;
|
||||||
|
|
||||||
PluginManager.prototype._register = function(obj, name) {
|
PluginManager.prototype._register = function(obj, name) {
|
||||||
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
|
preconditions.checkArgument(obj.type, 'Plugin has not type:' + name);
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
var request = require('request');
|
||||||
|
var cryptoUtil = require('../js/util/crypto');
|
||||||
|
var querystring = require('querystring');
|
||||||
|
var Identity = require('../js/models/Identity');
|
||||||
|
|
||||||
|
function InsightStorage(config) {
|
||||||
|
this.type = 'remote-backup';
|
||||||
|
this.storeUrl = config.url || 'https://insight.is/api/email';
|
||||||
|
this.request = config.request || request;
|
||||||
|
}
|
||||||
|
|
||||||
|
InsightStorage.prototype.init = function () {};
|
||||||
|
|
||||||
|
InsightStorage.prototype.retrieve = function(email, password, opts, callback) {
|
||||||
|
var key = cryptoUtil.kdf(password, email);
|
||||||
|
var secret = cryptoUtil.kdf(key, password);
|
||||||
|
var encodedEmail = encodeURIComponent(email);
|
||||||
|
var retrieveUrl = this.storeUrl + '/retrieve/' + encodedEmail;
|
||||||
|
this.request.get(retrieveUrl + '?' + querystring.encode({secret: secret}),
|
||||||
|
function(err, response, body) {
|
||||||
|
if (err) {
|
||||||
|
return callback('Connection error');
|
||||||
|
}
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
return callback('Connection error');
|
||||||
|
}
|
||||||
|
var decryptedJson = cryptoUtil.decrypt(key, body);
|
||||||
|
if (!decryptedJson) {
|
||||||
|
return callback('Internal Error');
|
||||||
|
}
|
||||||
|
return Identity.importFromJson(decryptedJson, password, opts, callback);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
InsightStorage.prototype.store = function(identity, opts, callback) {
|
||||||
|
var password = identity.profile.password;
|
||||||
|
var key = cryptoUtil.kdf(password, identity.profile.email);
|
||||||
|
var secret = cryptoUtil.kdf(key, password);
|
||||||
|
var record = cryptoUtil.encrypt(key, identity.exportAsJson());
|
||||||
|
var registerUrl = this.storeUrl + '/register';
|
||||||
|
this.request.post({
|
||||||
|
url: registerUrl,
|
||||||
|
body: querystring.encode({
|
||||||
|
email: identity.profile.email,
|
||||||
|
secret: secret,
|
||||||
|
record: record
|
||||||
|
})
|
||||||
|
}, function(err, response, body) {
|
||||||
|
if (err) {
|
||||||
|
return callback('Connection error');
|
||||||
|
}
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
return callback('Unable to store data on insight');
|
||||||
|
}
|
||||||
|
return callback();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = InsightStorage;
|
|
@ -95,6 +95,9 @@ var createBundle = function(opts) {
|
||||||
b.require('./plugins/GoogleDrive', {
|
b.require('./plugins/GoogleDrive', {
|
||||||
expose: '../plugins/GoogleDrive'
|
expose: '../plugins/GoogleDrive'
|
||||||
});
|
});
|
||||||
|
b.require('./plugins/InsightStorage', {
|
||||||
|
expose: '../plugins/InsightStorage'
|
||||||
|
});
|
||||||
b.require('./plugins/LocalStorage', {
|
b.require('./plugins/LocalStorage', {
|
||||||
expose: '../plugins/LocalStorage'
|
expose: '../plugins/LocalStorage'
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue