func (s *stack) String() string {
	var result string
	for _, stack := range s.stk {
		if len(stack) == 0 {
			result += "00000000  <empty>\n"
		}
		result += hex.Dump(stack)
	}
	return result
}
String returns the stack in a readable format.
package txscript
import (
	"encoding/hex"
	"fmt"
)
func asBool(t []byte) bool {
	for i := range t {
		if t[i] != 0 {
			if i == len(t)-1 && t[i] == 0x80 {
				return false
			}
			return true
		}
	}
	return false
}
func fromBool(v bool) []byte {
	if v {
		return []byte{1}
	}
	return nil
}
type stack struct {
	stk               [][]byte
	verifyMinimalData bool
}
func (s *stack) Depth() int32 {
	return int32(len(s.stk))
}
func (s *stack) PushByteArray(so []byte) {
	s.stk = append(s.stk, so)
}
func (s *stack) PushInt(val scriptNum) {
	s.PushByteArray(val.Bytes())
}
func (s *stack) PushBool(val bool) {
	s.PushByteArray(fromBool(val))
}
func (s *stack) PopByteArray() ([]byte, error) {
	return s.nipN(0)
}
func (s *stack) PopInt() (scriptNum, error) {
	so, err := s.PopByteArray()
	if err != nil {
		return 0, err
	}
	return makeScriptNum(so, s.verifyMinimalData, defaultScriptNumLen)
}
func (s *stack) PopBool() (bool, error) {
	so, err := s.PopByteArray()
	if err != nil {
		return false, err
	}
	return asBool(so), nil
}
func (s *stack) PeekByteArray(idx int32) ([]byte, error) {
	sz := int32(len(s.stk))
	if idx < 0 || idx >= sz {
		str := fmt.Sprintf("index %d is invalid for stack size %d", idx,
			sz)
		return nil, scriptError(ErrInvalidStackOperation, str)
	}
	return s.stk[sz-idx-1], nil
}
func (s *stack) PeekInt(idx int32) (scriptNum, error) {
	so, err := s.PeekByteArray(idx)
	if err != nil {
		return 0, err
	}
	return makeScriptNum(so, s.verifyMinimalData, defaultScriptNumLen)
}
func (s *stack) PeekBool(idx int32) (bool, error) {
	so, err := s.PeekByteArray(idx)
	if err != nil {
		return false, err
	}
	return asBool(so), nil
}
func (s *stack) nipN(idx int32) ([]byte, error) {
	sz := int32(len(s.stk))
	if idx < 0 || idx > sz-1 {
		str := fmt.Sprintf("index %d is invalid for stack size %d", idx,
			sz)
		return nil, scriptError(ErrInvalidStackOperation, str)
	}
	so := s.stk[sz-idx-1]
	if idx == 0 {
		s.stk = s.stk[:sz-1]
	} else if idx == sz-1 {
		s1 := make([][]byte, sz-1)
		copy(s1, s.stk[1:])
		s.stk = s1
	} else {
		s1 := s.stk[sz-idx : sz]
		s.stk = s.stk[:sz-idx-1]
		s.stk = append(s.stk, s1...)
	}
	return so, nil
}
func (s *stack) NipN(idx int32) error {
	_, err := s.nipN(idx)
	return err
}
func (s *stack) Tuck() error {
	so2, err := s.PopByteArray()
	if err != nil {
		return err
	}
	so1, err := s.PopByteArray()
	if err != nil {
		return err
	}
	s.PushByteArray(so2)  
	s.PushByteArray(so1)  
	s.PushByteArray(so2)  
	return nil
}
func (s *stack) DropN(n int32) error {
	if n < 1 {
		str := fmt.Sprintf("attempt to drop %d items from stack", n)
		return scriptError(ErrInvalidStackOperation, str)
	}
	for ; n > 0; n-- {
		_, err := s.PopByteArray()
		if err != nil {
			return err
		}
	}
	return nil
}
func (s *stack) DupN(n int32) error {
	if n < 1 {
		str := fmt.Sprintf("attempt to dup %d stack items", n)
		return scriptError(ErrInvalidStackOperation, str)
	}
	for i := n; i > 0; i-- {
		so, err := s.PeekByteArray(n - 1)
		if err != nil {
			return err
		}
		s.PushByteArray(so)
	}
	return nil
}
func (s *stack) RotN(n int32) error {
	if n < 1 {
		str := fmt.Sprintf("attempt to rotate %d stack items", n)
		return scriptError(ErrInvalidStackOperation, str)
	}
	entry := 3*n - 1
	for i := n; i > 0; i-- {
		so, err := s.nipN(entry)
		if err != nil {
			return err
		}
		s.PushByteArray(so)
	}
	return nil
}
func (s *stack) SwapN(n int32) error {
	if n < 1 {
		str := fmt.Sprintf("attempt to swap %d stack items", n)
		return scriptError(ErrInvalidStackOperation, str)
	}
	entry := 2*n - 1
	for i := n; i > 0; i-- {
		so, err := s.nipN(entry)
		if err != nil {
			return err
		}
		s.PushByteArray(so)
	}
	return nil
}
func (s *stack) OverN(n int32) error {
	if n < 1 {
		str := fmt.Sprintf("attempt to perform over on %d stack items",
			n)
		return scriptError(ErrInvalidStackOperation, str)
	}
	entry := 2*n - 1
	for ; n > 0; n-- {
		so, err := s.PeekByteArray(entry)
		if err != nil {
			return err
		}
		s.PushByteArray(so)
	}
	return nil
}
func (s *stack) PickN(n int32) error {
	so, err := s.PeekByteArray(n)
	if err != nil {
		return err
	}
	s.PushByteArray(so)
	return nil
}
func (s *stack) RollN(n int32) error {
	so, err := s.nipN(n)
	if err != nil {
		return err
	}
	s.PushByteArray(so)
	return nil
}