serialize method
Serializes the block header to bytes using Bitcoin protocol encoding.
Implementation
Uint8List serialize() {
final data = ByteData(blockHeaderLen);
var offset = 0;
// Version (4 bytes, little endian)
data.setUint32(offset, version, Endian.little);
offset += 4;
// Previous block hash (32 bytes)
final prevBlockBytes = prevBlock.bytes;
for (int i = 0; i < hashSize; i++) {
data.setUint8(offset + i, prevBlockBytes[i]);
}
offset += hashSize;
// Merkle root hash (32 bytes)
final merkleRootBytes = merkleRoot.bytes;
for (int i = 0; i < hashSize; i++) {
data.setUint8(offset + i, merkleRootBytes[i]);
}
offset += hashSize;
// Timestamp (4 bytes, little endian)
final timestampSeconds = timestamp.millisecondsSinceEpoch ~/ 1000;
data.setUint32(offset, timestampSeconds, Endian.little);
offset += 4;
// Bits (4 bytes, little endian)
data.setUint32(offset, bits, Endian.little);
offset += 4;
// Nonce (4 bytes, little endian)
data.setUint32(offset, nonce, Endian.little);
return data.buffer.asUint8List();
}