header method Null safety

Uint8List header()

Creates the Uint8List representation of the block header.

The block header is represented by a Uint8List of the block properties. Each item is prepended by its size calculate with compact_size.toSize. The Uint8List structure is:

Uint8List<Uint8List> header = Uin8List.fromList([
  ...compact_size.toSize(version),
  ...version,
  ...compact_size.toSize(timestamp),
  ...timestamp,
  ...compact_size.toSize(previousHash),
  ...previousHash,
  ...compact_size.toSize(transactionRoot),
  ...transactionRoot,
  ...compact_size.toSize(transactionCount),
  ...transactionCount,
]);

Implementation

Uint8List header() {
  Uint8List serializedVersion = encodeBigInt(BigInt.from(version));
  Uint8List serializedTimestamp = (BytesBuilder()
        ..add(encodeBigInt(
            BigInt.from(timestamp.millisecondsSinceEpoch ~/ 1000))))
      .toBytes();
  Uint8List serializedPreviousHash = previousHash;
  Uint8List serializedTransactionRoot = transactionRoot;
  Uint8List serializedTransactionCount =
      encodeBigInt(BigInt.from(transactionCount));
  return (BytesBuilder()
        ..add(compact_size.toSize(serializedVersion))
        ..add(serializedVersion)
        ..add(compact_size.toSize(serializedTimestamp))
        ..add(serializedTimestamp)
        ..add(compact_size.toSize(serializedPreviousHash))
        ..add(serializedPreviousHash)
        ..add(compact_size.toSize(serializedTransactionRoot))
        ..add(serializedTransactionRoot)
        ..add(compact_size.toSize(serializedTransactionCount))
        ..add(serializedTransactionCount))
      .toBytes();
}