Skip to content

Commit

Permalink
implement CommitmentHint, HintsBag, TransactionHintsBag, UnsignedTran…
Browse files Browse the repository at this point in the history
…saction, Transaction, ReducedTransaction, DataInput and ByteArray
  • Loading branch information
Alesfatalis committed Apr 7, 2024
1 parent 199e742 commit c7de203
Show file tree
Hide file tree
Showing 6 changed files with 736 additions and 0 deletions.
10 changes: 10 additions & 0 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
type BoxId interface {
// Base16 returns the BoxId as base16 encoded string
Base16() string
pointer() C.BoxIdPtr
}

type boxId struct {
Expand Down Expand Up @@ -69,6 +70,10 @@ func (b *boxId) Base16() string {
return C.GoString(boxIdStr)
}

func (b *boxId) pointer() C.BoxIdPtr {
return b.p
}

func finalizeBoxId(b *boxId) {
C.ergo_lib_box_id_delete(b.p)
}
Expand Down Expand Up @@ -581,6 +586,7 @@ type Boxes interface {
Get(index uint32) (Box, error)
// Add adds provided Box to the end of the collection
Add(box Box)
pointer() C.ErgoBoxesPtr
}

type boxes struct {
Expand Down Expand Up @@ -628,6 +634,10 @@ func (b *boxes) Add(box Box) {
C.ergo_lib_ergo_boxes_add(box.pointer(), b.p)
}

func (b *boxes) pointer() C.ErgoBoxesPtr {
return b.p
}

func finalizeBoxes(b *boxes) {
C.ergo_lib_ergo_boxes_delete(b.p)
}
104 changes: 104 additions & 0 deletions bytearray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package ergo

/*
#include "ergo.h"
*/
import "C"
import (
"runtime"
"unsafe"
)

type ByteArray interface {
pointer() C.ByteArrayPtr
}

type byteArray struct {
p C.ByteArrayPtr
}

func newByteArray(b *byteArray) ByteArray {
runtime.SetFinalizer(b, finalizeByteArray)
return b
}

func NewByteArray(bytes []byte) (ByteArray, error) {
var p C.ByteArrayPtr
byteData := C.CBytes(bytes)
defer C.free(unsafe.Pointer(byteData))

errPtr := C.ergo_lib_byte_array_from_raw_parts((*C.uchar)(byteData), C.ulong(len(bytes)), &p)
err := newError(errPtr)

if err.isError() {
return nil, err.error()
}

ba := &byteArray{p: p}
return newByteArray(ba), nil
}

func (b *byteArray) pointer() C.ByteArrayPtr {
return b.p
}

func finalizeByteArray(b *byteArray) {
C.ergo_lib_byte_array_delete(b.p)
}

type ByteArrays interface {
Len() uint32
Get(index uint32) (ByteArray, error)
Add(byteArray ByteArray)
pointer() C.ByteArraysPtr
}

type byteArrays struct {
p C.ByteArraysPtr
}

func newByteArrays(b *byteArrays) ByteArrays {
runtime.SetFinalizer(b, finalizeByteArrays)
return b
}

func NewByteArrays() ByteArrays {
var p C.ByteArraysPtr
C.ergo_lib_byte_arrays_new(&p)
ba := &byteArrays{p: p}
return newByteArrays(ba)
}

func (b *byteArrays) Len() uint32 {
res := C.ergo_lib_byte_arrays_len(b.p)
return uint32(res)
}

func (b *byteArrays) Get(index uint32) (ByteArray, error) {
var p C.ByteArrayPtr

res := C.ergo_lib_byte_arrays_get(b.p, C.ulong(index), &p)
err := newError(res.error)
if err.isError() {
return nil, err.error()
}

if res.is_some {
ba := &byteArray{p: p}
return newByteArray(ba), nil
}

return nil, nil
}

func (b *byteArrays) Add(byteArray ByteArray) {
C.ergo_lib_byte_arrays_add(byteArray.pointer(), b.p)
}

func (b *byteArrays) pointer() C.ByteArraysPtr {
return b.p
}

func finalizeByteArrays(b *byteArrays) {
C.ergo_lib_byte_arrays_delete(b.p)
}
103 changes: 103 additions & 0 deletions datainput.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package ergo

/*
#include "ergo.h"
*/
import "C"
import "runtime"

// DataInput represent inputs that are used to enrich script context, but won't be spent by the transaction
type DataInput interface {
// BoxId returns the BoxId of the DataInput
BoxId() BoxId
pointer() C.DataInputPtr
}

type dataInput struct {
p C.DataInputPtr
}

func newDataInput(d *dataInput) DataInput {
runtime.SetFinalizer(d, finalizeDataInput)
return d
}

// NewDataInput create DataInput from BoxId
func NewDataInput(boxId BoxId) DataInput {
var p C.DataInputPtr
C.ergo_lib_data_input_new(boxId.pointer(), &p)
d := &dataInput{p: p}
return newDataInput(d)
}

func (d *dataInput) BoxId() BoxId {
var p C.BoxIdPtr
C.ergo_lib_data_input_box_id(d.p, &p)
bi := &boxId{p: p}
return newBoxId(bi)
}

func (d *dataInput) pointer() C.DataInputPtr {
return d.p
}

func finalizeDataInput(d *dataInput) {
C.ergo_lib_data_input_delete(d.p)
}

// DataInputs an ordered collection if DataInput
type DataInputs interface {
// Len returns the length of the collection
Len() uint32
// Get returns the Input at the provided index if it exists
Get(index uint32) (DataInput, error)
// Add adds provided DataInput to the end of the collection
Add(dataInput DataInput)
}

type dataInputs struct {
p C.DataInputsPtr
}

func newDataInputs(d *dataInputs) DataInputs {
runtime.SetFinalizer(d, finalizeDataInputs)
return d
}

// NewDataInputs creates an empty DataInputs collection
func NewDataInputs() DataInputs {
var p C.DataInputsPtr
C.ergo_lib_data_inputs_new(&p)
d := &dataInputs{p: p}
return newDataInputs(d)
}

func (d *dataInputs) Len() uint32 {
res := C.ergo_lib_data_inputs_len(d.p)
return uint32(res)
}

func (d *dataInputs) Get(index uint32) (DataInput, error) {
var p C.DataInputPtr

res := C.ergo_lib_data_inputs_get(d.p, C.ulong(index), &p)
err := newError(res.error)
if err.isError() {
return nil, err.error()
}

if res.is_some {
di := &dataInput{p: p}
return newDataInput(di), nil
}

return nil, nil
}

func (d *dataInputs) Add(dataInput DataInput) {
C.ergo_lib_data_inputs_add(dataInput.pointer(), d.p)
}

func finalizeDataInputs(d *dataInputs) {
C.ergo_lib_data_inputs_delete(d.p)
}
102 changes: 102 additions & 0 deletions reducedtransaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ergo

/*
#include "ergo.h"
*/
import "C"
import (
"runtime"
"unsafe"
)

// ReducedTransaction represents reduced transaction, i.e. unsigned transaction where each unsigned input
// is augmented with ReducedInput which contains a script reduction result.
// After an unsigned transaction is reduced it can be signed without context.
// Thus, it can be serialized and transferred for example to Cold Wallet and signed
// in an environment where secrets are known.
// see EIP-19 for more details -
// https://github.com/ergoplatform/eips/blob/f280890a4163f2f2e988a0091c078e36912fc531/eip-0019.md
type ReducedTransaction interface {
// UnsignedTransaction returns the UnsignedTransaction
UnsignedTransaction() UnsignedTransaction
}

type reducedTransaction struct {
p C.ReducedTransactionPtr
}

func newReducedTransaction(r *reducedTransaction) ReducedTransaction {
runtime.SetFinalizer(r, finalizeReducedTransaction)
return r
}

// NewReducedTransaction creates a ReducedTransaction i.e unsigned transaction where each unsigned input
// is augmented with ReducedInput which contains a script reduction result
func NewReducedTransaction(unsignedTx UnsignedTransaction, boxesToSpent Boxes, dataBoxes Boxes, stateContext StateContext) (ReducedTransaction, error) {
var p C.ReducedTransactionPtr

errPtr := C.ergo_lib_reduced_tx_from_unsigned_tx(unsignedTx.pointer(), boxesToSpent.pointer(), dataBoxes.pointer(), stateContext.pointer(), &p)
err := newError(errPtr)

if err.isError() {
return nil, err.error()
}

r := &reducedTransaction{p: p}
return newReducedTransaction(r), nil
}

func (r *reducedTransaction) UnsignedTransaction() UnsignedTransaction {
var p C.UnsignedTransactionPtr
C.ergo_lib_reduced_tx_unsigned_tx(r.p, &p)
ut := &unsignedTransaction{p: p}
return newUnsignedTransaction(ut)
}

func finalizeReducedTransaction(r *reducedTransaction) {
C.ergo_lib_reduced_tx_delete(r.p)
}

// Propositions list(public keys)
type Propositions interface {
// Add adds new proposition
Add(bytes []byte) error
pointer() C.PropositionsPtr
}

type propositions struct {
p C.PropositionsPtr
}

func newPropositions(p *propositions) Propositions {
runtime.SetFinalizer(p, finalizePropositions)
return p
}

// NewPropositions creates empty proposition holder
func NewPropositions() Propositions {
var p C.PropositionsPtr
C.ergo_lib_propositions_new(&p)
prop := &propositions{p: p}
return newPropositions(prop)
}

func (p *propositions) Add(bytes []byte) error {
byteData := C.CBytes(bytes)
defer C.free(unsafe.Pointer(byteData))

errPtr := C.ergo_lib_propositions_add_proposition_from_bytes(p.p, (*C.uchar)(byteData), C.ulong(len(bytes)))
err := newError(errPtr)
if err.isError() {
return err.error()
}
return nil
}

func (p *propositions) pointer() C.PropositionsPtr {
return p.p
}

func finalizePropositions(p *propositions) {
C.ergo_lib_propositions_delete(p.p)
}
5 changes: 5 additions & 0 deletions statecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "runtime"

// StateContext represents blockchain state (last headers, etc.)
type StateContext interface {
pointer() C.ErgoStateContextPtr
}

type stateContext struct {
Expand Down Expand Up @@ -35,6 +36,10 @@ func NewStateContext(preHeader PreHeader, headers BlockHeaders) (StateContext, e
return newStateContext(st), nil
}

func (s *stateContext) pointer() C.ErgoStateContextPtr {
return s.p
}

func finalizeStateContext(s *stateContext) {
C.ergo_lib_ergo_state_context_delete(s.p)
}
Loading

0 comments on commit c7de203

Please sign in to comment.