Skip to content

Commit 0a66942

Browse files
committed
Add String() method for Txid
1 parent 765d2b1 commit 0a66942

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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)