@@ -197,10 +197,14 @@ Address._transformBuffer = function(buffer, network, type) {
197
197
throw new TypeError('Address buffers must be exactly 21 bytes.');
198
198
}
199
199
200
- network = Networks.get(network);
200
+ var networkObj = Networks.get(network);
201
201
var bufferVersion = Address._classifyFromVersion(buffer);
202
202
203
- if (!bufferVersion.network || (network && network !== bufferVersion.network)) {
203
+ if (network && !networkObj) {
204
+ throw new TypeError('Unknown network');
205
+ }
206
+
207
+ if (!bufferVersion.network || (networkObj && networkObj !== bufferVersion.network)) {
204
208
throw new TypeError('Address has mismatched network type.');
205
209
}
206
210
@@ -1103,6 +1107,7 @@ var BufferWriter = require('../encoding/bufferwriter');
1103
1107
var Hash = require('../crypto/hash');
1104
1108
var JSUtil = require('../util/js');
1105
1109
var Transaction = require('../transaction');
1110
+ var errors = require('../errors');
1106
1111
var $ = require('../util/preconditions');
1107
1112
1108
1113
/**
@@ -1158,6 +1163,7 @@ function MerkleBlock(arg) {
1158
1163
_.extend(this,info);
1159
1164
this._flagBitsUsed = 0;
1160
1165
this._hashesUsed = 0;
1166
+
1161
1167
return this;
1162
1168
}
1163
1169
@@ -1244,26 +1250,61 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
1244
1250
return BufferUtil.equals(root, this.header.merkleRoot);
1245
1251
};
1246
1252
1253
+ /**
1254
+ * Return a list of all the txs hash that match the filter
1255
+ * @returns {Array} - txs hash that match the filter
1256
+ */
1257
+ MerkleBlock.prototype.filterdTxsHash = function filterdTxsHash() {
1258
+ $.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
1259
+ $.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
1260
+
1261
+ // Can't have more hashes than numTransactions
1262
+ if(this.hashes.length > this.numTransactions) {
1263
+ throw new errors.MerkleBlock.InvalidMerkleTree();
1264
+ }
1265
+
1266
+ // Can't have more flag bits than num hashes
1267
+ if(this.flags.length * 8 < this.hashes.length) {
1268
+ throw new errors.MerkleBlock.InvalidMerkleTree();
1269
+ }
1270
+
1271
+ // If there is only one hash the filter do not match any txs in the block
1272
+ if(this.hashes.length === 1) {
1273
+ return [];
1274
+ };
1275
+
1276
+ var height = this._calcTreeHeight();
1277
+ var opts = { hashesUsed: 0, flagBitsUsed: 0 };
1278
+ var txs = this._traverseMerkleTree(height, 0, opts, true);
1279
+ if(opts.hashesUsed !== this.hashes.length) {
1280
+ throw new errors.MerkleBlock.InvalidMerkleTree();
1281
+ }
1282
+ return txs;
1283
+ };
1284
+
1247
1285
/**
1248
1286
* Traverse a the tree in this MerkleBlock, validating it along the way
1249
1287
* Modeled after Bitcoin Core merkleblock.cpp TraverseAndExtract()
1250
1288
* @param {Number} - depth - Current height
1251
1289
* @param {Number} - pos - Current position in the tree
1252
1290
* @param {Object} - opts - Object with values that need to be mutated throughout the traversal
1291
+ * @param {Boolean} - checkForTxs - if true return opts.txs else return the Merkle Hash
1253
1292
* @param {Number} - opts.flagBitsUsed - Number of flag bits used, should start at 0
1254
1293
* @param {Number} - opts.hashesUsed - Number of hashes used, should start at 0
1255
- * @param {Array} - opts.txs - Will finish populated by transactions found during traversal
1294
+ * @param {Array} - opts.txs - Will finish populated by transactions found during traversal that match the filter
1256
1295
* @returns {Buffer|null} - Buffer containing the Merkle Hash for that height
1296
+ * @returns {Array} - transactions found during traversal that match the filter
1257
1297
* @private
1258
1298
*/
1259
- MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, pos, opts) {
1299
+ MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, pos, opts, checkForTxs ) {
1260
1300
/* jshint maxcomplexity: 12*/
1261
1301
/* jshint maxstatements: 20 */
1262
1302
1263
1303
opts = opts || {};
1264
1304
opts.txs = opts.txs || [];
1265
1305
opts.flagBitsUsed = opts.flagBitsUsed || 0;
1266
1306
opts.hashesUsed = opts.hashesUsed || 0;
1307
+ var checkForTxs = checkForTxs || false;
1267
1308
1268
1309
if(opts.flagBitsUsed > this.flags.length * 8) {
1269
1310
return null;
@@ -1284,7 +1325,11 @@ MerkleBlock.prototype._traverseMerkleTree = function traverseMerkleTree(depth, p
1284
1325
if(pos*2+1 < this._calcTreeWidth(depth-1)) {
1285
1326
right = this._traverseMerkleTree(depth-1, pos*2+1, opts);
1286
1327
}
1287
- return Hash.sha256sha256(new Buffer.concat([left, right]));
1328
+ if (checkForTxs){
1329
+ return opts.txs;
1330
+ } else {
1331
+ return Hash.sha256sha256(new Buffer.concat([left, right]));
1332
+ };
1288
1333
}
1289
1334
};
1290
1335
@@ -1367,7 +1412,7 @@ MerkleBlock.fromObject = function fromObject(obj) {
1367
1412
module.exports = MerkleBlock;
1368
1413
1369
1414
}).call(this,require("buffer").Buffer)
1370
- },{"../crypto/hash":8,"../encoding/bufferreader":14,"../encoding/bufferwriter":15,"../transaction":28,"../util/buffer":42,"../util/js":43,"../util/preconditions":44,"./blockheader":3,"buffer":47,"lodash":319}],6:[function(require,module,exports){
1415
+ },{"../crypto/hash":8,"../encoding/bufferreader":14,"../encoding/bufferwriter":15,"../errors":17,"../ transaction":28,"../util/buffer":42,"../util/js":43,"../util/preconditions":44,"./blockheader":3,"buffer":47,"lodash":319}],6:[function(require,module,exports){
1371
1416
(function (Buffer){
1372
1417
'use strict';
1373
1418
@@ -3210,6 +3255,13 @@ module.exports = [{
3210
3255
'name': 'InvalidRate',
3211
3256
'message': 'Invalid exchange rate: {0}'
3212
3257
}]
3258
+ }, {
3259
+ name: 'MerkleBlock',
3260
+ message: 'Internal Error on MerkleBlock {0}',
3261
+ errors: [{
3262
+ 'name': 'InvalidMerkleTree',
3263
+ 'message': 'This MerkleBlock contain an invalid Merkle Tree'
3264
+ }]
3213
3265
}, {
3214
3266
name: 'Transaction',
3215
3267
message: 'Internal Error on Transaction {0}',
@@ -7130,15 +7182,17 @@ var Script = function Script(from) {
7130
7182
return Script.fromAddress(from);
7131
7183
} else if (from instanceof Script) {
7132
7184
return Script.fromBuffer(from.toBuffer());
7133
- } else if (typeof from === 'string' ) {
7185
+ } else if (_.isString( from) ) {
7134
7186
return Script.fromString(from);
7135
- } else if (typeof from !== 'undefined' ) {
7187
+ } else if (_.isObject( from) && _.isArray(from.chunks) ) {
7136
7188
this.set(from);
7137
7189
}
7138
7190
};
7139
7191
7140
7192
Script.prototype.set = function(obj) {
7141
- this.chunks = obj.chunks || this.chunks;
7193
+ $.checkArgument(_.isObject(obj));
7194
+ $.checkArgument(_.isArray(obj.chunks));
7195
+ this.chunks = obj.chunks;
7142
7196
return this;
7143
7197
};
7144
7198
@@ -7322,7 +7376,21 @@ Script.prototype._chunkToString = function(chunk, type) {
7322
7376
if (!chunk.buf) {
7323
7377
// no data chunk
7324
7378
if (typeof Opcode.reverseMap[opcodenum] !== 'undefined') {
7325
- str = str + ' ' + Opcode(opcodenum).toString();
7379
+ if (asm) {
7380
+ // A few cases where the opcode name differs from reverseMap
7381
+ // aside from 1 to 16 data pushes.
7382
+ if (opcodenum === 0) {
7383
+ // OP_0 -> 0
7384
+ str = str + ' 0';
7385
+ } else if(opcodenum === 79) {
7386
+ // OP_1NEGATE -> 1
7387
+ str = str + ' -1';
7388
+ } else {
7389
+ str = str + ' ' + Opcode(opcodenum).toString();
7390
+ }
7391
+ } else {
7392
+ str = str + ' ' + Opcode(opcodenum).toString();
7393
+ }
7326
7394
} else {
7327
7395
var numstr = opcodenum.toString(16);
7328
7396
if (numstr.length % 2 !== 0) {
@@ -7336,7 +7404,7 @@ Script.prototype._chunkToString = function(chunk, type) {
7336
7404
}
7337
7405
} else {
7338
7406
// data chunk
7339
- if (opcodenum === Opcode.OP_PUSHDATA1 ||
7407
+ if (!asm && opcodenum === Opcode.OP_PUSHDATA1 ||
7340
7408
opcodenum === Opcode.OP_PUSHDATA2 ||
7341
7409
opcodenum === Opcode.OP_PUSHDATA4) {
7342
7410
str = str + ' ' + Opcode(opcodenum).toString();
@@ -9762,13 +9830,13 @@ Transaction.prototype.fromObject = function fromObject(arg) {
9762
9830
9763
9831
Transaction.prototype._checkConsistency = function(arg) {
9764
9832
if (!_.isUndefined(this._changeIndex)) {
9765
- $.checkState(this._changeScript);
9766
- $.checkState(this.outputs[this._changeIndex]);
9833
+ $.checkState(this._changeScript, 'Change script is expected.' );
9834
+ $.checkState(this.outputs[this._changeIndex], 'Change index points to undefined output.' );
9767
9835
$.checkState(this.outputs[this._changeIndex].script.toString() ===
9768
- this._changeScript.toString());
9836
+ this._changeScript.toString(), 'Change output has an unexpected script.' );
9769
9837
}
9770
9838
if (arg && arg.hash) {
9771
- $.checkState(arg.hash === this.hash, 'Hash in object does not match transaction hash');
9839
+ $.checkState(arg.hash === this.hash, 'Hash in object does not match transaction hash. ');
9772
9840
}
9773
9841
};
9774
9842
@@ -10419,7 +10487,7 @@ Transaction.prototype.removeInput = function(txId, outputIndex) {
10419
10487
* @return {Transaction} this, for chaining
10420
10488
*/
10421
10489
Transaction.prototype.sign = function(privateKey, sigtype) {
10422
- $.checkState(this.hasAllUtxoInfo());
10490
+ $.checkState(this.hasAllUtxoInfo(), 'Not all utxo information is available to sign the transaction.' );
10423
10491
var self = this;
10424
10492
if (_.isArray(privateKey)) {
10425
10493
_.each(privateKey, function(privateKey) {
@@ -53953,7 +54021,7 @@ arguments[4][262][0].apply(exports,arguments)
53953
54021
},{}],320:[function(require,module,exports){
53954
54022
module.exports={
53955
54023
"name": "bitcore-lib",
53956
- "version": "5.0.0-beta.1 ",
54024
+ "version": "0.15.0 ",
53957
54025
"description": "A pure and powerful JavaScript Bitcoin library.",
53958
54026
"author": "BitPay <
[email protected] >",
53959
54027
"main": "index.js",
@@ -53988,6 +54056,7 @@ module.exports={
53988
54056
},
53989
54057
"dependencies": {
53990
54058
"bn.js": "=4.11.8",
54059
+ "bs58": "=4.0.1",
53991
54060
"buffer-compare": "=1.1.1",
53992
54061
"elliptic": "=6.4.0",
53993
54062
"inherits": "=2.0.1",
@@ -54004,13 +54073,23 @@ module.exports={
54004
54073
}
54005
54074
54006
54075
},{}],"bitcore-lib":[function(require,module,exports){
54007
- (function (Buffer){
54076
+ (function (global, Buffer){
54008
54077
'use strict';
54009
54078
54010
54079
var bitcore = module.exports;
54011
54080
54012
54081
// module information
54013
54082
bitcore.version = 'v' + require('./package.json').version;
54083
+ bitcore.versionGuard = function(version) {
54084
+ if (version !== undefined) {
54085
+ var message = 'More than one instance of bitcore-lib found. ' +
54086
+ 'Please make sure to require bitcore-lib and check that submodules do' +
54087
+ ' not also include their own bitcore-lib dependency.';
54088
+ throw new Error(message);
54089
+ }
54090
+ };
54091
+ bitcore.versionGuard(global._bitcore);
54092
+ global._bitcore = bitcore.version;
54014
54093
54015
54094
// crypto
54016
54095
bitcore.crypto = {};
@@ -54065,5 +54144,5 @@ bitcore.deps._ = require('lodash');
54065
54144
// Internal usage, exposed for testing/advanced tweaking
54066
54145
bitcore.Transaction.sighash = require('./lib/transaction/sighash');
54067
54146
54068
- }).call(this,require("buffer").Buffer)
54147
+ }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}, require("buffer").Buffer)
54069
54148
},{"./lib/address":1,"./lib/block":4,"./lib/block/blockheader":3,"./lib/block/merkleblock":5,"./lib/crypto/bn":6,"./lib/crypto/ecdsa":7,"./lib/crypto/hash":8,"./lib/crypto/point":9,"./lib/crypto/random":10,"./lib/crypto/signature":11,"./lib/encoding/base58":12,"./lib/encoding/base58check":13,"./lib/encoding/bufferreader":14,"./lib/encoding/bufferwriter":15,"./lib/encoding/varint":16,"./lib/errors":17,"./lib/hdprivatekey.js":19,"./lib/hdpublickey.js":20,"./lib/networks":21,"./lib/opcode":22,"./lib/privatekey":23,"./lib/publickey":24,"./lib/script":25,"./lib/transaction":28,"./lib/transaction/sighash":36,"./lib/unit":40,"./lib/uri":41,"./lib/util/buffer":42,"./lib/util/js":43,"./lib/util/preconditions":44,"./package.json":320,"bn.js":280,"bs58":281,"buffer":47,"elliptic":285,"lodash":319}]},{},[]);
0 commit comments