Skip to content

Commit

Permalink
Fix #344 Standardized UCP documents
Browse files Browse the repository at this point in the history
  • Loading branch information
c-geek committed Feb 7, 2016
1 parent 9ac1bf7 commit 1a185c5
Show file tree
Hide file tree
Showing 28 changed files with 488 additions and 471 deletions.
2 changes: 2 additions & 0 deletions app/controllers/wot.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,7 @@ function WOTBinding (server) {

this.add = (req) => this.pushEntity(req, http2raw.identity, parsers.parseIdentity);

this.certify = (req) => this.pushEntity(req, http2raw.certification, parsers.parseCertification);

this.revoke = (req) => this.pushEntity(req, http2raw.revocation, parsers.parseRevocation);
}
2 changes: 1 addition & 1 deletion app/lib/blockchainContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function BlockchainContext(conf, dal) {
let idty = Identity.statics.fromInline(identity);
// Computes the hash if not done yet
if (!idty.hash)
idty.hash = (hashf(rawer.getIdentity(idty)) + "").toUpperCase();
idty.hash = (hashf(rawer.getOfficialIdentity(idty)) + "").toUpperCase();
yield dal.newIdentity(idty, block.number);
yield cleanRejectedIdentities(idty);
}
Expand Down
29 changes: 27 additions & 2 deletions app/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ module.exports = {
WRONG_DOCUMENT: { httpCode: 400, uerr: { ucode: 1005, message: "Document has unkown fields or wrong line ending format" }},

HTTP_PARAM_PUBKEY_REQUIRED: { httpCode: 400, uerr: { ucode: 1101, message: "Parameter `pubkey` is required" }},
HTTP_PARAM_SELF_REQUIRED: { httpCode: 400, uerr: { ucode: 1102, message: "Parameter `self` is required" }},
HTTP_PARAM_IDENTITY_REQUIRED: { httpCode: 400, uerr: { ucode: 1102, message: "Parameter `identity` is required" }},
HTTP_PARAM_PEER_REQUIRED: { httpCode: 400, uerr: { ucode: 1103, message: "Requires a peer" }},
HTTP_PARAM_BLOCK_REQUIRED: { httpCode: 400, uerr: { ucode: 1104, message: "Requires a block" }},
HTTP_PARAM_MEMBERSHIP_REQUIRED: { httpCode: 400, uerr: { ucode: 1105, message: "Requires a membership" }},
HTTP_PARAM_TX_REQUIRED: { httpCode: 400, uerr: { ucode: 1106, message: "Requires a transaction" }},
HTTP_PARAM_SIG_REQUIRED: { httpCode: 400, uerr: { ucode: 1107, message: "Parameter `sig` is required" }},
HTTP_PARAM_CERT_REQUIRED: { httpCode: 400, uerr: { ucode: 1108, message: "Parameter `cert` is required" }},
HTTP_PARAM_REVOCATION_REQUIRED: { httpCode: 400, uerr: { ucode: 1109, message: "Parameter `revocation` is required" }},

// Business errors
NO_MATCHING_IDENTITY: { httpCode: 404, uerr: { ucode: 2001, message: "No matching identity" }},
Expand Down Expand Up @@ -96,7 +98,30 @@ module.exports = {
}
},
IDENTITY: {
INLINE: exact(PUBKEY + ":" + SIGNATURE + ":" + BLOCK_UID + ":" + USER_ID)
INLINE: exact(PUBKEY + ":" + SIGNATURE + ":" + BLOCK_UID + ":" + USER_ID),
IDTY_TYPE: find('Type: (Identity)'),
IDTY_UID: find('UniqueID: (' + USER_ID + ')')
},
DOCUMENTS: {
DOC_VERSION: find('Version: (2)'),
DOC_CURRENCY: find('Currency: (' + CURRENCY + ')'),
DOC_ISSUER: find('Issuer: (' + PUBKEY + ')'),
TIMESTAMP: find('Timestamp: (' + BLOCK_UID + ')')
},
CERTIFICATION: {
CERT_TYPE: find('Type: (Certification)'),
IDTY_ISSUER: find('IdtyIssuer: (' + PUBKEY + ')'),
IDTY_UID: find('IdtyUniqueID: (' + USER_ID + ')'),
IDTY_TIMESTAMP: find('IdtyTimestamp: (' + BLOCK_UID + ')'),
IDTY_SIG: find('IdtySignature: (' + SIGNATURE + ')'),
CERT_TIMESTAMP: find('CertTimestamp: (' + BLOCK_UID + ')')
},
REVOCATION: {
REVOC_TYPE: find('Type: (Certification)'),
IDTY_ISSUER: find('IdtyIssuer: (' + PUBKEY + ')'),
IDTY_UID: find('IdtyUniqueID: (' + USER_ID + ')'),
IDTY_TIMESTAMP: find('IdtyTimestamp: (' + BLOCK_UID + ')'),
IDTY_SIG: find('IdtySignature: (' + SIGNATURE + ')')
},
MEMBERSHIP: {
BLOCK: find('Block: (' + BLOCK_UID + ')'),
Expand Down
23 changes: 14 additions & 9 deletions app/lib/crypto.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"use strict";
var _ = require('underscore');
var nacl = require('tweetnacl');
var scrypt = require('scrypt');
var base58 = require('./base58');
var rawer = require('./rawer');
var naclBinding = require('naclb');

var crypto_sign_BYTES = 64;
Expand Down Expand Up @@ -102,16 +104,19 @@ module.exports = {
*
*****************************/

isValidCertification: function (selfCert, selfSig, otherPubkey, otherSig, blockID, done) {
var raw = selfCert + selfSig + '\n' + 'META:TS:' + blockID + '\n';
var verified = this.verify(raw, otherSig, otherPubkey);
isValidCertification: function (idty, from, sig, blockID, currency, done) {
var raw = rawer.getOfficialCertification(_.extend(idty, {
currency: currency,
idty_issuer: idty.pubkey,
idty_uid: idty.uid,
idty_buid: idty.buid,
idty_sig: idty.sig,
issuer: from,
buid: blockID,
sig: ''
}));
var verified = this.verify(raw, sig, from);
done(verified ? null : 'Wrong signature for certification', verified);
},

isValidRevocation: function (selfCert, selfSig, selfPubkey, selfRevocationSig, done) {
var raw = selfCert + selfSig + '\n' + 'META:REVOKE\n';
var verified = this.verify(raw, selfRevocationSig, selfPubkey);
done(verified ? null : 'Wrong signature for revocation');
}
};

Expand Down
35 changes: 0 additions & 35 deletions app/lib/dal/fileDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,38 +337,6 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
});
};

