forked from TrueCloudLab/neoneo-go
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package stack
|
|
|
|
import (
|
|
"errors"
|
|
"math/big"
|
|
"strconv"
|
|
)
|
|
|
|
// ByteArray represents a slice of bytes on the stack
|
|
type ByteArray struct {
|
|
*abstractItem
|
|
val []byte
|
|
}
|
|
|
|
//NewByteArray returns a ByteArray stack item
|
|
// given a byte slice
|
|
func NewByteArray(val []byte) *ByteArray {
|
|
return &ByteArray{
|
|
&abstractItem{},
|
|
val,
|
|
}
|
|
}
|
|
|
|
//ByteArray overrides the default abstractItem Bytes array method
|
|
func (ba *ByteArray) ByteArray() (*ByteArray, error) {
|
|
return ba, nil
|
|
}
|
|
|
|
//Integer overrides the default Integer method to convert an
|
|
// ByteArray Into an integer
|
|
func (ba *ByteArray) Integer() (*Int, error) {
|
|
dest := reverse(ba.val)
|
|
integerVal := new(big.Int).SetBytes(dest)
|
|
return NewInt(integerVal)
|
|
|
|
}
|
|
|
|
// Boolean will convert a byte array into a boolean stack item
|
|
func (ba *ByteArray) Boolean() (*Boolean, error) {
|
|
boolean, err := strconv.ParseBool(string(ba.val))
|
|
if err != nil {
|
|
return nil, errors.New("cannot convert byte array to a boolean")
|
|
}
|
|
return NewBoolean(boolean)
|
|
}
|
|
|
|
// XXX: move this into a pkg/util/slice folder
|
|
// Go mod not working
|
|
func reverse(b []byte) []byte {
|
|
if len(b) < 2 {
|
|
return b
|
|
}
|
|
|
|
dest := make([]byte, len(b))
|
|
|
|
for i, j := 0, len(b)-1; i < j+1; i, j = i+1, j-1 {
|
|
dest[i], dest[j] = b[j], b[i]
|
|
}
|
|
|
|
return dest
|
|
}
|