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');
|
||||
delete self._broadcast.map[key];
|
||||
}, this._broadcast.timout),
|
||||
value: item
|
||||
type: item.type,
|
||||
value: item.render()
|
||||
};
|
||||
|
||||
this._broadcast.map[key] = entry;
|
||||
@ -235,8 +236,6 @@ Peer.prototype._onPacket = function onPacket(packet) {
|
||||
if (this._res(cmd, payload)) {
|
||||
return;
|
||||
} else {
|
||||
if (cmd !== 'merkleblock')
|
||||
console.log(cmd);
|
||||
this.emit(cmd, payload);
|
||||
}
|
||||
};
|
||||
@ -258,7 +257,7 @@ Peer.prototype._handleGetData = function handleGetData(items) {
|
||||
return;
|
||||
|
||||
var entry = this._broadcast.map[hash];
|
||||
this._write(entry.value.render());
|
||||
this._write(this.framer.packet(entry.type, entry.value));
|
||||
entry.e.emit('request');
|
||||
}, this);
|
||||
};
|
||||
|
||||
@ -24,6 +24,10 @@ function Parser() {
|
||||
util.inherits(Parser, EventEmitter);
|
||||
module.exports = Parser;
|
||||
|
||||
Parser.prototype._error = function _error(str) {
|
||||
this.emit('error', new Error(str));
|
||||
};
|
||||
|
||||
Parser.prototype.feed = function feed(data) {
|
||||
this.pendingTotal += data.length;
|
||||
this.pending.push(data);
|
||||
@ -52,7 +56,7 @@ Parser.prototype.parse = function parse(chunk) {
|
||||
} else {
|
||||
this.packet.payload = chunk;
|
||||
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);
|
||||
if (this.packet.payload)
|
||||
@ -66,14 +70,13 @@ Parser.prototype.parse = function parse(chunk) {
|
||||
Parser.prototype.parseHeader = function parseHeader(h) {
|
||||
var magic = readU32(h, 0);
|
||||
if (magic !== constants.magic) {
|
||||
return this.emit('error',
|
||||
new Error('Invalid magic value: ' + magic.toString(16)));
|
||||
return this._error('Invalid magic value: ' + magic.toString(16));
|
||||
}
|
||||
|
||||
// Count length of the cmd
|
||||
for (var i = 0; h[i + 4] !== 0 && i < 12; i++);
|
||||
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));
|
||||
this.waiting = readU32(h, 16);
|
||||
@ -96,13 +99,15 @@ Parser.prototype.parsePayload = function parsePayload(cmd, p) {
|
||||
return this.parseBlock(p);
|
||||
else if (cmd === 'tx')
|
||||
return this.parseTx(p);
|
||||
else if (cmd === 'reject')
|
||||
return this.parseReject(p);
|
||||
else
|
||||
return p;
|
||||
};
|
||||
|
||||
Parser.prototype.parseVersion = function parseVersion(p) {
|
||||
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 services = readU64(p, 4);
|
||||
@ -156,7 +161,7 @@ Parser.prototype.parseInvList = function parseInvList(p) {
|
||||
p = p.slice(count.off);
|
||||
count = count.r;
|
||||
if (p.length < count * 36)
|
||||
return this.emit('error', new Error('Invalid getdata size'));
|
||||
return this._error('Invalid getdata size');
|
||||
|
||||
var items = [];
|
||||
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) {
|
||||
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 off = hashCount.off;
|
||||
hashCount = hashCount.r;
|
||||
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);
|
||||
for (var i = 0; i < hashCount; i++)
|
||||
@ -188,7 +193,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
||||
flagCount = flagCount.r;
|
||||
|
||||
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);
|
||||
|
||||
@ -207,7 +212,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) {
|
||||
|
||||
Parser.prototype.parseBlock = function parseBlock(p) {
|
||||
if (p.length < 84)
|
||||
return this.emit('error', new Error('Invalid block size'));
|
||||
return this._error('Invalid block size');
|
||||
|
||||
return {
|
||||
version: readU32(p, 0),
|
||||
@ -222,13 +227,13 @@ Parser.prototype.parseBlock = function parseBlock(p) {
|
||||
|
||||
Parser.prototype.parseTxIn = function parseTxIn(p) {
|
||||
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 off = scriptLen.off;
|
||||
scriptLen = scriptLen.r;
|
||||
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 {
|
||||
size: off + scriptLen + 4,
|
||||
@ -243,13 +248,13 @@ Parser.prototype.parseTxIn = function parseTxIn(p) {
|
||||
|
||||
Parser.prototype.parseTxOut = function parseTxOut(p) {
|
||||
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 off = scriptLen.off;
|
||||
scriptLen = scriptLen.r;
|
||||
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 {
|
||||
size: off + scriptLen,
|
||||
@ -260,15 +265,15 @@ Parser.prototype.parseTxOut = function parseTxOut(p) {
|
||||
|
||||
Parser.prototype.parseTx = function parseTx(p) {
|
||||
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 off = inCount.off;
|
||||
inCount = inCount.r;
|
||||
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)
|
||||
return this.emit('error', new Error('Invalid tx_in count'));
|
||||
return this._error('Invalid tx_in count');
|
||||
|
||||
var txIn = new Array(inCount);
|
||||
for (var i = 0; i < inCount; i++) {
|
||||
@ -279,16 +284,16 @@ Parser.prototype.parseTx = function parseTx(p) {
|
||||
off += tx.size;
|
||||
|
||||
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 off = outCount.off;
|
||||
outCount = outCount.r;
|
||||
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)
|
||||
return this.emit('error', new Error('Invalid tx_out count'));
|
||||
return this._error('Invalid tx_out count');
|
||||
|
||||
var txOut = new Array(outCount);
|
||||
for (var i = 0; i < outCount; i++) {
|
||||
@ -299,7 +304,7 @@ Parser.prototype.parseTx = function parseTx(p) {
|
||||
off += tx.size;
|
||||
|
||||
if (off + 4 > p.length)
|
||||
return this.emit('error', new Error('Invalid tx_out offset'));
|
||||
return this._error('Invalid tx_out offset');
|
||||
}
|
||||
|
||||
return {
|
||||
@ -310,3 +315,34 @@ Parser.prototype.parseTx = function parseTx(p) {
|
||||
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;
|
||||
|
||||
Wallet.prototype.getPrivateKey = function getPrivateKey() {
|
||||
return this.key.getPrivate().toArray();
|
||||
Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
||||
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() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user