Skip to content

Commit 164b7c9

Browse files
authored
Add String() methods for BlockHash and Txid (#15)
Implements Go `Stringer` interface for `BlockHash` and `Txid` types to return hex-encoded hash strings in standard display order (reversed from internal byte order), enabling automatic formatting in fmt functions. Also adds test coverage.
2 parents d4a2d35 + 0a66942 commit 164b7c9

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

kernel/block_hash.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package kernel
55
*/
66
import "C"
77
import (
8+
"encoding/hex"
89
"unsafe"
910
)
1011

@@ -85,3 +86,9 @@ func (bh *blockHashApi) Copy() *BlockHash {
8586
func (bh *blockHashApi) Equals(other BlockHashLike) bool {
8687
return C.btck_block_hash_equals(bh.ptr, other.blockHashPtr()) != 0
8788
}
89+
90+
// String returns the block hash as a hex string in display order (reversed).
91+
func (bh *blockHashApi) String() string {
92+
hashBytes := bh.Bytes()
93+
return hex.EncodeToString(ReverseBytes(hashBytes[:]))
94+
}

kernel/block_hash_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kernel
22

33
import (
4+
"encoding/hex"
45
"testing"
56
)
67

@@ -37,4 +38,10 @@ func TestBlockHash(t *testing.T) {
3738
if hash.Equals(differentHash) {
3839
t.Errorf("hash.Equals(differentHash) = true, want false")
3940
}
41+
42+
// Test String()
43+
expected := hex.EncodeToString(ReverseBytes(hashBytes[:]))
44+
if hash.String() != expected {
45+
t.Errorf("hash.String() = %s, want %s", hash.String(), expected)
46+
}
4047
}

kernel/block_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestBlock(t *testing.T) {
4848
// Expected genesis block hash (reversed byte order for display)
4949
expectedHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
5050
hashBytes := hash.Bytes()
51-
actualHashHex := hex.EncodeToString(reverseBytes(hashBytes[:]))
51+
actualHashHex := hex.EncodeToString(ReverseBytes(hashBytes[:]))
5252
if actualHashHex != expectedHash {
5353
t.Errorf("Expected hash %s, got %s", expectedHash, actualHashHex)
5454
}
@@ -134,11 +134,3 @@ func TestBlock(t *testing.T) {
134134
}
135135
})
136136
}
137-
138-
func reverseBytes(data []byte) []byte {
139-
result := make([]byte, len(data))
140-
for i, b := range data {
141-
result[len(data)-1-i] = b
142-
}
143-
return result
144-
}

kernel/common.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,11 @@ func (h *handle) Destroy() {
9292
func go_delete_handle(handle unsafe.Pointer) {
9393
cgo.Handle(handle).Delete()
9494
}
95+
96+
func ReverseBytes(data []byte) []byte {
97+
result := make([]byte, len(data))
98+
for i, b := range data {
99+
result[len(data)-1-i] = b
100+
}
101+
return result
102+
}

kernel/txid.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package kernel
55
*/
66
import "C"
77
import (
8+
"encoding/hex"
89
"unsafe"
910
)
1011

@@ -60,3 +61,9 @@ func (t *txidApi) Bytes() [32]byte {
6061
C.btck_txid_to_bytes(t.ptr, &output[0])
6162
return *(*[32]byte)(unsafe.Pointer(&output[0]))
6263
}
64+
65+
// String returns the txid as a hex string in display order (reversed).
66+
func (t *txidApi) String() string {
67+
hashBytes := t.Bytes()
68+
return hex.EncodeToString(ReverseBytes(hashBytes[:]))
69+
}

kernel/txid_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ func TestTxid(t *testing.T) {
3939
if !txid.Equals(copiedTxid) {
4040
t.Error("txid.Equals(copiedTxid) = false, want true")
4141
}
42+
43+
// Test String()
44+
expected := hex.EncodeToString(ReverseBytes(txidBytes[:]))
45+
txidStr := txid.String()
46+
if txidStr != expected {
47+
t.Errorf("hash.String() = %s, want %s", txidStr, expected)
48+
}
4249
}

0 commit comments

Comments
 (0)