Skip to content

Commit

Permalink
feat: add IsInscribed() method and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jadwahab committed Apr 9, 2023
1 parent 42d5cab commit 07f43a3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 53 deletions.
65 changes: 12 additions & 53 deletions bscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,55 +325,8 @@ func (s *Script) IsData() bool {
// IsInscribed returns true if this script includes an
// inscription with any prepended script (not just p2pkh).
func (s *Script) IsInscribed() bool {
/* TODO: write full code
code generated by ChatGPT to use as a start:
// bytesContainsTemplate searches for a template sequence of bytes in the byte array.
// The template sequence is a slice of byte slices, where each byte slice represents a sequence of bytes to search for.
// An empty byte slice represents any sequence of bytes.
// The function returns true if the template sequence is found in the byte array, and false otherwise.
func bytesContainsTemplate(byteArray []byte, templateSequence [][]byte) bool {
if len(templateSequence) == 0 {
return true
}
currentIndex := 0
for _, searchSequence := range templateSequence {
index := bytesIndex(byteArray[currentIndex:], searchSequence)
if index == -1 {
return false
}
currentIndex += index + len(searchSequence)
}
return true
}
// bytesIndex returns the index of the first occurrence of the search sequence in the byte array,
// or -1 if the search sequence is not found
func bytesIndex(byteArray []byte, searchSequence []byte) int {
if len(searchSequence) == 0 {
return 0
}
for i := 0; i < len(byteArray); i++ {
if byteArray[i] == searchSequence[0] && i+len(searchSequence) <= len(byteArray) {
match := true
for j := 1; j < len(searchSequence); j++ {
if searchSequence[j] != 0 && byteArray[i+j] != searchSequence[j] {
match = false
break
}
}
if match {
return i
}
}
}
return -1
}
*/
return false
isncPattern, _ := hex.DecodeString("0063036f7264")
return bytes.Contains(*s, isncPattern)
}

// IsP2PKHInscription checks if it's a standard
Expand All @@ -390,9 +343,10 @@ func (s *Script) IsP2PKHInscription() bool {
// isP2PKHInscriptionHelper helper so that we don't need to call
// `DecodeParts()` multiple times, such as in `ParseInscription()`
func isP2PKHInscriptionHelper(parts [][]byte) bool {
// TODO: cleanup
return len(parts) == 13 &&
parts[0][0] == OpDUP &&
if len(parts) < 13 {
return false
}
valid := parts[0][0] == OpDUP &&
parts[1][0] == OpHASH160 &&
parts[3][0] == OpEQUALVERIFY &&
parts[4][0] == OpCHECKSIG &&
Expand All @@ -402,11 +356,16 @@ func isP2PKHInscriptionHelper(parts [][]byte) bool {
parts[8][0] == OpTRUE &&
parts[10][0] == OpFALSE &&
parts[12][0] == OpENDIF

if len(parts) > 13 {
return parts[13][0] == OpRETURN && valid
}
return valid
}

// ParseInscription parses the script to
// return the inscription found. Will return
// an error if the scription doesn't contain
// an error if the script doesn't contain
// any inscriptions.
func (s *Script) ParseInscription() (*InscriptionArgs, error) {
p, err := DecodeParts(*s)
Expand Down
Loading

0 comments on commit 07f43a3

Please sign in to comment.