From 8401cf5aea43fb083c2d0ee1b86fd1663b261cf6 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 2 Aug 2017 13:52:41 +0200
Subject: [PATCH] [fix] #1042 Fix returned JSON via websockets

---
 app/modules/bma/lib/bma.ts  | 66 +++++++++++++++++++++++++++++++++++--
 app/modules/bma/lib/dtos.ts | 59 +++++++++++++++++++++++++++++++++
 2 files changed, 123 insertions(+), 2 deletions(-)

diff --git a/app/modules/bma/lib/bma.ts b/app/modules/bma/lib/bma.ts
index 850510edf..4576c5ce4 100644
--- a/app/modules/bma/lib/bma.ts
+++ b/app/modules/bma/lib/bma.ts
@@ -9,6 +9,9 @@ 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 {PeerDTO} from "../../../lib/dto/PeerDTO";
+import {BlockDTO} from "../../../lib/dto/BlockDTO";
 
 const co = require('co');
 const es = require('event-stream');
@@ -138,11 +141,70 @@ export const bma = function(server:Server, interfaces:NetworkInterface[], httpLo
           // Broadcast block
           if (data.joiners) {
             currentBlock = data;
-            wssBlock.broadcast(JSON.stringify(sanitize(currentBlock, dtos.Block)));
+            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))
           }
           // Broadcast peer
           if (data.endpoints) {
-            wssPeer.broadcast(JSON.stringify(sanitize(data, dtos.Peer)));
+            const peerDTO = PeerDTO.fromJSONObject(data)
+            const peerResult:HttpPeer = {
+              version: peerDTO.version,
+              currency: peerDTO.currency,
+              pubkey: peerDTO.pubkey,
+              block: peerDTO.blockstamp,
+              endpoints: peerDTO.endpoints,
+              signature: peerDTO.signature,
+              raw: peerDTO.getRaw()
+            }
+            wssPeer.broadcast(JSON.stringify(peerResult));
           }
         } catch (e) {
           logger && logger.error('error on ws mapSync:', e);
diff --git a/app/modules/bma/lib/dtos.ts b/app/modules/bma/lib/dtos.ts
index 0949bbb9f..5bd20dedd 100644
--- a/app/modules/bma/lib/dtos.ts
+++ b/app/modules/bma/lib/dtos.ts
@@ -91,6 +91,22 @@ export const TransactionOfBlock = {
   "issuers": [String]
 };
 
+export interface HttpTransactionOfBlock {
+  version: number
+  currency: string
+  comment: string
+  locktime: number
+  signatures: string[]
+  outputs: string[]
+  inputs: string[]
+  unlocks: string[]
+  block_number: number
+  blockstamp: string
+  blockstampTime: number
+  time: number
+  issuers: string[]
+}
+
 export const Block = {
   "version": Number,
   "currency": String,
@@ -124,6 +140,39 @@ export const Block = {
   "raw": String
 };
 
+export interface HttpBlock {
+  version: number
+  currency: string
+  number: number
+  issuer: string
+  issuersFrame: number
+  issuersFrameVar: number
+  issuersCount: number
+  parameters: string
+  membersCount: number
+  monetaryMass: number
+  powMin: number
+  time: number
+  medianTime: number
+  dividend: number
+  unitbase: number
+  hash: string
+  previousHash: string
+  previousIssuer: string
+  identities: string[]
+  certifications: string[]
+  joiners: string[]
+  actives: string[]
+  leavers: string[]
+  revoked: string[]
+  excluded: string[]
+  transactions: HttpTransactionOfBlock[]
+  nonce: number
+  inner_hash: string
+  signature: string
+  raw: string
+}
+
 export const Hardship = {
   "block": Number,
   "level": Number
@@ -161,6 +210,16 @@ export const Peer = {
   "raw": String
 };
 
+export interface HttpPeer {
+  version: number
+  currency: string
+  pubkey: string
+  block: string
+  endpoints: string[]
+  signature: string
+  raw: string
+}
+
 export const DBPeer = {
   "version": Number,
   "currency": String,
-- 
GitLab