File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -83,3 +83,17 @@ func (bh *BlockHeader) Version() int32 {
8383func (bh * BlockHeader ) Nonce () uint32 {
8484 return uint32 (C .btck_block_header_get_nonce ((* C .btck_BlockHeader )(bh .ptr )))
8585}
86+
87+ // Bytes returns the consensus serialized representation of the block header.
88+ //
89+ // Returns an error if the serialization fails.
90+ func (bh * BlockHeader ) Bytes () ([]byte , error ) {
91+ buf := make ([]byte , 80 )
92+ if C .btck_block_header_to_bytes (
93+ (* C .btck_BlockHeader )(bh .ptr ),
94+ (* C .uchar )(unsafe .Pointer (unsafe .SliceData (buf ))),
95+ ) != 0 {
96+ return nil , & SerializationError {"Failed to serialize block header" }
97+ }
98+ return buf , nil
99+ }
Original file line number Diff line number Diff line change @@ -120,4 +120,17 @@ func TestBlockHeader(t *testing.T) {
120120 t .Errorf ("Expected nonce %d, got %d" , expectedNonce , nonce )
121121 }
122122 })
123+
124+ t .Run ("Bytes" , func (t * testing.T ) {
125+ serialized , err := header .Bytes ()
126+ if err != nil {
127+ t .Fatalf ("Bytes() error = %v" , err )
128+ }
129+ if len (serialized ) != 80 {
130+ t .Fatalf ("Expected 80 bytes, got %d" , len (serialized ))
131+ }
132+ if hex .EncodeToString (serialized ) != genesisHeaderHex {
133+ t .Errorf ("Serialized header doesn't match original.\n Expected: %s\n Got: %s" , genesisHeaderHex , hex .EncodeToString (serialized ))
134+ }
135+ })
123136}
You can’t perform that action at this time.
0 commit comments