-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement CommitmentHint, HintsBag, TransactionHintsBag, UnsignedTran…
…saction, Transaction, ReducedTransaction, DataInput and ByteArray
- Loading branch information
1 parent
199e742
commit c7de203
Showing
6 changed files
with
736 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.