@@ -21,11 +21,38 @@ func (blockHashCFuncs) copy(ptr unsafe.Pointer) unsafe.Pointer {
2121// BlockHash is a type-safe identifier for a block.
2222type BlockHash struct {
2323 * handle
24+ blockHashApi
2425}
2526
2627func newBlockHash (ptr * C.btck_BlockHash , fromOwned bool ) * BlockHash {
2728 h := newHandle (unsafe .Pointer (ptr ), blockHashCFuncs {}, fromOwned )
28- return & BlockHash {handle : h }
29+ return & BlockHash {handle : h , blockHashApi : blockHashApi {(* C .btck_BlockHash )(h .ptr )}}
30+ }
31+
32+ // BlockHashView is a type-safe identifier for a block.
33+ type BlockHashView struct {
34+ blockHashApi
35+ ptr * C.btck_BlockHash
36+ }
37+
38+ func newBlockHashView (ptr * C.btck_BlockHash ) * BlockHashView {
39+ return & BlockHashView {
40+ blockHashApi : blockHashApi {ptr },
41+ ptr : ptr ,
42+ }
43+ }
44+
45+ type blockHashApi struct {
46+ ptr * C.btck_BlockHash
47+ }
48+
49+ func (bh * blockHashApi ) blockHashPtr () * C.btck_BlockHash {
50+ return bh .ptr
51+ }
52+
53+ // BlockHashLike is an interface for types that can provide a block hash pointer.
54+ type BlockHashLike interface {
55+ blockHashPtr () * C.btck_BlockHash
2956}
3057
3158// NewBlockHash creates a new BlockHash from a 32-byte hash value.
@@ -38,23 +65,23 @@ func NewBlockHash(hashBytes [32]byte) *BlockHash {
3865}
3966
4067// Bytes returns the 32-byte representation of the block hash.
41- func (bh * BlockHash ) Bytes () [32 ]byte {
68+ func (bh * blockHashApi ) Bytes () [32 ]byte {
4269 var output [32 ]C.uchar
43- C .btck_block_hash_to_bytes (( * C . btck_BlockHash )( bh .ptr ) , & output [0 ])
70+ C .btck_block_hash_to_bytes (bh .ptr , & output [0 ])
4471 return * (* [32 ]byte )(unsafe .Pointer (& output [0 ]))
4572}
4673
4774// Copy creates a copy of the block hash.
48- func (bh * BlockHash ) Copy () * BlockHash {
49- return newBlockHash (( * C . btck_BlockHash )( bh .ptr ) , false )
75+ func (bh * blockHashApi ) Copy () * BlockHash {
76+ return newBlockHash (bh .ptr , false )
5077}
5178
5279// Equals checks if two block hashes are equal.
5380//
5481// Parameters:
55- // - other: Block hash to compare against
82+ // - other: Block hash to compare against (can be *BlockHash or *BlockHashView)
5683//
5784// Returns true if the block hashes are equal.
58- func (bh * BlockHash ) Equals (other * BlockHash ) bool {
59- return C .btck_block_hash_equals (( * C . btck_BlockHash )( bh .ptr ), ( * C . btck_BlockHash )( other .ptr )) != 0
85+ func (bh * blockHashApi ) Equals (other BlockHashLike ) bool {
86+ return C .btck_block_hash_equals (bh .ptr , other .blockHashPtr ( )) != 0
6087}
0 commit comments