Skip to content

Commit

Permalink
implement BatchMerkleProof
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed Apr 8, 2024
1 parent e17f295 commit 80149e5
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions batchmerkleproof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ergo

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

type BatchMerkleProof interface {
Valid(expectedRoot []byte) bool
}

type batchMerkleProof struct {
p C.BatchMerkleProofPtr
}

func newBatchMerkleProof(b *batchMerkleProof) BatchMerkleProof {
runtime.SetFinalizer(b, finalizeBatchMerkleProof)
return b
}

func NewBatchMerkleProof(json string) (BatchMerkleProof, error) {
jsonStr := C.CString(json)
defer C.free(unsafe.Pointer(jsonStr))

var p C.BatchMerkleProofPtr
errPtr := C.ergo_lib_batch_merkle_proof_from_json(jsonStr, &p)
err := newError(errPtr)
if err.isError() {
return nil, err.error()
}
b := &batchMerkleProof{p: p}
return newBatchMerkleProof(b), nil
}

func (b *batchMerkleProof) Valid(expectedRoot []byte) bool {
byteData := C.CBytes(expectedRoot)
defer C.free(unsafe.Pointer(byteData))
res := C.ergo_lib_batch_merkle_proof_valid(b.p, (*C.uchar)(byteData), C.ulong(len(expectedRoot)))
return bool(res)
}

func finalizeBatchMerkleProof(b *batchMerkleProof) {
C.ergo_lib_batch_merkle_proof_delete(b.p)
}

0 comments on commit 80149e5

Please sign in to comment.