diff --git a/app/common/index.js b/app/common/index.js index 2a9483b86c60969e031f9a9802310eb4644438e9..0d75e558187f554d742725707fb56d9547ff92ad 100644 --- a/app/common/index.js +++ b/app/common/index.js @@ -1,8 +1,5 @@ "use strict"; module.exports = { - buid: require('./lib/buid'), - document: require('./lib/document'), - constants: require('./lib/constants'), - parsers: require('./lib/parsers') + document: require('./lib/document') } diff --git a/app/common/lib/buid.js b/app/common/lib/buid.js deleted file mode 100644 index a08fb70e4193fcf4dacb51cd4c020d733bfe7582..0000000000000000000000000000000000000000 --- a/app/common/lib/buid.js +++ /dev/null @@ -1,37 +0,0 @@ -"use strict"; -const BLOCK_UID = /^(0|[1-9]\d{0,18})-[A-F0-9]{64}$/; - -const buidFunctions = function(number, hash) { - if (arguments.length === 2) { - return [number, hash].join('-'); - } - if (arguments[0]) { - return [arguments[0].number, arguments[0].hash].join('-'); - } - return '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855'; -}; - -buidFunctions.fromTS = (line) => line.match(/TS:(.*)/)[1]; -buidFunctions.fromIdty = (idty) => this(idty.ts_number, idty.ts_hash); - -module.exports = { - - format: { - - hashf: (value) => require('../../lib/common-libs').hashf(String(value)), - - isBuid: (value) => { - return (typeof value === 'string') && value.match(BLOCK_UID) ? true : false; - }, - - buid: buidFunctions, - - obuid: (line) => { - let sp = this.buid.fromTS(line).split('-'); - return { - number: sp[0], - hash: sp[1] - }; - } - } -}; diff --git a/app/common/lib/document/block.js b/app/common/lib/document/block.js index 0abe95475b1fbc5aee1c1ffb119ad20aaf6499e9..046bcbbe80db4ffb1e76595f9208680820c163cc 100644 --- a/app/common/lib/document/block.js +++ b/app/common/lib/document/block.js @@ -1,7 +1,6 @@ "use strict"; const _ = require('underscore') -const constants = require('../constants'); -const regex = require('../regex'); +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants const Transaction = require('./transaction'); // Constants diff --git a/app/common/lib/document/certification.js b/app/common/lib/document/certification.js index 490d2ede414e8e0aae9a468c5a9197a66fd1f859..1eed58ac189c5b104082ca00ede5002ba0bc6d0b 100644 --- a/app/common/lib/document/certification.js +++ b/app/common/lib/document/certification.js @@ -1,10 +1,5 @@ "use strict"; -const constants = require('../constants'); -const regex = require('../regex'); - -// Constants -const SIGNED = true -const UNSIGNED = !SIGNED +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants module.exports = class Certification { diff --git a/app/common/lib/document/identity.js b/app/common/lib/document/identity.js index 23c5c93f8fbd06c9b4d2591b994e047b20b99f32..a82e324c181571b915736bc0b6cf251b8f077a9d 100644 --- a/app/common/lib/document/identity.js +++ b/app/common/lib/document/identity.js @@ -1,6 +1,5 @@ "use strict"; -const constants = require('../constants'); -const regex = require('../regex'); +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants // Constants const SIGNED = true diff --git a/app/common/lib/document/membership.js b/app/common/lib/document/membership.js index 14844db8c31e7840183e64510533cc44588a1222..77cc0c7b8c9e67acee113f65298092c755205282 100644 --- a/app/common/lib/document/membership.js +++ b/app/common/lib/document/membership.js @@ -1,10 +1,5 @@ "use strict"; -const constants = require('../constants'); -const regex = require('../regex'); - -// Constants -const SIGNED = false -const UNSIGNED = !SIGNED +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants module.exports = class Membership { diff --git a/app/common/lib/document/peer.js b/app/common/lib/document/peer.js index 92c0ce055ee7a7dc952f0317e7a4e8cab5d9b716..ea6434768ca7d73b41fd972f3156e344e0d04de7 100644 --- a/app/common/lib/document/peer.js +++ b/app/common/lib/document/peer.js @@ -1,6 +1,5 @@ "use strict"; -const constants = require('../constants'); -const regex = require('../regex'); +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants // Constants const SIGNED = false @@ -68,7 +67,7 @@ module.exports = class Peer { getBMA() { let bma = null; this.endpoints.forEach((ep) => { - const matches = !bma && ep.match(regex.BMA_REGEXP); + const matches = !bma && ep.match(constants.BMA_REGEXP); if (matches) { bma = { "dns": matches[2] || '', diff --git a/app/common/lib/document/transaction.js b/app/common/lib/document/transaction.js index f19dbd629b4abe544d613362ce78e3a3f1526798..044b8e0eac8daa2c008cc94a9ab1bdca44b50d71 100644 --- a/app/common/lib/document/transaction.js +++ b/app/common/lib/document/transaction.js @@ -1,12 +1,7 @@ "use strict"; -const constants = require('../constants'); -const regex = require('../regex'); +const constants = require('../../../../app/lib/common-libs/constants').CommonConstants const hashf = require('../../../lib/common-libs').hashf -// Constants -const SIGNED = false -const UNSIGNED = !SIGNED - module.exports = class Transaction { constructor( diff --git a/app/common/lib/parsers/GenericParser.js b/app/common/lib/parsers/GenericParser.js deleted file mode 100644 index 255c47aba4342c9586c4c8cb029b53001a806c47..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/GenericParser.js +++ /dev/null @@ -1,108 +0,0 @@ -"use strict"; -const util = require('util'); -const stream = require('stream'); -const hashf = require('../../../lib/common-libs').hashf -const constants = require('../constants'); - -module.exports = GenericParser; - -function GenericParser (captures, multipleLinesFields, rawerFunc) { - - stream.Transform.call(this, { decodeStrings: false, objectMode: true }); - - this.rawerFunc = rawerFunc; - - this._simpleLineExtraction = (pr, rawEntry, cap) => { - const fieldValue = rawEntry.match(cap.regexp); - if(fieldValue && fieldValue.length >= 2){ - pr[cap.prop] = cap.parser ? cap.parser(fieldValue[1], pr) : fieldValue[1]; - } - return; - }; - - this._multipleLinesExtraction = (am, wholeAmend, cap) => { - const fieldValue = wholeAmend.match(cap.regexp); - let line = 0; - am[cap.prop] = []; - if(fieldValue && fieldValue.length >= 2) - { - const lines = fieldValue[1].split(/\n/); - if(lines[lines.length - 1].match(/^$/)){ - for (let i = 0; i < lines.length - 1; i++) { - line = lines[i]; - let fprChange = line.match(/([+-][A-Z\d]{40})/); - if(fprChange && fprChange.length === 2){ - am[cap.prop].push(fprChange[1]); - } - else{ - return "Wrong structure for line: '" + line + "'"; - } - } - } - else return "Wrong structure for line: '" + line + "'"; - } - }; - - this.syncWrite = (str, logger) => { - let error; - const obj = {}; - this._parse(str, obj); - this._clean(obj); - if (!error) { - error = this._verify(obj); - } - if (!error) { - const raw = this.rawerFunc(obj); - if (hashf(str) !== hashf(raw)) - error = constants.ERRORS.WRONG_DOCUMENT; - if (error) { - logger && logger.trace(error); - logger && logger.trace('-----------------'); - logger && logger.trace('Written: %s', JSON.stringify({ str: str })); - logger && logger.trace('Extract: %s', JSON.stringify({ raw: raw })); - logger && logger.trace('-----------------'); - } - } - if (error){ - logger && logger.trace(error); - throw constants.ERRORS.WRONG_DOCUMENT; - } - return obj; - }; - - this._parse = (str, obj) => { - let error; - if(!str){ - error = "No document given"; - } else { - error = ""; - obj.hash = hashf(str).toUpperCase(); - // Divide in 2 parts: document & signature - const sp = str.split('\n'); - if (sp.length < 3) { - error = "Wrong document: must have at least 2 lines"; - } - else { - const endOffset = str.match(/\n$/) ? 2 : 1; - obj.signature = sp[sp.length - endOffset]; - obj.hash = hashf(str).toUpperCase(); - obj.raw = sp.slice(0, sp.length - endOffset).join('\n') + '\n'; - const docLF = obj.raw.replace(/\r\n/g, "\n"); - if(docLF.match(/\n$/)){ - captures.forEach((cap) => { - if(~multipleLinesFields.indexOf(multipleLinesFields)) - error = this._multipleLinesExtraction(obj, docLF, cap); - else - this._simpleLineExtraction(obj, docLF, cap); - }); - } - else{ - error = "Bad document structure: no new line character at the end of the document."; - } - } - } - return error; - }; -} - -util.inherits(GenericParser, stream.Transform); diff --git a/app/common/lib/parsers/certification.js b/app/common/lib/parsers/certification.js deleted file mode 100644 index 554eb22c873bbbe23b544b0820228acf6d7e195a..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/certification.js +++ /dev/null @@ -1,43 +0,0 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const util = require('util'); -const rawer = require('../../../../app/lib/common-libs').rawer -const constants = require('../../../../app/common/lib/constants'); - -module.exports = CertificationParser; - -function CertificationParser (onError) { - - const captures = [ - {prop: "version", regexp: constants.DOCUMENTS.DOC_VERSION }, - {prop: "type", regexp: constants.CERTIFICATION.CERT_TYPE }, - {prop: "currency", regexp: constants.DOCUMENTS.DOC_CURRENCY }, - {prop: "issuer", regexp: constants.DOCUMENTS.DOC_ISSUER }, - {prop: "idty_issuer", regexp: constants.CERTIFICATION.IDTY_ISSUER }, - {prop: "idty_sig", regexp: constants.CERTIFICATION.IDTY_SIG }, - {prop: "idty_buid", regexp: constants.CERTIFICATION.IDTY_TIMESTAMP}, - {prop: "idty_uid", regexp: constants.CERTIFICATION.IDTY_UID }, - {prop: "buid", regexp: constants.CERTIFICATION.CERT_TIMESTAMP } - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getOfficialCertification, onError); - - this._clean = (obj) => { - obj.documentType = 'certification'; - obj.sig = obj.signature; - obj.block = obj.buid; - if (obj.block) { - obj.number = obj.block.split('-')[0]; - obj.fpr = obj.block.split('-')[1]; - } else { - obj.number = '0'; - obj.fpr = ''; - } - }; - - this._verify = (obj) => ["version", "type", "currency", "issuer", "idty_issuer", "idty_sig", "idty_buid", "idty_uid", "block"].reduce(function (p, field) { - return p || (!obj[field] && "Wrong format for certification"); - }, null); -} - -util.inherits(CertificationParser, GenericParser); diff --git a/app/common/lib/parsers/identity.js b/app/common/lib/parsers/identity.js deleted file mode 100644 index 1cde03e90bb4a9844c61fb564daf66d9af3ea75b..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/identity.js +++ /dev/null @@ -1,47 +0,0 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const util = require('util'); -const rawer = require('../../../../app/lib/common-libs').rawer -const hashf = require('../../../../app/lib/common-libs').hashf -const constants = require('../../../../app/common/lib/constants'); - -module.exports = IdentityParser; - -function IdentityParser (onError) { - - const captures = [ - {prop: "version", regexp: constants.DOCUMENTS.DOC_VERSION }, - {prop: "type", regexp: constants.IDENTITY.IDTY_TYPE}, - {prop: "currency", regexp: constants.DOCUMENTS.DOC_CURRENCY }, - {prop: "pubkey", regexp: constants.DOCUMENTS.DOC_ISSUER }, - {prop: "uid", regexp: constants.IDENTITY.IDTY_UID }, - {prop: "buid", regexp: constants.DOCUMENTS.TIMESTAMP } - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getOfficialIdentity, onError); - - this._clean = (obj) => { - obj.documentType = 'identity'; - obj.sig = obj.signature; - if (obj.uid && obj.buid && obj.pubkey) { - obj.hash = hashf(obj.uid + obj.buid + obj.pubkey).toUpperCase(); - } - }; - - this._verify = (obj) => { - if (!obj.pubkey) { - return "No pubkey found"; - } - if (!obj.uid) { - return "Wrong user id format"; - } - if (!obj.buid) { - return "Could not extract block uid"; - } - if (!obj.sig) { - return "No signature found for self-certification"; - } - }; -} - -util.inherits(IdentityParser, GenericParser); diff --git a/app/common/lib/parsers/index.js b/app/common/lib/parsers/index.js deleted file mode 100644 index 7eec548c6ef8f3c710496fab37dbe00fbdf10690..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/index.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -module.exports = { - parseIdentity: (new (require('./identity'))), - parseCertification:(new (require('./certification'))), - parseRevocation: (new (require('./revocation'))), - parseTransaction: (new (require('./transaction'))), - parsePeer: (new (require('./peer'))), - parseMembership: (new (require('./membership'))), - parseBlock: (new (require('./block'))) -}; diff --git a/app/common/lib/parsers/membership.js b/app/common/lib/parsers/membership.js deleted file mode 100644 index 6cad686530b5ed3b98b85253999530b32a899191..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/membership.js +++ /dev/null @@ -1,75 +0,0 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const ucp = require('../buid'); -const rawer = require('../../../lib/common-libs/index').rawer -const util = require('util'); -const constants = require('../constants'); - -module.exports = MembershipParser; - -function MembershipParser (onError) { - - const captures = [ - {prop: "version", regexp: constants.MEMBERSHIP.VERSION }, - {prop: "currency", regexp: constants.MEMBERSHIP.CURRENCY }, - {prop: "issuer", regexp: constants.MEMBERSHIP.ISSUER }, - {prop: "membership", regexp: constants.MEMBERSHIP.MEMBERSHIP }, - {prop: "userid", regexp: constants.MEMBERSHIP.USERID }, - {prop: "block", regexp: constants.MEMBERSHIP.BLOCK}, - {prop: "certts", regexp: constants.MEMBERSHIP.CERTTS} - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getMembership, onError); - - this._clean = (obj) => { - obj.documentType = 'membership'; - if (obj.block) { - obj.number = obj.block.split('-')[0]; - obj.fpr = obj.block.split('-')[1]; - } else { - obj.number = '0'; - obj.fpr = ''; - } - }; - - this._verify = (obj) => { - let err = null; - const codes = { - 'BAD_VERSION': 150, - 'BAD_CURRENCY': 151, - 'BAD_ISSUER': 152, - 'BAD_MEMBERSHIP': 153, - 'BAD_REGISTRY_TYPE': 154, - 'BAD_BLOCK': 155, - 'BAD_USERID': 156, - 'BAD_CERTTS': 157 - }; - if(!err){ - if(!obj.version || !obj.version.match(constants.DOCUMENTS_VERSION_REGEXP)) - err = {code: codes.BAD_VERSION, message: "Version unknown"}; - } - if(!err){ - if(obj.issuer && !obj.issuer.match(constants.BASE58)) - err = {code: codes.BAD_ISSUER, message: "Incorrect issuer field"}; - } - if(!err){ - if(!(obj.membership || "").match(/^(IN|OUT)$/)) - err = {code: codes.BAD_MEMBERSHIP, message: "Incorrect Membership field: must be either IN or OUT"}; - } - if(!err){ - if(obj.block && !obj.block.match(constants.BLOCK_UID)) - err = {code: codes.BAD_BLOCK, message: "Incorrect Block field: must be a positive or zero integer, a dash and an uppercased SHA1 hash"}; - } - if(!err){ - if(obj.userid && !obj.userid.match(constants.USER_ID)) - err = {code: codes.BAD_USERID, message: "UserID must match udid2 format"}; - } - if(!err){ - if(!ucp.format.isBuid(obj.certts)) - err = {code: codes.BAD_CERTTS, message: "CertTS must be a valid timestamp"}; - } - return err && err.message; - }; -} - -util.inherits(MembershipParser, GenericParser); diff --git a/app/common/lib/parsers/revocation.js b/app/common/lib/parsers/revocation.js deleted file mode 100644 index 1713f7407a74721788433735f32182197de887b5..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/revocation.js +++ /dev/null @@ -1,52 +0,0 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const util = require('util'); -const rawer = require('../../../lib/common-libs').rawer -const hashf = require('../../../lib/common-libs').hashf -const constants = require('../constants'); - -module.exports = RevocationParser; - -function RevocationParser (onError) { - - const captures = [ - {prop: "version", regexp: constants.DOCUMENTS.DOC_VERSION }, - {prop: "type", regexp: constants.REVOCATION.REVOC_TYPE }, - {prop: "currency", regexp: constants.DOCUMENTS.DOC_CURRENCY }, - {prop: "issuer", regexp: constants.DOCUMENTS.DOC_ISSUER }, - {prop: "sig", regexp: constants.REVOCATION.IDTY_SIG }, - {prop: "buid", regexp: constants.REVOCATION.IDTY_TIMESTAMP}, - {prop: "uid", regexp: constants.REVOCATION.IDTY_UID } - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getOfficialRevocation, onError); - - this._clean = (obj) => { - obj.documentType = 'revocation'; - obj.pubkey = obj.issuer; - obj.revocation = obj.signature; - if (obj.uid && obj.buid && obj.pubkey) { - obj.hash = hashf(obj.uid + obj.buid + obj.pubkey).toUpperCase(); - } - }; - - this._verify = (obj) => { - if (!obj.pubkey) { - return "No pubkey found"; - } - if (!obj.uid) { - return "Wrong user id format"; - } - if (!obj.buid) { - return "Could not extract block uid"; - } - if (!obj.sig) { - return "No signature found for identity"; - } - if (!obj.revocation) { - return "No revocation signature found"; - } - }; -} - -util.inherits(RevocationParser, GenericParser); diff --git a/app/common/lib/parsers/transaction.js b/app/common/lib/parsers/transaction.js deleted file mode 100644 index 7b552a19c79c163670f23c15a6f896866b8dfe09..0000000000000000000000000000000000000000 --- a/app/common/lib/parsers/transaction.js +++ /dev/null @@ -1,128 +0,0 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const rawer = require('../../../lib/common-libs/index').rawer -const constants = require('../constants'); -const util = require('util'); - -module.exports = TransactionParser; - -function TransactionParser (onError) { - - const captures = [ - {prop: "version", regexp: /Version: (.*)/}, - {prop: "currency", regexp: /Currency: (.*)/}, - {prop: "issuers", regexp: /Issuers:\n([\s\S]*)Inputs/, parser: extractIssuers }, - {prop: "inputs", regexp: /Inputs:\n([\s\S]*)Unlocks/, parser: extractInputs }, - {prop: "unlocks", regexp: /Unlocks:\n([\s\S]*)Outputs/,parser: extractUnlocks }, - {prop: "outputs", regexp: /Outputs:\n([\s\S]*)/, parser: extractOutputs }, - {prop: "comment", regexp: constants.TRANSACTION.COMMENT }, - {prop: "locktime", regexp: constants.TRANSACTION.LOCKTIME }, - {prop: "blockstamp", regexp: constants.TRANSACTION.BLOCKSTAMP }, - {prop: "signatures", regexp: /Outputs:\n([\s\S]*)/, parser: extractSignatures } - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getTransaction, onError); - - this._clean = (obj) => { - obj.documentType = 'transaction'; - obj.comment = obj.comment || ""; - obj.locktime = parseInt(obj.locktime) || 0; - obj.signatures.push(obj.signature); - const compactSize = 2 // Header + blockstamp - + obj.issuers.length - + obj.inputs.length - + obj.unlocks.length - + obj.outputs.length - + (obj.comment ? 1 : 0) - + obj.signatures; - if (compactSize > 100) { - throw 'A transaction has a maximum size of 100 lines'; - } - }; - - this._verify = (obj) => { - let err = null; - const codes = { - 'BAD_VERSION': 150, - 'NO_BLOCKSTAMP': 151 - }; - if(!err){ - // Version - if(!obj.version || !obj.version.match(constants.DOCUMENTS_TRANSACTION_VERSION_REGEXP)) - err = {code: codes.BAD_VERSION, message: "Version unknown"}; - // Blockstamp - if(!obj.blockstamp || !obj.blockstamp.match(constants.BLOCKSTAMP_REGEXP)) - err = {code: codes.BAD_VERSION, message: "Blockstamp is required"}; - } - return err && err.message; - }; -} - -function extractIssuers(raw) { - const issuers = []; - const lines = raw.split(/\n/); - for (const line of lines) { - if (line.match(constants.TRANSACTION.SENDER)) { - issuers.push(line); - } else { - // Not a pubkey, stop reading - break; - } - } - return issuers; -} - -function extractInputs(raw) { - const inputs = []; - const lines = raw.split(/\n/); - for (const line of lines) { - if (line.match(constants.TRANSACTION.SOURCE_V3)) { - inputs.push(line); - } else { - // Not a transaction input, stop reading - break; - } - } - return inputs; -} - -function extractUnlocks(raw) { - const unlocks = []; - const lines = raw.split(/\n/); - for (const line of lines) { - if (line.match(constants.TRANSACTION.UNLOCK)) { - unlocks.push(line); - } else { - // Not a transaction unlock, stop reading - break; - } - } - return unlocks; -} - -function extractOutputs(raw) { - const outputs = []; - const lines = raw.split(/\n/); - for (const line of lines) { - if (line.match(constants.TRANSACTION.TARGET)) { - outputs.push(line); - } else { - // Not a transaction input, stop reading - break; - } - } - return outputs; -} - -function extractSignatures(raw) { - const signatures = []; - const lines = raw.split(/\n/); - for (const line of lines) { - if (line.match(constants.SIG)) { - signatures.push(line); - } - } - return signatures; -} - -util.inherits(TransactionParser, GenericParser); diff --git a/app/common/lib/regex.js b/app/common/lib/regex.js deleted file mode 100644 index a0ae4bdb5796c111db7dc06a3ffc540607b6183f..0000000000000000000000000000000000000000 --- a/app/common/lib/regex.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -module.exports = { - BMA_REGEXP: /^BASIC_MERKLED_API( ([a-z_][a-z0-9-_.]*))?( ([0-9.]+))?( ([0-9a-f:]+))?( ([0-9]+))$/ -} \ No newline at end of file diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts index 6ea0513afda8b792766688fd2009150ef7a7ab01..ede15c5b3cc38f6cb1e4f1da95e42038ae0a09e2 100644 --- a/app/lib/blockchain/DuniterBlockchain.ts +++ b/app/lib/blockchain/DuniterBlockchain.ts @@ -11,6 +11,7 @@ import {IdentityDTO} from "../dto/IdentityDTO" import {CertificationDTO} from "../dto/CertificationDTO" import {MembershipDTO} from "../dto/MembershipDTO" import {TransactionDTO} from "../dto/TransactionDTO" +import {CommonConstants} from "../common-libs/constants" const _ = require('underscore') const common = require('../../../app/common') @@ -278,7 +279,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { async createNewcomers(iindex:IindexEntry[], dal:any, logger:any) { for (const entry of iindex) { - if (entry.op == common.constants.IDX_CREATE) { + if (entry.op == CommonConstants.IDX_CREATE) { // Reserves a wotb ID entry.wotb_id = dal.wotb.addNode(); logger.trace('%s was affected wotb_id %s', entry.uid, entry.wotb_id); @@ -311,8 +312,8 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { async updateWallets(sindex:SindexEntry[], aDal:any, reverse = false) { const differentConditions = _.uniq(sindex.map((entry) => entry.conditions)) for (const conditions of differentConditions) { - const creates = _.filter(sindex, (entry:SindexEntry) => entry.conditions === conditions && entry.op === common.constants.IDX_CREATE) - const updates = _.filter(sindex, (entry:SindexEntry) => entry.conditions === conditions && entry.op === common.constants.IDX_UPDATE) + const creates = _.filter(sindex, (entry:SindexEntry) => entry.conditions === conditions && entry.op === CommonConstants.IDX_CREATE) + const updates = _.filter(sindex, (entry:SindexEntry) => entry.conditions === conditions && entry.op === CommonConstants.IDX_UPDATE) const positives = creates.reduce((sum:number, src:SindexEntry) => sum + src.amount * Math.pow(10, src.base), 0) const negatives = updates.reduce((sum:number, src:SindexEntry) => sum + src.amount * Math.pow(10, src.base), 0) const wallet = await aDal.getWallet(conditions) @@ -335,7 +336,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { for (const entry of writtenOn) { const from = await dal.getWrittenIdtyByPubkey(entry.issuer); const to = await dal.getWrittenIdtyByPubkey(entry.receiver); - if (entry.op == common.constants.IDX_CREATE) { + if (entry.op == CommonConstants.IDX_CREATE) { // We remove the created link dal.wotb.removeLink(from.wotb_id, to.wotb_id, true); } else { @@ -375,7 +376,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { for (const entry of joiners) { // Undo 'join' which can be either newcomers or comebackers // => equivalent to i_index.member = true AND i_index.op = 'UPDATE' - if (entry.member === true && entry.op === common.constants.IDX_UPDATE) { + if (entry.member === true && entry.op === CommonConstants.IDX_UPDATE) { const idty = await dal.getWrittenIdtyByPubkey(entry.pub); dal.wotb.setEnabled(false, idty.wotb_id); } @@ -384,7 +385,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { for (const entry of newcomers) { // Undo newcomers // => equivalent to i_index.op = 'CREATE' - if (entry.op === common.constants.IDX_CREATE) { + if (entry.op === CommonConstants.IDX_CREATE) { // Does not matter which one it really was, we pop the last X identities dal.wotb.removeNode(); } @@ -393,7 +394,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain { for (const entry of excluded) { // Undo excluded (make them become members again in wotb) // => equivalent to m_index.member = false - if (entry.member === false && entry.op === common.constants.IDX_UPDATE) { + if (entry.member === false && entry.op === CommonConstants.IDX_UPDATE) { const idty = await dal.getWrittenIdtyByPubkey(entry.pub); dal.wotb.setEnabled(true, idty.wotb_id); } diff --git a/app/lib/common-libs/buid.ts b/app/lib/common-libs/buid.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0cc71b9cb7e910ddbe7c6aeb0b5de26f2c42198 --- /dev/null +++ b/app/lib/common-libs/buid.ts @@ -0,0 +1,32 @@ +"use strict"; +const BLOCK_UID = /^(0|[1-9]\d{0,18})-[A-F0-9]{64}$/; + +const buidFunctions:any = function(number:number, hash:string) { + if (arguments.length === 2) { + return [number, hash].join('-'); + } + if (arguments[0]) { + return [arguments[0].number, arguments[0].hash].join('-'); + } + return '0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855'; +} + +buidFunctions.fromTS = (line:string) => { + const match = line.match(/TS:(.*)/) + return (match && match[1]) || "" +} +buidFunctions.fromIdty = (idty:any) => { + return buidFunctions(idty.ts_number, idty.ts_hash) +} + +export const Buid = { + + format: { + + isBuid: (value:any) => { + return (typeof value === 'string') && value.match(BLOCK_UID) ? true : false; + }, + + buid: buidFunctions + } +}; diff --git a/app/common/lib/constants.js b/app/lib/common-libs/constants.ts similarity index 98% rename from app/common/lib/constants.js rename to app/lib/common-libs/constants.ts index 93b7989907d45727eddbdff1dffc28e92d889df4..b47770bb1d7480dd2c4f316f42b2564fcf96b8fa 100644 --- a/app/common/lib/constants.js +++ b/app/lib/common-libs/constants.ts @@ -32,13 +32,14 @@ const MAXIMUM_LEN_OF_COMPACT_TX = 100 const MAXIMUM_LEN_OF_OUTPUT = 2000 const MAXIMUM_LEN_OF_UNLOCK = MAXIMUM_LEN_OF_OUTPUT -module.exports = { +export const CommonConstants = { FORMATS: { CURRENCY, PUBKEY, INTEGER, - FINGERPRINT + FINGERPRINT, + TIMESTAMP }, BLOCK_GENERATED_VERSION: 10, @@ -219,10 +220,10 @@ module.exports = { }, } -function exact (regexpContent) { +function exact (regexpContent:string) { return new RegExp("^" + regexpContent + "$"); } -function find (regexpContent) { +function find (regexpContent:string) { return new RegExp(regexpContent); } diff --git a/app/lib/common-libs/parsers/GenericParser.ts b/app/lib/common-libs/parsers/GenericParser.ts new file mode 100644 index 0000000000000000000000000000000000000000..9ab5b437ae6e1a6e01d8249997a63dd5b11c334f --- /dev/null +++ b/app/lib/common-libs/parsers/GenericParser.ts @@ -0,0 +1,81 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" +import * as stream from "stream" +import {hashf} from "../../../lib/common" + +export abstract class GenericParser extends stream.Transform { + + constructor( + private captures:any, + private rawerFunc:any) { + super({ decodeStrings: false, objectMode: true }) + } + + abstract _clean(obj:any): void + abstract _verify(obj:any): any + + static _simpleLineExtraction(pr:any, rawEntry:string, cap:any) { + const fieldValue = rawEntry.match(cap.regexp); + if(fieldValue && fieldValue.length >= 2){ + pr[cap.prop] = cap.parser ? cap.parser(fieldValue[1], pr) : fieldValue[1]; + } + return; + } + + syncWrite(str:string, logger:any = null): any { + let error; + const obj = {}; + this._parse(str, obj); + this._clean(obj); + if (!error) { + error = this._verify(obj); + } + if (!error) { + const raw = this.rawerFunc(obj); + if (hashf(str) !== hashf(raw)) + error = CommonConstants.ERRORS.WRONG_DOCUMENT; + if (error) { + logger && logger.trace(error); + logger && logger.trace('-----------------'); + logger && logger.trace('Written: %s', JSON.stringify({ str: str })); + logger && logger.trace('Extract: %s', JSON.stringify({ raw: raw })); + logger && logger.trace('-----------------'); + } + } + if (error){ + logger && logger.trace(error); + throw CommonConstants.ERRORS.WRONG_DOCUMENT; + } + return obj; + }; + + _parse(str:string, obj:any) { + let error; + if(!str){ + error = "No document given"; + } else { + error = ""; + obj.hash = hashf(str).toUpperCase(); + // Divide in 2 parts: document & signature + const sp = str.split('\n'); + if (sp.length < 3) { + error = "Wrong document: must have at least 2 lines"; + } + else { + const endOffset = str.match(/\n$/) ? 2 : 1; + obj.signature = sp[sp.length - endOffset]; + obj.hash = hashf(str).toUpperCase(); + obj.raw = sp.slice(0, sp.length - endOffset).join('\n') + '\n'; + const docLF = obj.raw.replace(/\r\n/g, "\n"); + if(docLF.match(/\n$/)){ + this.captures.forEach((cap:any) => { + GenericParser._simpleLineExtraction(obj, docLF, cap); + }); + } + else{ + error = "Bad document structure: no new line character at the end of the document."; + } + } + } + return error; + } +} diff --git a/app/common/lib/parsers/block.js b/app/lib/common-libs/parsers/block.ts similarity index 57% rename from app/common/lib/parsers/block.js rename to app/lib/common-libs/parsers/block.ts index 6bf04985b93155e28d80bfbdd974ab63f444aaaa..8b19ccd195b61cb645142c31ce5248b0a04c9c45 100644 --- a/app/common/lib/parsers/block.js +++ b/app/lib/common-libs/parsers/block.ts @@ -1,48 +1,45 @@ -"use strict"; -const util = require('util'); -const GenericParser = require('./GenericParser'); -const Block = require('../../../../app/common/lib/document/block'); -const hashf = require('../../../../app/lib/common-libs').hashf -const rawer = require('../../../../app/lib/common-libs').rawer -const constants = require('../../../../app/common/lib/constants'); +import {CommonConstants} from "../../../lib/common-libs/constants" +import {GenericParser} from "./GenericParser" +import {hashf} from "../../../lib/common" +import {rawer} from "../../../lib/common-libs/index" -module.exports = BlockParser; +const Block = require('../../../../app/common/lib/document/block'); -function BlockParser (onError) { +export class BlockParser extends GenericParser { - const captures = [ - {prop: "version", regexp: constants.BLOCK.VERSION}, - {prop: "type", regexp: constants.BLOCK.TYPE}, - {prop: "currency", regexp: constants.BLOCK.CURRENCY}, - {prop: "number", regexp: constants.BLOCK.BNUMBER}, - {prop: "powMin", regexp: constants.BLOCK.POWMIN}, - {prop: "time", regexp: constants.BLOCK.TIME}, - {prop: "medianTime", regexp: constants.BLOCK.MEDIAN_TIME}, - {prop: "dividend", regexp: constants.BLOCK.UD}, - {prop: "unitbase", regexp: constants.BLOCK.UNIT_BASE}, - {prop: "issuer", regexp: constants.BLOCK.BLOCK_ISSUER}, - {prop: "issuersFrame", regexp: constants.BLOCK.BLOCK_ISSUERS_FRAME}, - {prop: "issuersFrameVar", regexp: constants.BLOCK.BLOCK_ISSUERS_FRAME_VAR}, - {prop: "issuersCount", regexp: constants.BLOCK.DIFFERENT_ISSUERS_COUNT}, - {prop: "parameters", regexp: constants.BLOCK.PARAMETERS}, - {prop: "previousHash", regexp: constants.BLOCK.PREV_HASH}, - {prop: "previousIssuer", regexp: constants.BLOCK.PREV_ISSUER}, - {prop: "membersCount", regexp: constants.BLOCK.MEMBERS_COUNT}, - {prop: "identities", regexp: /Identities:\n([\s\S]*)Joiners/, parser: splitAndMatch('\n', constants.IDENTITY.INLINE)}, - {prop: "joiners", regexp: /Joiners:\n([\s\S]*)Actives/, parser: splitAndMatch('\n', constants.BLOCK.JOINER)}, - {prop: "actives", regexp: /Actives:\n([\s\S]*)Leavers/, parser: splitAndMatch('\n', constants.BLOCK.ACTIVE)}, - {prop: "leavers", regexp: /Leavers:\n([\s\S]*)Excluded/, parser: splitAndMatch('\n', constants.BLOCK.LEAVER)}, - {prop: "revoked", regexp: /Revoked:\n([\s\S]*)Excluded/, parser: splitAndMatch('\n', constants.BLOCK.REVOCATION)}, - {prop: "excluded", regexp: /Excluded:\n([\s\S]*)Certifications/, parser: splitAndMatch('\n', constants.PUBLIC_KEY)}, - {prop: "certifications", regexp: /Certifications:\n([\s\S]*)Transactions/, parser: splitAndMatch('\n', constants.CERT.OTHER.INLINE)}, - {prop: "transactions", regexp: /Transactions:\n([\s\S]*)/, parser: extractTransactions}, - {prop: "inner_hash", regexp: constants.BLOCK.INNER_HASH}, - {prop: "nonce", regexp: constants.BLOCK.NONCE} - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getBlock, onError); + constructor() { + super([ + {prop: "version", regexp: CommonConstants.BLOCK.VERSION}, + {prop: "type", regexp: CommonConstants.BLOCK.TYPE}, + {prop: "currency", regexp: CommonConstants.BLOCK.CURRENCY}, + {prop: "number", regexp: CommonConstants.BLOCK.BNUMBER}, + {prop: "powMin", regexp: CommonConstants.BLOCK.POWMIN}, + {prop: "time", regexp: CommonConstants.BLOCK.TIME}, + {prop: "medianTime", regexp: CommonConstants.BLOCK.MEDIAN_TIME}, + {prop: "dividend", regexp: CommonConstants.BLOCK.UD}, + {prop: "unitbase", regexp: CommonConstants.BLOCK.UNIT_BASE}, + {prop: "issuer", regexp: CommonConstants.BLOCK.BLOCK_ISSUER}, + {prop: "issuersFrame", regexp: CommonConstants.BLOCK.BLOCK_ISSUERS_FRAME}, + {prop: "issuersFrameVar", regexp: CommonConstants.BLOCK.BLOCK_ISSUERS_FRAME_VAR}, + {prop: "issuersCount", regexp: CommonConstants.BLOCK.DIFFERENT_ISSUERS_COUNT}, + {prop: "parameters", regexp: CommonConstants.BLOCK.PARAMETERS}, + {prop: "previousHash", regexp: CommonConstants.BLOCK.PREV_HASH}, + {prop: "previousIssuer", regexp: CommonConstants.BLOCK.PREV_ISSUER}, + {prop: "membersCount", regexp: CommonConstants.BLOCK.MEMBERS_COUNT}, + {prop: "identities", regexp: /Identities:\n([\s\S]*)Joiners/, parser: splitAndMatch('\n', CommonConstants.IDENTITY.INLINE)}, + {prop: "joiners", regexp: /Joiners:\n([\s\S]*)Actives/, parser: splitAndMatch('\n', CommonConstants.BLOCK.JOINER)}, + {prop: "actives", regexp: /Actives:\n([\s\S]*)Leavers/, parser: splitAndMatch('\n', CommonConstants.BLOCK.ACTIVE)}, + {prop: "leavers", regexp: /Leavers:\n([\s\S]*)Excluded/, parser: splitAndMatch('\n', CommonConstants.BLOCK.LEAVER)}, + {prop: "revoked", regexp: /Revoked:\n([\s\S]*)Excluded/, parser: splitAndMatch('\n', CommonConstants.BLOCK.REVOCATION)}, + {prop: "excluded", regexp: /Excluded:\n([\s\S]*)Certifications/, parser: splitAndMatch('\n', CommonConstants.PUBLIC_KEY)}, + {prop: "certifications", regexp: /Certifications:\n([\s\S]*)Transactions/, parser: splitAndMatch('\n', CommonConstants.CERT.OTHER.INLINE)}, + {prop: "transactions", regexp: /Transactions:\n([\s\S]*)/, parser: extractTransactions}, + {prop: "inner_hash", regexp: CommonConstants.BLOCK.INNER_HASH}, + {prop: "nonce", regexp: CommonConstants.BLOCK.NONCE} + ], rawer.getBlock) + } - this._clean = (obj) => { + _clean(obj:any) { obj.documentType = 'block'; obj.identities = obj.identities || []; obj.joiners = obj.joiners || []; @@ -68,14 +65,14 @@ function BlockParser (onError) { obj.previousHash = obj.previousHash || ''; obj.previousIssuer = obj.previousIssuer || ''; obj.membersCount = obj.membersCount || ''; - obj.transactions.map((tx) => { + obj.transactions.map((tx:any) => { tx.currency = obj.currency; tx.hash = hashf(rawer.getTransaction(tx)).toUpperCase(); }); obj.len = Block.getLen(obj); }; - this._verify = (obj) => { + _verify(obj:any) { let err = null; const codes = { 'BAD_VERSION': 150, @@ -93,11 +90,12 @@ function BlockParser (onError) { 'BAD_MEDIAN_TIME': 162, 'BAD_INNER_HASH': 163, 'BAD_MEMBERS_COUNT': 164, - 'BAD_UNITBASE': 165 + 'BAD_UNITBASE': 165, + 'BAD_ISSUER': 166 }; if(!err){ // Version - if(!obj.version || !obj.version.match(constants.DOCUMENTS_BLOCK_VERSION_REGEXP)) + if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_BLOCK_VERSION_REGEXP)) err = {code: codes.BAD_VERSION, message: "Version unknown"}; } if(!err){ @@ -107,54 +105,54 @@ function BlockParser (onError) { } if(!err){ // Nonce - if(!obj.nonce || !obj.nonce.match(constants.INTEGER)) + if(!obj.nonce || !obj.nonce.match(CommonConstants.INTEGER)) err = {code: codes.BAD_NONCE, message: "Nonce must be an integer value"}; } if(!err){ // Number - if(!obj.number || !obj.number.match(constants.INTEGER)) + if(!obj.number || !obj.number.match(CommonConstants.INTEGER)) err = {code: codes.BAD_NUMBER, message: "Incorrect Number field"}; } if(!err){ // Time - if(!obj.time || !obj.time.match(constants.INTEGER)) + if(!obj.time || !obj.time.match(CommonConstants.INTEGER)) err = {code: codes.BAD_TIME, message: "Time must be an integer"}; } if(!err){ // MedianTime - if(!obj.medianTime || !obj.medianTime.match(constants.INTEGER)) + if(!obj.medianTime || !obj.medianTime.match(CommonConstants.INTEGER)) err = {code: codes.BAD_MEDIAN_TIME, message: "MedianTime must be an integer"}; } if(!err){ - if(obj.dividend && !obj.dividend.match(constants.INTEGER)) + if(obj.dividend && !obj.dividend.match(CommonConstants.INTEGER)) err = {code: codes.BAD_DIVIDEND, message: "Incorrect UniversalDividend field"}; } if(!err){ - if(obj.unitbase && !obj.unitbase.match(constants.INTEGER)) + if(obj.unitbase && !obj.unitbase.match(CommonConstants.INTEGER)) err = {code: codes.BAD_UNITBASE, message: "Incorrect UnitBase field"}; } if(!err){ - if(!obj.issuer || !obj.issuer.match(constants.BASE58)) + if(!obj.issuer || !obj.issuer.match(CommonConstants.BASE58)) err = {code: codes.BAD_ISSUER, message: "Incorrect Issuer field"}; } if(!err){ // MembersCount - if(!obj.nonce || !obj.nonce.match(constants.INTEGER)) + if(!obj.nonce || !obj.nonce.match(CommonConstants.INTEGER)) err = {code: codes.BAD_MEMBERS_COUNT, message: "MembersCount must be an integer value"}; } if(!err){ // InnerHash - if(!obj.inner_hash || !obj.inner_hash.match(constants.FINGERPRINT)) + if(!obj.inner_hash || !obj.inner_hash.match(CommonConstants.FINGERPRINT)) err = {code: codes.BAD_INNER_HASH, message: "InnerHash must be a hash value"}; } return err && err.message; }; } -function splitAndMatch (separator, regexp) { - return function (raw) { +function splitAndMatch (separator:string, regexp:RegExp) { + return function (raw:string) { const lines = raw.split(new RegExp(separator)); - const kept = []; + const kept:string[] = []; lines.forEach(function(line){ if (line.match(regexp)) kept.push(line); @@ -163,23 +161,23 @@ function splitAndMatch (separator, regexp) { }; } -function extractTransactions(raw) { - const regexps = { - "issuers": constants.TRANSACTION.SENDER, - "inputs": constants.TRANSACTION.SOURCE_V3, - "unlocks": constants.TRANSACTION.UNLOCK, - "outputs": constants.TRANSACTION.TARGET, - "comments": constants.TRANSACTION.INLINE_COMMENT, - "signatures": constants.SIG +function extractTransactions(raw:string) { + const regexps:any = { + "issuers": CommonConstants.TRANSACTION.SENDER, + "inputs": CommonConstants.TRANSACTION.SOURCE_V3, + "unlocks": CommonConstants.TRANSACTION.UNLOCK, + "outputs": CommonConstants.TRANSACTION.TARGET, + "comments": CommonConstants.TRANSACTION.INLINE_COMMENT, + "signatures": CommonConstants.SIG }; const transactions = []; const lines = raw.split(/\n/); for (let i = 0; i < lines.length; i++) { const line = lines[i]; // On each header - if (line.match(constants.TRANSACTION.HEADER)) { + if (line.match(CommonConstants.TRANSACTION.HEADER)) { // Parse the transaction - const currentTX = { raw: line + '\n' }; + const currentTX:any = { raw: line + '\n' }; const sp = line.split(':'); const version = parseInt(sp[1]); const nbIssuers = parseInt(sp[2]); @@ -192,7 +190,7 @@ function extractTransactions(raw) { currentTX.blockstamp = lines[i + 1]; currentTX.raw += currentTX.blockstamp + '\n'; currentTX.locktime = parseInt(sp[7]); - const linesToExtract = { + const linesToExtract:any = { issuers: { start: start, end: (start - 1) + nbIssuers @@ -245,5 +243,3 @@ function extractTransactions(raw) { } return transactions; } - -util.inherits(BlockParser, GenericParser); diff --git a/app/lib/common-libs/parsers/certification.ts b/app/lib/common-libs/parsers/certification.ts new file mode 100644 index 0000000000000000000000000000000000000000..f73753942a96035f91dbf44ac1292af78dcfc80b --- /dev/null +++ b/app/lib/common-libs/parsers/certification.ts @@ -0,0 +1,39 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" +import {GenericParser} from "./GenericParser" +import {rawer} from "../../../lib/common-libs/index" + +export class CertificationParser extends GenericParser { + + constructor() { + super([ + {prop: "version", regexp: CommonConstants.DOCUMENTS.DOC_VERSION }, + {prop: "type", regexp: CommonConstants.CERTIFICATION.CERT_TYPE }, + {prop: "currency", regexp: CommonConstants.DOCUMENTS.DOC_CURRENCY }, + {prop: "issuer", regexp: CommonConstants.DOCUMENTS.DOC_ISSUER }, + {prop: "idty_issuer", regexp: CommonConstants.CERTIFICATION.IDTY_ISSUER }, + {prop: "idty_sig", regexp: CommonConstants.CERTIFICATION.IDTY_SIG }, + {prop: "idty_buid", regexp: CommonConstants.CERTIFICATION.IDTY_TIMESTAMP}, + {prop: "idty_uid", regexp: CommonConstants.CERTIFICATION.IDTY_UID }, + {prop: "buid", regexp: CommonConstants.CERTIFICATION.CERT_TIMESTAMP } + ], rawer.getOfficialCertification) + } + + _clean(obj:any) { + obj.documentType = 'certification'; + obj.sig = obj.signature; + obj.block = obj.buid; + if (obj.block) { + obj.number = obj.block.split('-')[0]; + obj.fpr = obj.block.split('-')[1]; + } else { + obj.number = '0'; + obj.fpr = ''; + } + } + + _verify(obj:any) { + return ["version", "type", "currency", "issuer", "idty_issuer", "idty_sig", "idty_buid", "idty_uid", "block"].reduce(function (p, field) { + return p || (!obj[field] && "Wrong format for certification"); + }, null) + } +} \ No newline at end of file diff --git a/app/lib/common-libs/parsers/identity.ts b/app/lib/common-libs/parsers/identity.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc63231ee77c1adb20c17f446ad2638d0500e2a7 --- /dev/null +++ b/app/lib/common-libs/parsers/identity.ts @@ -0,0 +1,42 @@ +import {GenericParser} from "./GenericParser" +import {CommonConstants} from "../../../lib/common-libs/constants" +import {hashf} from "../../../lib/common" +import {rawer} from "../../../lib/common-libs/index" + +export class IdentityParser extends GenericParser { + + constructor() { + super([ + {prop: "version", regexp: CommonConstants.DOCUMENTS.DOC_VERSION }, + {prop: "type", regexp: CommonConstants.IDENTITY.IDTY_TYPE}, + {prop: "currency", regexp: CommonConstants.DOCUMENTS.DOC_CURRENCY }, + {prop: "pubkey", regexp: CommonConstants.DOCUMENTS.DOC_ISSUER }, + {prop: "uid", regexp: CommonConstants.IDENTITY.IDTY_UID }, + {prop: "buid", regexp: CommonConstants.DOCUMENTS.TIMESTAMP } + ], rawer.getOfficialIdentity) + } + + _clean(obj:any) { + obj.documentType = 'identity'; + obj.sig = obj.signature; + if (obj.uid && obj.buid && obj.pubkey) { + obj.hash = hashf(obj.uid + obj.buid + obj.pubkey).toUpperCase(); + } + } + + _verify(obj:any) { + if (!obj.pubkey) { + return "No pubkey found"; + } + if (!obj.uid) { + return "Wrong user id format"; + } + if (!obj.buid) { + return "Could not extract block uid"; + } + if (!obj.sig) { + return "No signature found for self-certification"; + } + return "" + } +} diff --git a/app/lib/common-libs/parsers/index.ts b/app/lib/common-libs/parsers/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..fdf5e42c17f3597a9508f658461a98a40ada1446 --- /dev/null +++ b/app/lib/common-libs/parsers/index.ts @@ -0,0 +1,17 @@ +import {BlockParser} from "./block" +import {CertificationParser} from "./certification" +import {IdentityParser} from "./identity" +import {MembershipParser} from "./membership" +import {PeerParser} from "./peer" +import {RevocationParser} from "./revocation" +import {TransactionParser} from "./transaction" + +export const parsers = { + parseIdentity: new IdentityParser(), + parseCertification: new CertificationParser(), + parseRevocation: new RevocationParser(), + parseTransaction: new TransactionParser(), + parsePeer: new PeerParser(), + parseMembership: new MembershipParser(), + parseBlock: new BlockParser() +} diff --git a/app/lib/common-libs/parsers/membership.ts b/app/lib/common-libs/parsers/membership.ts new file mode 100644 index 0000000000000000000000000000000000000000..97938346a1e068387593b7c996de767d00d75920 --- /dev/null +++ b/app/lib/common-libs/parsers/membership.ts @@ -0,0 +1,69 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" +import {GenericParser} from "./GenericParser" +import {rawer} from "../../../lib/common-libs/index" +import {Buid} from "../../../lib/common-libs/buid" + +export class MembershipParser extends GenericParser { + + constructor() { + super([ + {prop: "version", regexp: CommonConstants.MEMBERSHIP.VERSION }, + {prop: "currency", regexp: CommonConstants.MEMBERSHIP.CURRENCY }, + {prop: "issuer", regexp: CommonConstants.MEMBERSHIP.ISSUER }, + {prop: "membership", regexp: CommonConstants.MEMBERSHIP.MEMBERSHIP }, + {prop: "userid", regexp: CommonConstants.MEMBERSHIP.USERID }, + {prop: "block", regexp: CommonConstants.MEMBERSHIP.BLOCK}, + {prop: "certts", regexp: CommonConstants.MEMBERSHIP.CERTTS} + ], rawer.getMembership) + } + + _clean(obj:any) { + obj.documentType = 'membership'; + if (obj.block) { + obj.number = obj.block.split('-')[0]; + obj.fpr = obj.block.split('-')[1]; + } else { + obj.number = '0'; + obj.fpr = ''; + } + } + + _verify(obj:any) { + let err = null; + const codes = { + 'BAD_VERSION': 150, + 'BAD_CURRENCY': 151, + 'BAD_ISSUER': 152, + 'BAD_MEMBERSHIP': 153, + 'BAD_REGISTRY_TYPE': 154, + 'BAD_BLOCK': 155, + 'BAD_USERID': 156, + 'BAD_CERTTS': 157 + }; + if(!err){ + if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_VERSION_REGEXP)) + err = {code: codes.BAD_VERSION, message: "Version unknown"}; + } + if(!err){ + if(obj.issuer && !obj.issuer.match(CommonConstants.BASE58)) + err = {code: codes.BAD_ISSUER, message: "Incorrect issuer field"}; + } + if(!err){ + if(!(obj.membership || "").match(/^(IN|OUT)$/)) + err = {code: codes.BAD_MEMBERSHIP, message: "Incorrect Membership field: must be either IN or OUT"}; + } + if(!err){ + if(obj.block && !obj.block.match(CommonConstants.BLOCK_UID)) + err = {code: codes.BAD_BLOCK, message: "Incorrect Block field: must be a positive or zero integer, a dash and an uppercased SHA1 hash"}; + } + if(!err){ + if(obj.userid && !obj.userid.match(CommonConstants.USER_ID)) + err = {code: codes.BAD_USERID, message: "UserID must match udid2 format"}; + } + if(!err){ + if(!Buid.format.isBuid(obj.certts)) + err = {code: codes.BAD_CERTTS, message: "CertTS must be a valid timestamp"}; + } + return err && err.message; + }; +} \ No newline at end of file diff --git a/app/common/lib/parsers/peer.js b/app/lib/common-libs/parsers/peer.ts similarity index 73% rename from app/common/lib/parsers/peer.js rename to app/lib/common-libs/parsers/peer.ts index 9d106084dc9d0e3eae9d266647add3802a19fbe1..24715a21902754373db206bd1faa3958df1197de 100644 --- a/app/common/lib/parsers/peer.js +++ b/app/lib/common-libs/parsers/peer.ts @@ -1,35 +1,31 @@ -"use strict"; -const GenericParser = require('./GenericParser'); -const rawer = require('../../../lib/common-libs/index').rawer -const util = require('util'); -const constants = require('../constants'); +import {GenericParser} from "./GenericParser" +import {CommonConstants} from "../../../lib/common-libs/constants" +import {rawer} from "../../../lib/common-libs/index" -module.exports = PeerParser; +export class PeerParser extends GenericParser { -function PeerParser (onError) { + constructor() { + super([ + {prop: "version", regexp: /Version: (.*)/}, + {prop: "currency", regexp: /Currency: (.*)/}, + {prop: "pubkey", regexp: /PublicKey: (.*)/}, + {prop: "block", regexp: CommonConstants.PEER.BLOCK}, + { + prop: "endpoints", regexp: /Endpoints:\n([\s\S]*)/, parser: (str:string) => str.split("\n") + } + ], rawer.getPeer) + } - const captures = [ - {prop: "version", regexp: /Version: (.*)/}, - {prop: "currency", regexp: /Currency: (.*)/}, - {prop: "pubkey", regexp: /PublicKey: (.*)/}, - {prop: "block", regexp: constants.PEER.BLOCK}, - { - prop: "endpoints", regexp: /Endpoints:\n([\s\S]*)/, parser: (str) => str.split("\n") - } - ]; - const multilineFields = []; - GenericParser.call(this, captures, multilineFields, rawer.getPeer, onError); - - this._clean = (obj) => { + _clean(obj:any) { obj.documentType = 'peer'; obj.endpoints = obj.endpoints || []; // Removes trailing space if (obj.endpoints.length > 0) obj.endpoints.splice(obj.endpoints.length - 1, 1); obj.getBMA = function() { - let bma = null; - obj.endpoints.forEach((ep) => { - let matches = !bma && ep.match(constants.BMA_REGEXP); + let bma:any = null; + obj.endpoints.forEach((ep:string) => { + let matches = !bma && ep.match(CommonConstants.BMA_REGEXP); if (matches) { bma = { "dns": matches[2] || '', @@ -41,9 +37,9 @@ function PeerParser (onError) { }); return bma || {}; }; - }; + } - this._verify = (obj) => { + _verify(obj:any) { let err = null; const codes = { 'BAD_VERSION': 150, @@ -58,12 +54,12 @@ function PeerParser (onError) { }; if(!err){ // Version - if(!obj.version || !obj.version.match(constants.DOCUMENTS_VERSION_REGEXP)) + if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_VERSION_REGEXP)) err = {code: codes.BAD_VERSION, message: "Version unknown"}; } if(!err){ // PublicKey - if(!obj.pubkey || !obj.pubkey.match(constants.BASE58)) + if(!obj.pubkey || !obj.pubkey.match(CommonConstants.BASE58)) err = {code: codes.BAD_FINGERPRINT, message: "Incorrect PublicKey field"}; } if(!err){ @@ -100,6 +96,4 @@ function PeerParser (onError) { } return err && err.message; }; -} - -util.inherits(PeerParser, GenericParser); +} \ No newline at end of file diff --git a/app/lib/common-libs/parsers/revocation.ts b/app/lib/common-libs/parsers/revocation.ts new file mode 100644 index 0000000000000000000000000000000000000000..777c878bc937d415393e4f0151308dbfdf2431a6 --- /dev/null +++ b/app/lib/common-libs/parsers/revocation.ts @@ -0,0 +1,47 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" +import {GenericParser} from "./GenericParser" +import {hashf} from "../../../lib/common" +import {rawer} from "../../../lib/common-libs/index" + +export class RevocationParser extends GenericParser { + + constructor() { + super([ + {prop: "version", regexp: CommonConstants.DOCUMENTS.DOC_VERSION }, + {prop: "type", regexp: CommonConstants.REVOCATION.REVOC_TYPE }, + {prop: "currency", regexp: CommonConstants.DOCUMENTS.DOC_CURRENCY }, + {prop: "issuer", regexp: CommonConstants.DOCUMENTS.DOC_ISSUER }, + {prop: "sig", regexp: CommonConstants.REVOCATION.IDTY_SIG }, + {prop: "buid", regexp: CommonConstants.REVOCATION.IDTY_TIMESTAMP}, + {prop: "uid", regexp: CommonConstants.REVOCATION.IDTY_UID } + ], rawer.getOfficialRevocation) + } + + _clean(obj:any) { + obj.documentType = 'revocation'; + obj.pubkey = obj.issuer; + obj.revocation = obj.signature; + if (obj.uid && obj.buid && obj.pubkey) { + obj.hash = hashf(obj.uid + obj.buid + obj.pubkey).toUpperCase(); + } + } + + _verify(obj:any) { + if (!obj.pubkey) { + return "No pubkey found"; + } + if (!obj.uid) { + return "Wrong user id format"; + } + if (!obj.buid) { + return "Could not extract block uid"; + } + if (!obj.sig) { + return "No signature found for identity"; + } + if (!obj.revocation) { + return "No revocation signature found"; + } + return "" + } +} \ No newline at end of file diff --git a/app/lib/common-libs/parsers/transaction.ts b/app/lib/common-libs/parsers/transaction.ts new file mode 100644 index 0000000000000000000000000000000000000000..d94b58c0ef11ec60c05b487c737ced7e77f1f969 --- /dev/null +++ b/app/lib/common-libs/parsers/transaction.ts @@ -0,0 +1,122 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" +import {GenericParser} from "./GenericParser" +import {rawer} from "../../../lib/common-libs/index" + +export class TransactionParser extends GenericParser { + + constructor() { + super([ + {prop: "version", regexp: /Version: (.*)/}, + {prop: "currency", regexp: /Currency: (.*)/}, + {prop: "issuers", regexp: /Issuers:\n([\s\S]*)Inputs/, parser: extractIssuers }, + {prop: "inputs", regexp: /Inputs:\n([\s\S]*)Unlocks/, parser: extractInputs }, + {prop: "unlocks", regexp: /Unlocks:\n([\s\S]*)Outputs/,parser: extractUnlocks }, + {prop: "outputs", regexp: /Outputs:\n([\s\S]*)/, parser: extractOutputs }, + {prop: "comment", regexp: CommonConstants.TRANSACTION.COMMENT }, + {prop: "locktime", regexp: CommonConstants.TRANSACTION.LOCKTIME }, + {prop: "blockstamp", regexp: CommonConstants.TRANSACTION.BLOCKSTAMP }, + {prop: "signatures", regexp: /Outputs:\n([\s\S]*)/, parser: extractSignatures } + ], rawer.getTransaction) + } + + _clean(obj:any) { + obj.documentType = 'transaction'; + obj.comment = obj.comment || ""; + obj.locktime = parseInt(obj.locktime) || 0; + obj.signatures.push(obj.signature); + const compactSize = 2 // Header + blockstamp + + obj.issuers.length + + obj.inputs.length + + obj.unlocks.length + + obj.outputs.length + + (obj.comment ? 1 : 0) + + obj.signatures; + if (compactSize > 100) { + throw 'A transaction has a maximum size of 100 lines'; + } + } + + _verify(obj:any) { + let err = null; + const codes = { + 'BAD_VERSION': 150, + 'NO_BLOCKSTAMP': 151 + }; + if(!err){ + // Version + if(!obj.version || !obj.version.match(CommonConstants.DOCUMENTS_TRANSACTION_VERSION_REGEXP)) + err = {code: codes.BAD_VERSION, message: "Version unknown"}; + // Blockstamp + if(!obj.blockstamp || !obj.blockstamp.match(CommonConstants.BLOCKSTAMP_REGEXP)) + err = {code: codes.BAD_VERSION, message: "Blockstamp is required"}; + } + return err && err.message; + } +} + +function extractIssuers(raw:string) { + const issuers = []; + const lines = raw.split(/\n/); + for (const line of lines) { + if (line.match(CommonConstants.TRANSACTION.SENDER)) { + issuers.push(line); + } else { + // Not a pubkey, stop reading + break; + } + } + return issuers; +} + +function extractInputs(raw:string) { + const inputs = []; + const lines = raw.split(/\n/); + for (const line of lines) { + if (line.match(CommonConstants.TRANSACTION.SOURCE_V3)) { + inputs.push(line); + } else { + // Not a transaction input, stop reading + break; + } + } + return inputs; +} + +function extractUnlocks(raw:string) { + const unlocks = []; + const lines = raw.split(/\n/); + for (const line of lines) { + if (line.match(CommonConstants.TRANSACTION.UNLOCK)) { + unlocks.push(line); + } else { + // Not a transaction unlock, stop reading + break; + } + } + return unlocks; +} + +function extractOutputs(raw:string) { + const outputs = []; + const lines = raw.split(/\n/); + for (const line of lines) { + if (line.match(CommonConstants.TRANSACTION.TARGET)) { + outputs.push(line); + } else { + // Not a transaction input, stop reading + break; + } + } + return outputs; +} + +function extractSignatures(raw:string) { + const signatures = []; + const lines = raw.split(/\n/); + for (const line of lines) { + if (line.match(CommonConstants.SIG)) { + signatures.push(line); + } + } + return signatures; +} \ No newline at end of file diff --git a/app/lib/common-libs/txunlock.ts b/app/lib/common-libs/txunlock.ts index a136cfd30406fe096c6a662b18f84b8ee086edc5..fd156e2b9c47a3c5595ff608856103e0cad3e13a 100644 --- a/app/lib/common-libs/txunlock.ts +++ b/app/lib/common-libs/txunlock.ts @@ -1,7 +1,8 @@ "use strict"; +import {hashf} from "../common" let Parser = require("jison").Parser; -let buid = require('../../../app/common/lib/buid') +let buid = require('../../../app/lib/common-libs/buid').Buid let grammar = { "lex": { @@ -55,7 +56,7 @@ export function unlock(conditionsStr:string, executions:any, metadata:any) { }, xHx: function(hash:string) { let xhxParam = executions[this.i++]; - return buid.format.hashf(xhxParam) === hash; + return hashf(xhxParam) === hash; }, cltv: function(deadline:string) { return metadata.currentTime && metadata.currentTime >= parseInt(deadline); diff --git a/app/lib/constants.ts b/app/lib/constants.ts index a410214f93b46061d8de4494d023f4ea2cde0c66..378532b7ef56adbe3ea48018839766a8bfcb7fd9 100644 --- a/app/lib/constants.ts +++ b/app/lib/constants.ts @@ -1,10 +1,11 @@ "use strict"; +import {CommonConstants} from "./common-libs/constants" const common = require('../../app/common') const UDID2 = "udid2;c;([A-Z-]*);([A-Z-]*);(\\d{4}-\\d{2}-\\d{2});(e\\+\\d{2}\\.\\d{2}(\\+|-)\\d{3}\\.\\d{2});(\\d+)(;?)"; -const PUBKEY = common.constants.FORMATS.PUBKEY -const TIMESTAMP = common.constants.FORMATS.TIMESTAMP +const PUBKEY = CommonConstants.FORMATS.PUBKEY +const TIMESTAMP = CommonConstants.FORMATS.TIMESTAMP const IPV4_REGEXP = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/; const IPV6_REGEXP = /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(([0-9A-Fa-f]{1,4}:){0,5}:((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|(::([0-9A-Fa-f]{1,4}:){0,5}((b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b).){3}(b((25[0-5])|(1d{2})|(2[0-4]d)|(d{1,2}))b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/; @@ -31,7 +32,7 @@ module.exports = { UNHANDLED: { httpCode: 500, uerr: { ucode: 1002, message: "An unhandled error occured" }}, SIGNATURE_DOES_NOT_MATCH: { httpCode: 400, uerr: { ucode: 1003, message: "Signature does not match" }}, ALREADY_UP_TO_DATE: { httpCode: 400, uerr: { ucode: 1004, message: "Already up-to-date" }}, - WRONG_DOCUMENT: common.constants.ERRORS.WRONG_DOCUMENT, + WRONG_DOCUMENT: CommonConstants.ERRORS.WRONG_DOCUMENT, SANDBOX_FOR_IDENTITY_IS_FULL: { httpCode: 503, uerr: { ucode: 1007, message: "The identities' sandbox is full. Please retry with another document or retry later." }}, SANDBOX_FOR_CERT_IS_FULL: { httpCode: 503, uerr: { ucode: 1008, message: "The certifications' sandbox is full. Please retry with another document or retry later." }}, SANDBOX_FOR_MEMERSHIP_IS_FULL: { httpCode: 503, uerr: { ucode: 1009, message: "The memberships' sandbox is full. Please retry with another document or retry later." }}, @@ -51,34 +52,34 @@ module.exports = { MEMBERSHIP_A_NON_MEMBER_CANNOT_LEAVE: { httpCode: 400, uerr: { ucode: 2008, message: "A non-member cannot leave" }}, NOT_A_MEMBER: { httpCode: 400, uerr: { ucode: 2009, message: "Not a member" }}, BLOCK_NOT_FOUND: { httpCode: 404, uerr: { ucode: 2011, message: "Block not found" }}, - WRONG_UNLOCKER: common.constants.ERRORS.WRONG_UNLOCKER, - LOCKTIME_PREVENT: common.constants.ERRORS.LOCKTIME_PREVENT, - SOURCE_ALREADY_CONSUMED: common.constants.ERRORS.SOURCE_ALREADY_CONSUMED, - WRONG_AMOUNTS: common.constants.ERRORS.WRONG_AMOUNTS, - WRONG_OUTPUT_BASE: common.constants.ERRORS.WRONG_OUTPUT_BASE, - CANNOT_ROOT_BLOCK_NO_MEMBERS: common.constants.ERRORS.CANNOT_ROOT_BLOCK_NO_MEMBERS, - IDENTITY_WRONGLY_SIGNED: common.constants.ERRORS.IDENTITY_WRONGLY_SIGNED, - TOO_OLD_IDENTITY: common.constants.ERRORS.TOO_OLD_IDENTITY, + WRONG_UNLOCKER: CommonConstants.ERRORS.WRONG_UNLOCKER, + LOCKTIME_PREVENT: CommonConstants.ERRORS.LOCKTIME_PREVENT, + SOURCE_ALREADY_CONSUMED: CommonConstants.ERRORS.SOURCE_ALREADY_CONSUMED, + WRONG_AMOUNTS: CommonConstants.ERRORS.WRONG_AMOUNTS, + WRONG_OUTPUT_BASE: CommonConstants.ERRORS.WRONG_OUTPUT_BASE, + CANNOT_ROOT_BLOCK_NO_MEMBERS: CommonConstants.ERRORS.CANNOT_ROOT_BLOCK_NO_MEMBERS, + IDENTITY_WRONGLY_SIGNED: CommonConstants.ERRORS.IDENTITY_WRONGLY_SIGNED, + TOO_OLD_IDENTITY: CommonConstants.ERRORS.TOO_OLD_IDENTITY, NO_IDTY_MATCHING_PUB_OR_UID: { httpCode: 404, uerr: { ucode: 2021, message: "No identity matching this pubkey or uid" }}, NEWER_PEER_DOCUMENT_AVAILABLE: { httpCode: 409, uerr: { ucode: 2022, message: "A newer peer document is available" }}, PEER_DOCUMENT_ALREADY_KNOWN: { httpCode: 400, uerr: { ucode: 2023, message: "Peer document already known" }}, - TX_INPUTS_OUTPUTS_NOT_EQUAL: common.constants.ERRORS.TX_INPUTS_OUTPUTS_NOT_EQUAL, - TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS: common.constants.ERRORS.TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS, - BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK: common.constants.ERRORS.BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK, - A_TRANSACTION_HAS_A_MAX_SIZE: common.constants.ERRORS.A_TRANSACTION_HAS_A_MAX_SIZE, + TX_INPUTS_OUTPUTS_NOT_EQUAL: CommonConstants.ERRORS.TX_INPUTS_OUTPUTS_NOT_EQUAL, + TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS: CommonConstants.ERRORS.TX_OUTPUT_SUM_NOT_EQUALS_PREV_DELTAS, + BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK: CommonConstants.ERRORS.BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK, + A_TRANSACTION_HAS_A_MAX_SIZE: CommonConstants.ERRORS.A_TRANSACTION_HAS_A_MAX_SIZE, BLOCK_ALREADY_PROCESSED: { httpCode: 400, uerr: { ucode: 2028, message: 'Already processed' }}, - TOO_OLD_MEMBERSHIP: common.constants.ERRORS.TOO_OLD_MEMBERSHIP, + TOO_OLD_MEMBERSHIP: CommonConstants.ERRORS.TOO_OLD_MEMBERSHIP, TX_ALREADY_PROCESSED: { httpCode: 400, uerr: { ucode: 2030, message: "Transaction already processed" }}, A_MORE_RECENT_MEMBERSHIP_EXISTS: { httpCode: 400, uerr: { ucode: 2031, message: "A more recent membership already exists" }}, - MAXIMUM_LEN_OF_OUTPUT: common.constants.ERRORS.MAXIMUM_LEN_OF_OUTPUT, - MAXIMUM_LEN_OF_UNLOCK: common.constants.ERRORS.MAXIMUM_LEN_OF_UNLOCK + MAXIMUM_LEN_OF_OUTPUT: CommonConstants.ERRORS.MAXIMUM_LEN_OF_OUTPUT, + MAXIMUM_LEN_OF_UNLOCK: CommonConstants.ERRORS.MAXIMUM_LEN_OF_UNLOCK }, DEBUG: { LONG_DAL_PROCESS: 50 }, - BMA_REGEXP: common.constants.BMA_REGEXP, + BMA_REGEXP: CommonConstants.BMA_REGEXP, IPV4_REGEXP: IPV4_REGEXP, IPV6_REGEXP: IPV6_REGEXP, @@ -86,15 +87,15 @@ module.exports = { UDID2_FORMAT: exact(UDID2), PUBLIC_KEY: exact(PUBKEY), - DOCUMENTS_VERSION: common.constants.DOCUMENTS_VERSION, - BLOCK_GENERATED_VERSION: common.constants.BLOCK_GENERATED_VERSION, + DOCUMENTS_VERSION: CommonConstants.DOCUMENTS_VERSION, + BLOCK_GENERATED_VERSION: CommonConstants.BLOCK_GENERATED_VERSION, LAST_VERSION_FOR_TX: 10, - TRANSACTION_VERSION: common.constants.TRANSACTION_VERSION, + TRANSACTION_VERSION: CommonConstants.TRANSACTION_VERSION, - REVOCATION_FACTOR: common.constants.REVOCATION_FACTOR, // This is protocol fixed value + REVOCATION_FACTOR: CommonConstants.REVOCATION_FACTOR, // This is protocol fixed value FIRST_UNIT_BASE: 0, - PEER: common.constants.PEER, + PEER: CommonConstants.PEER, NETWORK: { MAX_MEMBERS_TO_FORWARD_TO_FOR_SELF_DOCUMENTS: 10, MAX_NON_MEMBERS_TO_FORWARD_TO_FOR_SELF_DOCUMENTS: 6, @@ -112,7 +113,7 @@ module.exports = { }, PROOF_OF_WORK: { EVALUATION: 1000, - UPPER_BOUND: common.constants.PROOF_OF_WORK.UPPER_BOUND.slice() + UPPER_BOUND: CommonConstants.PROOF_OF_WORK.UPPER_BOUND.slice() }, DEFAULT_CURRENCY_NAME: "no_currency", diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index f02c58a8d565ac1d5f6ba8e3ec4b0d381cbb77ed..6eaa6ba44ebeaaeb4c55cd71775f0c92dbb8574f 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -14,6 +14,7 @@ import {DBTx} from "./sqliteDAL/TxsDAL" import {DBBlock} from "../db/DBBlock" import {DBMembership} from "./sqliteDAL/MembershipDAL" import {MerkleDTO} from "../dto/MerkleDTO" +import {CommonConstants} from "../common-libs/constants" const fs = require('fs') const path = require('path') @@ -649,7 +650,7 @@ export class FileDAL { for (const entry of cindex) { const from = await this.getWrittenIdtyByPubkey(entry.issuer); const to = await this.getWrittenIdtyByPubkey(entry.receiver); - if (entry.op == common.constants.IDX_CREATE) { + if (entry.op == CommonConstants.IDX_CREATE) { this.wotb.addLink(from.wotb_id, to.wotb_id); } else { // Update = removal @@ -671,7 +672,7 @@ export class FileDAL { await this.certDAL.trimExpiredCerts(block.medianTime); await this.msDAL.trimExpiredMemberships(block.medianTime); await this.idtyDAL.trimExpiredIdentities(block.medianTime); - await this.txsDAL.trimExpiredNonWrittenTxs(block.medianTime - common.constants.TX_WINDOW) + await this.txsDAL.trimExpiredNonWrittenTxs(block.medianTime - CommonConstants.TX_WINDOW) return true; } diff --git a/app/lib/dal/sqliteDAL/MetaDAL.ts b/app/lib/dal/sqliteDAL/MetaDAL.ts index 073ef573076efc716dea3d62f196e3e3b8f1a244..5a6d21e6da85b3570a3510614530f8d8de628967 100644 --- a/app/lib/dal/sqliteDAL/MetaDAL.ts +++ b/app/lib/dal/sqliteDAL/MetaDAL.ts @@ -11,7 +11,8 @@ import {WalletDAL} from "./WalletDAL" import {MIndexDAL} from "./index/MIndexDAL" import {DBBlock} from "../../db/DBBlock" import {IdentityDTO} from "../../dto/IdentityDTO" -import {rawer} from "../../common-libs/index"; +import {rawer} from "../../common-libs/index" +import {CommonConstants} from "../../common-libs/constants" const _ = require('underscore') const logger = require('../../logger').NewLogger('metaDAL'); @@ -255,8 +256,8 @@ export class MetaDAL extends AbstractSQLite<DBMeta> { amountMissing += src.amount; const block = src.block; sourcesMovements.push({ - index: common.constants.I_INDEX, - op: common.constants.IDX_CREATE, + index: CommonConstants.I_INDEX, + op: CommonConstants.IDX_CREATE, tx: src.tx, identifier: src.identifier, pos: src.pos, @@ -321,7 +322,7 @@ export class MetaDAL extends AbstractSQLite<DBMeta> { let blockDAL = new BlockDAL(this.driverCopy) let mindexDAL = new MIndexDAL(this.driverCopy) await mindexDAL.exec('ALTER TABLE m_index ADD COLUMN chainable_on INTEGER NULL;') - const memberships = await mindexDAL.query('SELECT * FROM m_index WHERE op = ?', [common.constants.IDX_CREATE]) + const memberships = await mindexDAL.query('SELECT * FROM m_index WHERE op = ?', [CommonConstants.IDX_CREATE]) for (const ms of memberships) { const reference = await blockDAL.getBlock(parseInt(ms.written_on.split('-')[0])) const updateQuery = 'UPDATE m_index SET chainable_on = ' + (reference.medianTime + conf.msPeriod) + ' WHERE pub = \'' + ms.pub + '\' AND op = \'CREATE\'' diff --git a/app/lib/dal/sqliteDAL/index/CIndexDAL.ts b/app/lib/dal/sqliteDAL/index/CIndexDAL.ts index 8cea743347e1da356060a161a2404285414f496a..11afb7ca60916bc422f496f42cd4b40b07b469f0 100644 --- a/app/lib/dal/sqliteDAL/index/CIndexDAL.ts +++ b/app/lib/dal/sqliteDAL/index/CIndexDAL.ts @@ -1,6 +1,7 @@ -import {AbstractIndex} from "../AbstractIndex"; -import {SQLiteDriver} from "../../drivers/SQLiteDriver"; -import {CindexEntry} from "../../../indexer"; +import {AbstractIndex} from "../AbstractIndex" +import {SQLiteDriver} from "../../drivers/SQLiteDriver" +import {CindexEntry} from "../../../indexer" +import {CommonConstants} from "../../../common-libs/constants" const constants = require('./../../../constants'); const common = require('../../../../../app/common'); @@ -89,7 +90,7 @@ export class CIndexDAL extends AbstractIndex<CindexEntry> { ' AND c1.receiver = c2.receiver' + ' AND c1.created_on = c2.created_on' + ' AND c2.op = ?' + - ')', [medianTime, common.constants.IDX_UPDATE]) + ')', [medianTime, CommonConstants.IDX_UPDATE]) } getValidLinksTo(receiver:string) { @@ -102,7 +103,7 @@ export class CIndexDAL extends AbstractIndex<CindexEntry> { ' AND c1.receiver = c2.receiver' + ' AND c1.created_on = c2.created_on' + ' AND c2.op = ?' + - ')', [receiver, common.constants.IDX_UPDATE]) + ')', [receiver, CommonConstants.IDX_UPDATE]) } getValidLinksFrom(issuer:string) { @@ -115,7 +116,7 @@ export class CIndexDAL extends AbstractIndex<CindexEntry> { ' AND c1.receiver = c2.receiver' + ' AND c1.created_on = c2.created_on' + ' AND c2.op = ?' + - ')', [issuer, common.constants.IDX_UPDATE]) + ')', [issuer, CommonConstants.IDX_UPDATE]) } async existsNonReplayableLink(issuer:string, receiver:string) { @@ -128,7 +129,7 @@ export class CIndexDAL extends AbstractIndex<CindexEntry> { ' AND c1.receiver = c2.receiver' + ' AND c1.created_on = c2.created_on' + ' AND c2.op = ?' + - ')', [issuer, receiver, common.constants.IDX_UPDATE]); + ')', [issuer, receiver, CommonConstants.IDX_UPDATE]); return results.length > 0; } diff --git a/app/lib/dal/sqliteDAL/index/SIndexDAL.ts b/app/lib/dal/sqliteDAL/index/SIndexDAL.ts index b1d97503b7246fbc607df948c0f5023c6d1682c7..7e133389cd6b54aafebafa3b7b6159c028f73969 100644 --- a/app/lib/dal/sqliteDAL/index/SIndexDAL.ts +++ b/app/lib/dal/sqliteDAL/index/SIndexDAL.ts @@ -1,6 +1,7 @@ -import {Indexer, SindexEntry} from "../../../indexer"; -import {SQLiteDriver} from "../../drivers/SQLiteDriver"; -import {AbstractIndex} from "../AbstractIndex"; +import {Indexer, SindexEntry} from "../../../indexer" +import {SQLiteDriver} from "../../drivers/SQLiteDriver" +import {AbstractIndex} from "../AbstractIndex" +import {CommonConstants} from "../../../common-libs/constants" const _ = require('underscore'); const common = require('../../../../../app/common'); const constants = require('../../../constants'); @@ -107,7 +108,7 @@ export class SIndexDAL extends AbstractIndex<SindexEntry> { ' AND s2.pos = s1.pos ' + ' AND s2.op = ?' + ') ' + - 'ORDER BY CAST(SUBSTR(written_on, 0, INSTR(written_on, "-")) as number)', [common.constants.IDX_CREATE, conditionsStr, common.constants.IDX_UPDATE]); + 'ORDER BY CAST(SUBSTR(written_on, 0, INSTR(written_on, "-")) as number)', [CommonConstants.IDX_CREATE, conditionsStr, CommonConstants.IDX_UPDATE]); const sources = potentials.map((src) => { src.type = src.tx ? 'T' : 'D'; return src; diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts index 7ef5df43f63e3a19fcce05ae8bb4a3569e530118..26d9ab2199c81c2bd40fd6ab0110e653ab42d48c 100644 --- a/app/lib/indexer.ts +++ b/app/lib/indexer.ts @@ -7,13 +7,14 @@ import {CertificationDTO} from "./dto/CertificationDTO" import {TransactionDTO} from "./dto/TransactionDTO" import {DBHead} from "./db/DBHead" import {LOCAL_RULES_HELPERS} from "./rules/local_rules" -import {verify} from "./common-libs/crypto/keyring"; -import {rawer, txunlock} from "./common-libs/index"; +import {verify} from "./common-libs/crypto/keyring" +import {rawer, txunlock} from "./common-libs/index" +import {CommonConstants} from "./common-libs/constants" const _ = require('underscore'); const common = require('../../app/common'); -const constants = common.constants +const constants = CommonConstants const Block = common.document.Block const Membership = common.document.Membership diff --git a/app/lib/rules/global_rules.ts b/app/lib/rules/global_rules.ts index 077323c777ee4a3397b193e998aff455def12c7e..e1061a2976d314c6e0a15b317e13afa70598c184 100644 --- a/app/lib/rules/global_rules.ts +++ b/app/lib/rules/global_rules.ts @@ -6,13 +6,14 @@ import {TransactionDTO} from "../dto/TransactionDTO" import * as local_rules from "./local_rules" import {BlockDTO} from "../dto/BlockDTO" import {verify} from "../common-libs/crypto/keyring" -import {rawer, txunlock} from "../common-libs/index"; +import {rawer, txunlock} from "../common-libs/index" +import {CommonConstants} from "../common-libs/constants" const _ = require('underscore'); const common = require('../../../app/common'); const indexer = require('../indexer').Indexer -const constants = common.constants +const constants = CommonConstants const Identity = common.document.Identity const Transaction = common.document.Transaction diff --git a/app/lib/rules/helpers.ts b/app/lib/rules/helpers.ts index 7203dc22b01b5eb132d1bb205f1c75c1e402c8de..26894dd9e519d08a6c6a9ab4008f95ea19a0d366 100644 --- a/app/lib/rules/helpers.ts +++ b/app/lib/rules/helpers.ts @@ -1,7 +1,7 @@ import {ConfDTO} from "../dto/ConfDTO" +import {CommonConstants} from "../common-libs/constants" -const common = require('../../../app/common'); -const constants = common.constants +const constants = CommonConstants export function maxAcceleration (conf:ConfDTO) { let maxGenTime = Math.ceil(conf.avgGenTime * constants.POW_DIFFICULTY_RANGE_RATIO); diff --git a/app/lib/rules/local_rules.ts b/app/lib/rules/local_rules.ts index 7913db3916ccca67cb1fac0d8ed23b56d60e6a09..a590eb9d024818347d7ca8aa5e1d1a7bae9f127f 100644 --- a/app/lib/rules/local_rules.ts +++ b/app/lib/rules/local_rules.ts @@ -6,11 +6,12 @@ import {BaseDTO, TransactionDTO} from "../dto/TransactionDTO" import {DBBlock} from "../db/DBBlock" import {verify} from "../common-libs/crypto/keyring" import {hashf} from "../common" +import {CommonConstants} from "../common-libs/constants" const _ = require('underscore'); const common = require('../../../app/common'); -const constants = common.constants +const constants = CommonConstants const Block = common.document.Block const Identity = common.document.Identity const Membership = common.document.Membership diff --git a/app/modules/crawler/lib/constants.ts b/app/modules/crawler/lib/constants.ts index a3ce7dd4de953786172fbf5a430c6dbf0d25e5dd..788e5c15c10960d58bd2321b8839c90e24d25145 100644 --- a/app/modules/crawler/lib/constants.ts +++ b/app/modules/crawler/lib/constants.ts @@ -1,3 +1,4 @@ +import {CommonConstants} from "../../../lib/common-libs/constants" const common = require('../../../../app/common') export const CrawlerConstants = { @@ -7,7 +8,7 @@ export const CrawlerConstants = { DEFAULT_TIMEOUT: 10 * 1000, // 10 seconds SWITCH_ON_BRANCH_AHEAD_BY_X_MINUTES: 30, - TRANSACTION_VERSION: common.constants.TRANSACTION_VERSION, + TRANSACTION_VERSION: CommonConstants.TRANSACTION_VERSION, FORK_ALLOWED: true, MAX_NUMBER_OF_PEERS_FOR_PULLING: 4, PULLING_MINIMAL_DELAY: 20, diff --git a/app/modules/crawler/lib/sandbox.ts b/app/modules/crawler/lib/sandbox.ts index 954bd31580013d1baabba654fe196c0b6f1b6870..dee8367730956b87004738394a1f4418f570b798 100644 --- a/app/modules/crawler/lib/sandbox.ts +++ b/app/modules/crawler/lib/sandbox.ts @@ -1,9 +1,8 @@ "use strict"; import {Contacter} from "./contacter" import {Server} from "../../../../server" -import {rawer} from "../../../lib/common-libs/index"; - -const parsers = require('../../../../app/common').parsers; +import {rawer} from "../../../lib/common-libs/index" +import {parsers} from "../../../lib/common-libs/parsers/index" export const pullSandbox = async (currency:string, fromHost:string, fromPort:number, toHost:string, toPort:number, logger:any) => { const from = new Contacter(fromHost, fromPort); diff --git a/app/modules/prover/index.ts b/app/modules/prover/index.ts index e4e12e548fb30f4e64d277c701aef248513db8ce..87f70335614b5ed0daf573caa9bacbc46f57b6a2 100644 --- a/app/modules/prover/index.ts +++ b/app/modules/prover/index.ts @@ -3,7 +3,8 @@ import {BlockGenerator, BlockGeneratorWhichProves} from "./lib/blockGenerator" import {Constants} from "./lib/constants" import {BlockProver} from "./lib/blockProver" import {Prover} from "./lib/prover" -import {Contacter} from "../crawler/lib/contacter"; +import {Contacter} from "../crawler/lib/contacter" +import {parsers} from "../../lib/common-libs/parsers/index" const async = require('async'); const common = require('../../../app/common'); @@ -150,7 +151,7 @@ function generateAndSend(program:any, host:string, port:string, difficulty:strin program.show && console.log(block.getRawSigned()); (async() => { try { - const parsed = common.parsers.parseBlock.syncWrite(block.getRawSigned()); + const parsed = parsers.parseBlock.syncWrite(block.getRawSigned()); await server.BlockchainService.checkBlock(parsed, false); logger.info('Acceptable block'); next(); diff --git a/app/modules/prover/lib/blockGenerator.ts b/app/modules/prover/lib/blockGenerator.ts index 7e49eef84b0abaf9ad2e35f8b9465481e2c9a5ed..7e888b473ca08b534f1dc36fcfb7e7ae87bb8ff4 100644 --- a/app/modules/prover/lib/blockGenerator.ts +++ b/app/modules/prover/lib/blockGenerator.ts @@ -8,8 +8,9 @@ import {Indexer} from "../../../lib/indexer" import {FileDAL} from "../../../lib/dal/fileDAL" import {DBBlock} from "../../../lib/db/DBBlock" import {verify} from "../../../lib/common-libs/crypto/keyring" -import {rawer} from "../../../lib/common-libs/index"; -import {hashf} from "../../../lib/common"; +import {rawer} from "../../../lib/common-libs/index" +import {hashf} from "../../../lib/common" +import {CommonConstants} from "../../../lib/common-libs/constants" const _ = require('underscore'); const moment = require('moment'); @@ -21,7 +22,7 @@ const Membership = common.document.Membership; const Transaction = common.document.Transaction; const Identity = common.document.Identity; const Certification = common.document.Certification; -const constants = common.constants +const constants = CommonConstants export class BlockGenerator { @@ -104,7 +105,7 @@ export class BlockGenerator { } private async findTransactions(current:DBBlock) { - const versionMin = current ? Math.min(common.constants.LAST_VERSION_FOR_TX, current.version) : common.constants.DOCUMENTS_VERSION; + const versionMin = current ? Math.min(CommonConstants.LAST_VERSION_FOR_TX, current.version) : CommonConstants.DOCUMENTS_VERSION; const txs = await this.dal.getTransactionsPending(versionMin); const transactions = []; const passingTxs:any[] = []; @@ -130,7 +131,7 @@ export class BlockGenerator { const blockstamp = tx.blockstamp || (currentNumber + '-'); const txBlockNumber = parseInt(blockstamp.split('-')[0]); // 10 blocks before removing the transaction - if (currentNumber - txBlockNumber + 1 >= common.constants.TRANSACTION_MAX_TRIES) { + if (currentNumber - txBlockNumber + 1 >= CommonConstants.TRANSACTION_MAX_TRIES) { await this.dal.removeTxByHash(tx.hash); } } @@ -261,7 +262,7 @@ export class BlockGenerator { memberships.forEach((ms:any) => joiners.push(ms.issuer)); for (const ms of memberships) { try { - if (ms.block !== common.constants.SPECIAL_BLOCK) { + if (ms.block !== CommonConstants.SPECIAL_BLOCK) { let msBasedBlock = await this.dal.getBlockByBlockstampOrNull(ms.block); if (!msBasedBlock) { throw constants.ERRORS.BLOCKSTAMP_DOES_NOT_MATCH_A_BLOCK; @@ -339,10 +340,10 @@ export class BlockGenerator { if (!identity) { throw 'Identity with hash \'' + idHash + '\' not found'; } - if (current && identity.buid == common.constants.SPECIAL_BLOCK && !identity.wasMember) { + if (current && identity.buid == CommonConstants.SPECIAL_BLOCK && !identity.wasMember) { throw constants.ERRORS.TOO_OLD_IDENTITY; } - else if (!identity.wasMember && identity.buid != common.constants.SPECIAL_BLOCK) { + else if (!identity.wasMember && identity.buid != CommonConstants.SPECIAL_BLOCK) { const idtyBasedBlock = await this.dal.getBlock(identity.buid); const age = current.medianTime - idtyBasedBlock.medianTime; if (age > this.conf.idtyWindow) { @@ -594,7 +595,7 @@ export class BlockGenerator { if (blockLen < maxLenOfBlock) { transactions.forEach((tx:any) => { const txLen = Transaction.getLen(tx); - if (txLen <= common.constants.MAXIMUM_LEN_OF_COMPACT_TX && blockLen + txLen <= maxLenOfBlock && tx.version == common.constants.TRANSACTION_VERSION) { + if (txLen <= CommonConstants.MAXIMUM_LEN_OF_COMPACT_TX && blockLen + txLen <= maxLenOfBlock && tx.version == CommonConstants.TRANSACTION_VERSION) { block.transactions.push({ raw: tx.getCompactVersion() }); } blockLen += txLen; @@ -691,7 +692,7 @@ class NextBlockGenerator implements BlockGeneratorInterface { cert.idty_uid = targetIdty.uid; cert.idty_buid = targetIdty.buid; cert.idty_sig = targetIdty.sig; - cert.buid = current ? [cert.block_number, targetBlock.hash].join('-') : common.constants.SPECIAL_BLOCK; + cert.buid = current ? [cert.block_number, targetBlock.hash].join('-') : CommonConstants.SPECIAL_BLOCK; const rawCert = Certification.fromJSON(cert).getRaw(); if (verify(rawCert, certSig, cert.from)) { cert.sig = certSig; diff --git a/app/modules/prover/lib/blockProver.ts b/app/modules/prover/lib/blockProver.ts index bb54b6b9dd777a83fe96d800ea6861a4ebf1cae7..511edf9bbb9e78425d8c6d63f7213fbfdae91447 100644 --- a/app/modules/prover/lib/blockProver.ts +++ b/app/modules/prover/lib/blockProver.ts @@ -2,6 +2,7 @@ import {Constants} from "./constants" import {ConfDTO, Keypair} from "../../../lib/dto/ConfDTO" import {PowEngine} from "./engine" import {DBBlock} from "../../../lib/db/DBBlock" +import {CommonConstants} from "../../../lib/common-libs/constants" const querablep = require('querablep'); const common = require('../../../../app/common'); @@ -139,7 +140,7 @@ export class BlockProver { const remainder = difficulty % 16; const nbZeros = (difficulty - remainder) / 16; - const highMark = common.constants.PROOF_OF_WORK.UPPER_BOUND[remainder]; + const highMark = CommonConstants.PROOF_OF_WORK.UPPER_BOUND[remainder]; return (async () => { diff --git a/app/modules/prover/lib/permanentProver.ts b/app/modules/prover/lib/permanentProver.ts index 09c0ff6a4e8f54cdee4543df3309e4c5742e7c62..65a5d0bed3441a74125145ed2bddc5b98f219705 100644 --- a/app/modules/prover/lib/permanentProver.ts +++ b/app/modules/prover/lib/permanentProver.ts @@ -4,10 +4,9 @@ import {BlockProver} from "./blockProver" import {Constants} from "./constants" import {DBBlock} from "../../../lib/db/DBBlock" import {dos2unix} from "../../../lib/common-libs/dos2unix" +import {parsers} from "../../../lib/common-libs/parsers/index" const querablep = require('querablep'); -const common = require('../../../../app/common'); -const parsers = common.parsers; export class PermanentProver { diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index 760c37de53be87d638620f08da86adf716041d15..0ab223863cb10b6e3af1ca1602b9c39d7db1370d 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -8,10 +8,9 @@ import {BlockDTO} from "../lib/dto/BlockDTO" import {DBIdentity} from "../lib/dal/sqliteDAL/IdentityDAL" import {DBBlock} from "../lib/db/DBBlock" import {GLOBAL_RULES_HELPERS} from "../lib/rules/global_rules" +import {parsers} from "../lib/common-libs/parsers/index" const _ = require('underscore'); -const co = require('co'); -const parsers = require('../../app/common').parsers; const constants = require('../lib/constants'); const CHECK_ALL_RULES = true; diff --git a/server.ts b/server.ts index 7a76c5f54214fffac3ff17ed796a31d278c9b1da..a5525f977f0b3c0ee44f2c587ba9ac9902d3897f 100644 --- a/server.ts +++ b/server.ts @@ -9,6 +9,7 @@ import {DuniterBlockchain} from "./app/lib/blockchain/DuniterBlockchain" import {SQLBlockchain} from "./app/lib/blockchain/SqlBlockchain" import * as stream from "stream" import {KeyGen, randomKey} from "./app/lib/common-libs/crypto/keyring" +import {parsers} from "./app/lib/common-libs/parsers/index" interface HookableServer { getMainEndpoint: (...args:any[]) => Promise<any> @@ -26,7 +27,6 @@ const archiver = require('archiver'); const unzip = require('unzip2'); const fs = require('fs'); const daemonize = require("daemonize2") -const parsers = require('./app/common').parsers; const constants = require('./app/lib/constants'); const jsonpckg = require('./package.json'); const directory = require('./app/lib/system/directory'); diff --git a/test/fast/block_format.js b/test/fast/block_format.js index ee0ca79b36b29f5be2fdfddb4b222818c42a5bff..b9adfc16fe996458b71b6c569f4dc22c52d35302 100644 --- a/test/fast/block_format.js +++ b/test/fast/block_format.js @@ -1,6 +1,6 @@ "use strict"; const should = require('should'); -const parsers = require('../../app/common').parsers; +const parsers = require('../../app/lib/common-libs/parsers').parsers const raw = "Version: 10\n" + "Type: Block\n" + diff --git a/test/fast/block_local.js b/test/fast/block_local.js index 2bcb15fae3502ce7ae709a9b9dcfe21a9bd6532e..f17fd996f1e7affeb661c258fe6e75fecedeab28 100644 --- a/test/fast/block_local.js +++ b/test/fast/block_local.js @@ -1,7 +1,7 @@ "use strict"; const co = require('co'); const should = require('should'); -const parsers = require('../../app/common').parsers; +const parsers = require('../../app/lib/common-libs/parsers').parsers const indexer = require('../../app/lib/indexer').Indexer const LOCAL_RULES = require('../../app/lib/rules/local_rules').LOCAL_RULES_FUNCTIONS const ALIAS = require('../../app/lib/rules').ALIAS diff --git a/test/fast/modules/common/peering.js b/test/fast/modules/common/peering.js index 864b290017e0e5017a17b86319c8851c80d4d081..090ec407d4dd62a3d9dbc649e2b131def5bc8e7f 100644 --- a/test/fast/modules/common/peering.js +++ b/test/fast/modules/common/peering.js @@ -1,7 +1,7 @@ "use strict"; const should = require('should'); const assert = require('assert'); -const parsers = require('../../../../app/common/lib/parsers'); +const parsers = require('../../../../app/lib/common-libs/parsers').parsers const rawPeer = "" + "Version: 10\n" + diff --git a/test/fast/modules/common/tx_format.js b/test/fast/modules/common/tx_format.js index 10fc83238c200aa284e9857f3d1cfc352fe044cf..d5c45a5a8711314cbe7b45d11f5e464aecb1edfa 100644 --- a/test/fast/modules/common/tx_format.js +++ b/test/fast/modules/common/tx_format.js @@ -1,6 +1,6 @@ "use strict"; var should = require('should'); -var parsers = require('../../../../app/common/lib/parsers'); +var parsers = require('../../../../app/lib/common-libs/parsers').parsers var raw = "Version: 10\n" + "Type: Transaction\n" + diff --git a/test/fast/v1.0-local-index.js b/test/fast/v1.0-local-index.js index 1f27f3624336e1104d931954a4111b7a2e6a51b2..ce2c2b835a367cbcc76c67b0495f81bb1fcc5616 100644 --- a/test/fast/v1.0-local-index.js +++ b/test/fast/v1.0-local-index.js @@ -2,9 +2,9 @@ const _ = require('underscore'); const should = require('should'); -const parsers = require('../../app/common').parsers; +const parsers = require('../../app/lib/common-libs/parsers').parsers const indexer = require('../../app/lib/indexer').Indexer -const constants = require('../../app/common').constants; +const constants = require('../../app/lib/common-libs/constants').CommonConstants const BlockDTO = require('../../app/lib/dto/BlockDTO').BlockDTO const raw = "Version: 10\n" + diff --git a/test/integration/server-sandbox.js b/test/integration/server-sandbox.js index be25bc56fededdd431c962c0351e95ed4acc6e40..b4ad927fcfafd24500e7289f39d97dcdd64d66de 100644 --- a/test/integration/server-sandbox.js +++ b/test/integration/server-sandbox.js @@ -3,11 +3,11 @@ const co = require('co'); const should = require('should'); const bma = require('../../app/modules/bma').BmaDependency.duniter.methods.bma; -const common = require('../../app/common'); const user = require('./tools/user'); const commit = require('./tools/commit'); const toolbox = require('./tools/toolbox'); const constants = require('../../app/lib/constants'); +const CommonConstants = require('../../app/lib/common-libs/constants').CommonConstants const now = 1482300000; @@ -249,10 +249,10 @@ describe("Sandboxes", function() { describe('Transaction', () => { - const tmp = common.constants.TRANSACTION_MAX_TRIES; + const tmp = CommonConstants.TRANSACTION_MAX_TRIES; before(() => { - common.constants.TRANSACTION_MAX_TRIES = 2; + CommonConstants.TRANSACTION_MAX_TRIES = 2; }) it('should accept 2 transactions of 20, 30 units', () => co(function *() { @@ -273,7 +273,7 @@ describe("Sandboxes", function() { yield s1.commit(); yield s1.commit(); (yield s1.dal.txsDAL.getSandboxRoom()).should.equal(2); - common.constants.TRANSACTION_MAX_TRIES = tmp; + CommonConstants.TRANSACTION_MAX_TRIES = tmp; })); }); }); diff --git a/test/integration/tools/user.js b/test/integration/tools/user.js index cbf62fa4fb4941da6421744067e02aac6d210450..75e8023fac6f100af398135c206e052b69553a51 100644 --- a/test/integration/tools/user.js +++ b/test/integration/tools/user.js @@ -4,9 +4,9 @@ const _ = require('underscore'); const async = require('async'); const request = require('request'); const contacter = require('../../../app/modules/crawler').CrawlerDependency.duniter.methods.contacter; -const common = require('../../../app/common'); -const ucp = common.buid; -const parsers = require('../../../app/common').parsers; +const CommonConstants = require('../../../app/lib/common-libs/constants').CommonConstants +const ucp = require('../../../app/lib/common-libs/buid').Buid +const parsers = require('../../../app/lib/common-libs/parsers').parsers const rawer = require('../../../app/lib/common-libs').rawer const keyring = require('../../../app/lib/common-libs/crypto/keyring') const constants = require('../../../app/lib/constants'); @@ -214,7 +214,7 @@ function User (uid, options, node) { } let http = yield getContacter(); let current = yield http.getCurrent(); - let version = current && Math.min(common.constants.LAST_VERSION_FOR_TX, current.version); + let version = current && Math.min(CommonConstants.LAST_VERSION_FOR_TX, current.version); let json = yield http.getSources(pub); let i = 0; let cumulated = 0; diff --git a/test/integration/transactions-chaining.js b/test/integration/transactions-chaining.js index c372b05b8be0c355ec149209fc9738df5b56a3cd..ccd78a7d1849e5be85ea4a8960678148f8e38fb0 100644 --- a/test/integration/transactions-chaining.js +++ b/test/integration/transactions-chaining.js @@ -6,7 +6,7 @@ const should = require('should'); const assert = require('assert'); const constants = require('../../app/lib/constants'); const bma = require('../../app/modules/bma').BmaDependency.duniter.methods.bma; -const common = require('../../app/common'); +const CommonConstants = require('../../app/lib/common-libs/constants').CommonConstants const toolbox = require('./tools/toolbox'); const node = require('./tools/node'); const user = require('./tools/user'); @@ -66,8 +66,8 @@ describe("Transaction chaining", function() { blockstamp: [current.number, current.hash].join('-'), theseOutputsStart: 1 }); - const tmp = common.constants.TRANSACTION_MAX_TRIES; - common.constants.TRANSACTION_MAX_TRIES = 2; + const tmp = CommonConstants.TRANSACTION_MAX_TRIES; + CommonConstants.TRANSACTION_MAX_TRIES = 2; yield unit.shouldNotFail(toc.sendTX(tx1)); yield unit.shouldNotFail(toc.sendTX(tx2)); (yield s1.get('/tx/sources/DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo')).should.have.property('sources').length(1); @@ -78,7 +78,7 @@ describe("Transaction chaining", function() { yield s1.commit({ time: now + 7210 }); // TX2 commited (yield s1.get('/tx/sources/DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo')).should.have.property('sources').length(0); (yield s1.get('/tx/sources/DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV')).should.have.property('sources').length(3); // The UD + 1040 + 160 units sent by toc - common.constants.TRANSACTION_MAX_TRIES = tmp; + CommonConstants.TRANSACTION_MAX_TRIES = tmp; })); }); }); diff --git a/test/integration/transactions-pruning.js b/test/integration/transactions-pruning.js index 7b3be185ffa7dbe7f4af6345422bc17a2426fbe2..fdb1d534c1605d94444790471c5c179f2015c8ec 100644 --- a/test/integration/transactions-pruning.js +++ b/test/integration/transactions-pruning.js @@ -6,7 +6,7 @@ const user = require('./tools/user'); const commit = require('./tools/commit'); const toolbox = require('./tools/toolbox'); const constants = require('../../app/lib/constants'); -const common = require('../../app/common'); +const CommonConstants = require('../../app/lib/common-libs/constants').CommonConstants const s1 = toolbox.server({ currency: 'currency_one', @@ -59,12 +59,12 @@ describe("Transactions pruning", function() { })); it('double spending transaction should have been pruned', () => co(function*() { - const tmp = common.constants.TRANSACTION_MAX_TRIES; - common.constants.TRANSACTION_MAX_TRIES = 1; + const tmp = CommonConstants.TRANSACTION_MAX_TRIES; + CommonConstants.TRANSACTION_MAX_TRIES = 1; yield s1.commit(); yield s1.expect('/tx/history/HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', (res) => { res.history.should.have.property('sending').length(0); }); - common.constants.TRANSACTION_MAX_TRIES = tmp; + CommonConstants.TRANSACTION_MAX_TRIES = tmp; })); }); diff --git a/test/integration/v1.0-modules-api.js b/test/integration/v1.0-modules-api.js index c5f9c787472edf8a8236292225e23428add59a31..f1a62982cad1b147e290fe5d56edabb677e71ddf 100644 --- a/test/integration/v1.0-modules-api.js +++ b/test/integration/v1.0-modules-api.js @@ -7,7 +7,7 @@ const util = require('util'); const path = require('path'); const stream = require('stream'); const duniter = require('../../index'); -const parsers = require('../../app/common').parsers; +const parsers = require('../../app/lib/common-libs/parsers').parsers const querablep = require('querablep'); describe("v1.0 Module API", () => {