bitcoin-inventory
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -12,7 +12,10 @@ 'use strict'; | ||
var EventEmitter = require('events'); | ||
var INV = require('bitcoin-protocol').constants.inventory; | ||
var protocol = require('bitcoin-protocol'); | ||
var MapDeque = require('map-deque'); | ||
var old = require('old'); | ||
var reverse = require('buffer-reverse'); | ||
var createHash = require('create-hash'); | ||
var INV = protocol.constants.inventory; | ||
var encodeTx = protocol.types.transaction.encode; | ||
// TODO: prevent DoS (e.g. rate limiting, cap on stored data) | ||
@@ -25,3 +28,3 @@ // TODO: add optional tx verification (user-provided function), and broadcast valid txs | ||
function Inventory(peers) { | ||
var opts = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
@@ -34,3 +37,3 @@ _classCallCheck(this, Inventory); | ||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Inventory).call(this)); | ||
var _this = _possibleConstructorReturn(this, (Inventory.__proto__ || Object.getPrototypeOf(Inventory)).call(this)); | ||
@@ -55,3 +58,3 @@ var ttl = opts.ttl != null ? opts.ttl : 2 * 60 * 1000; | ||
value: function _onInv(items) { | ||
var peer = arguments.length <= 1 || arguments[1] === undefined ? this.peers : arguments[1]; | ||
var peer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.peers; | ||
@@ -68,4 +71,5 @@ var getData = []; | ||
if (item.type !== INV.MSG_TX) continue; | ||
var hash = getHash(item.hash); | ||
var hash = hashToString(item.hash); | ||
if (this.requesting[hash] || this.data.has(hash)) continue; | ||
item.hash = item.hash.reverse(); | ||
getData.push(item); | ||
@@ -96,5 +100,5 @@ this.requesting[hash] = true; | ||
value: function _onTx(tx) { | ||
var peer = arguments.length <= 1 || arguments[1] === undefined ? this.peers : arguments[1]; | ||
var peer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.peers; | ||
var hash = getHash(tx.getHash()); | ||
var hash = hashToString(getTxHash(tx)); | ||
delete this.requesting[hash]; | ||
@@ -109,3 +113,3 @@ if (this.data.has(hash)) return; | ||
value: function _onGetdata(items) { | ||
var peer = arguments.length <= 1 || arguments[1] === undefined ? this.peers : arguments[1]; | ||
var peer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.peers; | ||
var _iteratorNormalCompletion2 = true; | ||
@@ -120,3 +124,3 @@ var _didIteratorError2 = false; | ||
if (item.type !== INV.MSG_TX) continue; | ||
var hash = getHash(item.hash); | ||
var hash = hashToString(item.hash); | ||
if (!this.data.has(hash)) continue; | ||
@@ -153,4 +157,4 @@ var entry = this.data.get(hash); | ||
value: function _add(tx, broadcast) { | ||
var hashBuf = tx.getHash(); | ||
var hash = getHash(hashBuf); | ||
var hashBuf = getTxHash(tx); | ||
var hash = hashToString(hashBuf); | ||
if (!this.data.has(hash)) { | ||
@@ -171,3 +175,3 @@ this.data.push(hash, { tx: tx, broadcast: broadcast }); | ||
value: function _sendInv(tx, peer) { | ||
peer.send('inv', [{ hash: tx.getHash(), type: INV.MSG_TX }]); | ||
peer.send('inv', [{ hash: getTxHash(tx), type: INV.MSG_TX }]); | ||
} | ||
@@ -177,3 +181,3 @@ }, { | ||
value: function get(hash) { | ||
var entry = this.data.get(getHash(hash)); | ||
var entry = this.data.get(hashToString(hash)); | ||
if (entry) return entry.tx; | ||
@@ -192,6 +196,15 @@ } | ||
function getHash(hash) { | ||
return reverse(hash).toString('hex'); | ||
function hashToString(hash) { | ||
return hash.reverse().toString('hex'); | ||
} | ||
function getTxHash(tx) { | ||
var txBytes = encodeTx(tx); | ||
return sha256(sha256(txBytes)); | ||
} | ||
function sha256(data) { | ||
return createHash('sha256').update(data).digest(); | ||
} | ||
module.exports = old(Inventory); |
{ | ||
"name": "bitcoin-inventory", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Exchange transactions with peers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
'use strict' | ||
var EventEmitter = require('events') | ||
var INV = require('bitcoin-protocol').constants.inventory | ||
var MapDeque = require('map-deque') | ||
var old = require('old') | ||
var reverse = require('buffer-reverse') | ||
const EventEmitter = require('events') | ||
const protocol = require('bitcoin-protocol') | ||
const MapDeque = require('map-deque') | ||
const old = require('old') | ||
const createHash = require('create-hash') | ||
const INV = protocol.constants.inventory | ||
const encodeTx = protocol.types.transaction.encode | ||
// TODO: prevent DoS (e.g. rate limiting, cap on stored data) | ||
@@ -18,3 +21,3 @@ // TODO: add optional tx verification (user-provided function), and broadcast valid txs | ||
super() | ||
var ttl = opts.ttl != null ? opts.ttl : 2 * 60 * 1000 | ||
let ttl = opts.ttl != null ? opts.ttl : 2 * 60 * 1000 | ||
this.peers = peers | ||
@@ -34,7 +37,8 @@ this.data = new MapDeque() | ||
_onInv (items, peer = this.peers) { | ||
var getData = [] | ||
let getData = [] | ||
for (let item of items) { | ||
if (item.type !== INV.MSG_TX) continue | ||
let hash = getHash(item.hash) | ||
let hash = hashToString(item.hash) | ||
if (this.requesting[hash] || this.data.has(hash)) continue | ||
item.hash = item.hash.reverse() | ||
getData.push(item) | ||
@@ -49,3 +53,3 @@ this.requesting[hash] = true | ||
_onTx (tx, peer = this.peers) { | ||
var hash = getHash(tx.getHash()) | ||
let hash = hashToString(getTxHash(tx)) | ||
delete this.requesting[hash] | ||
@@ -61,3 +65,3 @@ if (this.data.has(hash)) return | ||
if (item.type !== INV.MSG_TX) continue | ||
let hash = getHash(item.hash) | ||
let hash = hashToString(item.hash) | ||
if (!this.data.has(hash)) continue | ||
@@ -78,4 +82,4 @@ let entry = this.data.get(hash) | ||
_add (tx, broadcast) { | ||
var hashBuf = tx.getHash() | ||
var hash = getHash(hashBuf) | ||
let hashBuf = getTxHash(tx) | ||
let hash = hashToString(hashBuf) | ||
if (!this.data.has(hash)) { | ||
@@ -95,3 +99,3 @@ this.data.push(hash, { tx, broadcast }) | ||
peer.send('inv', [ | ||
{ hash: tx.getHash(), type: INV.MSG_TX } | ||
{ hash: getTxHash(tx), type: INV.MSG_TX } | ||
]) | ||
@@ -101,3 +105,3 @@ } | ||
get (hash) { | ||
var entry = this.data.get(getHash(hash)) | ||
let entry = this.data.get(hashToString(hash)) | ||
if (entry) return entry.tx | ||
@@ -112,6 +116,15 @@ } | ||
function getHash (hash) { | ||
return reverse(hash).toString('hex') | ||
function hashToString (hash) { | ||
return hash.reverse().toString('hex') | ||
} | ||
function getTxHash (tx) { | ||
let txBytes = encodeTx(tx) | ||
return sha256(sha256(txBytes)) | ||
} | ||
function sha256 (data) { | ||
return createHash('sha256').update(data).digest() | ||
} | ||
module.exports = old(Inventory) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25235
556