diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index e017c02a..8bb237b2 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -609,7 +609,7 @@ WalletService.prototype._endpointGetTransactions = function() { var rs = new Readable; transactions.forEach(function(transaction) { - rs.push(JSON.stringify(self._formatTransaction(transaction))); + rs.push(utils.toJSONL(self._formatTransaction(transaction))); }); rs.push(null); @@ -626,6 +626,7 @@ WalletService.prototype._formatTransactions = function(txs) { WalletService.prototype._formatTransaction = function(tx) { var obj = tx.toObject(); + //jsonl parser will not allow newline characters here for(var i = 0; i < tx.inputs.length; i++) { obj.inputs[i].inputSatoshis = tx.__inputValues[i]; } diff --git a/lib/services/wallet-api/utils.js b/lib/services/wallet-api/utils.js index 02c66eaa..4a0326ee 100644 --- a/lib/services/wallet-api/utils.js +++ b/lib/services/wallet-api/utils.js @@ -752,4 +752,14 @@ exports.hasRequiredArgsForEncoding = function(args) { return true; }; +exports.toJSONL = function(obj) { + //this should be a standard obj that JSON.stringify will handle + //general newlines within key values or data values are not permitted + //this is intended to be used for bitcoin tx's that don't have newlines + //within keys or values themselves + var str = JSON.stringify(obj); + str = str.replace(/\n/g, ''); + return str + '\n'; +}; + module.exports = exports; diff --git a/test/services/wallet-api/utils.js b/test/services/wallet-api/utils.js new file mode 100644 index 00000000..f38ba161 --- /dev/null +++ b/test/services/wallet-api/utils.js @@ -0,0 +1,15 @@ +'use strict'; + +var should = require('chai').should(); +var utils = require('../../../lib/services/wallet-api/utils'); + +describe('Wallet-Api service utils', function() { + it('should create jsonl from obj', function() { + var obj = { + 1: 'test', + 'foo bar': 'test1', + array: [1,2,'test'] + }; + utils.toJSONL(obj).should.equal('{"1":"test","foo bar":"test1","array":[1,2,"test"]}\n'); + }); +});