Refactor Int, Boolean, ByteArray conversion
This commit is contained in:
parent
e2ef6bd2f4
commit
04c56b514c
4 changed files with 54 additions and 26 deletions
|
@ -11,6 +11,8 @@ type Int struct {
|
|||
// NewInt will convert a big integer into
|
||||
// a StackInteger
|
||||
func NewInt(val *big.Int) (*Int, error) {
|
||||
// TOODO: check it is 32 bytes
|
||||
|
||||
return &Int{
|
||||
abstractItem: &abstractItem{},
|
||||
val: val,
|
||||
|
@ -76,20 +78,19 @@ func (i *Int) Integer() (*Int, error) {
|
|||
// ByteArray override the default ByteArray method
|
||||
// to convert a Integer into a byte Array
|
||||
func (i *Int) ByteArray() (*ByteArray, error) {
|
||||
return &ByteArray{
|
||||
i.abstractItem,
|
||||
i.val.Bytes(),
|
||||
}, nil
|
||||
b := i.val.Bytes()
|
||||
dest := reverse(b)
|
||||
return NewByteArray(dest), nil
|
||||
}
|
||||
|
||||
//Boolean override the default Boolean method
|
||||
// to convert an Integer into a Boolean StackItem
|
||||
func (i *Int) Boolean() (*Boolean, error) {
|
||||
|
||||
boolean := (i.val.Int64() != 0)
|
||||
return &Boolean{
|
||||
i.abstractItem,
|
||||
boolean,
|
||||
}, nil
|
||||
|
||||
return NewBoolean(boolean)
|
||||
}
|
||||
|
||||
//Value returns the underlying big.Int
|
||||
func (i *Int) Value() *big.Int {
|
||||
return i.val
|
||||
}
|
||||
|
|
|
@ -6,6 +6,14 @@ type Boolean struct {
|
|||
val bool
|
||||
}
|
||||
|
||||
//NewBoolean returns a new boolean stack item
|
||||
func NewBoolean(val bool) (*Boolean, error) {
|
||||
return &Boolean{
|
||||
&abstractItem{},
|
||||
val,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Boolean overrides the default implementation
|
||||
// by the abstractItem, returning a Boolean struct
|
||||
func (b *Boolean) Boolean() (*Boolean, error) {
|
||||
|
|
|
@ -29,29 +29,33 @@ func (ba *ByteArray) ByteArray() (*ByteArray, error) {
|
|||
//Integer overrides the default Integer method to convert an
|
||||
// ByteArray Into an integer
|
||||
func (ba *ByteArray) Integer() (*Int, error) {
|
||||
|
||||
dest := make([]byte, 0)
|
||||
|
||||
for i, j := 0, len(ba.val)-1; i < j+1; i, j = i+1, j-1 {
|
||||
dest[i], dest[j] = ba.val[j], ba.val[i]
|
||||
}
|
||||
|
||||
dest := reverse(ba.val)
|
||||
integerVal := new(big.Int).SetBytes(dest)
|
||||
return NewInt(integerVal)
|
||||
|
||||
return &Int{
|
||||
ba.abstractItem,
|
||||
integerVal,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Boolean will convert
|
||||
// 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 &Boolean{
|
||||
ba.abstractItem,
|
||||
boolean,
|
||||
}, nil
|
||||
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
|
||||
}
|
||||
|
|
|
@ -55,3 +55,18 @@ func TestRsh(t *testing.T) {
|
|||
assert.Nil(t, err)
|
||||
assert.Equal(t, true, expected.Equal(c))
|
||||
}
|
||||
|
||||
func TestByteArrConversion(t *testing.T) {
|
||||
|
||||
var num int64 = 100000
|
||||
|
||||
a := testMakeStackInt(t, num)
|
||||
ba, err := a.ByteArray()
|
||||
assert.Nil(t, err)
|
||||
|
||||
have, err := ba.Integer()
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.Equal(t, num, have.val.Int64())
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue