Commit 8db3a5ce authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Votes handling

parent 8214423c
......@@ -43,7 +43,16 @@ function JPGP() {
};
this.verify = function(callback) {
command('V', this.args, callback);
command('V', this.args, function (err, stdout, stderr) {
if(!err && !stderr){
var verified = JSON.parse(stdout).data;
if(verified){
callback();
}
else callback("Signature does not match.\n" + err + "\n" + stdout + "\n" + stderr);
}
else callback(err + "\n" + stderr);
});
};
this.isSigned = function(callback) {
......@@ -67,6 +76,7 @@ function JPGP() {
}
function call(c, args, callback) {
// var cmd = 'java -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,address=8044 -jar '+ JPGP_JAR + ' -' + c + args;
var cmd = 'java -jar '+ JPGP_JAR + ' -' + c + args;
var start = new Date();
exec(cmd, function (err, stdout, stderr) {
......
......@@ -97,20 +97,7 @@ PublicKeySchema.statics.verify = function (asciiArmored, signature, done) {
.data(asciiArmored)
.noCarriage()
.signature(signature)
.verify(function (err, stdout, stderr) {
if(!err && !stderr){
var verified = JSON.parse(stdout).data;
if(verified){
done();
}
else{
done("Signature does not match.\n" + err + "\n" + stdout + "\n" + stderr);
}
}
else{
done(err + "\n" + stderr);
}
});
.verify(done);
};
var PublicKey = mongoose.model('PublicKey', PublicKeySchema);
\ No newline at end of file
var sha1 = require('sha1');
var async = require('async');
var jpgp = require('../lib/jpgp');
var merkle = require('../lib/merkle');
var mongoose = require('mongoose');
var fs = require('fs');
var PublicKey = mongoose.model('PublicKey');
var Amendment = mongoose.model('Amendment');
var Schema = mongoose.Schema;
var VoteSchema = new Schema({
issuer: String,
signature: String,
amendment: String,
created: Date,
updated: Date
});
VoteSchema.methods = {
verify: function (done) {
var obj = this;
async.waterfall([
function(callback){
callback(null, obj.publicKey);
},
function(publicKey, callback){
if(publicKey)
callback(null, [publicKey]);
else
PublicKey.search("0x" + this.issuer, callback);
}
], function (err, keys) {
if(!err){
if(keys.length > 0){
var publicKey = keys[0];
VoteSchema.statics.verify(obj.amendment, obj.signature, publicKey, function (err) {
done(err);
});
}
else done("Key not found");
}
else done(err);
});
return this;
},
loadFromFiles: function(voteFile, amendFile, done) {
var obj = this;
fs.readFile(voteFile, {encoding: "utf8"}, function (err, voteData) {
if(!err){
fs.readFile(amendFile, {encoding: "utf8"}, function (err, amendData) {
if(!err){
obj.signature = voteData;
obj.amendment = amendData;
done();
}
else done(err);
});
}
else done(err);
});
return this;
}
};
VoteSchema.statics.verify = function (amendment, signature, publicKey, done) {
jpgp()
.publicKey(publicKey)
.data(amendment)
.noCarriage()
.signature(signature)
.verify(done);
};
var Vote = mongoose.model('Vote', VoteSchema);
\ No newline at end of file
-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.4.12 (GNU/Linux)
owGbwMvMwMG4aNqZYEa1z0WMaxkfJnE6Oek6+hro+nsHvv7WF5ZaVJyZn2elYMjl
XFpUlJqXXGmlkJRakhifVJRfWpxfmsblV5qblFpkpWDAFZZfAlQelJ9fYqXgZuro
7OZiZu7mbGlg4WJk4WwA5Dq6WFiYGZlYOjobmRmYGpoC5aCanPNL84C6jGHcjMS8
9NRiKy5tI1czS0NLczdHJwMjSxcLMzNLVzcLU1cLIxNTc0dDUwtzZ0cDVxdLZy5t
Y2MnJzegNWbmBuYWLuZGjm6GRhZOpk6ORpZmzs6mxgaGRmZuxuZGXNrO5sYWFkZO
ZiZO5q7mRkbG5o5GbiZmBs6uls6OTuZmLoaWjhZmpoauXL6pIK+R6iOoLriXYPzB
4adOxjksDIwcDGysTKAIZuDiFIDFv7EYD8PyrBeLN2lsW7E17u6StxOqnu6q1u+O
Kyq+vfSL8MWtgnt1/oUZ3Ltr3p5qwTzHvzB54aRLDXxLW0N/OIY6X7ij+Dz5ybyp
XIJfAhiYnhUe9vvFlzX5ohrf069fq8Ju3NycY7Rmqazjys12AaXXOYQEPtUb7Oeq
d8gLDjv65wXv7g7Z8ArLhMq9u5vquFJznda+9Lc9oaf1t/abXQBfl5iiWpCrr4n7
rqDjxx/MXF8fu9e24dd2u/NMNbVTb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Zn2elYMjl
XFpUlJqXXGmlkJRakhifVJRfWpxfmsblV5qblFpkpWDAFZZfAlQelJ9fYqXgZuro
7OZiZu7mbGlg4WJk4WwA5Dq6WFiYGZlYOjobmRmYGpoC5aCanPNL84C6jGHcjMS8
9NRiKy5tI1czS0NLczdHJwMjSxcLMzNLVzcLU1cLIxNTc0dDUwtzZ0cDVxdLZy5t
Y2MnJzegNWbmBuYWLuZGjm6GRhZOpk6ORpZmzs6mxgaGRmZuxuZGXNrO5sYWFkZO
ZiZO5q7mRkbG5o5GbiZmBs6uls6OTuZmLoaWjhZmpoauXL6pIK+R5iNLmC64l2D8
weGnTkYZFgZGDgY2ViZQBDNwcQrA4r+jk4PhQqro6wc9XyYaqbnYBIsr/t6/cy+7
2Psy5t/Pdy/I23r50dKzUpcN1XPDZNl+POlI/+63o/34poVcUnkiHIyxMpzvVgoc
f649Z/7r+5U/I1frKs95tJFN0KWqUXjJ+czHq6xeKMw24yyIFJaT2BI/LS+63X3a
2fvv4tkfKfAsW6Hdtu794tc2J7ffa+u6W7aj7XdLcN/0da99rrWHftp4U+/hA45X
a6+GZp2InbPX+VgFp1Xv8lubgmOVr3W8EdgesPtmdP6ip61xs1NiPs1aU+FzgDVz
2e+oL22Oc9f/Zz7SerKyrHvZ736BljvrtOcwBWq4zWW0fy4QmHk14gPXR845t2xW
vsnd7nKtunbjIgA=
=hiy9
-----END PGP MESSAGE-----
var should = require('should');
var assert = require('assert');
var mongoose = require('mongoose');
var sha1 = require('sha1');
var fs = require('fs');
var async = require('async');
var nodecoin = require('../app/lib/nodecoin');
nodecoin.database.init();
var Amendment = mongoose.model('Amendment');
var Vote = mongoose.model('Vote');
describe('Votes for', function(){
describe('-beta_brousouf- AM0', function(){
var tests = {
cat: {
vote: new Vote({"issuer": "C73882B64B7E72237A2F460CE9CAB76D19A8651E"}),
file: __dirname + "/data/votes/BB-AM0/BB-AM0-OK-LOLCAT.asc",
amend: __dirname + "/data/amendments/BB-AM0-OK",
pubKey: __dirname + "/data/lolcat.pub",
verified: false
},
john: {
vote: new Vote({"issuer": "33BBFC0C67078D72AF128B5BA296CC530126F372"}),
file: __dirname + "/data/votes/BB-AM0/BB-AM0-OK-JOHNSNOW.asc",
amend: __dirname + "/data/amendments/BB-AM0-OK",
pubKey: __dirname + "/data/snow.pub",
verified: false
},
tobi: {
vote: new Vote({"issuer": "2E69197FAB029D8669EF85E82457A1587CA0ED9C"}),
file: __dirname + "/data/votes/BB-AM0/BB-AM0-OK-UCHIWA.asc",
amend: __dirname + "/data/amendments/BB-AM0-OK",
pubKey: __dirname + "/data/uchiha.pub",
verified: false
},
cat2: {
vote: new Vote({"issuer": "C73882B64B7E72237A2F460CE9CAB76D19A8651E"}),
file: __dirname + "/data/votes/BB-AM0/BB-AM0-WRONG-LOLCAT.asc",
amend: __dirname + "/data/amendments/BB-AM0-OK",
pubKey: __dirname + "/data/lolcat.pub",
verified: false
},
};
before(function (done) {
async.forEach([tests.cat, tests.john, tests.tobi, tests.cat2], function (test, callback) {
test.vote.loadFromFiles(test.file, test.amend, function (err) {
if(!err){
fs.readFile(test.pubKey, {encoding: "utf8"}, function (err, pkData) {
test.vote.publicKey = pkData;
test.vote.verify(function (err) {
if(!err) test.verified = true;
callback();
});
});
}
else callback(err);
});
}, done);
});
it('legit LOL CAT vote should be verified', function(){
(tests.cat.verified).should.be.ok;
});
it('legit JOHN SNOW vote should be verified', function(){
(tests.john.verified).should.be.ok;
});
it('legit OBITO UCHIWA vote should be verified', function(){
(tests.tobi.verified).should.be.ok;
});
it('non-legit CAT vote should be verified', function(){
(tests.cat2.verified).should.not.be.ok;
});
});
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment