You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
2.1 KiB
58 lines
2.1 KiB
10 years ago
|
# > `bitcore.Block`
|
||
|
|
||
|
## Description
|
||
10 years ago
|
|
||
10 years ago
|
A Block instance represents the information on a block in the bitcoin network. Given a hexa or base64 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.
|
||
10 years ago
|
|
||
|
```javascript
|
||
|
|
||
10 years ago
|
// instantiate a new block instance
|
||
10 years ago
|
var block = new Block(hexaEncodedBlock);
|
||
10 years ago
|
|
||
|
// will verify that the correspending block transactions match the header
|
||
|
assert(block.validMerkleRoot());
|
||
|
|
||
|
// blocks have several properties
|
||
|
assert(block.magicnum);
|
||
10 years ago
|
assert(block.size);
|
||
10 years ago
|
assert(block.header); // an instance of block header, more info below
|
||
|
assert(block.txs); // an array of transactions, more info below
|
||
|
|
||
10 years ago
|
```
|
||
|
|
||
10 years ago
|
For detailed technical information about a block please visit [Blocks](https://en.bitcoin.it/wiki/Blocks#Block_structure) on the Bitcoin Wiki.
|
||
10 years ago
|
|
||
10 years ago
|
## Block Header
|
||
|
|
||
|
Each instance of Block has a BlockHeader *(which can be instantiated seperately)*. The header has validation methods, to verify that the block.
|
||
10 years ago
|
|
||
|
```javascript
|
||
10 years ago
|
|
||
|
// will verify that the nonce demonstrates enough proof of work
|
||
|
assert(block.header.validProofOfWork());
|
||
|
|
||
|
// will verify that timestamp is not too far in the future
|
||
|
assert(block.header.validTimestamp());
|
||
|
|
||
|
// each header has the following properties
|
||
|
assert(block.header.version);
|
||
|
assert(block.header.prevHash);
|
||
|
assert(block.header.merkleRoot);
|
||
|
assert(block.header.time);
|
||
|
assert(block.header.bits);
|
||
|
assert(block.header.nonce);
|
||
|
|
||
|
```
|
||
|
For 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.
|
||
|
|
||
|
## Transactions
|
||
|
|
||
|
The 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.
|
||
|
|
||
|
```javascript
|
||
10 years ago
|
for (var i in block.txs) {
|
||
|
var transaction = block.txs[i];
|
||
10 years ago
|
}
|
||
|
```
|
||
10 years ago
|
|