mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #3407 from gabrielbazan7/feat/exportMetaData2
Incluiding address book and transaction history cache in export file
This commit is contained in:
commit
173bf4b13f
|
@ -45,6 +45,14 @@
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<h4></h4>
|
<h4></h4>
|
||||||
|
|
||||||
|
<ul class="no-bullet m0">
|
||||||
|
<li>
|
||||||
|
<switch id="no-metaData" name="metaData" ng-model="metaData" class="green right"></switch>
|
||||||
|
<span translate>Include address book and history cache</span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
<ul class="no-bullet m0" ng-show="index.canSign">
|
<ul class="no-bullet m0" ng-show="index.canSign">
|
||||||
<li>
|
<li>
|
||||||
<switch id="no-sign" name="noSign" ng-model="noSign" class="green right"></switch>
|
<switch id="no-sign" name="noSign" ng-model="noSign" class="green right"></switch>
|
||||||
|
|
|
@ -1,62 +1,127 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('backupController',
|
angular.module('copayApp.controllers').controller('backupController',
|
||||||
function($rootScope, $scope, $timeout, backupService, profileService, isMobile, notification, go, gettext, gettextCatalog) {
|
function($rootScope, $scope, $timeout, $log, backupService, storageService, profileService, isMobile, notification, go, gettext, gettextCatalog) {
|
||||||
this.error = null;
|
|
||||||
this.success = null;
|
|
||||||
|
|
||||||
var fc = profileService.focusedClient;
|
|
||||||
this.isEncrypted = fc.isPrivKeyEncrypted();
|
|
||||||
|
|
||||||
this.downloadWalletBackup = function() {
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var opts = {
|
|
||||||
noSign: $scope.noSign,
|
self.error = null;
|
||||||
};
|
self.success = null;
|
||||||
backupService.walletDownload(this.password, opts, function(err) {
|
$scope.metaData = true;
|
||||||
|
var fc = profileService.focusedClient;
|
||||||
|
self.isEncrypted = fc.isPrivKeyEncrypted();
|
||||||
|
|
||||||
|
self.downloadWalletBackup = function() {
|
||||||
|
self.getMetaData($scope.metaData, function(err, txsFromLocal, localAddressBook) {
|
||||||
if (err) {
|
if (err) {
|
||||||
self.error = true;
|
self.error = true;
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
$rootScope.$emit('Local/BackupDone');
|
var opts = {
|
||||||
|
noSign: $scope.noSign,
|
||||||
|
historyCache: txsFromLocal,
|
||||||
|
addressBook: localAddressBook
|
||||||
|
};
|
||||||
|
|
||||||
|
backupService.walletDownload(self.password, opts, function(err) {
|
||||||
|
if (err) {
|
||||||
|
self.error = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
|
notification.success(gettext('Success'), gettext('Encrypted export file saved'));
|
||||||
go.walletHome();
|
go.walletHome();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getBackup = function() {
|
self.getMetaData = function(metaData, cb) {
|
||||||
|
if (metaData == false) return cb();
|
||||||
|
self.getHistoryCache(function(err, txsFromLocal) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
self.getAddressbook(function(err, localAddressBook) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
return cb(null, txsFromLocal, localAddressBook)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.getHistoryCache = function(cb) {
|
||||||
|
storageService.getTxHistory(fc.credentials.walletId, function(err, txs) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
var localTxs = [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
localTxs = JSON.parse(txs);
|
||||||
|
} catch (ex) {
|
||||||
|
$log.warn(ex);
|
||||||
|
}
|
||||||
|
if (!localTxs[0]) return cb(null, null);
|
||||||
|
|
||||||
|
return cb(null, localTxs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.getAddressbook = function(cb) {
|
||||||
|
storageService.getAddressbook(fc.credentials.network, function(err, addressBook) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
var localAddressBook = [];
|
||||||
|
try {
|
||||||
|
localAddressBook = JSON.parse(addressBook);
|
||||||
|
} catch (ex) {
|
||||||
|
$log.warn(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cb(null, localAddressBook);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.getBackup = function(cb) {
|
||||||
|
self.getMetaData(function(err, txsFromLocal, localAddressBook) {
|
||||||
|
if (err) {
|
||||||
|
self.error = true;
|
||||||
|
return cb(null);
|
||||||
|
}
|
||||||
var opts = {
|
var opts = {
|
||||||
noSign: $scope.noSign,
|
noSign: $scope.noSign,
|
||||||
|
historyCache: txsFromLocal,
|
||||||
|
addressBook: localAddressBook
|
||||||
};
|
};
|
||||||
|
|
||||||
var ew = backupService.walletExport(this.password, opts);
|
var ew = backupService.walletExport(self.password, opts);
|
||||||
if (!ew) {
|
if (!ew) {
|
||||||
this.error = true;
|
self.error = true;
|
||||||
} else {
|
} else {
|
||||||
this.error = false;
|
self.error = false;
|
||||||
|
}
|
||||||
|
return cb(ew);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return ew;
|
|
||||||
};
|
|
||||||
|
|
||||||
this.viewWalletBackup = function() {
|
self.viewWalletBackup = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
var ew = self.getBackup();
|
self.getBackup(function(backup) {
|
||||||
|
var ew = backup;
|
||||||
if (!ew) return;
|
if (!ew) return;
|
||||||
self.backupWalletPlainText = ew;
|
self.backupWalletPlainText = ew;
|
||||||
$rootScope.$emit('Local/BackupDone');
|
});
|
||||||
}, 100);
|
}, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.copyWalletBackup = function() {
|
self.copyWalletBackup = function() {
|
||||||
var ew = this.getBackup();
|
self.getBackup(function(backup) {
|
||||||
|
var ew = backup;
|
||||||
if (!ew) return;
|
if (!ew) return;
|
||||||
window.cordova.plugins.clipboard.copy(ew);
|
window.cordova.plugins.clipboard.copy(ew);
|
||||||
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
|
window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard'));
|
||||||
$rootScope.$emit('Local/BackupDone');
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.sendWalletBackup = function() {
|
self.sendWalletBackup = function() {
|
||||||
var fc = profileService.focusedClient;
|
var fc = profileService.focusedClient;
|
||||||
if (isMobile.Android() || isMobile.Windows()) {
|
if (isMobile.Android() || isMobile.Windows()) {
|
||||||
window.ignoreMobilePause = true;
|
window.ignoreMobilePause = true;
|
||||||
|
@ -66,10 +131,11 @@ angular.module('copayApp.controllers').controller('backupController',
|
||||||
if (fc.alias) {
|
if (fc.alias) {
|
||||||
name = fc.alias + ' [' + name + ']';
|
name = fc.alias + ' [' + name + ']';
|
||||||
}
|
}
|
||||||
var ew = this.getBackup();
|
self.getBackup(function(backup) {
|
||||||
|
var ew = backup;
|
||||||
if (!ew) return;
|
if (!ew) return;
|
||||||
|
|
||||||
if( $scope.noSign)
|
if ($scope.noSign)
|
||||||
name = name + '(No Private Key)';
|
name = name + '(No Private Key)';
|
||||||
|
|
||||||
var properties = {
|
var properties = {
|
||||||
|
@ -77,8 +143,8 @@ angular.module('copayApp.controllers').controller('backupController',
|
||||||
body: 'Here is the encrypted backup of the wallet ' + name + ': \n\n' + ew + '\n\n To import this backup, copy all text between {...}, including the symbols {}',
|
body: 'Here is the encrypted backup of the wallet ' + name + ': \n\n' + ew + '\n\n To import this backup, copy all text between {...}, including the symbols {}',
|
||||||
isHtml: false
|
isHtml: false
|
||||||
};
|
};
|
||||||
$rootScope.$emit('Local/BackupDone');
|
|
||||||
window.plugin.email.open(properties);
|
window.plugin.email.open(properties);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -53,6 +53,14 @@ angular.module('copayApp.services')
|
||||||
return cb();
|
return cb();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
root.addMetadata = function(b, opts) {
|
||||||
|
|
||||||
|
b = JSON.parse(b);
|
||||||
|
if (opts.historyCache) b.historyCache = opts.historyCache;
|
||||||
|
if (opts.addressBook) b.addressBook = opts.addressBook;
|
||||||
|
return JSON.stringify(b);
|
||||||
|
}
|
||||||
|
|
||||||
root.walletExport = function(password, opts) {
|
root.walletExport = function(password, opts) {
|
||||||
if (!password) {
|
if (!password) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -61,6 +69,8 @@ angular.module('copayApp.services')
|
||||||
try {
|
try {
|
||||||
opts = opts || {};
|
opts = opts || {};
|
||||||
var b = fc.export(opts);
|
var b = fc.export(opts);
|
||||||
|
if (opts.historyCache || opts.addressBook) b = root.addMetadata(b, opts);
|
||||||
|
|
||||||
var e = sjcl.encrypt(password, b, {
|
var e = sjcl.encrypt(password, b, {
|
||||||
iter: 10000
|
iter: 10000
|
||||||
});
|
});
|
||||||
|
|
|
@ -337,6 +337,25 @@ angular.module('copayApp.services')
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
root.setMetaData = function(walletClient, addressBook, historyCache, cb) {
|
||||||
|
storageService.getAddressbook(walletClient.credentials.network, function(err, localAddressBook) {
|
||||||
|
var localAddressBook1 = {};
|
||||||
|
try {
|
||||||
|
localAddressBook1 = JSON.parse(localAddressBook);
|
||||||
|
} catch (ex) {
|
||||||
|
$log.warn(ex);
|
||||||
|
}
|
||||||
|
var mergeAddressBook = lodash.merge(addressBook, localAddressBook1);
|
||||||
|
storageService.setAddressbook(walletClient.credentials.network, JSON.stringify(addressBook), function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
storageService.setTxHistory(JSON.stringify(historyCache), walletClient.credentials.walletId, function(err) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
return cb(null);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
root._addWalletClient = function(walletClient, opts, cb) {
|
root._addWalletClient = function(walletClient, opts, cb) {
|
||||||
var walletId = walletClient.credentials.walletId;
|
var walletId = walletClient.credentials.walletId;
|
||||||
|
|
||||||
|
@ -383,7 +402,19 @@ angular.module('copayApp.services')
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return cb(gettext('Could not import. Check input file and password'));
|
return cb(gettext('Could not import. Check input file and password'));
|
||||||
}
|
}
|
||||||
root._addWalletClient(walletClient, opts, cb);
|
|
||||||
|
str = JSON.parse(str);
|
||||||
|
|
||||||
|
var addressBook = str.addressBook || {};
|
||||||
|
var historyCache = str.historyCache || [];
|
||||||
|
|
||||||
|
root._addWalletClient(walletClient, opts, function(err, walletId) {
|
||||||
|
if (err) return cb(err);
|
||||||
|
root.setMetaData(walletClient, addressBook, historyCache, function(error) {
|
||||||
|
if (error) console.log(error);
|
||||||
|
return cb(err, walletId);
|
||||||
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
root.importExtendedPrivateKey = function(xPrivKey, opts, cb) {
|
root.importExtendedPrivateKey = function(xPrivKey, opts, cb) {
|
||||||
|
|
Loading…
Reference in New Issue