diff --git a/.eslintignore b/.eslintignore index 2a1974b2a4d1f49d6fefa5bfd3e25d19a0f8374e..5f18e8995321f7d39bbcca7ec81b81db40066048 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,6 +3,7 @@ app/lib/blockchain/*.js app/lib/blockchain/interfaces/*.js app/lib/computation/*.js app/lib/common-libs/*.js +app/lib/common-libs/**/*.js app/lib/db/*.js app/lib/dto/*.js app/lib/indexer.js diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 94247cf4776f95f55fe883bc3c94f10529877e36..3e2068b97158ebf91d5f0081b71e1b802c76d99b 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -741,7 +741,13 @@ export class FileDAL { } async getTransactionsHistory(pubkey:string) { - const history = { + const history:{ + sent: DBTx[] + received: DBTx[] + sending: DBTx[] + receiving: DBTx[] + pending: DBTx[] + } = { sent: [], received: [], sending: [], diff --git a/app/lib/dal/sqliteDAL/TxsDAL.ts b/app/lib/dal/sqliteDAL/TxsDAL.ts index 4b73d3b5233d41ecb8accbc30b4114995b18c3de..b6330cb934f2f89f89554d6bca7443d0c7fa2bc3 100644 --- a/app/lib/dal/sqliteDAL/TxsDAL.ts +++ b/app/lib/dal/sqliteDAL/TxsDAL.ts @@ -25,7 +25,7 @@ export class DBTx { recipients: string[] written: boolean removed: boolean - received: boolean + received: number output_base: number output_amount: number @@ -178,7 +178,7 @@ export class TxsDAL extends AbstractSQLite<DBTx> { return this.saveEntity(dbTx) } - getLinkedWithIssuer(pubkey:string) { + getLinkedWithIssuer(pubkey:string): Promise<DBTx[]> { return this.sqlFind({ issuers: { $contains: pubkey }, written: true diff --git a/app/lib/helpers/merkle.ts b/app/lib/helpers/merkle.ts index 2c5edd38a7ecad8a342890541033e2c8ad293765..938e95ca51d66ef458de87aa4ab3be285497c382 100644 --- a/app/lib/helpers/merkle.ts +++ b/app/lib/helpers/merkle.ts @@ -4,7 +4,8 @@ export const processForURL = async (req:any, merkle:any, valueCoroutine:any) => "depth": merkle.depth, "nodesCount": merkle.nodes, "leavesCount": merkle.levels[merkle.depth].length, - "root": merkle.levels[0][0] || "" + "root": merkle.levels[0][0] || "", + "leaves": [] }; if (req.query.leaves) { // Leaves @@ -12,7 +13,7 @@ export const processForURL = async (req:any, merkle:any, valueCoroutine:any) => return json; } else if (req.query.leaf) { // Extract of a leaf - json.leaves = {}; + json.leaves = [] const hashes = [req.query.leaf]; // This code is in a loop for historic reasons. Should be set to non-loop style. const values = await valueCoroutine(hashes); diff --git a/app/modules/bma/index.ts b/app/modules/bma/index.ts index 7b4a958c7fe54d8e661e546e08841e6558129433..717ac1111dd1a9eedaa52219298492fb5a50884b 100644 --- a/app/modules/bma/index.ts +++ b/app/modules/bma/index.ts @@ -14,7 +14,6 @@ const _ = require('underscore'); const upnp = require('./lib/upnp').Upnp const bma = require('./lib/bma').bma const dtos = require('./lib/dtos') -const sanitize = require('./lib/sanitize'); const http2raw = require('./lib/http2raw'); const inquirer = require('inquirer'); @@ -146,7 +145,7 @@ export const BmaDependency = { methods: { noLimit: () => BMALimitation.noLimit(), - bma, sanitize, dtos, + bma, dtos, getMainEndpoint: (conf:NetworkConfDTO) => Promise.resolve(getEndpoint(conf)) } } diff --git a/app/modules/bma/lib/bma.ts b/app/modules/bma/lib/bma.ts index 4576c5ce4e688f8baa83f2416f5df089337545f1..07277734df59118f384a89328f55dc3ecbda696c 100644 --- a/app/modules/bma/lib/bma.ts +++ b/app/modules/bma/lib/bma.ts @@ -1,4 +1,3 @@ -"use strict"; import {Server} from "../../../../server" import {Network, NetworkInterface} from "./network" import * as dtos from "./dtos" @@ -9,13 +8,12 @@ import {NetworkBinding} from "./controllers/network" import {WOTBinding} from "./controllers/wot" import {TransactionBinding} from "./controllers/transactions" import {UDBinding} from "./controllers/uds" -import {HttpBlock, HttpPeer, HttpTransactionOfBlock} from "./dtos"; +import {block2HttpBlock, HttpBlock, HttpPeer, HttpTransactionOfBlock} from "./dtos"; import {PeerDTO} from "../../../lib/dto/PeerDTO"; import {BlockDTO} from "../../../lib/dto/BlockDTO"; const co = require('co'); const es = require('event-stream'); -const sanitize = require('./sanitize'); const WebSocketServer = require('ws').Server; export const bma = function(server:Server, interfaces:NetworkInterface[], httpLogs:boolean, logger:any) { @@ -46,54 +44,54 @@ export const bma = function(server:Server, interfaces:NetworkInterface[], httpLo const wot = new WOTBinding(server) const transactions = new TransactionBinding(server) const dividend = new UDBinding(server) - httpMethods.httpGET( '/', (req:any) => node.summary(), dtos.Summary, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/node/summary', (req:any) => node.summary(), dtos.Summary, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/node/sandboxes', (req:any) => node.sandboxes(), dtos.Sandboxes, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/parameters', (req:any) => blockchain.parameters(), dtos.Parameters, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/blockchain/membership', (req:any) => blockchain.parseMembership(req), dtos.Membership, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/memberships/:search', (req:any) => blockchain.memberships(req), dtos.Memberships, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/blockchain/block', (req:any) => blockchain.parseBlock(req), dtos.Block, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/block/:number', (req:any) => blockchain.promoted(req), dtos.Block, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/blocks/:count/:from', (req:any) => blockchain.blocks(req), dtos.Blocks, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/current', (req:any) => blockchain.current(), dtos.Block, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/hardship/:search', (req:any) => blockchain.hardship(req), dtos.Hardship, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/difficulties', (req:any) => blockchain.difficulties(), dtos.Difficulties, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/newcomers', (req:any) => blockchain.with.newcomers(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/certs', (req:any) => blockchain.with.certs(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/joiners', (req:any) => blockchain.with.joiners(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/actives', (req:any) => blockchain.with.actives(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/leavers', (req:any) => blockchain.with.leavers(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/excluded', (req:any) => blockchain.with.excluded(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/revoked', (req:any) => blockchain.with.revoked(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/ud', (req:any) => blockchain.with.ud(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/with/tx', (req:any) => blockchain.with.tx(req), dtos.Stat, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/blockchain/branches', (req:any) => blockchain.branches(), dtos.Branches, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/network/peering', (req:any) => net.peer(), dtos.Peer, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/network/peering/peers', (req:any) => net.peersGet(req), dtos.MerkleOfPeers, BMALimitation.limitAsVeryHighUsage()); - httpMethods.httpPOST( '/network/peering/peers', (req:any) => net.peersPost(req), dtos.Peer, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/network/peers', (req:any) => net.peers(), dtos.Peers, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/wot/add', (req:any) => wot.add(req), dtos.Identity, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/wot/certify', (req:any) => wot.certify(req), dtos.Cert, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/wot/revoke', (req:any) => wot.revoke(req), dtos.Result, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/lookup/:search', (req:any) => wot.lookup(req), dtos.Lookup, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/members', (req:any) => wot.members(), dtos.Members, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/pending', (req:any) => wot.pendingMemberships(), dtos.MembershipList, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/requirements/:search', (req:any) => wot.requirements(req), dtos.Requirements, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/requirements-of-pending/:minsig', (req:any) => wot.requirementsOfPending(req), dtos.Requirements, BMALimitation.limitAsLowUsage()); - httpMethods.httpGET( '/wot/certifiers-of/:search', (req:any) => wot.certifiersOf(req), dtos.Certifications, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/certified-by/:search', (req:any) => wot.certifiedBy(req), dtos.Certifications, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/wot/identity-of/:search', (req:any) => wot.identityOf(req), dtos.SimpleIdentity, BMALimitation.limitAsHighUsage()); - httpMethods.httpPOST( '/tx/process', (req:any) => transactions.parseTransaction(req), dtos.Transaction, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/hash/:hash', (req:any) => transactions.getByHash(req), dtos.Transaction, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/sources/:pubkey', (req:any) => transactions.getSources(req), dtos.Sources, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/history/:pubkey', (req:any) => transactions.getHistory(req), dtos.TxHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/history/:pubkey/blocks/:from/:to', (req:any) => transactions.getHistoryBetweenBlocks(req), dtos.TxHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/history/:pubkey/times/:from/:to', (req:any) => transactions.getHistoryBetweenTimes(req), dtos.TxHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/history/:pubkey/pending', (req:any) => transactions.getPendingForPubkey(req), dtos.TxHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/tx/pending', (req:any) => transactions.getPending(), dtos.TxPending, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/ud/history/:pubkey', (req:any) => dividend.getHistory(req), dtos.UDHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/ud/history/:pubkey/blocks/:from/:to', (req:any) => dividend.getHistoryBetweenBlocks(req), dtos.UDHistory, BMALimitation.limitAsHighUsage()); - httpMethods.httpGET( '/ud/history/:pubkey/times/:from/:to', (req:any) => dividend.getHistoryBetweenTimes(req), dtos.UDHistory, BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/', (req:any) => node.summary(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/node/summary', (req:any) => node.summary(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/node/sandboxes', (req:any) => node.sandboxes(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/parameters', (req:any) => blockchain.parameters(), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/blockchain/membership', (req:any) => blockchain.parseMembership(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/memberships/:search', (req:any) => blockchain.memberships(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/blockchain/block', (req:any) => blockchain.parseBlock(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/block/:number', (req:any) => blockchain.promoted(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/blocks/:count/:from', (req:any) => blockchain.blocks(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/current', (req:any) => blockchain.current(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/hardship/:search', (req:any) => blockchain.hardship(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/difficulties', (req:any) => blockchain.difficulties(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/newcomers', (req:any) => blockchain.with.newcomers(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/certs', (req:any) => blockchain.with.certs(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/joiners', (req:any) => blockchain.with.joiners(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/actives', (req:any) => blockchain.with.actives(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/leavers', (req:any) => blockchain.with.leavers(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/excluded', (req:any) => blockchain.with.excluded(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/revoked', (req:any) => blockchain.with.revoked(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/ud', (req:any) => blockchain.with.ud(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/with/tx', (req:any) => blockchain.with.tx(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/blockchain/branches', (req:any) => blockchain.branches(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/network/peering', (req:any) => net.peer(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/network/peering/peers', (req:any) => net.peersGet(req), BMALimitation.limitAsVeryHighUsage()); + httpMethods.httpPOST( '/network/peering/peers', (req:any) => net.peersPost(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/network/peers', (req:any) => net.peers(), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/wot/add', (req:any) => wot.add(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/wot/certify', (req:any) => wot.certify(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/wot/revoke', (req:any) => wot.revoke(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/lookup/:search', (req:any) => wot.lookup(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/members', (req:any) => wot.members(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/pending', (req:any) => wot.pendingMemberships(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/requirements/:search', (req:any) => wot.requirements(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/requirements-of-pending/:minsig', (req:any) => wot.requirementsOfPending(req), BMALimitation.limitAsLowUsage()); + httpMethods.httpGET( '/wot/certifiers-of/:search', (req:any) => wot.certifiersOf(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/certified-by/:search', (req:any) => wot.certifiedBy(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/wot/identity-of/:search', (req:any) => wot.identityOf(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpPOST( '/tx/process', (req:any) => transactions.parseTransaction(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/hash/:hash', (req:any) => transactions.getByHash(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/sources/:pubkey', (req:any) => transactions.getSources(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/history/:pubkey', (req:any) => transactions.getHistory(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/history/:pubkey/blocks/:from/:to', (req:any) => transactions.getHistoryBetweenBlocks(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/history/:pubkey/times/:from/:to', (req:any) => transactions.getHistoryBetweenTimes(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/history/:pubkey/pending', (req:any) => transactions.getPendingForPubkey(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/tx/pending', (req:any) => transactions.getPending(), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/ud/history/:pubkey', (req:any) => dividend.getHistory(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/ud/history/:pubkey/blocks/:from/:to', (req:any) => dividend.getHistoryBetweenBlocks(req), BMALimitation.limitAsHighUsage()); + httpMethods.httpGET( '/ud/history/:pubkey/times/:from/:to', (req:any) => dividend.getHistoryBetweenTimes(req), BMALimitation.limitAsHighUsage()); }, (httpServer:any) => { @@ -117,7 +115,8 @@ export const bma = function(server:Server, interfaces:NetworkInterface[], httpLo try { currentBlock = yield server.dal.getCurrentBlockOrNull(); if (currentBlock) { - ws.send(JSON.stringify(sanitize(currentBlock, dtos.Block))); + const blockDTO:BlockDTO = BlockDTO.fromJSONObject(currentBlock) + ws.send(JSON.stringify(block2HttpBlock(blockDTO))) } } catch (e) { logger.error(e); @@ -142,55 +141,7 @@ export const bma = function(server:Server, interfaces:NetworkInterface[], httpLo if (data.joiners) { currentBlock = data; const blockDTO:BlockDTO = BlockDTO.fromJSONObject(currentBlock) - const blockResult:HttpBlock = { - version: blockDTO.version, - currency: blockDTO.currency, - number: blockDTO.number, - issuer: blockDTO.issuer, - issuersFrame: blockDTO.issuersFrame, - issuersFrameVar: blockDTO.issuersFrameVar, - issuersCount: blockDTO.issuersCount, - parameters: blockDTO.parameters, - membersCount: blockDTO.membersCount, - monetaryMass: blockDTO.monetaryMass, - powMin: blockDTO.powMin, - time: blockDTO.time, - medianTime: blockDTO.medianTime, - dividend: blockDTO.dividend, - unitbase: blockDTO.unitbase, - hash: blockDTO.hash, - previousHash: blockDTO.previousHash, - previousIssuer: blockDTO.previousIssuer, - identities: blockDTO.identities, - certifications: blockDTO.certifications, - joiners: blockDTO.joiners, - actives: blockDTO.actives, - leavers: blockDTO.leavers, - revoked: blockDTO.revoked, - excluded: blockDTO.excluded, - transactions: blockDTO.transactions.map((tx):HttpTransactionOfBlock => { - return { - version: tx.version, - currency: tx.currency, - comment: tx.comment, - locktime: tx.locktime, - issuers: tx.issuers, - signatures: tx.signatures, - outputs: tx.outputs, - inputs: tx.inputs, - unlocks: tx.unlocks, - block_number: tx.blockNumber, - blockstamp: tx.blockstamp, - blockstampTime: tx.blockstampTime, - time: tx.blockstampTime - } - }), - nonce: blockDTO.nonce, - inner_hash: blockDTO.inner_hash, - signature: blockDTO.signature, - raw: blockDTO.getRawSigned() - } - wssBlock.broadcast(JSON.stringify(blockResult)) + wssBlock.broadcast(JSON.stringify(block2HttpBlock(blockDTO))) } // Broadcast peer if (data.endpoints) { diff --git a/app/modules/bma/lib/controllers/blockchain.ts b/app/modules/bma/lib/controllers/blockchain.ts index 315c816e13560f92be42765336b9a45dd524f773..cce9b53726ff0ae0d44750f52cbac89dd4b2bd46 100644 --- a/app/modules/bma/lib/controllers/blockchain.ts +++ b/app/modules/bma/lib/controllers/blockchain.ts @@ -4,7 +4,10 @@ import {AbstractController} from "./AbstractController"; import {ParametersService} from "../parameters"; import {BMAConstants} from "../constants"; import {MembershipDTO} from "../../../../lib/dto/MembershipDTO"; -import {HttpMembership} from "../dtos"; +import { + block2HttpBlock, HttpBlock, HttpBranches, HttpDifficulties, HttpHardship, HttpMembership, HttpMemberships, + HttpParameters, HttpStat +} from "../dtos"; const _ = require('underscore'); const http2raw = require('../http2raw'); @@ -46,24 +49,27 @@ export class BlockchainBinding extends AbstractController { } } - parseBlock = (req:any) => this.pushEntity(req, http2raw.block, (raw:string) => this.server.writeRawBlock(raw)) + async parseBlock(req:any): Promise<HttpBlock> { + const res = await this.pushEntity(req, http2raw.block, (raw:string) => this.server.writeRawBlock(raw)) + return block2HttpBlock(res) + } - parameters = () => this.server.dal.getParameters(); + parameters = (): Promise<HttpParameters> => this.server.dal.getParameters(); - private getStat(statName:string) { + private getStat(statName:string): () => Promise<HttpStat> { return async () => { let stat = await this.server.dal.getStat(statName); return { result: toJson.stat(stat) }; } } - async promoted(req:any) { + async promoted(req:any): Promise<HttpBlock> { const number = await ParametersService.getNumberP(req); const promoted = await this.BlockchainService.promoted(number); return toJson.block(promoted); } - async blocks(req:any) { + async blocks(req:any): Promise<HttpBlock[]> { const params = ParametersService.getCountAndFrom(req); const count = parseInt(params.count); const from = parseInt(params.from); @@ -72,13 +78,13 @@ export class BlockchainBinding extends AbstractController { return blocks; } - async current() { + async current(): Promise<HttpBlock> { const current = await this.server.dal.getCurrentBlockOrNull(); if (!current) throw BMAConstants.ERRORS.NO_CURRENT_BLOCK; return toJson.block(current); } - async hardship(req:any) { + async hardship(req:any): Promise<HttpHardship> { let nextBlockNumber = 0; const search = await ParametersService.getSearchP(req); const idty = await this.IdentityService.findMemberWithoutMemberships(search); @@ -99,7 +105,7 @@ export class BlockchainBinding extends AbstractController { }; } - async difficulties() { + async difficulties(): Promise<HttpDifficulties> { const current = await this.server.dal.getCurrentBlockOrNull(); const number = (current && current.number) || 0; const issuers = await this.server.dal.getUniqueIssuersBetween(number - 1 - current.issuersFrame, number - 1); @@ -118,32 +124,31 @@ export class BlockchainBinding extends AbstractController { }; } - async memberships(req:any) { + async memberships(req:any): Promise<HttpMemberships> { const search = await ParametersService.getSearchP(req); const idty:any = await this.IdentityService.findMember(search); const json = { pubkey: idty.pubkey, uid: idty.uid, sigDate: idty.buid, - memberships: [] - }; - json.memberships = idty.memberships.map((msObj:any) => { - const ms = MembershipDTO.fromJSONObject(msObj); - return { - version: ms.version, - currency: this.conf.currency, - membership: ms.membership, - blockNumber: ms.block_number, - blockHash: ms.block_hash, - written: (!msObj.written_number && msObj.written_number !== 0) ? null : msObj.written_number - }; - }); + memberships: idty.memberships.map((msObj:any) => { + const ms = MembershipDTO.fromJSONObject(msObj); + return { + version: ms.version, + currency: this.conf.currency, + membership: ms.membership, + blockNumber: ms.block_number, + blockHash: ms.block_hash, + written: (!msObj.written_number && msObj.written_number !== 0) ? null : msObj.written_number + }; + }) + } json.memberships = _.sortBy(json.memberships, 'blockNumber'); json.memberships.reverse(); return json; } - async branches() { + async branches(): Promise<HttpBranches> { const branches = await this.BlockchainService.branches(); const blocks = branches.map((b) => toJson.block(b)); return { diff --git a/app/modules/bma/lib/controllers/network.ts b/app/modules/bma/lib/controllers/network.ts index 918e4a7f4ce3de08a07f58d80566181201d46d80..96069b9b15752a27fd1097dc2a155b2a89eb8324 100644 --- a/app/modules/bma/lib/controllers/network.ts +++ b/app/modules/bma/lib/controllers/network.ts @@ -1,13 +1,13 @@ import {AbstractController} from "./AbstractController"; import {BMAConstants} from "../constants"; -import {HttpPeer} from "../dtos"; +import {HttpMerkleOfPeers, HttpPeer, HttpPeers} from "../dtos"; const _ = require('underscore'); const http2raw = require('../http2raw'); export class NetworkBinding extends AbstractController { - async peer() { + async peer(): Promise<HttpPeer> { const p = await this.PeeringService.peer(); if (!p) { throw BMAConstants.ERRORS.SELF_PEER_NOT_FOUND; @@ -15,7 +15,7 @@ export class NetworkBinding extends AbstractController { return p.json(); } - async peersGet(req:any) { + async peersGet(req:any): Promise<HttpMerkleOfPeers> { let merkle = await this.server.dal.merkleForPeers(); return await this.MerkleService(req, merkle, async (hashes:string[]) => { try { @@ -47,7 +47,7 @@ export class NetworkBinding extends AbstractController { } } - async peers() { + async peers(): Promise<HttpPeers> { let peers = await this.server.dal.listAllPeers(); return { peers: peers.map((p:any) => { diff --git a/app/modules/bma/lib/controllers/node.ts b/app/modules/bma/lib/controllers/node.ts index 420008dfb0707116190da84f763183e138e960b1..3cc4c9dd58947d8221a69934af41fe1a7a0ae1f1 100644 --- a/app/modules/bma/lib/controllers/node.ts +++ b/app/modules/bma/lib/controllers/node.ts @@ -1,9 +1,10 @@ "use strict"; import {AbstractController} from "./AbstractController" +import {HttpSandbox, HttpSandboxes, HttpSummary} from "../dtos"; export class NodeBinding extends AbstractController { - summary = () => { + summary = (): HttpSummary => { return { "duniter": { "software": "duniter", @@ -13,7 +14,7 @@ export class NodeBinding extends AbstractController { } } - async sandboxes() { + async sandboxes(): Promise<HttpSandboxes> { return { identities: await sandboxIt(this.server.dal.idtyDAL.sandbox), memberships: await sandboxIt(this.server.dal.msDAL.sandbox), @@ -22,7 +23,7 @@ export class NodeBinding extends AbstractController { } } -async function sandboxIt(sandbox:any) { +async function sandboxIt(sandbox:any): Promise<HttpSandbox> { return { size: sandbox.maxSize, free: await sandbox.getSandboxRoom() diff --git a/app/modules/bma/lib/controllers/transactions.ts b/app/modules/bma/lib/controllers/transactions.ts index cf2d1d06bb0d8206ef0df421d6731c4995421928..28b89e4f2cc368b12ee61419d06dd938333d861c 100644 --- a/app/modules/bma/lib/controllers/transactions.ts +++ b/app/modules/bma/lib/controllers/transactions.ts @@ -3,17 +3,33 @@ import {ParametersService} from "../parameters"; import {Source} from "../entity/source"; import {BMAConstants} from "../constants"; import {TransactionDTO} from "../../../../lib/dto/TransactionDTO"; +import {HttpSources, HttpTransaction, HttpTxHistory, HttpTxOfHistory, HttpTxPending} from "../dtos"; +import {DBTx} from "../../../../lib/dal/sqliteDAL/TxsDAL"; const _ = require('underscore'); const http2raw = require('../http2raw'); export class TransactionBinding extends AbstractController { - parseTransaction(req:any) { - return this.pushEntity(req, http2raw.transaction, (raw:string) => this.server.writeRawTransaction(raw)) + async parseTransaction(req:any): Promise<HttpTransaction> { + const res = await this.pushEntity(req, http2raw.transaction, (raw:string) => this.server.writeRawTransaction(raw)) + return { + version: res.version, + currency: res.currency, + issuers: res.issuers, + inputs: res.inputs, + outputs: res.outputs, + unlocks: res.unlocks, + signatures: res.signatures, + comment: res.comment, + locktime: res.locktime, + hash: res.hash, + written_block: res.blockNumber, + raw: res.getRaw() + } } - async getSources(req:any) { + async getSources(req:any): Promise<HttpSources> { const pubkey = await ParametersService.getPubkeyP(req); const sources = await this.server.dal.getAvailableSourcesByPubkey(pubkey); const result:any = { @@ -27,26 +43,41 @@ export class TransactionBinding extends AbstractController { return result; } - async getByHash(req:any) { + async getByHash(req:any): Promise<HttpTransaction> { const hash = ParametersService.getHash(req); - const tx = await this.server.dal.getTxByHash(hash); + const tx:DBTx = await this.server.dal.getTxByHash(hash); if (!tx) { throw BMAConstants.ERRORS.TX_NOT_FOUND; } - if (tx.block_number) { - tx.written_block = tx.block_number - } tx.inputs = tx.inputs.map((i:any) => i.raw || i) tx.outputs = tx.outputs.map((o:any) => o.raw || o) - return tx; + return { + version: tx.version, + currency: tx.currency, + locktime: tx.locktime, + // blockstamp: tx.blockstamp, + // blockstampTime: tx.blockstampTime, + issuers: tx.issuers, + inputs: tx.inputs, + outputs: tx.outputs, + unlocks: tx.unlocks, + signatures: tx.signatures, + comment: tx.comment, + hash: tx.hash, + // time: tx.time, + // block_number: tx.block_number, + written_block: tx.block_number, + // received: tx.received, + raw: "" + } } - async getHistory(req:any) { + async getHistory(req:any): Promise<HttpTxHistory> { const pubkey = await ParametersService.getPubkeyP(req); return this.getFilteredHistory(pubkey, (results:any) => results); } - async getHistoryBetweenBlocks(req:any) { + async getHistoryBetweenBlocks(req:any): Promise<HttpTxHistory> { const pubkey = await ParametersService.getPubkeyP(req); const from = await ParametersService.getFromP(req); const to = await ParametersService.getToP(req); @@ -59,7 +90,7 @@ export class TransactionBinding extends AbstractController { }); } - async getHistoryBetweenTimes(req:any) { + async getHistoryBetweenTimes(req:any): Promise<HttpTxHistory> { const pubkey = await ParametersService.getPubkeyP(req); const from = await ParametersService.getFromP(req); const to = await ParametersService.getToP(req); @@ -72,7 +103,7 @@ export class TransactionBinding extends AbstractController { }); } - async getPendingForPubkey(req:any) { + async getPendingForPubkey(req:any): Promise<HttpTxHistory> { const pubkey = await ParametersService.getPubkeyP(req); return this.getFilteredHistory(pubkey, function(res:any) { const histo = res.history; @@ -81,7 +112,7 @@ export class TransactionBinding extends AbstractController { }); } - async getPending() { + async getPending(): Promise<HttpTxPending> { const pending = await this.server.dal.getTransactionsPending(); const res = { "currency": this.conf.currency, @@ -93,19 +124,38 @@ export class TransactionBinding extends AbstractController { return res; } - private async getFilteredHistory(pubkey:string, filter:any) { - let history:any = await this.server.dal.getTransactionsHistory(pubkey); + private async getFilteredHistory(pubkey:string, filter:any): Promise<HttpTxHistory> { + let history = await this.server.dal.getTransactionsHistory(pubkey); let result = { "currency": this.conf.currency, "pubkey": pubkey, - "history": history - }; - _.keys(history).map((key:any) => { - history[key].map((tx:any, index:number) => { - history[key][index] = _.omit(TransactionDTO.fromJSONObject(tx).json(), 'currency', 'raw'); - _.extend(history[key][index], {block_number: tx && tx.block_number, time: tx && tx.time}); - }); - }); + "history": { + sending: history.sending.map(dbtx2HttpTxOfHistory), + received: history.received.map(dbtx2HttpTxOfHistory), + receiving: history.receiving.map(dbtx2HttpTxOfHistory), + sent: history.sent.map(dbtx2HttpTxOfHistory), + pending: history.pending.map(dbtx2HttpTxOfHistory) + } + } return filter(result); } } + +function dbtx2HttpTxOfHistory(tx:DBTx): HttpTxOfHistory { + return { + version: tx.version, + locktime: tx.locktime, + blockstamp: tx.blockstamp, + blockstampTime: tx.blockstampTime, + issuers: tx.issuers, + inputs: tx.inputs, + outputs: tx.outputs, + unlocks: tx.unlocks, + signatures: tx.signatures, + comment: tx.comment, + hash: tx.hash, + time: tx.time, + block_number: tx.block_number, + received: tx.received + } +} diff --git a/app/modules/bma/lib/controllers/uds.ts b/app/modules/bma/lib/controllers/uds.ts index 566f59fcd4c2e5a2fac6d59247913c1d7dbb81cd..16ee005f9302ec60225750f4025f130647ec16a2 100644 --- a/app/modules/bma/lib/controllers/uds.ts +++ b/app/modules/bma/lib/controllers/uds.ts @@ -1,12 +1,13 @@ import {AbstractController} from "./AbstractController" import {ParametersService} from "../parameters" import {Source} from "../entity/source" +import {HttpUDHistory} from "../dtos"; const _ = require('underscore'); export class UDBinding extends AbstractController { - async getHistory(req:any) { + async getHistory(req:any): Promise<HttpUDHistory> { const pubkey = await ParametersService.getPubkeyP(req); return this.getUDSources(pubkey, (results:any) => results); } diff --git a/app/modules/bma/lib/controllers/wot.ts b/app/modules/bma/lib/controllers/wot.ts index 68eec5f8633a258ecc2fed39deb8797a8835cf7f..163591ce6fff53cb34a1a2c569f81208dbc315a7 100644 --- a/app/modules/bma/lib/controllers/wot.ts +++ b/app/modules/bma/lib/controllers/wot.ts @@ -1,7 +1,17 @@ import {AbstractController} from "./AbstractController"; import {BMAConstants} from "../constants"; import {DBIdentity} from "../../../../lib/dal/sqliteDAL/IdentityDAL"; -import {HttpCert, HttpCertIdentity} from "../dtos"; +import { + HttpCert, + HttpCertIdentity, HttpCertifications, + HttpIdentity, + HttpIdentityRequirement, + HttpLookup, + HttpMembers, + HttpMembershipList, + HttpRequirements, + HttpResult, HttpSimpleIdentity +} from "../dtos"; const _ = require('underscore'); const http2raw = require('../http2raw'); @@ -10,7 +20,7 @@ const ParametersService = require('../parameters').ParametersService export class WOTBinding extends AbstractController { - async lookup(req:any) { + async lookup(req:any): Promise<HttpLookup> { // Get the search parameter from HTTP query const search = await ParametersService.getSearchP(req); // Make the research @@ -73,7 +83,7 @@ export class WOTBinding extends AbstractController { }; } - async members() { + async members(): Promise<HttpMembers> { const identities = await this.server.dal.getMembers(); const json:any = { results: [] @@ -82,7 +92,7 @@ export class WOTBinding extends AbstractController { return json; } - async certifiersOf(req:any) { + async certifiersOf(req:any): Promise<HttpCertifications> { const search = await ParametersService.getSearchP(req); const idty = await this.IdentityService.findMemberWithoutMemberships(search); const certs = await this.server.dal.certsToTarget(idty.pubkey, idty.getTargetHash()); @@ -129,10 +139,10 @@ export class WOTBinding extends AbstractController { return json; } - async requirements(req:any) { + async requirements(req:any): Promise<HttpRequirements> { const search = await ParametersService.getSearchP(req); const identities:any = await this.IdentityService.searchIdentities(search); - const all = await this.BlockchainService.requirementsOfIdentities(identities); + const all:HttpIdentityRequirement[] = await this.BlockchainService.requirementsOfIdentities(identities); if (!all || !all.length) { throw BMAConstants.ERRORS.NO_IDTY_MATCHING_PUB_OR_UID; } @@ -141,7 +151,7 @@ export class WOTBinding extends AbstractController { }; } - async requirementsOfPending(req:any) { + async requirementsOfPending(req:any): Promise<HttpRequirements> { const minsig = ParametersService.getMinSig(req) const identities = await this.server.dal.idtyDAL.query('SELECT i.*, count(c.sig) as nbSig FROM idty i, cert c WHERE c.target = i.hash group by i.hash having nbSig >= ?', minsig) const all = await this.BlockchainService.requirementsOfIdentities(identities); @@ -153,7 +163,7 @@ export class WOTBinding extends AbstractController { }; } - async certifiedBy(req:any) { + async certifiedBy(req:any): Promise<HttpCertifications> { const search = await ParametersService.getSearchP(req); const idty = await this.IdentityService.findMemberWithoutMemberships(search); const certs = await this.server.dal.certsFrom(idty.pubkey); @@ -199,7 +209,7 @@ export class WOTBinding extends AbstractController { return json; } - async identityOf(req:any) { + async identityOf(req:any): Promise<HttpSimpleIdentity> { let search = await ParametersService.getSearchP(req); let idty = await this.IdentityService.findMemberWithoutMemberships(search); if (!idty) { @@ -215,8 +225,13 @@ export class WOTBinding extends AbstractController { }; } - add(req:any) { - return this.pushEntity(req, http2raw.identity, (raw:string) => this.server.writeRawIdentity(raw)) + async add(req:any): Promise<HttpIdentity> { + const res = await this.pushEntity(req, http2raw.identity, (raw:string) => this.server.writeRawIdentity(raw)) + return { + pubkey: res.pubkey, + uids: [], + signed: [] + } } async certify(req:any): Promise<HttpCert> { @@ -235,27 +250,29 @@ export class WOTBinding extends AbstractController { } } - revoke(req:any) { - return this.pushEntity(req, http2raw.revocation, (raw:string) => this.server.writeRawRevocation(raw)) + async revoke(req:any): Promise<HttpResult> { + const res = await this.pushEntity(req, http2raw.revocation, (raw:string) => this.server.writeRawRevocation(raw)) + return { + result: true + } } - async pendingMemberships() { + async pendingMemberships(): Promise<HttpMembershipList> { const memberships = await this.server.dal.findNewcomers(); const json = { - memberships: [] + memberships: memberships.map((ms:any) => { + return { + pubkey: ms.issuer, + uid: ms.userid, + version: ms.version || 0, + currency: this.server.conf.currency, + membership: ms.membership, + blockNumber: parseInt(ms.blockNumber), + blockHash: ms.blockHash, + written: (!ms.written_number && ms.written_number !== 0) ? null : ms.written_number + }; + }) }; - json.memberships = memberships.map((ms:any) => { - return { - pubkey: ms.issuer, - uid: ms.userid, - version: ms.version, - currency: this.server.conf.currency, - membership: ms.membership, - blockNumber: parseInt(ms.blockNumber), - blockHash: ms.blockHash, - written: (!ms.written_number && ms.written_number !== 0) ? null : ms.written_number - }; - }); json.memberships = _.sortBy(json.memberships, 'blockNumber'); json.memberships.reverse(); return json; diff --git a/app/modules/bma/lib/dtos.ts b/app/modules/bma/lib/dtos.ts index fd09131cc52ed27a749f0094e0eac5e9c11eb769..6695086a690ba026a7bed222af30c7495bd6d829 100644 --- a/app/modules/bma/lib/dtos.ts +++ b/app/modules/bma/lib/dtos.ts @@ -1,3 +1,5 @@ +import {BlockDTO} from "../../../lib/dto/BlockDTO"; +import {DBPeer as DBPeer2} from "../../../lib/dal/sqliteDAL/PeerDAL"; export const Summary = { duniter: { @@ -7,6 +9,14 @@ export const Summary = { } }; +export interface HttpSummary { + duniter: { + software: string + version: string + forkWindowSize: number + } +} + export const Parameters = { currency: String, c: Number, @@ -31,6 +41,30 @@ export const Parameters = { dtReeval: Number }; +export interface HttpParameters { + currency: string + c: number + dt: number + ud0: number + sigPeriod: number + sigStock: number + sigWindow: number + sigValidity: number + sigQty: number + idtyWindow: number + msWindow: number + xpercent: number + msValidity: number + stepMax: number + medianTimeBlocks: number + avgGenTime: number + dtDiffEval: number + percentRot: number + udTime0: number + udReevalTime0: number + dtReeval: number +} + export const Membership = { "signature": String, "membership": { @@ -73,6 +107,22 @@ export const Memberships = { ] }; +export interface HttpMemberships { + pubkey: string + uid: string + sigDate: string + memberships: [ + { + version: number + currency: string + membership: string + blockNumber: number + blockHash: string + written: number + } + ] +} + export const MembershipList = { "memberships": [ { @@ -88,6 +138,21 @@ export const MembershipList = { ] }; +export interface HttpMembershipList { + memberships: [ + { + pubkey: string + uid: string + version: number + currency: string + membership: string + blockNumber: number + blockHash: string + written: number + } + ] +} + export const TransactionOfBlock = { "version": Number, "currency": String, @@ -186,21 +251,87 @@ export interface HttpBlock { raw: string } +export function block2HttpBlock(blockDTO:BlockDTO): HttpBlock { + return { + version: blockDTO.version, + currency: blockDTO.currency, + number: blockDTO.number, + issuer: blockDTO.issuer, + issuersFrame: blockDTO.issuersFrame, + issuersFrameVar: blockDTO.issuersFrameVar, + issuersCount: blockDTO.issuersCount, + parameters: blockDTO.parameters, + membersCount: blockDTO.membersCount, + monetaryMass: blockDTO.monetaryMass, + powMin: blockDTO.powMin, + time: blockDTO.time, + medianTime: blockDTO.medianTime, + dividend: blockDTO.dividend, + unitbase: blockDTO.unitbase, + hash: blockDTO.hash, + previousHash: blockDTO.previousHash, + previousIssuer: blockDTO.previousIssuer, + identities: blockDTO.identities, + certifications: blockDTO.certifications, + joiners: blockDTO.joiners, + actives: blockDTO.actives, + leavers: blockDTO.leavers, + revoked: blockDTO.revoked, + excluded: blockDTO.excluded, + transactions: blockDTO.transactions.map((tx):HttpTransactionOfBlock => { + return { + version: tx.version, + currency: tx.currency, + comment: tx.comment, + locktime: tx.locktime, + issuers: tx.issuers, + signatures: tx.signatures, + outputs: tx.outputs, + inputs: tx.inputs, + unlocks: tx.unlocks, + block_number: tx.blockNumber, + blockstamp: tx.blockstamp, + blockstampTime: tx.blockstampTime, + time: tx.blockstampTime + } + }), + nonce: blockDTO.nonce, + inner_hash: blockDTO.inner_hash, + signature: blockDTO.signature, + raw: blockDTO.getRawSigned() + } +} + export const Hardship = { "block": Number, "level": Number }; +export interface HttpHardship { + block: number + level: number +} + export const Difficulty = { "uid": String, "level": Number }; +export interface HttpDifficulty { + uid: string + level: number +} + export const Difficulties = { "block": Number, "levels": [Difficulty] }; +export interface HttpDifficulties { + block: number + levels: HttpDifficulty[] +} + export const Blocks = [Block]; export const Stat = { @@ -209,10 +340,20 @@ export const Stat = { } }; +export interface HttpStat { + result: { + blocks: number[] + } +} + export const Branches = { "blocks": [Block] }; +export interface HttpBranches { + blocks: HttpBlock[] +} + export const Peer = { "version": Number, "currency": String, @@ -250,6 +391,10 @@ export const Peers = { "peers": [DBPeer] }; +export interface HttpPeers { + peers: DBPeer2[] +} + export const MerkleOfPeers = { "depth": Number, "nodesCount": Number, @@ -262,6 +407,18 @@ export const MerkleOfPeers = { } }; +export interface HttpMerkleOfPeers { + depth: number + nodesCount: number + leavesCount: number + root: string + leaves: string[] + leaf: { + hash: string + value: DBPeer2 + } +} + export const Other = { "pubkey": String, "meta": { @@ -274,6 +431,18 @@ export const Other = { "signature": String }; +export interface HttpOther { + pubkey: string, + meta: { + block_number: number, + block_hash: string + }, + uids: string[], + isMember: boolean, + wasMember: boolean, + signature: string +} + export const UID = { "uid": String, "meta": { @@ -286,6 +455,18 @@ export const UID = { "others": [Other] }; +export interface HttpUID { + uid: string, + meta: { + timestamp: string + }, + self: string, + revocation_sig: string, + revoked: boolean, + revoked_on: number, + others: HttpOther[] +} + export const Signed = { "uid": String, "pubkey": String, @@ -301,6 +482,21 @@ export const Signed = { "signature": String }; +export interface HttpSigned { + uid: string, + pubkey: string, + meta: { + timestamp: string + }, + cert_time: { + block: number, + block_hash: string + }, + isMember: boolean, + wasMember: boolean, + signature: string +} + export const CertIdentity = { "issuer": String, "uid": String, @@ -335,15 +531,30 @@ export const Identity = { "signed": [Signed] }; +export interface HttpIdentity { + pubkey: string, + uids: HttpUID[], + signed: HttpSigned[] +} + export const Result = { "result": Boolean }; +export interface HttpResult { + result: boolean +} + export const Lookup = { "partial": Boolean, "results": [Identity] }; +export interface HttpLookup { + partial: boolean + results: HttpIdentity[] +} + export const Members = { "results": [{ pubkey: String, @@ -351,6 +562,13 @@ export const Members = { }] }; +export interface HttpMembers { + results: { + pubkey: string, + uid: string + }[] +} + export const RequirementsCert = { from: String, to: String, @@ -358,6 +576,13 @@ export const RequirementsCert = { sig: String }; +export interface HttpRequirementsCert { + from: string + to: string + expiresIn: number + sig: string +} + export const RequirementsPendingCert = { from: String, to: String, @@ -365,12 +590,25 @@ export const RequirementsPendingCert = { sig: String }; +export interface HttpRequirementsPendingCert { + from: string + to: string + blockstamp: string + sig: string +} + export const RequirementsPendingMembership = { type: String, blockstamp: String, sig: String }; +export interface HttpRequirementsPendingMembership { + type: string, + blockstamp: string, + sig: string +} + export const Requirements = { "identities": [{ pubkey: String, @@ -394,6 +632,31 @@ export const Requirements = { }] }; +export interface HttpRequirements { + identities: HttpIdentityRequirement[] +} + +export interface HttpIdentityRequirement { + pubkey: string + uid: string + meta: { + timestamp: string + } + sig: string + revocation_sig: string | null + revoked: boolean + revoked_on: number | null + expired: boolean + outdistanced: boolean + isSentry: boolean + wasMember: boolean + certifications: HttpRequirementsCert[] + pendingCerts: HttpRequirementsPendingCert[] + pendingMemberships: HttpRequirementsPendingMembership[] + membershipPendingExpiresIn: number + membershipExpiresIn: number +} + export const Certification = { "pubkey": String, "uid": String, @@ -411,6 +674,23 @@ export const Certification = { "signature": String }; +export interface HttpCertification { + pubkey: string + uid: string + isMember: boolean + wasMember: boolean + cert_time: { + block: number + medianTime: number + } + sigDate: string + written: { + number: number + hash: string + } + signature: string +} + export const Certifications = { "pubkey": String, "uid": String, @@ -419,12 +699,26 @@ export const Certifications = { "certifications": [Certification] }; +export interface HttpCertifications { + pubkey: string + uid: string + sigDate: string + isMember: boolean + certifications: HttpCertification[] +} + export const SimpleIdentity = { "pubkey": String, "uid": String, "sigDate": String }; +export interface HttpSimpleIdentity { + pubkey: string + uid: string + sigDate: string +} + export const Transaction = { "version": Number, "currency": String, @@ -440,6 +734,21 @@ export const Transaction = { "hash": String }; +export interface HttpTransaction { + version: number + currency: string + issuers: string[] + inputs: string[] + unlocks: string[] + outputs: string[] + comment: string + locktime: number + signatures: string[] + raw: string + written_block: number|null + hash: string +} + export const Source = { "type": String, "noffset": Number, @@ -449,12 +758,27 @@ export const Source = { "conditions": String }; +export interface HttpSource { + type: string + noffset: number + identifier: string + amount: number + base: number + conditions: string +} + export const Sources = { "currency": String, "pubkey": String, "sources": [Source] }; +export interface HttpSources { + currency: string + pubkey: string + sources: HttpSource[] +} + export const TxOfHistory = { "version": Number, "issuers": [String], @@ -472,6 +796,23 @@ export const TxOfHistory = { "blockstampTime": Number }; +export interface HttpTxOfHistory { + version: number + issuers: string[] + inputs: string[] + unlocks: string[] + outputs: string[] + comment: string + locktime: number + received: number + signatures: string[] + hash: string + block_number: number|null + time: number|null + blockstamp: string + blockstampTime: number|null +} + export const TxHistory = { "currency": String, "pubkey": String, @@ -484,11 +825,28 @@ export const TxHistory = { } }; +export interface HttpTxHistory { + currency: string + pubkey: string + history: { + sent: HttpTxOfHistory[] + received: HttpTxOfHistory[] + sending: HttpTxOfHistory[] + receiving: HttpTxOfHistory[] + pending: HttpTxOfHistory[] + } +} + export const TxPending = { "currency": String, "pending": [Transaction] }; +export interface HttpTxPending { + currency: string + pending: HttpTransaction[] +} + export const UD = { "block_number": Number, "consumed": Boolean, @@ -497,6 +855,14 @@ export const UD = { "base": Number }; +export interface HttpUD { + block_number: number + consumed: boolean + time: number + amount: number + base: number +} + export const UDHistory = { "currency": String, "pubkey": String, @@ -505,6 +871,14 @@ export const UDHistory = { } }; +export interface HttpUDHistory { + currency: string + pubkey: string + history: { + history: HttpUD[] + } +} + export const BooleanDTO = { "success": Boolean }; @@ -540,6 +914,11 @@ export const Sandbox = { free: Number }; +export interface HttpSandbox { + size: number + free: number +} + export const IdentitySandbox = Sandbox; export const MembershipSandbox = Sandbox; export const TransactionSandbox = Sandbox; @@ -550,6 +929,12 @@ export const Sandboxes = { transactions: TransactionSandbox }; +export interface HttpSandboxes { + identities: HttpSandbox + memberships: HttpSandbox + transactions: HttpSandbox +} + export const LogLink = { link: String }; diff --git a/app/modules/bma/lib/network.ts b/app/modules/bma/lib/network.ts index cdb1468017ed2b298e862627a6a6debea4317479..03e2a7863b025b5d74b58e6b3df17c39d6b87e73 100644 --- a/app/modules/bma/lib/network.ts +++ b/app/modules/bma/lib/network.ts @@ -15,7 +15,6 @@ const errorhandler = require('errorhandler'); const bodyParser = require('body-parser'); const cors = require('cors'); const fileUpload = require('express-fileupload'); -const sanitize = require('./sanitize'); export interface NetworkInterface { ip:string|null @@ -77,7 +76,7 @@ export const Network = { app.use(errorhandler()); } - const handleRequest = (method:any, uri:string, promiseFunc:(...args:any[])=>Promise<any>, dtoContract:any, theLimiter:any) => { + const handleRequest = (method:any, uri:string, promiseFunc:(...args:any[])=>Promise<any>, theLimiter:any) => { const limiter = theLimiter || BMALimitation.limitAsUnlimited(); method(uri, async function(req:any, res:any) { res.set('Access-Control-Allow-Origin', '*'); @@ -88,8 +87,6 @@ export const Network = { } limiter.processRequest(); let result = await promiseFunc(req); - // Ensure of the answer format - result = sanitize(result, dtoContract); // HTTP answer res.status(200).send(JSON.stringify(result, null, " ")); } catch (e) { @@ -122,9 +119,9 @@ export const Network = { }; routingCallback(app, { - httpGET: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, dtoContract:any, limiter:any) => handleRequest(app.get.bind(app), uri, promiseFunc, dtoContract, limiter), - httpPOST: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, dtoContract:any, limiter:any) => handleRequest(app.post.bind(app), uri, promiseFunc, dtoContract, limiter), - httpGETFile: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, dtoContract:any, limiter:any) => handleFileRequest(app.get.bind(app), uri, promiseFunc, limiter) + httpGET: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, limiter:any) => handleRequest(app.get.bind(app), uri, promiseFunc, limiter), + httpPOST: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, limiter:any) => handleRequest(app.post.bind(app), uri, promiseFunc, limiter), + httpGETFile: (uri:string, promiseFunc:(...args:any[])=>Promise<any>, limiter:any) => handleFileRequest(app.get.bind(app), uri, promiseFunc, limiter) }); if (staticPath) { diff --git a/app/modules/crawler/lib/contacter.ts b/app/modules/crawler/lib/contacter.ts index 96cda407b0c4e418b0a93a60ccb6fb46a853ee15..fde7ee1ea5ac07c2a20841e767e69b3b94e65df5 100644 --- a/app/modules/crawler/lib/contacter.ts +++ b/app/modules/crawler/lib/contacter.ts @@ -1,7 +1,7 @@ import {CrawlerConstants} from "./constants" const rp = require('request-promise'); -const sanitize = require('../../../modules/bma').BmaDependency.duniter.methods.sanitize; +const sanitize = require('../../../modules/bma/lib/sanitize') const dtos = require('../../../modules/bma').BmaDependency.duniter.methods.dtos; export class Contacter { diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts index 03c16d935b82407cff12f7378b0baff57629605b..64badeae3994d10fdcc1dd0057580f50f5667eb4 100644 --- a/app/service/BlockchainService.ts +++ b/app/service/BlockchainService.ts @@ -9,6 +9,7 @@ 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" +import {HttpIdentityRequirement} from "../modules/bma/lib/dtos"; const _ = require('underscore'); const constants = require('../lib/constants'); @@ -245,7 +246,7 @@ export class BlockchainService { async requirementsOfIdentities(identities:DBIdentity[]) { - let all = []; + let all:HttpIdentityRequirement[] = []; let current = await this.dal.getCurrentBlockOrNull(); for (const obj of identities) { try { @@ -258,7 +259,7 @@ export class BlockchainService { return all; } - async requirementsOfIdentity(idty:DBIdentity, current:DBBlock) { + async requirementsOfIdentity(idty:DBIdentity, current:DBBlock): Promise<HttpIdentityRequirement> { // TODO: this is not clear let expired = false; let outdistanced = false; diff --git a/server.ts b/server.ts index 8bdd0d876c2c5dd567e424a3ac48422059ed78a6..6866ec7bb47b49631a4e5d710c3928a2db4efba8 100644 --- a/server.ts +++ b/server.ts @@ -40,7 +40,7 @@ export class Server extends stream.Duplex implements HookableServer { dal:FileDAL home:string - version:number + version:string logger:any rawer:any keyPair:any diff --git a/test/integration/tools/toolbox.ts b/test/integration/tools/toolbox.ts index da415ea6e9b7a5eeb9aef2cdfd2be278e759da8c..46c56985a053305d8a3ff2d9517e8b8f48b2c86f 100644 --- a/test/integration/tools/toolbox.ts +++ b/test/integration/tools/toolbox.ts @@ -146,10 +146,10 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe return { endpoints: [['BASIC_MERKLED_API', host, port].join(' ')] } - }, dtos.Peer, noLimit); + }, noLimit); // Mock BMA method for sync mocking - httpMethods.httpGET('/network/peering/peers', onPeersRequested, dtos.MerkleOfPeers, noLimit); + httpMethods.httpGET('/network/peering/peers', onPeersRequested, noLimit); // Another mock BMA method for sync mocking httpMethods.httpGET('/blockchain/blocks/:count/:from', (req:any) => { @@ -160,7 +160,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe return readBlocksMethod(count, from); - }, dtos.Blocks, noLimit); + }, noLimit); // Another mock BMA method for sync mocking httpMethods.httpGET('/blockchain/block/:number', (req:any) => { @@ -170,7 +170,7 @@ export const fakeSyncServer = async (readBlocksMethod:any, readParticularBlockMe return readParticularBlockMethod(number); - }, dtos.Block, noLimit); + }, noLimit); }, null) await fakeServer.openConnections();