Skip to content

Commit

Permalink
feat: add helper methods to work on decoded script
Browse files Browse the repository at this point in the history
  • Loading branch information
wregulski committed Oct 17, 2024
1 parent 5cc21f5 commit d18278d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math/big"
"math/bits"
"strings"

"github.com/pkg/errors"
)

// ScriptKey types.
Expand Down Expand Up @@ -342,6 +344,31 @@ func MinPushSize(bb []byte) int {
return l + 5
}

// GetParts extracts the decoded chunks from the script.
func (s *Script) GetParts() ([]*ScriptChunk, error) {
return DecodeScript([]byte(*s))
}

func (s *Script) GetPublicKey() (string, error) {
if !s.IsP2PK() {
return "", errors.New("script is not of type ScriptTypePubKey")
}

parts, err := s.GetParts()
if err != nil {
return "", err
}

if len(parts) == 0 || parts[0] == nil {
return "", errors.New("invalid script parts or missing public key part")
}

pubKey := parts[0].Data
pubKeyHex := hex.EncodeToString(pubKey)

return pubKeyHex, nil
}

// MarshalJSON convert script into json.
func (s *Script) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, s.String())), nil
Expand Down

0 comments on commit d18278d

Please sign in to comment.