Skip to content

Commit

Permalink
implement missing constant creation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed Apr 12, 2024
1 parent 26ec3b7 commit 2221e80
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
52 changes: 52 additions & 0 deletions constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,58 @@ func NewConstant(s string) (Constant, error) {
return newConstant(c), nil
}

// NewConstantFromInt32 creates a new Constant from int32 value
func NewConstantFromInt32(i int32) Constant {
var p C.ConstantPtr
C.ergo_lib_constant_from_i32(C.int(i), &p)
c := &constant{p}
return newConstant(c)
}

// NewConstantFromInt64 creates a new Constant from int64 value
func NewConstantFromInt64(i int64) Constant {
var p C.ConstantPtr
C.ergo_lib_constant_from_i64(C.longlong(i), &p)

Check failure on line 63 in constant.go

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

cannot use _Ctype_longlong(i) (value of type _Ctype_longlong) as _Ctype_long value in variable declaration
c := &constant{p}
return newConstant(c)
}

// NewConstantFromBytes creates a new Constant from byte array
func NewConstantFromBytes(b []byte) (Constant, error) {
byteData := C.CBytes(b)
defer C.free(unsafe.Pointer(byteData))
var p C.ConstantPtr
errPtr := C.ergo_lib_constant_from_bytes((*C.uchar)(byteData), C.ulong(len(b)), &p)
err := newError(errPtr)
if err.isError() {
return nil, err.error()
}
c := &constant{p}
return newConstant(c), nil
}

// NewConstantFromECPointBytes parse from raw EcPoint value from bytes and make ProveDlog Constant
func NewConstantFromECPointBytes(b []byte) (Constant, error) {
byteData := C.CBytes(b)
defer C.free(unsafe.Pointer(byteData))
var p C.ConstantPtr
errPtr := C.ergo_lib_constant_from_ecpoint_bytes((*C.uchar)(byteData), C.ulong(len(b)), &p)
err := newError(errPtr)
if err.isError() {
return nil, err.error()
}
c := &constant{p}
return newConstant(c), nil
}

// NewConstantFromBox creates a new Constant from Box
func NewConstantFromBox(box Box) Constant {
var p C.ConstantPtr
C.ergo_lib_constant_from_ergo_box(box.pointer(), &p)
c := &constant{p}
return newConstant(c)
}

func (c *constant) Base16() (string, error) {
var constantStr *C.char

Expand Down
47 changes: 47 additions & 0 deletions constant_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ergo

import (
"encoding/hex"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -58,3 +59,49 @@ func TestNewConstant_TupleExpression(t *testing.T) {
constValue, _ := con.Value()
assert.Equal(t, "BoundedVec{inner:[102,99]}", constValue)
}

func TestNewConstantFromInt32(t *testing.T) {
c := NewConstantFromInt32(999999999)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
}

func TestNewConstantFromInt64(t *testing.T) {
c := NewConstantFromInt64(9223372036854775807)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
}

func TestNewConstantFromBytes(t *testing.T) {
b := []byte{1, 1, 2, 255}
c, _ := NewConstantFromBytes(b)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
}

func TestNewConstantFromECPointBytes(t *testing.T) {
str, _ := hex.DecodeString("02d6b2141c21e4f337e9b065a031a6269fb5a49253094fc6243d38662eb765db00")
_, err := NewConstantFromECPointBytes(str)
assert.NoError(t, err)
}

func TestNewConstantFromBox(t *testing.T) {
json := `{
"boxId": "e56847ed19b3dc6b72828fcfb992fdf7310828cf291221269b7ffc72fd66706e",
"value": 67500000000,
"ergoTree": "100204a00b08cd021dde34603426402615658f1d970cfa7c7bd92ac81a8b16eeebff264d59ce4604ea02d192a39a8cc7a70173007301",
"assets": [],
"creationHeight": 284761,
"additionalRegisters": {},
"transactionId": "9148408c04c2e38a6402a7950d6157730fa7d49e9ab3b9cadec481d7769918e9",
"index": 1
}`
testBox, _ := NewBoxFromJson(json)
c := NewConstantFromBox(testBox)
encoded, _ := c.Base16()
decoded, _ := NewConstant(encoded)
assert.Equal(t, c, decoded)
}

0 comments on commit 2221e80

Please sign in to comment.