parser: parse reject
This commit is contained in:
parent
4328d7c3ec
commit
62d9af1194
@ -106,7 +106,8 @@ Peer.prototype.broadcast = function broadcast(items) {
|
|||||||
entry.e.emit('timeout');
|
entry.e.emit('timeout');
|
||||||
delete self._broadcast.map[key];
|
delete self._broadcast.map[key];
|
||||||
}, this._broadcast.timout),
|
}, this._broadcast.timout),
|
||||||
value: item
|
type: item.type,
|
||||||
|
value: item.render()
|
||||||
};
|
};
|
||||||
|
|
||||||
this._broadcast.map[key] = entry;
|
this._broadcast.map[key] = entry;
|
||||||
@ -235,8 +236,6 @@ Peer.prototype._onPacket = function onPacket(packet) {
|
|||||||
if (this._res(cmd, payload)) {
|
if (this._res(cmd, payload)) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
if (cmd !== 'merkleblock')
|
|
||||||
console.log(cmd);
|
|
||||||
this.emit(cmd, payload);
|
this.emit(cmd, payload);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -258,7 +257,7 @@ Peer.prototype._handleGetData = function handleGetData(items) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
var entry = this._broadcast.map[hash];
|
var entry = this._broadcast.map[hash];
|
||||||
this._write(entry.value.render());
|
this._write(this.framer.packet(entry.type, entry.value));
|
||||||
entry.e.emit('request');
|
entry.e.emit('request');
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -24,6 +24,10 @@ function Parser() {
|
|||||||
util.inherits(Parser, EventEmitter);
|
util.inherits(Parser, EventEmitter);
|
||||||
module.exports = Parser;
|
module.exports = Parser;
|
||||||
|
|
||||||
|
Parser.prototype._error = function _error(str) {
|
||||||
|
this.emit('error', new Error(str));
|
||||||
|
};
|
||||||
|
|
||||||
Parser.prototype.feed = function feed(data) {
|
Parser.prototype.feed = function feed(data) {
|
||||||
this.pendingTotal += data.length;
|
this.pendingTotal += data.length;
|
||||||
this.pending.push(data);
|
this.pending.push(data);
|
||||||
@ -52,7 +56,7 @@ Parser.prototype.parse = function parse(chunk) {
|
|||||||
} else {
|
} else {
|
||||||
this.packet.payload = chunk;
|
this.packet.payload = chunk;
|
||||||
if (readU32(utils.checksum(this.packet.payload)) !== this.packet.checksum)
|
if (readU32(utils.checksum(this.packet.payload)) !== this.packet.checksum)
|
||||||
return this.emit('error', new Error('Invalid checksum'));
|
return this._error('Invalid checksum');
|
||||||
this.packet.payload = this.parsePayload(this.packet.cmd,
|
this.packet.payload = this.parsePayload(this.packet.cmd,
|
||||||
this.packet.payload);
|
this.packet.payload);
|
||||||
if (this.packet.payload)
|
if (this.packet.payload)
|
||||||
@ -66,14 +70,13 @@ Parser.prototype.parse = function parse(chunk) {
|
|||||||
Parser.prototype.parseHeader = function parseHeader(h) {
|
Parser.prototype.parseHeader = function parseHeader(h) {
|
||||||
var magic = readU32(h, 0);
|
var magic = readU32(h, 0);
|
||||||
if (magic !== constants.magic) {
|
if (magic !== constants.magic) {
|
||||||
return this.emit('error',
|
return this._error('Invalid magic value: ' + magic.toString(16));
|
||||||
new Error('Invalid magic value: ' + magic.toString(16)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count length of the cmd
|
// Count length of the cmd
|
||||||
for (var i = 0; h[i + 4] !== 0 && i < 12; i++);
|
for (var i = 0; h[i + 4] !== 0 && i < 12; i++);
|
||||||
if (i === 12)
|
if (i === 12)
|
||||||
return this.emit('error', new Error('Not NULL-terminated cmd'));
|
return this._error('Not NULL-terminated cmd');
|
||||||
|
|
||||||
var cmd = utils.stringify(h.slice(4, 4 + i));
|
var cmd = utils.stringify(h.slice(4, 4 + i));
|
||||||
this.waiting = readU32(h, 16);
|
this.waiting = readU32(h, 16);
|
||||||
@ -96,13 +99,15 @@ Parser.prototype.parsePayload = function parsePayload(cmd, p) {
|
|||||||
return this.parseBlock(p);
|
return this.parseBlock(p);
|
||||||
else if (cmd === 'tx')
|
else if (cmd === 'tx')
|
||||||
return this.parseTx(p);
|
return this.parseTx(p);
|
||||||
|
else if (cmd === 'reject')
|
||||||
|
return this.parseReject(p);
|
||||||
else
|
else
|
||||||
return p;
|
return p;
|
||||||
};
|
};
|
||||||
|
|
||||||
Parser.prototype.parseVersion = function parseVersion(p) {
|
Parser.prototype.parseVersion = function parseVersion(p) {
|
||||||
if (p.length < 85)
|
if (p.length < 85)
|
||||||
return this.emit('error', new Error('version packet is too small'));
|
return this._error('version packet is too small');
|
||||||
|
|
||||||
var v = readU32(p, 0);
|
var v = readU32(p, 0);
|
||||||
var services = readU64(p, 4);
|
var services = readU64(p, 4);
|
||||||
@ -156,7 +161,7 @@ Parser.prototype.parseInvList = function parseInvList(p) {
|
|||||||
p = p.slice(count.off);
|
p = p.slice(count.off);
|
||||||
count = count.r;
|
count = count.r;
|
||||||
if (p.length < count * 36)
|
if (p.length < count * 36)
|
||||||
return this.emit('error', new Error('Invalid getdata size'));
|
return this._error('Invalid getdata size');
|
||||||
|
|
||||||
var items = [];
|
var items = [];
|
||||||
for (var i = 0, off = 0; i < count; i++, off += 36) {
|
for (var i = 0, off = 0; i < count; i++, off += 36) {
|
||||||
@ -170,13 +175,13 @@ Parser.prototype.parseInvList = function parseInvList(p) {
|
|||||||
|
|
||||||
Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
||||||
if (p.length < 86)
|
if (p.length < 86)
|
||||||
return this.emit('error', new Error('Invalid merkleblock size'));
|
return this._error('Invalid merkleblock size');
|
||||||
|
|
||||||
var hashCount = readIntv(p, 84);
|
var hashCount = readIntv(p, 84);
|
||||||
var off = hashCount.off;
|
var off = hashCount.off;
|
||||||
hashCount = hashCount.r;
|
hashCount = hashCount.r;
|
||||||
if (off + 32 * hashCount + 1 > p.length)
|
if (off + 32 * hashCount + 1 > p.length)
|
||||||
return this.emit('error', new Error('Invalid hash count'));
|
return this._error('Invalid hash count');
|
||||||
|
|
||||||
var hashes = new Array(hashCount);
|
var hashes = new Array(hashCount);
|
||||||
for (var i = 0; i < hashCount; i++)
|
for (var i = 0; i < hashCount; i++)
|
||||||
@ -188,7 +193,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
|||||||
flagCount = flagCount.r;
|
flagCount = flagCount.r;
|
||||||
|
|
||||||
if (off + flagCount > p.length)
|
if (off + flagCount > p.length)
|
||||||
return this.emit('error', new Error('Invalid flag count'));
|
return this._error('Invalid flag count');
|
||||||
|
|
||||||
var flags = p.slice(off, off + flagCount);
|
var flags = p.slice(off, off + flagCount);
|
||||||
|
|
||||||
@ -207,7 +212,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
|||||||
|
|
||||||
Parser.prototype.parseBlock = function parseBlock(p) {
|
Parser.prototype.parseBlock = function parseBlock(p) {
|
||||||
if (p.length < 84)
|
if (p.length < 84)
|
||||||
return this.emit('error', new Error('Invalid block size'));
|
return this._error('Invalid block size');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version: readU32(p, 0),
|
version: readU32(p, 0),
|
||||||
@ -222,13 +227,13 @@ Parser.prototype.parseBlock = function parseBlock(p) {
|
|||||||
|
|
||||||
Parser.prototype.parseTxIn = function parseTxIn(p) {
|
Parser.prototype.parseTxIn = function parseTxIn(p) {
|
||||||
if (p.length < 41)
|
if (p.length < 41)
|
||||||
return this.emit('error', new Error('Invalid tx_in size'));
|
return this._error('Invalid tx_in size');
|
||||||
|
|
||||||
var scriptLen = readIntv(p, 36);
|
var scriptLen = readIntv(p, 36);
|
||||||
var off = scriptLen.off;
|
var off = scriptLen.off;
|
||||||
scriptLen = scriptLen.r;
|
scriptLen = scriptLen.r;
|
||||||
if (off + scriptLen + 4 > p.length)
|
if (off + scriptLen + 4 > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_in script length'));
|
return this._error('Invalid tx_in script length');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
size: off + scriptLen + 4,
|
size: off + scriptLen + 4,
|
||||||
@ -243,13 +248,13 @@ Parser.prototype.parseTxIn = function parseTxIn(p) {
|
|||||||
|
|
||||||
Parser.prototype.parseTxOut = function parseTxOut(p) {
|
Parser.prototype.parseTxOut = function parseTxOut(p) {
|
||||||
if (p.length < 9)
|
if (p.length < 9)
|
||||||
return this.emit('error', new Error('Invalid tx_out size'));
|
return this._error('Invalid tx_out size');
|
||||||
|
|
||||||
var scriptLen = readIntv(p, 8);
|
var scriptLen = readIntv(p, 8);
|
||||||
var off = scriptLen.off;
|
var off = scriptLen.off;
|
||||||
scriptLen = scriptLen.r;
|
scriptLen = scriptLen.r;
|
||||||
if (off + scriptLen > p.length)
|
if (off + scriptLen > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_out script length'));
|
return this._error('Invalid tx_out script length');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
size: off + scriptLen,
|
size: off + scriptLen,
|
||||||
@ -260,15 +265,15 @@ Parser.prototype.parseTxOut = function parseTxOut(p) {
|
|||||||
|
|
||||||
Parser.prototype.parseTx = function parseTx(p) {
|
Parser.prototype.parseTx = function parseTx(p) {
|
||||||
if (p.length < 60)
|
if (p.length < 60)
|
||||||
return this.emit('error', new Error('Invalid tx size'));
|
return this._error('Invalid tx size');
|
||||||
|
|
||||||
var inCount = readIntv(p, 4);
|
var inCount = readIntv(p, 4);
|
||||||
var off = inCount.off;
|
var off = inCount.off;
|
||||||
inCount = inCount.r;
|
inCount = inCount.r;
|
||||||
if (inCount <= 0)
|
if (inCount <= 0)
|
||||||
return this.emit('error', new Error('Invalid tx_in count'));
|
return this._error('Invalid tx_in count');
|
||||||
if (off + 41 * inCount + 14 > p.length)
|
if (off + 41 * inCount + 14 > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_in count'));
|
return this._error('Invalid tx_in count');
|
||||||
|
|
||||||
var txIn = new Array(inCount);
|
var txIn = new Array(inCount);
|
||||||
for (var i = 0; i < inCount; i++) {
|
for (var i = 0; i < inCount; i++) {
|
||||||
@ -279,16 +284,16 @@ Parser.prototype.parseTx = function parseTx(p) {
|
|||||||
off += tx.size;
|
off += tx.size;
|
||||||
|
|
||||||
if (off + 14 > p.length)
|
if (off + 14 > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_in offset'));
|
return this._error('Invalid tx_in offset');
|
||||||
}
|
}
|
||||||
|
|
||||||
var outCount = readIntv(p, off);
|
var outCount = readIntv(p, off);
|
||||||
var off = outCount.off;
|
var off = outCount.off;
|
||||||
outCount = outCount.r;
|
outCount = outCount.r;
|
||||||
if (outCount <= 0)
|
if (outCount <= 0)
|
||||||
return this.emit('error', new Error('Invalid tx_out count'));
|
return this._error('Invalid tx_out count');
|
||||||
if (off + 9 * outCount + 4 > p.length)
|
if (off + 9 * outCount + 4 > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_out count'));
|
return this._error('Invalid tx_out count');
|
||||||
|
|
||||||
var txOut = new Array(outCount);
|
var txOut = new Array(outCount);
|
||||||
for (var i = 0; i < outCount; i++) {
|
for (var i = 0; i < outCount; i++) {
|
||||||
@ -299,7 +304,7 @@ Parser.prototype.parseTx = function parseTx(p) {
|
|||||||
off += tx.size;
|
off += tx.size;
|
||||||
|
|
||||||
if (off + 4 > p.length)
|
if (off + 4 > p.length)
|
||||||
return this.emit('error', new Error('Invalid tx_out offset'));
|
return this._error('Invalid tx_out offset');
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -310,3 +315,34 @@ Parser.prototype.parseTx = function parseTx(p) {
|
|||||||
lock: readU32(p, off)
|
lock: readU32(p, off)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Parser.prototype.parseReject = function parseReject(p) {
|
||||||
|
if (p.length < 3)
|
||||||
|
return this._error('Invalid reject size');
|
||||||
|
|
||||||
|
var messageLen = readIntv(p, 0);
|
||||||
|
var off = messageLen.off;
|
||||||
|
messageLen = messageLen.r;
|
||||||
|
if (off + messageLen + 2 > p.length)
|
||||||
|
return this._error('Invalid reject message');
|
||||||
|
|
||||||
|
var message = utils.stringify(p.slice(off, off + messageLen));
|
||||||
|
off += messageLen;
|
||||||
|
|
||||||
|
var ccode = p[off];
|
||||||
|
off++;
|
||||||
|
|
||||||
|
var reasonLen = readIntv(p, off);
|
||||||
|
off = reasonLen.off;
|
||||||
|
reasonLen = reasonLen.r;
|
||||||
|
if (off + reasonLen > p.length)
|
||||||
|
return this._error('Invalid reject reason');
|
||||||
|
|
||||||
|
var reason = utils.stringify(p.slice(off, off + reasonLen));
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: message,
|
||||||
|
ccode: ccode,
|
||||||
|
reason: reason
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|||||||
@ -32,8 +32,19 @@ function Wallet(options, passphrase) {
|
|||||||
}
|
}
|
||||||
module.exports = Wallet;
|
module.exports = Wallet;
|
||||||
|
|
||||||
Wallet.prototype.getPrivateKey = function getPrivateKey() {
|
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
||||||
return this.key.getPrivate().toArray();
|
var priv = this.key.getPrivate().toArray();
|
||||||
|
if (!enc)
|
||||||
|
return priv;
|
||||||
|
|
||||||
|
if (enc === 'base58') {
|
||||||
|
// We'll be using uncompressed public key as an address
|
||||||
|
var arr = [ 128 ].concat(priv);
|
||||||
|
var chk = utils.checksum(arr);
|
||||||
|
return utils.toBase58(arr.concat(chk));
|
||||||
|
} else {
|
||||||
|
return priv;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getPublicKey = function getPublicKey() {
|
Wallet.prototype.getPublicKey = function getPublicKey() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user