diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 16f30b1e..02432202 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -113,7 +113,7 @@ Block.prototype.getRaw = function getRaw() { }; Block.prototype._getSize = function _getSize() { - var sizes = bcoin.protocol.framer.block._sizes(this); + var sizes = bcoin.protocol.framer.block.sizes(this); this._size = sizes.size; this._witnessSize = sizes.witnessSize; }; diff --git a/lib/bcoin/bloom.js b/lib/bcoin/bloom.js index 0eaa425b..79d796d5 100644 --- a/lib/bcoin/bloom.js +++ b/lib/bcoin/bloom.js @@ -28,6 +28,7 @@ function Bloom(size, n, tweak) { this.size = size; this.n = n; this.tweak = tweak; + this.update = null; this.reset(); } diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 8492466e..5a6dfb56 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -2314,7 +2314,7 @@ Chain.prototype.checkFinal = function checkFinal(prev, tx, flags, callback, forc if (err) return callback(err); - return callback(null, tx.isFinal(ts, height)); + return callback(null, tx.isFinal(height, ts)); } if (flags & constants.flags.MEDIAN_TIME_PAST) diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 3bf75847..217c4397 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -225,28 +225,6 @@ Coin.fromRaw = function fromRaw(data, enc) { return new Coin(Coin._fromRaw(data, enc)); }; -Coin.prototype.toUTXO = function toUTXO(enc) { - var data = bcoin.protocol.framer.utxo(this); - - if (enc === 'hex') - data = utils.toHex(data); - - return data; -}; - -Coin._fromUTXO = function _fromUTXO(data, enc) { - if (enc === 'hex') - data = new Buffer(data, 'hex'); - - data = bcoin.protocol.parser.parseUTXO(data); - - return data; -}; - -Coin.fromUTXO = function fromUTXO(data, enc) { - return new Coin(Coin._fromUTXO(data, enc)); -}; - /** * Serialize the coin to an "extended" format, * including both the hash and the index. diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index 79253e18..96ea81b3 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -115,9 +115,9 @@ Coins.prototype.remove = function remove(index) { * @returns {Coin} */ -Coins.prototype.spend = function spend(hash, index) { - var coin = this.get(hash, index); - this.remove(hash, index); +Coins.prototype.spend = function spend(index) { + var coin = this.get(index); + this.remove(index); return coin; }; @@ -169,7 +169,7 @@ Coins.prototype.fill = function fill(tx, spend) { * @returns {Number} Total. */ -Coins.prototype.count = function count(index) { +Coins.prototype.count = function count() { return this.outputs.reduce(function(total, output) { if (!output) return total; @@ -177,10 +177,13 @@ Coins.prototype.count = function count(index) { }, 0); }; -Coins.prototype.forEach = function forEach(callback, context) { - this.outputs.forEach(function(output, i) { - callback.call(context || this, output, i); - }, this); +/** + * Convert collection to an array. + * @returns {Coin[]} + */ + +Coins.prototype.toArray = function toArray() { + return this.outputs.filter(Boolean); }; /** diff --git a/lib/bcoin/coinview.js b/lib/bcoin/coinview.js index 13b7f6af..05757680 100644 --- a/lib/bcoin/coinview.js +++ b/lib/bcoin/coinview.js @@ -91,7 +91,7 @@ CoinView.prototype.count = function count(hash) { CoinView.prototype.has = function has(hash, index) { if (!this.coins[hash]) - return; + return false; return this.coins[hash].has(index); }; diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index f305eb40..60a0e80f 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -196,7 +196,9 @@ function Environment(options) { this.coins = require('./coins')(this); this.coinview = require('./coinview')(this); this.tx = require('./tx')(this); + this.transaction = this.tx; this.mtx = require('./mtx')(this); + this.mutabletransaction = this.mtx; this.txdb = require('./txdb')(this); this.abstractblock = require('./abstractblock')(this); this.compactblock = require('./compactblock')(this); diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index 4617fd40..261e9abd 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -110,7 +110,7 @@ Fullnode.prototype._init = function _init() { key: this.options.sslKey, cert: this.options.sslCert, port: this.options.httpPort || 8080, - host: '0.0.0.0' + host: this.options.httpHost || '0.0.0.0' }); // Bind to errors diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 05d816cd..479c5908 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -855,7 +855,7 @@ HDPrivateKey.fromJSON = function fromJSON(json, passphrase) { * @returns {Boolean} */ -HDPublicKey.isHDPrivateKey = function isHDPrivateKey(obj) { +HDPrivateKey.isHDPrivateKey = function isHDPrivateKey(obj) { return obj && obj.isPrivate && typeof obj.derive === 'function'; }; diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index 41d3ea47..3922256a 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -79,7 +79,7 @@ Headers.prototype.getSize = function getSize() { Headers.prototype.getRaw = function getRaw() { if (!this._raw) { - this._raw = bcoin.protocol.framer.headers(this); + this._raw = bcoin.protocol.framer.headers([this]); this._size = this._raw.length; } return this._raw; @@ -95,7 +95,6 @@ Headers.prototype.inspect = function inspect() { var copy = bcoin.headers(this); copy.__proto__ = null; delete copy._raw; - delete copy._chain; copy.hash = this.hash('hex'); copy.rhash = this.rhash; copy.date = new Date((copy.ts || 0) * 1000).toISOString(); @@ -131,7 +130,7 @@ Headers._fromRaw = function _fromRaw(data, enc) { if (enc === 'hex') data = new Buffer(data, 'hex'); - return bcoin.protocol.parser.parseHeaders(data); + return bcoin.protocol.parser.parseHeaders(data)[0]; }; /** diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index ab811280..66f89646 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -335,7 +335,7 @@ Client.prototype.getWallet = function getWallet(id, passphrase, callback) { return callback(e); } - json.provider = new bcoin.http.provider(self.url); + json.provider = new bcoin.http.provider(self.uri); return callback(null, new bcoin.wallet(json)); }); @@ -361,7 +361,7 @@ Client.prototype.createWallet = function createWallet(options, callback) { return callback(e); } - json.provider = new bcoin.http.provider(self.url); + json.provider = new bcoin.http.provider(self.uri); return callback(null, new bcoin.wallet(json)); }); @@ -435,7 +435,7 @@ Client.prototype.getWalletPending = function getPending(id, callback) { try { body = body.map(function(data) { - return bcoin.coin.fromJSON(data); + return bcoin.tx.fromJSON(data); }); } catch (e) { return callback(e); @@ -474,7 +474,7 @@ Client.prototype.getWalletBalance = function getBalance(id, callback) { * @param {Function} callback - Returns [Error, {@link TX}[]]. */ -Client.prototype.getWalletLast = function getLast(id, limit, callback) { +Client.prototype.getWalletLast = function getWalletLast(id, limit, callback) { var options = { limit: limit }; return this._get('/wallet/' + id + '/tx/last', options, function(err, body) { if (err) diff --git a/lib/bcoin/http/provider.js b/lib/bcoin/http/provider.js index 7d9de7f8..27ef873f 100644 --- a/lib/bcoin/http/provider.js +++ b/lib/bcoin/http/provider.js @@ -20,14 +20,14 @@ var Client = bcoin.http.client; * @param {String} uri */ -function Provider(url) { +function Provider(uri) { if (!(this instanceof Provider)) - return new Provider(url); + return new Provider(uri); EventEmitter.call(this); - this.client = new Client(url); - this.url = url; + this.client = new Client(uri); + this.uri = uri; this.id = null; this._init(); } diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js index 27851e7d..ddd83381 100644 --- a/lib/bcoin/keypair.js +++ b/lib/bcoin/keypair.js @@ -169,7 +169,7 @@ KeyPair._fromSecret = function _fromSecret(secret) { privateKey = p.readBytes(32); - if (p.left() > 1) { + if (p.left() > 4) { assert(p.readU8() === 1); compressed = true; } diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index 7aba09a6..3ed3c79e 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -80,7 +80,6 @@ function Mempool(options) { this.relayPriority = this.options.relayPriority !== false; this.requireStandard = this.options.requireStandard !== false; this.rejectInsaneFees = this.options.rejectInsaneFees !== false; - this.relay = this.options.relay || false; // Use an in-memory binary search tree by default this.backend = this.options.memory === false ? 'leveldb' : 'memory'; diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index d2c41b2e..4fb86457 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -32,7 +32,7 @@ function Output(options, tx) { value = options.value; if (typeof value === 'number') { - assert(value % 1 === 0); + assert(value % 1 === 0, 'Output value cannot be a float.'); value = new bn(value); } diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 1ff0e6a0..921b6f76 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -296,10 +296,6 @@ Framer.prototype.getBlocks = function getBlocks(data) { return this.packet('getblocks', Framer.getBlocks(data)); }; -Framer.prototype.utxo = function _coin(coin) { - return this.packet('utxo', Framer.coin(coin, false)); -}; - /** * Create a tx packet with a header. * @param {TX} tx - See {@link Framer.tx}. @@ -356,8 +352,8 @@ Framer.prototype.merkleBlock = function merkleBlock(block) { * @returns {Buffer} headers packet. */ -Framer.prototype.headers = function headers(block) { - return this.packet('headers', Framer.headers(block)); +Framer.prototype.headers = function _headers(headers) { + return this.packet('headers', Framer.headers(headers)); }; /** @@ -640,26 +636,6 @@ Framer._getBlocks = function _getBlocks(data, writer, headers) { return p; }; -Framer.utxo = function _utxo(coin, writer) { - var p = new BufferWriter(writer); - var height = coin.height; - - if (height === -1) - height = 0x7fffffff; - - assert(coin.value.byteLength() <= 8); - - p.writeU32(coin.version); - p.writeU32(height); - p.write64(coin.value); - Framer.script(coin.script, p); - - if (!writer) - p = p.render(); - - return p; -}; - /** * Serialize a coin. * @param {NakedCoin|Coin} coin @@ -1361,7 +1337,7 @@ Framer.filterClear = function filterClear() { * @returns {Object} In the form of `{size: Number, witnessSize: Number}`. */ -Framer.block._sizes = function blockSizes(block) { +Framer.block.sizes = function blockSizes(block) { var writer = new BufferWriter(); Framer.witnessBlock(block, writer); return { @@ -1377,7 +1353,7 @@ Framer.block._sizes = function blockSizes(block) { * @returns {Object} In the form of `{size: Number, witnessSize: Number}`. */ -Framer.tx._sizes = function txSizes(tx) { +Framer.tx.sizes = function txSizes(tx) { var writer = new BufferWriter(); Framer.renderTX(tx, true, writer); return { @@ -1393,7 +1369,7 @@ Framer.tx._sizes = function txSizes(tx) { */ Framer.block.witnessSize = function blockWitnessSize(block) { - return Framer.block._sizes(block).size; + return Framer.block.sizes(block).size; }; /** @@ -1403,7 +1379,7 @@ Framer.block.witnessSize = function blockWitnessSize(block) { */ Framer.tx.witnessSize = function txWitnessSize(tx) { - return Framer.tx._sizes(tx).size; + return Framer.tx.sizes(tx).size; }; /** @@ -1437,7 +1413,7 @@ Framer.tx.size = function txSize(tx) { */ Framer.block.virtualSize = function blockVirtualSize(block) { - var sizes = Framer.block._sizes(block); + var sizes = Framer.block.sizes(block); var base = sizes.size - sizes.witnessSize; return (base * 4 + sizes.witnessSize + 3) / 4 | 0; }; @@ -1449,7 +1425,7 @@ Framer.block.virtualSize = function blockVirtualSize(block) { */ Framer.tx.virtualSize = function txVirtualSize(tx) { - var sizes = Framer.tx._sizes(tx); + var sizes = Framer.tx.sizes(tx); var base = sizes.size - sizes.witnessSize; return (base * 4 + sizes.witnessSize + 3) / 4 | 0; }; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 4cbff4f2..56ec359b 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -33,7 +33,6 @@ function Parser() { this.pendingTotal = 0; this.waiting = 24; this.packet = null; - this.version = constants.minVersion; } utils.inherits(Parser, EventEmitter); @@ -1032,29 +1031,6 @@ Parser.parseOutput = function parseOutput(p) { }; }; -Parser.parseUTXO = function parseUTXO(p) { - var version, height, value, script; - - p = new BufferReader(p); - p.start(); - - version = p.readU32(); - height = p.readU32(); - value = p.read64(); - script = Parser.parseScript(p); - - if (height === 0x7fffffff) - height = -1; - - return { - version: version, - height: height, - value: value, - script: script, - _size: p.end() - }; -}; - /** * @typedef {Object} NakedCoin * @global diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index c97d6dd2..5d92aab8 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -825,7 +825,7 @@ Script.prototype.encode = function encode() { */ Script.prototype.getSubscript = function getSubscript(lastSep) { - var res = []; + var code = []; var i; if (lastSep == null) @@ -835,18 +835,18 @@ Script.prototype.getSubscript = function getSubscript(lastSep) { for (i = lastSep + 1; i < this.code.length; i++) { if (this.code[i] !== opcodes.OP_CODESEPARATOR) - res.push(this.code[i]); + code.push(this.code[i]); } // Optimization: avoid re-rendering // of the script in 99.9% of cases. - if (res.length === this.code.length) { - res = this.clone(); - res.raw = this.raw; - return res; + if (code.length === this.code.length) { + code = this.clone(); + code.raw = this.raw; + return code; } - return new Script(res); + return new Script(code); }; Script.prototype._next = function _next(to, code, ip) { @@ -1657,6 +1657,8 @@ Script.array = function(value) { Script.prototype.removeData = function removeData(data) { for (var i = this.code.length - 1; i >= 0; i--) { + if (!Buffer.isBuffer(this.code[i])) + continue; if (utils.isEqual(this.code[i], data)) this.code.splice(i, 1); } @@ -3452,9 +3454,7 @@ Script.sign = function sign(msg, key, type) { // Add the sighash type as a single byte // to the signature. - sig = Buffer.concat([sig, new Buffer([type])]); - - return sig; + return Buffer.concat([sig, new Buffer([type])]); }; /** diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index bde0eff6..3c381556 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -277,7 +277,7 @@ TX.prototype.getRaw = function getRaw() { else raw = bcoin.protocol.framer.tx(this); - // this._raw = raw; + this._raw = raw; this._size = raw.length; this._witnessSize = raw._witnessSize; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index b5271459..b01eba71 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -776,11 +776,11 @@ Wallet.prototype.fillCoins = function fillCoins(tx, callback) { * @param {Function} callback - Returns [Error, {@link Coin}]. */ -Wallet.prototype.getCoin = function getCoin(id, callback) { +Wallet.prototype.getCoin = function getCoin(hash, index, callback) { if (!this.provider) return callback(new Error('No wallet provider available.')); - return this.provider.getCoin(id, callback); + return this.provider.getCoin(hash, index, callback); }; /** diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index fb55c9f1..bb5f51ae 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -1162,8 +1162,8 @@ Provider.prototype.update = function update(wallet, address) { * @param {Function} callback */ -Provider.prototype.zap = function zap(wallet, address) { - return this.db.zapWallet(wallet, address); +Provider.prototype.zap = function zap(now, age, callback) { + return this.db.zapWallet(this.id, now, age, callback); }; WalletDB.Provider = Provider;