Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion kernel/script_pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func newScriptPubkey(ptr *C.btck_ScriptPubkey, fromOwned bool) *ScriptPubkey {
// Parameters:
// - rawScriptPubkey: Serialized script pubkey data
func NewScriptPubkey(rawScriptPubkey []byte) *ScriptPubkey {
ptr := C.btck_script_pubkey_create(unsafe.Pointer(&rawScriptPubkey[0]), C.size_t(len(rawScriptPubkey)))
var buf unsafe.Pointer
if len(rawScriptPubkey) > 0 {
buf = unsafe.Pointer(&rawScriptPubkey[0])
}
ptr := C.btck_script_pubkey_create(buf, C.size_t(len(rawScriptPubkey)))
return newScriptPubkey(check(ptr), true)
}

Expand Down
60 changes: 43 additions & 17 deletions kernel/script_pubkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,44 @@ import (
)

func TestScriptPubkeyFromRaw(t *testing.T) {
scriptHex := "76a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe26158088ac"
scriptBytes, err := hex.DecodeString(scriptHex)
if err != nil {
t.Fatalf("Failed to decode script hex: %v", err)
tests := []struct {
name string
scriptHex string
}{
{
name: "standard_p2pkh",
scriptHex: "76a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe26158088ac",
},
{
name: "empty_script",
scriptHex: "",
},
}

scriptPubkey := NewScriptPubkey(scriptBytes)
defer scriptPubkey.Destroy()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
scriptBytes, err := hex.DecodeString(tt.scriptHex)
if err != nil {
t.Fatalf("Failed to decode script hex: %v", err)
}

// Test getting the serialized script pubkey
data, err := scriptPubkey.Bytes()
if err != nil {
t.Fatalf("ScriptPubkey.Bytes() error = %v", err)
}
scriptPubkey := NewScriptPubkey(scriptBytes)
defer scriptPubkey.Destroy()

if len(data) != len(scriptBytes) {
t.Errorf("Expected data length %d, got %d", len(scriptBytes), len(data))
}
data, err := scriptPubkey.Bytes()
if err != nil {
t.Fatalf("ScriptPubkey.Bytes() error = %v", err)
}

hexStr := hex.EncodeToString(data)
if hexStr != scriptHex {
t.Errorf("Expected data hex: %s, got %s", scriptHex, hexStr)
if len(data) != len(scriptBytes) {
t.Errorf("Expected data length %d, got %d", len(scriptBytes), len(data))
}

hexStr := hex.EncodeToString(data)
if hexStr != tt.scriptHex {
t.Errorf("Expected data hex: %s, got %s", tt.scriptHex, hexStr)
}
})
}
}

Expand Down Expand Up @@ -173,6 +189,16 @@ func TestInvalidScripts(t *testing.T) {
inputIndex: 0,
description: "a random segwit transaction from the blockchain using native segwit - WITH WRONG SEGWIT",
},
{
name: "empty_scriptpubkey",
scriptPubkeyHex: "",
amount: 0,
// Minimal coinbase-style transaction with a single empty scriptSig and zero-value output;
// used to trigger verification paths.
txToHex: "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff00ffffffff0100000000000000000000000000",
inputIndex: 0,
description: "empty scriptPubkey should fail verification",
},
}

for _, tt := range tests {
Expand Down
Loading