this.getIdentityByHashWithCertsOrNull = function(hash) {
return that.getIdentityByHashOrNull(hash)
.catch(function(){
return null;
})
.then(function(idty){
return that.fillIdentityWithCerts(idty);
});
};

this.fillIdentitiesWithCerts = function(idties) {
return idties.reduce(function(p, aIdty) {
return that.certDAL.getToTarget(aIdty.hash)
.then(function(certs){
aIdty.certs = certs;
return Q();
});
}, Q())
.then(() => idties);
};

this.fillIdentityWithCerts = function(idty) {
if (!idty) {
return Q(null);
}
return that.certDAL.getToTarget(idty.hash)
.then(function(certs){
idty.certs = certs;
return idty;
});
};

this.getMembers = function(done) {
return that.idtyDAL.getWhoIsOrWasMember()
.then(function(idties) {
Expand Down Expand Up @@ -491,9 +459,6 @@ function FileDAL(home, localDir, myFS, dalName, sqlite, wotbInstance) {
return that.idtyDAL.getFromUID(uid)
.catch(function(){
return null;
})
.then(function(idty){
return that.fillIdentityWithCerts(idty);
});
};

Expand Down
3 changes: 2 additions & 1 deletion app/lib/dal/sqliteDAL/LinksDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function LinksDAL(db, wotb) {
timestamp: { $lte: minTimestamp },
obsolete: false
});
linksToObsolete.forEach((link) => wotb.removeLink(link.from_wotb_id, link.to_wotb_id));
linksToObsolete.forEach((link) => wotb.removeLink(link.from_wotb_id, link.to_wotb_id, true));
return that.sqlUpdateWhere({ obsolete: true }, {
timestamp: { $lte: minTimestamp },
obsolete: false
Expand Down Expand Up @@ -123,6 +123,7 @@ function LinksDAL(db, wotb) {
});
if (links.length) {
queries.push(insert + '\n' + values.join(',\n') + ';');
logger.query(queries.join('\n'));
}
if (queries.length) {
return that.exec(queries.join('\n'));
Expand Down
24 changes: 23 additions & 1 deletion app/lib/entity/certification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";
var _ = require('underscore');
var rawer = require('../rawer');
var ucp = require('../ucp');

var Certification = function(json) {

Expand All @@ -11,12 +13,30 @@ var Certification = function(json) {
that[key] = json[key];
});

this.from = this.pubkey = this.from || this.pubkey;
this.from = this.pubkey = this.from || this.pubkey || this.issuer;
this.block = this.block_number = parseInt(this.block || this.block_number);

this.getRaw = () => rawer.getOfficialCertification(this);

this.getTargetHash = () => ucp.format.hashf(this.idty_uid + this.idty_buid + this.idty_issuer);

this.inline = function () {
return [this.pubkey, this.to, this.block_number, this.sig].join(':');
};

this.json = () => {
return {
"issuer": this.issuer,
"timestamp": this.buid,
"sig": this.sig,
"target": {
"issuer": this.idty_issuer,
"uid": this.idty_uid,
"timestamp": this.idty_buid,
"sig": this.idty_sig
}
};
};
};

Certification.statics = {};
Expand All @@ -42,4 +62,6 @@ Certification.statics.toInline = function (entity, certificationModel) {
return [entity.pubkey, entity.to, entity.block_number, entity.sig].join(':');
};

Certification.statics.fromJSON = (json) => new Certification(json);

module.exports = Certification;
29 changes: 7 additions & 22 deletions app/lib/entity/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,35 +77,20 @@ var Identity = function(json) {
};

this.selfCert = function () {
return rawer.getSelfIdentity(this);
return rawer.getOfficialIdentity(this);
};

this.othersCerts = function () {
var certs = [];
this.certs.forEach(function(cert){
if (cert.to == that.pubkey) {
// Signature for this pubkey
certs.push(cert);
}
});
return certs;
this.rawWithoutSig = () => {
let sig = this.sig;
this.sig = '';
let raw = rawer.getOfficialIdentity(this);
this.sig = sig;
return raw;
};

this.getTargetHash = function () {
return hashf(this.uid + this.buid + this.pubkey).toUpperCase();
};

this.getRawPubkey = function () {
return rawer.getIdentityPubkey(this);
};

this.getRawSelf = function () {
return rawer.getIdentitySelf(this);
};

this.getRawOther = function () {
return rawer.getIdentityOthers(this);
};
};

Identity.statics = {};
Expand Down
29 changes: 29 additions & 0 deletions app/lib/entity/revocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";
var _ = require('underscore');
var rawer = require('../rawer');
var ucp = require('../ucp');

var Revocation = function(json) {

var that = this;

_(json).keys().forEach(function(key) {
that[key] = json[key];
});

this.getRaw = () => rawer.getOfficialRevocation(this);

this.rawWithoutSig = () => {
let revocation = this.revocation;
this.revocation = '';
let raw = rawer.getOfficialRevocation(this);
this.revocation = revocation;
return raw;
};
};

Revocation.statics = {};

Revocation.statics.fromJSON = (json) => new Revocation(json);

module.exports = Revocation;
10 changes: 6 additions & 4 deletions app/lib/globalValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ function GlobalValidator (conf, dao) {
else if (cert.from == res.idty.pubkey)
next('Rejected certification: certifying its own self-certification has no meaning');
else {
var selfCert = new Identity(res.idty).selfCert();
var targetId = [cert.block_number, res.target.hash].join('-');
crypto.isValidCertification(selfCert, res.idty.sig, cert.from, cert.sig, targetId, next);
var buid = [cert.block_number, res.target.hash].join('-');
res.idty.currency = conf.currency;
crypto.isValidCertification(new Identity(res.idty), cert.from, cert.sig, buid, block.currency, next);
}
}
], function(err) {
Expand Down Expand Up @@ -852,7 +852,9 @@ function GlobalValidator (conf, dao) {
if (idty.revoked) {
throw "A revoked identity cannot be revoked again";
}
let rawRevocation = rawer.getSelfRevocation({
let rawRevocation = rawer.getOfficialRevocation({
currency: block.currency,
issuer: idty.pubkey,
uid: idty.uid,
buid: idty.buid,
sig: idty.sig,
Expand Down
3 changes: 2 additions & 1 deletion app/lib/localValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ function hasWrongSignatureForIdentities (block) {
var wrongSig = false;
while (!wrongSig && i < block.identities.length) {
var idty = Identity.statics.fromInline(block.identities[i]);
wrongSig = !crypto.verify(idty.selfCert(), idty.sig, idty.pubkey);
idty.currency = block.currency;
wrongSig = !crypto.verify(idty.rawWithoutSig(), idty.sig, idty.pubkey);
i++;
}
return wrongSig;
Expand Down
Loading

0 comments on commit 1a185c5

Please sign in to comment.