{"action":"created","comment":{"url":"https://api.github.com/repos/bitpay/bitcore/pulls/comments/25176958","id":25176958,"diff_hunk":"@@ -0,0 +1,280 @@\n+'use strict';\n+var _ = require('lodash');\n+var BlockHeader = require('./blockheader');\n+var BufferUtil = require('../util/buffer');\n+var BufferReader = require('../encoding/bufferreader');\n+var BufferWriter = require('../encoding/bufferwriter');\n+var Hash = require('../crypto/hash');\n+var JSUtil = require('../util/js');\n+var Transaction = require('../transaction');\n+var $ = require('../util/preconditions');\n+\n+/**\n+ * Instantiate a MerkleBlock from a Buffer, JSON object, or Object with\n+ * the properties of the Block\n+ *\n+ * @param {*} - A Buffer, JSON string, or Object representing a MerkleBlock\n+ * @returns {MerkleBlock}\n+ * @constructor\n+ */\n+function MerkleBlock(arg) {\n+  if (!(this instanceof MerkleBlock)) {\n+    return new MerkleBlock(arg);\n+  }\n+\n+  var info = {};\n+  if (BufferUtil.isBuffer(arg)) {\n+    info = MerkleBlock._fromBufferReader(BufferReader(arg));\n+  } else if (JSUtil.isValidJSON(arg)) {\n+    info = MerkleBlock._fromJSON(arg);\n+  } else if (_.isObject(arg)) {\n+    var header;\n+    if(arg.header instanceof BlockHeader) {\n+      header = arg.header\n+    } else {\n+      header = BlockHeader.fromJSON(JSON.stringify(arg.header));\n+    }\n+    info = {\n+      /**\n+       * @name MerkleBlock#header\n+       * @type {BlockHeader}\n+       */\n+      header: header,\n+      /**\n+       * @name MerkleBlock#numTransactions\n+       * @type {Number}\n+       */\n+      numTransactions: arg.numTransactions,\n+      /**\n+       * @name MerkleBlock#hashes\n+       * @type {String[]}\n+       */\n+      hashes: arg.hashes,\n+      /**\n+       * @name MerkleBlock#flags\n+       * @type {Number[]}\n+       */\n+      flags: arg.flags\n+    };\n+  } else {\n+    throw new TypeError('Unrecognized argument for Block');\n+  }\n+  _.extend(this,info);\n+  this._validMerkleTree = null;\n+  return this;\n+}\n+\n+/**\n+ * @param {Buffer} - MerkleBlock data in a Buffer object\n+ * @returns {MerkleBlock} - A MerkleBlock object\n+ */\n+MerkleBlock.fromBuffer = function fromBuffer(buf) {\n+  return MerkleBlock.fromBufferReader(BufferReader(buf));\n+}\n+\n+/**\n+ * @param {BufferReader} - MerkleBlock data in a BufferReader object\n+ * @returns {MerkleBlock} - A MerkleBlock object\n+ */\n+MerkleBlock.fromBufferReader = function fromBufferReader(br) {\n+  return new MerkleBlock(MerkleBlock._fromBufferReader(br));\n+}\n+\n+/**\n+ * @param {String|Object} - A JSON String or Object\n+ * @returns {MerkleBlock} - A MerkleBlock object\n+ */\n+MerkleBlock.fromJSON = function fromJSON(buf) {\n+  return new MerkleBlock(MerkleBlock._fromJSON(buf));\n+}\n+\n+/**\n+ * @returns {Buffer} - A buffer of the block\n+ */\n+MerkleBlock.prototype.toBuffer = function toBuffer() {\n+  return this.toBufferWriter().concat();\n+};\n+\n+/**\n+ * @param {BufferWriter} - An existing instance of BufferWriter\n+ * @returns {BufferWriter} - An instance of BufferWriter representation of the MerkleBlock\n+ */\n+MerkleBlock.prototype.toBufferWriter = function toBufferWriter(bw) {\n+  if (!bw) {\n+    bw = new BufferWriter();\n+  }\n+  bw.write(this.header.toBuffer());\n+  bw.writeUInt32LE(this.numTransactions);\n+  bw.writeVarintNum(this.hashes.length);\n+  for (var i = 0; i < this.hashes.length; i++) {\n+    bw.write(new Buffer(this.hashes[i], 'hex'));\n+  }\n+  bw.writeVarintNum(this.flags.length);\n+  for (i = 0; i < this.flags.length; i++) {\n+    bw.writeUInt8(this.flags[i]);\n+  }\n+  return bw;\n+};\n+\n+/**\n+ * @returns {Object} - A plain object with the MerkleBlock properties\n+ */\n+MerkleBlock.prototype.toObject = function toObject() {\n+  return {\n+    header: this.header.toObject(),\n+    numTransactions: this.numTransactions,\n+    hashes: this.hashes,\n+    flags: this.flags\n+  };\n+};\n+\n+/**\n+ * @returns {String} - A JSON string of a MerkleBlock\n+ */\n+MerkleBlock.prototype.toJSON = function toJSON() {\n+  return JSON.stringify(this.toObject());\n+};\n+\n+/**\n+ * Verify that the MerkleBlock is valid\n+ * @returns {Bool} - True/False whether this MerkleBlock is Valid\n+ */\n+MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {\n+  $.checkState(this.flags instanceof Array, 'MerkleBlock flags is not an array');\n+  $.checkState(this.hashes instanceof Array, 'MerkleBlock flags is not an array');\n+  var self = this;\n+  if(this._validMerkleTree === true) {\n+    return true;\n+  } else if (this._validMerkleTree === false) {\n+    return false;\n+  }","path":"lib/block/merkleblock.js","position":150,"original_position":150,"commit_id":"a67084d1768cfb257ebba0df60a31c3b98f0b33f","original_commit_id":"a67084d1768cfb257ebba0df60a31c3b98f0b33f","user":{"login":"throughnothing","id":61443,"avatar_url":"https://avatars.githubusercontent.com/u/61443?v=3","gravatar_id":"","url":"https://api.github.com/users/throughnothing","html_url":"https://github.com/throughnothing","followers_url":"https://api.github.com/users/throughnothing/followers","following_url":"https://api.github.com/users/throughnothing/following{/other_user}","gists_url":"https://api.github.com/users/throughnothing/gists{/gist_id}","starred_url":"https://api.github.com/users/throughnothing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/throughnothing/subscriptions","organizations_url":"https://api.github.com/users/throughnothing/orgs","repos_url":"https://api.github.com/users/throughnothing/repos","events_url":"https://api.github.com/users/throughnothing/events{/privacy}","received_events_url":"https://api.github.com/users/throughnothing/received_events","type":"User","site_admin":false},"body":"@braydonf I don't think the hashes would change in a single instance.  Why/how would they?","created_at":"2015-02-23T16:23:01Z","updated_at":"2015-02-23T16:23:01Z","html_url":"https://github.com/bitpay/bitcore/pull/1102#discussion_r25176958","pull_request_url":"https://api.github.com/repos/bitpay/bitcore/pulls/1102","_links":{"self":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/comments/25176958"},"html":{"href":"https://github.com/bitpay/bitcore/pull/1102#discussion_r25176958"},"pull_request":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/1102"}}},"pull_request":{"url":"https://api.github.com/repos/bitpay/bitcore/pulls/1102","id":29678027,"html_url":"https://github.com/bitpay/bitcore/pull/1102","diff_url":"https://github.com/bitpay/bitcore/pull/1102.diff","patch_url":"https://github.com/bitpay/bitcore/pull/1102.patch","issue_url":"https://api.github.com/repos/bitpay/bitcore/issues/1102","number":1102,"state":"open","locked":false,"title":"Add MerkleBlock","user":{"login":"throughnothing","id":61443,"avatar_url":"https://avatars.githubusercontent.com/u/61443?v=3","gravatar_id":"","url":"https://api.github.com/users/throughnothing","html_url":"https://github.com/throughnothing","followers_url":"https://api.github.com/users/throughnothing/followers","following_url":"https://api.github.com/users/throughnothing/following{/other_user}","gists_url":"https://api.github.com/users/throughnothing/gists{/gist_id}","starred_url":"https://api.github.com/users/throughnothing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/throughnothing/subscriptions","organizations_url":"https://api.github.com/users/throughnothing/orgs","repos_url":"https://api.github.com/users/throughnothing/repos","events_url":"https://api.github.com/users/throughnothing/events{/privacy}","received_events_url":"https://api.github.com/users/throughnothing/received_events","type":"User","site_admin":false},"body":"Relates to #1082","created_at":"2015-02-20T07:45:33Z","updated_at":"2015-02-23T16:23:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"de6032bcbcadb9813f86dfbe465a892a42edbece","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bitpay/bitcore/pulls/1102/commits","review_comments_url":"https://api.github.com/repos/bitpay/bitcore/pulls/1102/comments","review_comment_url":"https://api.github.com/repos/bitpay/bitcore/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bitpay/bitcore/issues/1102/comments","statuses_url":"https://api.github.com/repos/bitpay/bitcore/statuses/a67084d1768cfb257ebba0df60a31c3b98f0b33f","head":{"label":"throughnothing:merkleblock","ref":"merkleblock","sha":"a67084d1768cfb257ebba0df60a31c3b98f0b33f","user":{"login":"throughnothing","id":61443,"avatar_url":"https://avatars.githubusercontent.com/u/61443?v=3","gravatar_id":"","url":"https://api.github.com/users/throughnothing","html_url":"https://github.com/throughnothing","followers_url":"https://api.github.com/users/throughnothing/followers","following_url":"https://api.github.com/users/throughnothing/following{/other_user}","gists_url":"https://api.github.com/users/throughnothing/gists{/gist_id}","starred_url":"https://api.github.com/users/throughnothing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/throughnothing/subscriptions","organizations_url":"https://api.github.com/users/throughnothing/orgs","repos_url":"https://api.github.com/users/throughnothing/repos","events_url":"https://api.github.com/users/throughnothing/events{/privacy}","received_events_url":"https://api.github.com/users/throughnothing/received_events","type":"User","site_admin":false},"repo":{"id":31050932,"name":"bitcore","full_name":"throughnothing/bitcore","owner":{"login":"throughnothing","id":61443,"avatar_url":"https://avatars.githubusercontent.com/u/61443?v=3","gravatar_id":"","url":"https://api.github.com/users/throughnothing","html_url":"https://github.com/throughnothing","followers_url":"https://api.github.com/users/throughnothing/followers","following_url":"https://api.github.com/users/throughnothing/following{/other_user}","gists_url":"https://api.github.com/users/throughnothing/gists{/gist_id}","starred_url":"https://api.github.com/users/throughnothing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/throughnothing/subscriptions","organizations_url":"https://api.github.com/users/throughnothing/orgs","repos_url":"https://api.github.com/users/throughnothing/repos","events_url":"https://api.github.com/users/throughnothing/events{/privacy}","received_events_url":"https://api.github.com/users/throughnothing/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/throughnothing/bitcore","description":"A pure and powerful JavaScript Bitcoin library","fork":true,"url":"https://api.github.com/repos/throughnothing/bitcore","forks_url":"https://api.github.com/repos/throughnothing/bitcore/forks","keys_url":"https://api.github.com/repos/throughnothing/bitcore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/throughnothing/bitcore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/throughnothing/bitcore/teams","hooks_url":"https://api.github.com/repos/throughnothing/bitcore/hooks","issue_events_url":"https://api.github.com/repos/throughnothing/bitcore/issues/events{/number}","events_url":"https://api.github.com/repos/throughnothing/bitcore/events","assignees_url":"https://api.github.com/repos/throughnothing/bitcore/assignees{/user}","branches_url":"https://api.github.com/repos/throughnothing/bitcore/branches{/branch}","tags_url":"https://api.github.com/repos/throughnothing/bitcore/tags","blobs_url":"https://api.github.com/repos/throughnothing/bitcore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/throughnothing/bitcore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/throughnothing/bitcore/git/refs{/sha}","trees_url":"https://api.github.com/repos/throughnothing/bitcore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/throughnothing/bitcore/statuses/{sha}","languages_url":"https://api.github.com/repos/throughnothing/bitcore/languages","stargazers_url":"https://api.github.com/repos/throughnothing/bitcore/stargazers","contributors_url":"https://api.github.com/repos/throughnothing/bitcore/contributors","subscribers_url":"https://api.github.com/repos/throughnothing/bitcore/subscribers","subscription_url":"https://api.github.com/repos/throughnothing/bitcore/subscription","commits_url":"https://api.github.com/repos/throughnothing/bitcore/commits{/sha}","git_commits_url":"https://api.github.com/repos/throughnothing/bitcore/git/commits{/sha}","comments_url":"https://api.github.com/repos/throughnothing/bitcore/comments{/number}","issue_comment_url":"https://api.github.com/repos/throughnothing/bitcore/issues/comments{/number}","contents_url":"https://api.github.com/repos/throughnothing/bitcore/contents/{+path}","compare_url":"https://api.github.com/repos/throughnothing/bitcore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/throughnothing/bitcore/merges","archive_url":"https://api.github.com/repos/throughnothing/bitcore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/throughnothing/bitcore/downloads","issues_url":"https://api.github.com/repos/throughnothing/bitcore/issues{/number}","pulls_url":"https://api.github.com/repos/throughnothing/bitcore/pulls{/number}","milestones_url":"https://api.github.com/repos/throughnothing/bitcore/milestones{/number}","notifications_url":"https://api.github.com/repos/throughnothing/bitcore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/throughnothing/bitcore/labels{/name}","releases_url":"https://api.github.com/repos/throughnothing/bitcore/releases{/id}","created_at":"2015-02-20T04:27:28Z","updated_at":"2015-02-20T04:27:30Z","pushed_at":"2015-02-23T09:06:35Z","git_url":"git://github.com/throughnothing/bitcore.git","ssh_url":"git@github.com:throughnothing/bitcore.git","clone_url":"https://github.com/throughnothing/bitcore.git","svn_url":"https://github.com/throughnothing/bitcore","homepage":"","size":10385,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"bitpay:master","ref":"master","sha":"a9feb2b8696631bdb071bde1598cf79648d257f5","user":{"login":"bitpay","id":2554930,"avatar_url":"https://avatars.githubusercontent.com/u/2554930?v=3","gravatar_id":"","url":"https://api.github.com/users/bitpay","html_url":"https://github.com/bitpay","followers_url":"https://api.github.com/users/bitpay/followers","following_url":"https://api.github.com/users/bitpay/following{/other_user}","gists_url":"https://api.github.com/users/bitpay/gists{/gist_id}","starred_url":"https://api.github.com/users/bitpay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitpay/subscriptions","organizations_url":"https://api.github.com/users/bitpay/orgs","repos_url":"https://api.github.com/users/bitpay/repos","events_url":"https://api.github.com/users/bitpay/events{/privacy}","received_events_url":"https://api.github.com/users/bitpay/received_events","type":"Organization","site_admin":false},"repo":{"id":15259244,"name":"bitcore","full_name":"bitpay/bitcore","owner":{"login":"bitpay","id":2554930,"avatar_url":"https://avatars.githubusercontent.com/u/2554930?v=3","gravatar_id":"","url":"https://api.github.com/users/bitpay","html_url":"https://github.com/bitpay","followers_url":"https://api.github.com/users/bitpay/followers","following_url":"https://api.github.com/users/bitpay/following{/other_user}","gists_url":"https://api.github.com/users/bitpay/gists{/gist_id}","starred_url":"https://api.github.com/users/bitpay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitpay/subscriptions","organizations_url":"https://api.github.com/users/bitpay/orgs","repos_url":"https://api.github.com/users/bitpay/repos","events_url":"https://api.github.com/users/bitpay/events{/privacy}","received_events_url":"https://api.github.com/users/bitpay/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/bitpay/bitcore","description":"A pure and powerful JavaScript Bitcoin library","fork":false,"url":"https://api.github.com/repos/bitpay/bitcore","forks_url":"https://api.github.com/repos/bitpay/bitcore/forks","keys_url":"https://api.github.com/repos/bitpay/bitcore/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bitpay/bitcore/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bitpay/bitcore/teams","hooks_url":"https://api.github.com/repos/bitpay/bitcore/hooks","issue_events_url":"https://api.github.com/repos/bitpay/bitcore/issues/events{/number}","events_url":"https://api.github.com/repos/bitpay/bitcore/events","assignees_url":"https://api.github.com/repos/bitpay/bitcore/assignees{/user}","branches_url":"https://api.github.com/repos/bitpay/bitcore/branches{/branch}","tags_url":"https://api.github.com/repos/bitpay/bitcore/tags","blobs_url":"https://api.github.com/repos/bitpay/bitcore/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bitpay/bitcore/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bitpay/bitcore/git/refs{/sha}","trees_url":"https://api.github.com/repos/bitpay/bitcore/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bitpay/bitcore/statuses/{sha}","languages_url":"https://api.github.com/repos/bitpay/bitcore/languages","stargazers_url":"https://api.github.com/repos/bitpay/bitcore/stargazers","contributors_url":"https://api.github.com/repos/bitpay/bitcore/contributors","subscribers_url":"https://api.github.com/repos/bitpay/bitcore/subscribers","subscription_url":"https://api.github.com/repos/bitpay/bitcore/subscription","commits_url":"https://api.github.com/repos/bitpay/bitcore/commits{/sha}","git_commits_url":"https://api.github.com/repos/bitpay/bitcore/git/commits{/sha}","comments_url":"https://api.github.com/repos/bitpay/bitcore/comments{/number}","issue_comment_url":"https://api.github.com/repos/bitpay/bitcore/issues/comments{/number}","contents_url":"https://api.github.com/repos/bitpay/bitcore/contents/{+path}","compare_url":"https://api.github.com/repos/bitpay/bitcore/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bitpay/bitcore/merges","archive_url":"https://api.github.com/repos/bitpay/bitcore/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bitpay/bitcore/downloads","issues_url":"https://api.github.com/repos/bitpay/bitcore/issues{/number}","pulls_url":"https://api.github.com/repos/bitpay/bitcore/pulls{/number}","milestones_url":"https://api.github.com/repos/bitpay/bitcore/milestones{/number}","notifications_url":"https://api.github.com/repos/bitpay/bitcore/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bitpay/bitcore/labels{/name}","releases_url":"https://api.github.com/repos/bitpay/bitcore/releases{/id}","created_at":"2013-12-17T16:10:28Z","updated_at":"2015-02-23T15:29:08Z","pushed_at":"2015-02-23T15:29:08Z","git_url":"git://github.com/bitpay/bitcore.git","ssh_url":"git@github.com:bitpay/bitcore.git","clone_url":"https://github.com/bitpay/bitcore.git","svn_url":"https://github.com/bitpay/bitcore","homepage":"","size":22240,"stargazers_count":1455,"watchers_count":1455,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":324,"mirror_url":null,"open_issues_count":56,"forks":324,"open_issues":56,"watchers":1455,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/1102"},"html":{"href":"https://github.com/bitpay/bitcore/pull/1102"},"issue":{"href":"https://api.github.com/repos/bitpay/bitcore/issues/1102"},"comments":{"href":"https://api.github.com/repos/bitpay/bitcore/issues/1102/comments"},"review_comments":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/1102/comments"},"review_comment":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bitpay/bitcore/pulls/1102/commits"},"statuses":{"href":"https://api.github.com/repos/bitpay/bitcore/statuses/a67084d1768cfb257ebba0df60a31c3b98f0b33f"}}}}