Add example for go bindings (#238)
* Add example for go bindings * Remove clean cmd & do go mod tidy
This commit is contained in:
parent
c3c150f31e
commit
ee218752c0
|
@ -6,7 +6,7 @@ exported functions in [C-KZG-4844](https://github.com/ethereum/c-kzg-4844).
|
|||
## Installation
|
||||
|
||||
```
|
||||
go get github.com/ethereum/c-kzg-4844/bindings/go
|
||||
go get github.com/ethereum/c-kzg-4844
|
||||
```
|
||||
|
||||
## Go version
|
||||
|
@ -17,39 +17,15 @@ functions.
|
|||
|
||||
## Example
|
||||
|
||||
For example, a module with this source file:
|
||||
```go
|
||||
package main
|
||||
For reference, see the `example` module in this directory. You can test it out with `go run`:
|
||||
|
||||
import "fmt"
|
||||
import "encoding/hex"
|
||||
import ckzg "github.com/ethereum/c-kzg-4844/bindings/go"
|
||||
|
||||
func main() {
|
||||
ret := ckzg.LoadTrustedSetupFile("trusted_setup.txt")
|
||||
if ret != ckzg.C_KZG_OK {
|
||||
panic("failed to load trusted setup")
|
||||
}
|
||||
defer ckzg.FreeTrustedSetup()
|
||||
|
||||
blob := ckzg.Blob{1, 2, 3}
|
||||
commitment, ret := ckzg.BlobToKZGCommitment(blob)
|
||||
if ret != ckzg.C_KZG_OK {
|
||||
panic("failed to get commitment for blob")
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(commitment[:]))
|
||||
}
|
||||
```
|
||||
|
||||
Will produce this output:
|
||||
```
|
||||
$ go run .
|
||||
user@system ~/c-kzg-4844/bindings/go/example $ go run .
|
||||
go: downloading github.com/ethereum/c-kzg-4844 v0.0.0-20230321204456-577d146c0a5a
|
||||
go: downloading github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2
|
||||
88f1aea383b825371cb98acfbae6c81cce601a2e3129461c3c2b816409af8f3e5080db165fd327db687b3ed632153a62
|
||||
```
|
||||
|
||||
The trusted setup file in the example can be downloaded here:
|
||||
* https://github.com/ethereum/c-kzg-4844/raw/main/src/trusted_setup.txt
|
||||
|
||||
## Tests
|
||||
|
||||
Run the tests with this command:
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
module example
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/ethereum/c-kzg-4844 v0.0.0-20230321204456-577d146c0a5a
|
||||
|
||||
require github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2 // indirect
|
|
@ -0,0 +1,8 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230321204456-577d146c0a5a h1:9JrfUHZY75ml0uPjFYVt+vRr/XHrNMvIpXnRDbVNyVE=
|
||||
github.com/ethereum/c-kzg-4844 v0.0.0-20230321204456-577d146c0a5a/go.mod h1:ECrzdyCNp8d5bLD8Id8Y0mZ8+HDZ0LIdshCrDDTO944=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2 h1:wh1wzwAhZBNiZO37uWS/nDaKiIwHz4mDo4pnA+fqTO0=
|
||||
github.com/supranational/blst v0.3.11-0.20230124161941-ca03e11a3ff2/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
ckzg "github.com/ethereum/c-kzg-4844/bindings/go"
|
||||
)
|
||||
|
||||
var TrustedSetupPath = "../../../src/trusted_setup.txt"
|
||||
|
||||
func main() {
|
||||
ret := ckzg.LoadTrustedSetupFile(TrustedSetupPath)
|
||||
if ret != ckzg.C_KZG_OK {
|
||||
panic("failed to load trusted setup")
|
||||
}
|
||||
defer ckzg.FreeTrustedSetup()
|
||||
|
||||
blob := ckzg.Blob{1, 2, 3}
|
||||
commitment, ret := ckzg.BlobToKZGCommitment(blob)
|
||||
if ret != ckzg.C_KZG_OK {
|
||||
panic("failed to get commitment for blob")
|
||||
}
|
||||
fmt.Println(hex.EncodeToString(commitment[:]))
|
||||
}
|
Loading…
Reference in New Issue