UNPKG

2.11 kBMarkdownView Raw
1# Bitcoin Block
2A Block instance represents the information of a block in the bitcoin network. Given a hexadecimal string representation of the serialization of a block with its transactions, you can instantiate a Block instance. Methods are provided to calculate and check the merkle root hash (if enough data is provided), but transactions won't necessarily be valid spends, and this class won't validate them. A binary representation as a `Buffer` instance is also valid input for a Block's constructor.
3
4```javascript
5// instantiate a new block instance
6var block = new Block(hexaEncodedBlock);
7
8// will verify that the corresponding block transactions match the header
9assert(block.validMerkleRoot());
10
11// blocks have several properties
12assert(block.header); // an instance of block header, more info below
13assert(block.transactions); // an array of transactions, more info below
14```
15
16For detailed technical information about a block please visit [Blocks](https://en.bitcoin.it/wiki/Blocks#Block_structure) on the Bitcoin Wiki.
17
18## Block Header
19Each instance of Block has a BlockHeader _(which can be instantiated separately)_. The header has validation methods, to verify that the block.
20
21```javascript
22// will verify that the nonce demonstrates enough proof of work
23assert(block.header.validProofOfWork());
24
25// will verify that timestamp is not too far in the future
26assert(block.header.validTimestamp());
27
28// each header has the following properties
29assert(block.header.version);
30assert(block.header.prevHash);
31assert(block.header.merkleRoot);
32assert(block.header.time);
33assert(block.header.bits);
34assert(block.header.nonce);
35```
36
37For more information about the specific properties of a block header please visit the [Block hashing algorithm](https://en.bitcoin.it/wiki/Block_hashing_algorithm) page on the Bitcoin Wiki.
38
39## Transactions
40The set of transactions in a block is an array of instances of [Transaction](transaction.md) and can be explored by iterating on the block's `transactions` member.
41
42```javascript
43for (var i in block.transactions) {
44 var transaction = block.transactions[i];
45}
46```