Block.prototype.abbr = function abbr() { var res = new Array(80); utils.writeU32(res, this.version, 0); utils.copy(utils.toArray(this.prevBlock, 'hex'), res, 4); utils.copy(utils.toArray(this.merkleRoot, 'hex'), res, 36); utils.writeU32(res, this.ts, 68); utils.writeU32(res, this.bits, 72); utils.writeU32(res, this.nonce, 76); return res; };
it('should encode/decode script', function() { var src = '20' + '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' + '20' + '101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f' + 'ac'; var decoded = script.decode(utils.toArray(src, 'hex')); assert.equal(decoded.length, 3); assert.equal( utils.toHex(decoded[0]), '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'); assert.equal( utils.toHex(decoded[1]), '101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f'); assert.equal(decoded[2], 'OP_CHECKSIG'); var dst = script.encode(decoded); assert.equal(utils.toHex(dst), src); });
Block.fromJSON = function fromJSON(network, json) { utils.assert.equal(json.v, 1); utils.assert.equal(json.type, 'block'); var raw = utils.toArray(json.block, 'hex'); var parser = new protocol.parser(network); var data = json.subtype === 'merkleblock' ? parser.parseMerkleBlock(raw) : parser.parseBlock(raw); var block = new Block(network, data, json.subtype); block._hash = json.hash; return block; };
Block.prototype.hash = function hash(enc) { // Hash it if (!this._hash) this._hash = utils.toHex(utils.dsha256(this.abbr())); return enc === 'hex' ? this._hash : utils.toArray(this._hash, 'hex'); };