Skip to content

fix file hash #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions public/views/walletHome.html
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ <h4 class="title m0" style="padding:10px 0 0; background-color: #F1F3F5;">
<i class="fi-plus size-18 m10r"></i>
<span class="text-close size-12" translate>Add fields</span>
</a>
<div ng-show="home.android && home.androidVersion < 5">
<button ng-click="home.oldAndroidInputFileClick()" class="button small black round" translate>Choose File</button>
{{home.previousFileHashName}}
</div>
<input ng-show="!home.android || home.androidVersion >= 5" class="post-hash-file" id="post-hash-file" type="file" onchange="angular.element(this).scope().home.sendAttachedFile(event)">
<label ng-show="!home.android || home.androidVersion >= 5" for="post-hash-file" ng-class="{'active': home.previousFileHashName}"><i class="fi-upload size-18 m10r"></i>{{home.previousFileHashName ? "Posting hash of file " + home.previousFileHashName : "Post hash of a file"}}</label>
</div>
</div>
</div>
Expand Down Expand Up @@ -747,10 +753,10 @@ <h4 ng-if="home.aa_dry_run_error || home.aa_message_results.length || home.aa_st
<div ng-if="assetIndexSelectorValue == -4">
<div ng-show="home.android && home.androidVersion < 5">
<button ng-click="home.oldAndroidInputFileClick()" class="button small black round" translate>Choose File</button>
{{home.oldAndroidFileName}}
{{home.previousFileHashName}}
</div>
<input ng-show="!home.android || home.androidVersion >= 5" class="post-hash-file" id="post-hash-file" type="file" onchange="angular.element(this).scope().home.sendAttachedFile(event)">
<label ng-show="!home.android || home.androidVersion >= 5" for="post-hash-file" ng-class="{'active': home.attachedFile}"><i class="fi-upload size-18 m10r"></i>{{home.attachedFile ? "Posting hash of file " + home.attachedFile.name : "Post hash of a file"}}</label>
<label ng-show="!home.android || home.androidVersion >= 5" for="post-hash-file" ng-class="{'active': home.previousFileHashName}"><i class="fi-upload size-18 m10r"></i>{{home.previousFileHashName ? "Posting hash of file " + home.previousFileHashName : "Post hash of a file"}}</label>
</div>

</div>
Expand Down
83 changes: 38 additions & 45 deletions src/js/controllers/walletHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,20 @@ angular.module('copayApp.controllers')
self.android = isMobile.Android() && window.cordova;
self.androidVersion = isMobile.Android() ? parseFloat(navigator.userAgent.slice(navigator.userAgent.indexOf("Android")+8)) : null;
self.oldAndroidFilePath = null;
self.oldAndroidFileName = '';
self.previousFileHashName = '';

self.oldAndroidInputFileClick = function() {
if(isMobile.Android() && self.androidVersion < 5) {
window.plugins.mfilechooser.open([], function(uri) {
self.oldAndroidFilePath = 'file://' + uri;
self.oldAndroidFileName = uri.split('/').pop();
self.previousFileHashName = uri.split('/').pop();
$timeout(function() {
$rootScope.$apply();
});
if (!self.oldAndroidFilePath)
if (!self.previousFileHashName)
return;
self.importing = true;
fileSystemService.readFile(self.oldAndroidFilePath, function(err, data) {
if (err) {
self.setSendError("cannot read the file whose hash is going to be posted");
return;
}
const hash = require("crypto")
.createHash("sha256")
.update(data)
.digest("hex");
home.feedvaluespairs.push({
name: home.attachedFile.name,
value: hash,
});
$scope.$apply();
})
this.addHashToFeedValues(self.oldAndroidFilePath);
}, function(error) {
alert(error);
});
Expand Down Expand Up @@ -185,6 +171,33 @@ angular.module('copayApp.controllers')
}
});

this.addHashToFeedValues = function(filePath) {
var read = isCordova
? (file, cb) => fileSystemService.readFileFromForm(file, cb)
: (file, cb) => fileSystemService.readFile(file.path, cb);
read(filePath, function (err, data) {
if (err) {
self.setSendError("cannot read the file whose hash is going to be posted");
return;
}
const hash = require("crypto")
.createHash("sha256")
.update(data)
.digest("hex");
// if the last element is empty, remove it
if (home.feedvaluespairs.length > 0) {
var last_pair = home.feedvaluespairs[home.feedvaluespairs.length - 1];
if (!last_pair.name && !last_pair.value)
home.feedvaluespairs.pop();
}
home.feedvaluespairs.push({
name: home.attachedFile.name,
value: hash,
});
$scope.$apply();
});
};

this.countChecker = function() {
self.newPaymentsCount = $rootScope.newPaymentsCount;
};
Expand Down Expand Up @@ -2015,6 +2028,7 @@ angular.module('copayApp.controllers')

this.resetDataForm = function() {
this.resetError();
self.previousFileHashName = '';
$scope.home.feedvaluespairs = [{}];
$timeout(function() {
$rootScope.$digest();
Expand Down Expand Up @@ -2180,6 +2194,7 @@ angular.module('copayApp.controllers')
var paymentData = Buffer.from(base64data, 'base64').toString('utf8');
paymentData = paymentData ? JSON.parse(paymentData) : null;
if (paymentData) {
self.previousFileHashName = '';
$scope.home.feedvaluespairs = [];
for (var key in paymentData) {
$scope.home.feedvaluespairs.push({name: key, value: paymentData[key], readonly: true});
Expand Down Expand Up @@ -2299,6 +2314,7 @@ angular.module('copayApp.controllers')
delete dataPrompt.content;
break;
}
self.previousFileHashName = '';
$scope.home.feedvaluespairs = [];
for (var key in dataPrompt) {
var value = dataPrompt[key];
Expand Down Expand Up @@ -2472,33 +2488,8 @@ angular.module('copayApp.controllers')
home.attachedFile = $ev.target.files[0];
if (!home.attachedFile)
return;
var read = isCordova
? (file, cb) => fileSystemService.readFileFromForm(file, cb)
: (file, cb) => fileSystemService.readFile(file.path, cb);
read(home.attachedFile, function (
err,
data
) {
if (err) {
self.setSendError("cannot read the file whose hash is going to be posted");
return;
}
const hash = require("crypto")
.createHash("sha256")
.update(data)
.digest("hex");
// if the last element is empty, remove it
if (home.feedvaluespairs.length > 0) {
var last_pair = home.feedvaluespairs[home.feedvaluespairs.length - 1];
if (!last_pair.name && !last_pair.value)
home.feedvaluespairs.pop();
}
home.feedvaluespairs.push({
name: home.attachedFile.name,
value: hash,
});
$scope.$apply();
});
self.previousFileHashName = home.attachedFile.name;
this.addHashToFeedValues(home.attachedFile);
};

this.openTxModal = function(btx) {
Expand Down Expand Up @@ -2793,6 +2784,7 @@ angular.module('copayApp.controllers')

this._amount = this._address = null;
this.bSendAll = false;
self.previousFileHashName = '';
$scope.home.feedvaluespairs = [];
resetAAFields();
var form = $scope.sendPaymentForm;
Expand Down Expand Up @@ -2878,6 +2870,7 @@ angular.module('copayApp.controllers')
data = {};
break;
}
self.previousFileHashName = '';
$scope.home.feedvaluespairs = [];
for (var key in data) {
var value = data[key];
Expand Down