From 1458366441cdb05007c24d55885a000169972f9a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 15 Mar 2017 06:49:46 -0700 Subject: [PATCH] reader: better errors. --- lib/http/rpc.js | 36 ++++++---------- lib/http/rpcbase.js | 3 ++ lib/utils/encoding.js | 83 ++++++++++++++++++++++++++----------- lib/utils/reader.js | 95 ++++++++++++++++++++++++++----------------- lib/wallet/rpc.js | 16 ++------ 5 files changed, 136 insertions(+), 97 deletions(-) diff --git a/lib/http/rpc.js b/lib/http/rpc.js index 7c5316fd..30b3fcd6 100644 --- a/lib/http/rpc.js +++ b/lib/http/rpc.js @@ -856,7 +856,7 @@ RPC.prototype.verifyTXOutProof = co(function* verifyTXOutProof(args, help) { if (!data) throw new RPCError(errs.TYPE_ERROR, 'Invalid hex string.'); - block = fromRaw(MerkleBlock, data); + block = MerkleBlock.fromRaw(data); if (!block.verify()) return out; @@ -936,11 +936,7 @@ RPC.prototype._submitWork = co(function* _submitWork(data) { if (data.length !== 128) throw new RPCError(errs.INVALID_PARAMETER, 'Invalid work size.'); - try { - header = Headers.fromAbbr(data); - } catch (e) { - throw new RPCError(errs.DESERIALIZATION_ERROR, 'Deserialization error.'); - } + header = Headers.fromAbbr(data); data = data.slice(0, 80); data = swap32(data); @@ -1058,7 +1054,7 @@ RPC.prototype.submitBlock = co(function* submitBlock(args, help) { 'submitblock "hexdata" ( "jsonparametersobject" )'); } - block = fromRaw(Block, data); + block = Block.fromRaw(data); return yield this.addBlock(block); }); @@ -1090,7 +1086,7 @@ RPC.prototype.getBlockTemplate = co(function* getBlockTemplate(args, help) { if (!data) throw new RPCError(errs.TYPE_ERROR, 'Missing data parameter.'); - block = fromRaw(Block, data); + block = Block.fromRaw(data); if (block.prevBlock !== this.chain.tip.hash) return 'inconclusive-not-best-prevblk'; @@ -1414,7 +1410,7 @@ RPC.prototype.verifyBlock = co(function* verifyBlock(args, help) { if (this.chain.options.spv) throw new RPCError(errs.MISC_ERROR, 'Cannot verify block in SPV mode.'); - block = fromRaw(Block, data); + block = Block.fromRaw(data); try { yield this.chain.verifyBlock(block); @@ -1609,7 +1605,7 @@ RPC.prototype.decodeRawTransaction = co(function* decodeRawTransaction(args, hel if (!data) throw new RPCError(errs.TYPE_ERROR, 'Invalid hex string.'); - tx = fromRaw(TX, data); + tx = TX.fromRaw(data); return this.txToJSON(tx); }); @@ -1625,7 +1621,7 @@ RPC.prototype.decodeScript = co(function* decodeScript(args, help) { script = new Script(); if (data) - script = fromRaw(Script, data); + script = Script.fromRaw(data); address = Address.fromScripthash(script.hash160()); @@ -1680,7 +1676,7 @@ RPC.prototype.sendRawTransaction = co(function* sendRawTransaction(args, help) { if (!data) throw new RPCError(errs.TYPE_ERROR, 'Invalid hex string.'); - tx = fromRaw(TX, data); + tx = TX.fromRaw(data); this.node.relay(tx); @@ -1707,7 +1703,7 @@ RPC.prototype.signRawTransaction = co(function* signRawTransaction(args, help) { if (!this.mempool) throw new RPCError(errs.MISC_ERROR, 'No mempool available.'); - tx = fromRaw(MTX, data); + tx = MTX.fromRaw(data); tx.view = yield this.mempool.getSpentView(tx); return yield this._signRawTransaction(tx, args); @@ -1749,7 +1745,7 @@ RPC.prototype._signRawTransaction = co(function* _signRawTransaction(tx, args) { if (!hash || index == null || !script || value == null) throw new RPCError(errs.INVALID_PARAMETER, 'Invalid UTXO.'); - script = fromRaw(Script, script); + script = Script.fromRaw(script); coin = new Output(); coin.script = script; @@ -1763,7 +1759,7 @@ RPC.prototype._signRawTransaction = co(function* _signRawTransaction(tx, args) { if (!script.isScripthash() && !script.isWitnessScripthash()) continue; - redeem = fromRaw(Script, redeem); + redeem = Script.fromRaw(redeem); for (j = 0; j < redeem.code.length; j++) { op = redeem.code[j]; @@ -1863,7 +1859,7 @@ RPC.prototype.createWitnessAddress = co(function* createWitnessAddress(args, hel if (!raw) throw new RPCError(errs.TYPE_ERROR, 'Invalid script hex.'); - script = fromRaw(Script, raw); + script = Script.fromRaw(raw); program = script.forWitness(); address = program.getAddress(); @@ -2657,14 +2653,6 @@ function Nonces(n1, n2) { this.nonce2 = n2; } -function fromRaw(ctor, raw) { - try { - return ctor.fromRaw(raw); - } catch (e) { - throw new RPCError(errs.DESERIALIZATION_ERROR, 'Deserialization error.'); - } -} - function parseAddress(raw, network) { try { return Address.fromBase58(raw, network); diff --git a/lib/http/rpcbase.js b/lib/http/rpcbase.js index c527d416..f53f460d 100644 --- a/lib/http/rpcbase.js +++ b/lib/http/rpcbase.js @@ -192,6 +192,9 @@ RPCBase.prototype.call = co(function* call(body, query) { case 'ValidationError': code = RPCBase.errors.TYPE_ERROR; break; + case 'EncodingError': + code = RPCBase.errors.DESERIALIZATION_ERROR; + break; default: code = RPCBase.errors.INTERNAL_ERROR; this.logger.error('RPC internal error.'); diff --git a/lib/utils/encoding.js b/lib/utils/encoding.js index 1fa96fe9..917c78f4 100644 --- a/lib/utils/encoding.js +++ b/lib/utils/encoding.js @@ -11,7 +11,6 @@ * @module utils/encoding */ -var assert = require('assert'); var BN = require('bn.js'); var encoding = exports; @@ -221,7 +220,7 @@ encoding._readU64 = function _readU64(data, off, force53, be) { if (force53) hi &= 0x1fffff; - assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1'); + enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1'); return (hi * 0x100000000) + lo; }; @@ -303,7 +302,7 @@ encoding._read64 = function _read64(data, off, force53, be) { if (force53) hi &= 0x1fffff; - assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1'); + enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1'); return -(hi * 0x100000000 + lo + 1); } @@ -311,7 +310,7 @@ encoding._read64 = function _read64(data, off, force53, be) { if (force53) hi &= 0x1fffff; - assert((hi & 0xffe00000) === 0, 'Number exceeds 2^53-1'); + enforce((hi & 0xffe00000) === 0, off, 'Number exceeds 2^53-1'); return hi * 0x100000000 + lo; }; @@ -384,7 +383,7 @@ encoding._write64 = function _write64(dst, num, off, be) { num -= 1; } - assert(num <= encoding.MAX_SAFE_INTEGER, 'Number exceeds 2^53-1'); + enforce(num <= encoding.MAX_SAFE_INTEGER, off, 'Number exceeds 2^53-1'); lo = num % 0x100000000; hi = (num - lo) / 0x100000000; @@ -614,26 +613,26 @@ encoding.write64BEBN = function write64BEBN(dst, num, off) { encoding.readVarint = function readVarint(data, off) { var value, size; - assert(off < data.length); + assert(off < data.length, off); switch (data[off]) { case 0xff: size = 9; - assert(off + size <= data.length); + assert(off + size <= data.length, off); value = encoding.readU64(data, off + 1); - assert(value > 0xffffffff); + enforce(value > 0xffffffff, off, 'Non-canonical varint'); break; case 0xfe: size = 5; - assert(off + size <= data.length); + assert(off + size <= data.length, off); value = data.readUInt32LE(off + 1, true); - assert(value > 0xffff); + enforce(value > 0xffff, off, 'Non-canonical varint'); break; case 0xfd: size = 3; - assert(off + size <= data.length); + assert(off + size <= data.length, off); value = data[off + 1] | (data[off + 2] << 8); - assert(value >= 0xfd); + enforce(value >= 0xfd, off, 'Non-canonical varint'); break; default: size = 1; @@ -687,7 +686,7 @@ encoding.writeVarint = function writeVarint(dst, num, off) { */ encoding.skipVarint = function skipVarint(data, off) { - assert(off < data.length); + assert(off < data.length, off); switch (data[off]) { case 0xff: @@ -730,14 +729,14 @@ encoding.sizeVarint = function sizeVarint(num) { encoding.readVarintBN = function readVarintBN(data, off) { var result, value, size; - assert(off < data.length); + assert(off < data.length, off); switch (data[off]) { case 0xff: size = 9; - assert(off + size <= data.length); + assert(off + size <= data.length, off); value = encoding.readU64BN(data, off + 1); - assert(value.bitLength() > 32); + enforce(value.bitLength() > 32, off, 'Non-canonical varint'); return new Varint(size, value); default: result = encoding.readVarint(data, off); @@ -790,12 +789,12 @@ encoding.readVarint2 = function readVarint2(data, off) { var ch; for (;;) { - assert(off < data.length); + assert(off < data.length, off); ch = data[off++]; size++; - assert(num < 0x3fffffffffff, 'Number exceeds 2^53-1.'); + enforce(num < 0x3fffffffffff, off, 'Number exceeds 2^53-1'); num = (num * 0x80) + (ch & 0x7f); @@ -828,7 +827,7 @@ encoding.writeVarint2 = function writeVarint2(dst, num, off) { len++; } - assert(off + len <= dst.length); + assert(off + len <= dst.length, off); do { dst[off++] = tmp[len]; @@ -849,7 +848,7 @@ encoding.skipVarint2 = function skipVarint2(data, off) { var ch; for (;;) { - assert(off < data.length); + assert(off < data.length, off); ch = data[off++]; size++; if ((ch & 0x80) === 0) @@ -891,7 +890,7 @@ encoding.readVarint2BN = function readVarint2BN(data, off) { var ch; while (num < 0x3fffffffffff) { - assert(off < data.length); + assert(off < data.length, off); ch = data[off++]; size++; @@ -907,12 +906,12 @@ encoding.readVarint2BN = function readVarint2BN(data, off) { num = new BN(num); for (;;) { - assert(off < data.length); + assert(off < data.length, off); ch = data[off++]; size++; - assert(num.bitLength() <= 64, 'Number exceeds 64 bits.'); + enforce(num.bitLength() <= 64, off, 'Number exceeds 64 bits'); num.iushln(7).iaddn(ch & 0x7f); @@ -948,7 +947,7 @@ encoding.writeVarint2BN = function writeVarint2BN(dst, num, off) { len++; } - assert(off + len <= dst.length); + enforce(off + len <= dst.length, off, 'Out of bounds write'); do { dst[off++] = tmp[len]; @@ -1054,6 +1053,25 @@ encoding.sizeVarString = function sizeVarString(str, enc) { return encoding.sizeVarint(len) + len; }; +/** + * EncodingError + * @constructor + * @param {Number} offset + * @param {String} reason + */ + +encoding.EncodingError = function EncodingError(offset, reason) { + Error.call(this); + + if (Error.captureStackTrace) + Error.captureStackTrace(this, EncodingError); + + this.type = 'EncodingError'; + this.message = reason + ' (offset=' + offset + ').'; +}; + +inherits(encoding.EncodingError, Error); + /* * Helpers */ @@ -1062,3 +1080,20 @@ function Varint(size, value) { this.size = size; this.value = value; } + +function inherits(obj, from) { + var f = function() {}; + f.prototype = from.prototype; + obj.prototype = new f; + obj.prototype.constructor = obj; +} + +function enforce(value, offset, reason) { + if (!value) + throw new EncodingError(offset, reason); +} + +function assert(value, offset) { + if (!value) + throw new EncodingError(offset, 'Out of bounds read'); +} diff --git a/lib/utils/reader.js b/lib/utils/reader.js index 9aad2f35..c68dc315 100644 --- a/lib/utils/reader.js +++ b/lib/utils/reader.js @@ -33,6 +33,27 @@ function BufferReader(data, zeroCopy) { this.stack = []; } +/** + * Assertion. + * @param {Boolean} value + */ + +BufferReader.prototype.assert = function assert(value) { + if (!value) + throw new encoding.EncodingError(this.offset, 'Out of bounds read'); +}; + +/** + * Assertion. + * @param {Boolean} value + * @param {String} reason + */ + +BufferReader.prototype.enforce = function enforce(value, reason) { + if (!value) + throw new encoding.EncodingError(this.offset, reason); +}; + /** * Get total size of passed-in Buffer. * @returns {Buffer} @@ -48,7 +69,7 @@ BufferReader.prototype.getSize = function getSize() { */ BufferReader.prototype.left = function left() { - assert(this.offset <= this.data.length); + this.assert(this.offset <= this.data.length); return this.data.length - this.offset; }; @@ -58,8 +79,8 @@ BufferReader.prototype.left = function left() { */ BufferReader.prototype.seek = function seek(off) { - assert(this.offset + off >= 0); - assert(this.offset + off <= this.data.length); + this.assert(this.offset + off >= 0); + this.assert(this.offset + off <= this.data.length); this.offset += off; return off; }; @@ -140,7 +161,7 @@ BufferReader.prototype.destroy = function destroy() { BufferReader.prototype.readU8 = function readU8() { var ret; - assert(this.offset + 1 <= this.data.length); + this.assert(this.offset + 1 <= this.data.length); ret = this.data[this.offset]; this.offset += 1; return ret; @@ -153,7 +174,7 @@ BufferReader.prototype.readU8 = function readU8() { BufferReader.prototype.readU16 = function readU16() { var ret; - assert(this.offset + 2 <= this.data.length); + this.assert(this.offset + 2 <= this.data.length); ret = this.data.readUInt16LE(this.offset, true); this.offset += 2; return ret; @@ -166,7 +187,7 @@ BufferReader.prototype.readU16 = function readU16() { BufferReader.prototype.readU16BE = function readU16BE() { var ret; - assert(this.offset + 2 <= this.data.length); + this.assert(this.offset + 2 <= this.data.length); ret = this.data.readUInt16BE(this.offset, true); this.offset += 2; return ret; @@ -179,7 +200,7 @@ BufferReader.prototype.readU16BE = function readU16BE() { BufferReader.prototype.readU32 = function readU32() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readUInt32LE(this.offset, true); this.offset += 4; return ret; @@ -192,7 +213,7 @@ BufferReader.prototype.readU32 = function readU32() { BufferReader.prototype.readU32BE = function readU32BE() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readUInt32BE(this.offset, true); this.offset += 4; return ret; @@ -206,7 +227,7 @@ BufferReader.prototype.readU32BE = function readU32BE() { BufferReader.prototype.readU64 = function readU64() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU64(this.data, this.offset); this.offset += 8; return ret; @@ -220,7 +241,7 @@ BufferReader.prototype.readU64 = function readU64() { BufferReader.prototype.readU64BE = function readU64BE() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU64BE(this.data, this.offset); this.offset += 8; return ret; @@ -234,7 +255,7 @@ BufferReader.prototype.readU64BE = function readU64BE() { BufferReader.prototype.readU53 = function readU53() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU53(this.data, this.offset); this.offset += 8; return ret; @@ -248,7 +269,7 @@ BufferReader.prototype.readU53 = function readU53() { BufferReader.prototype.readU53BE = function readU53BE() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU53BE(this.data, this.offset); this.offset += 8; return ret; @@ -261,7 +282,7 @@ BufferReader.prototype.readU53BE = function readU53BE() { BufferReader.prototype.read8 = function read8() { var ret; - assert(this.offset + 1 <= this.data.length); + this.assert(this.offset + 1 <= this.data.length); ret = this.data.readInt8(this.offset, true); this.offset += 1; return ret; @@ -274,7 +295,7 @@ BufferReader.prototype.read8 = function read8() { BufferReader.prototype.read16 = function read16() { var ret; - assert(this.offset + 2 <= this.data.length); + this.assert(this.offset + 2 <= this.data.length); ret = this.data.readInt16LE(this.offset, true); this.offset += 2; return ret; @@ -287,7 +308,7 @@ BufferReader.prototype.read16 = function read16() { BufferReader.prototype.read16BE = function read16BE() { var ret; - assert(this.offset + 2 <= this.data.length); + this.assert(this.offset + 2 <= this.data.length); ret = this.data.readInt16BE(this.offset, true); this.offset += 2; return ret; @@ -300,7 +321,7 @@ BufferReader.prototype.read16BE = function read16BE() { BufferReader.prototype.read32 = function read32() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readInt32LE(this.offset, true); this.offset += 4; return ret; @@ -313,7 +334,7 @@ BufferReader.prototype.read32 = function read32() { BufferReader.prototype.read32BE = function read32BE() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readInt32BE(this.offset, true); this.offset += 4; return ret; @@ -327,7 +348,7 @@ BufferReader.prototype.read32BE = function read32BE() { BufferReader.prototype.read64 = function read64() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read64(this.data, this.offset); this.offset += 8; return ret; @@ -341,7 +362,7 @@ BufferReader.prototype.read64 = function read64() { BufferReader.prototype.read64BE = function read64BE() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read64BE(this.data, this.offset); this.offset += 8; return ret; @@ -355,7 +376,7 @@ BufferReader.prototype.read64BE = function read64BE() { BufferReader.prototype.read53 = function read53() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read53(this.data, this.offset); this.offset += 8; return ret; @@ -369,7 +390,7 @@ BufferReader.prototype.read53 = function read53() { BufferReader.prototype.read53BE = function read53BE() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read53BE(this.data, this.offset); this.offset += 8; return ret; @@ -382,7 +403,7 @@ BufferReader.prototype.read53BE = function read53BE() { BufferReader.prototype.readU64BN = function readU64BN() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU64BN(this.data, this.offset); this.offset += 8; return ret; @@ -395,7 +416,7 @@ BufferReader.prototype.readU64BN = function readU64BN() { BufferReader.prototype.readU64BEBN = function readU64BEBN() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.readU64BEBN(this.data, this.offset); this.offset += 8; return ret; @@ -408,7 +429,7 @@ BufferReader.prototype.readU64BEBN = function readU64BEBN() { BufferReader.prototype.read64BN = function read64BN() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read64BN(this.data, this.offset); this.offset += 8; return ret; @@ -421,7 +442,7 @@ BufferReader.prototype.read64BN = function read64BN() { BufferReader.prototype.read64BEBN = function read64BEBN() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = encoding.read64BEBN(this.data, this.offset); this.offset += 8; return ret; @@ -434,7 +455,7 @@ BufferReader.prototype.read64BEBN = function read64BEBN() { BufferReader.prototype.readFloat = function readFloat() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readFloatLE(this.offset, true); this.offset += 4; return ret; @@ -447,7 +468,7 @@ BufferReader.prototype.readFloat = function readFloat() { BufferReader.prototype.readFloatBE = function readFloatBE() { var ret; - assert(this.offset + 4 <= this.data.length); + this.assert(this.offset + 4 <= this.data.length); ret = this.data.readFloatBE(this.offset, true); this.offset += 4; return ret; @@ -460,7 +481,7 @@ BufferReader.prototype.readFloatBE = function readFloatBE() { BufferReader.prototype.readDouble = function readDouble() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = this.data.readDoubleLE(this.offset, true); this.offset += 8; return ret; @@ -473,7 +494,7 @@ BufferReader.prototype.readDouble = function readDouble() { BufferReader.prototype.readDoubleBE = function readDoubleBE() { var ret; - assert(this.offset + 8 <= this.data.length); + this.assert(this.offset + 8 <= this.data.length); ret = this.data.readDoubleBE(this.offset, true); this.offset += 8; return ret; @@ -497,7 +518,7 @@ BufferReader.prototype.readVarint = function readVarint() { BufferReader.prototype.skipVarint = function skipVarint() { var size = encoding.skipVarint(this.data, this.offset); - assert(this.offset + size <= this.data.length); + this.assert(this.offset + size <= this.data.length); this.offset += size; }; @@ -530,7 +551,7 @@ BufferReader.prototype.readVarint2 = function readVarint2() { BufferReader.prototype.skipVarint2 = function skipVarint2() { var size = encoding.skipVarint2(this.data, this.offset); - assert(this.offset + size <= this.data.length); + this.assert(this.offset + size <= this.data.length); this.offset += size; }; @@ -558,7 +579,7 @@ BufferReader.prototype.readBytes = function readBytes(size, zeroCopy) { var ret; assert(size >= 0); - assert(this.offset + size <= this.data.length); + this.assert(this.offset + size <= this.data.length); if (this.zeroCopy || zeroCopy) { ret = this.data.slice(this.offset, this.offset + size); @@ -594,7 +615,7 @@ BufferReader.prototype.readVarBytes = function readVarBytes(zeroCopy) { BufferReader.prototype.readString = function readString(enc, size) { var ret; assert(size >= 0); - assert(this.offset + size <= this.data.length); + this.assert(this.offset + size <= this.data.length); ret = this.data.toString(enc, this.offset, this.offset + size); this.offset += size; return ret; @@ -621,7 +642,7 @@ BufferReader.prototype.readHash = function readHash(enc) { BufferReader.prototype.readVarString = function readVarString(enc, limit) { var size = this.readVarint(); - assert(!limit || size <= limit, 'String exceeds limit.'); + this.enforce(!limit || size <= limit, 'String exceeds limit.'); return this.readString(enc, size); }; @@ -633,12 +654,12 @@ BufferReader.prototype.readVarString = function readVarString(enc, limit) { BufferReader.prototype.readNullString = function readNullString(enc) { var i, ret; - assert(this.offset + 1 <= this.data.length); + this.assert(this.offset + 1 <= this.data.length); for (i = this.offset; i < this.data.length; i++) { if (this.data[i] === 0) break; } - assert(i !== this.data.length); + this.assert(i !== this.data.length); ret = this.readString(enc, i - this.offset); this.offset = i + 1; return ret; @@ -664,7 +685,7 @@ BufferReader.prototype.createChecksum = function createChecksum() { BufferReader.prototype.verifyChecksum = function verifyChecksum() { var chk = this.createChecksum(); var checksum = this.readU32(); - assert(chk === checksum, 'Checksum mismatch.'); + this.enforce(chk === checksum, 'Checksum mismatch.'); return checksum; }; diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index 6791bd94..88e679f7 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -150,7 +150,7 @@ RPC.prototype.fundRawTransaction = co(function* fundRawTransaction(args, help) { if (!data) throw new RPCError(errs.TYPE_ERROR, 'Invalid hex string.'); - tx = fromRaw(MTX, data); + tx = MTX.fromRaw(data); if (tx.outputs.length === 0) { throw new RPCError(errs.INVALID_PARAMETER, @@ -801,7 +801,7 @@ RPC.prototype.importAddress = co(function* importAddress(args, help) { if (!script) throw new RPCError(errs.TYPE_ERROR, 'Invalid parameters.'); - script = fromRaw(Script, script); + script = Script.fromRaw(script); script = Script.fromScripthash(script.hash160()); addr = script.getAddress(); @@ -1591,8 +1591,8 @@ RPC.prototype.importPrunedFunds = co(function* importPrunedFunds(args, help) { if (!tx || !block) throw new RPCError(errs.TYPE_ERROR, 'Invalid parameter.'); - tx = fromRaw(TX, tx); - block = fromRaw(MerkleBlock, block); + tx = TX.fromRaw(tx); + block = MerkleBlock.fromRaw(block); hash = block.hash('hex'); if (!block.verify()) @@ -1676,14 +1676,6 @@ RPC.prototype.setLogLevel = co(function* setLogLevel(args, help) { * Helpers */ -function fromRaw(ctor, raw) { - try { - return ctor.fromRaw(raw); - } catch (e) { - throw new RPCError(errs.DESERIALIZATION_ERROR, 'Deserialization error.'); - } -} - function parseAddress(raw, network) { try { return Address.fromBase58(raw, network);