Skip to content

FloRichardAloeCorp/caigo

Repository files navigation

Golang Library for StarkNet

Go Reference

Examples

deploy/call/invoke

Deploy a compiled contrac to testnet, query the initial state, and invoke a state transition.

package main

import (
	"fmt"

	"github.com/dontpanicdao/caigo"
)

func main() {
	// init the stark curve with constants
	curve, err := caigo.SCWithConstants("../pedersen_params.json")
	if err != nil {
		panic(err.Error())
	}
	
	// init starknet gateway client
	gw := caigo.NewGateway() //defaults to goerli

	// get random value for salt
	priv, _ := curve.GetRandomPrivateKey()

	// deploy StarkNet contract with random salt
	deployRequest := caigo.DeployRequest{
		ContractAddressSalt: caigo.BigToHex(priv),
		ConstructorCalldata: []string{},
	}

	// example: https://github.com/starknet-edu/ultimate-env/blob/main/counter.cairo
	deployResponse, err := gw.Deploy("counter_compiled.json", deployRequest)
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("Deployment Response: \n\t%+v\n\n", deployResponse)

	// poll until the desired transaction status
	pollInterval := 5
	n, status, err := gw.PollTx(deployResponse.TransactionHash, caigo.ACCEPTED_ON_L2, pollInterval, 150)
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n * pollInterval, n, deployResponse.TransactionHash, status)

	// fetch transaction details
	tx, err := gw.GetTransaction(deployResponse.TransactionHash)
	if err != nil {
		panic(err.Error())
	}

	// call StarkNet contract
	callResp, err := gw.Call(caigo.StarknetRequest{
		ContractAddress:    tx.Transaction.ContractAddress,
		EntryPointSelector: caigo.BigToHex(caigo.GetSelectorFromName("get_count")),
	})
	if err != nil {
		panic(err.Error())
	}
	fmt.Println("Counter is currently at: ", callResp[0])
	
	// invoke StarkNet contract external function
	invResp, err := gw.Invoke(caigo.StarknetRequest{
		ContractAddress:    tx.Transaction.ContractAddress,
		EntryPointSelector: caigo.BigToHex(caigo.GetSelectorFromName("increment")),
	})
	if err != nil {
		panic(err.Error())
	}

	n, status, err = gw.PollTx(invResp.TransactionHash, caigo.ACCEPTED_ON_L2, 5, 150)
	if err != nil {
		panic(err.Error())
	}
	fmt.Printf("Poll %dsec %dx \n\ttransaction(%s) status: %s\n\n", n * pollInterval, n, deployResponse.TransactionHash, status)

	callResp, err = gw.Call(caigo.StarknetRequest{
		ContractAddress:    tx.Transaction.ContractAddress,
		EntryPointSelector: caigo.BigToHex(caigo.GetSelectorFromName("get_count")),
	})
	if err != nil {
		panic(err.Error())
	}
	fmt.Println("Counter is currently at: ", callResp[0])
}

sign/verify

Although the library adheres to the 'elliptic/curve' interface. All testing has been done against library function explicity. It is recommended to use in the same way(i.e. curve.Sign and not ecdsa.Sign).

package main

import (
	"fmt"
	"math/big"

	"github.com/dontpanicdao/caigo"
)

func main() {
	// NOTE: when not given local file path this pulls the curve data from Starkware github repo
	curve, err := caigo.SCWithConstants("")
	if err != nil {
		panic(err.Error())
	}

	hash, err := curve.PedersenHash([]*big.Int{caigo.HexToBN("0x12773"), caigo.HexToBN("0x872362")})
	if err != nil {
		panic(err.Error())
	}

	priv, _ := curve.GetRandomPrivateKey()

	x, y, err := curve.PrivateToPoint(priv)
	if err != nil {
		panic(err.Error())
	}

	r, s, err := curve.Sign(hash, priv)
	if err != nil {
		panic(err.Error())
	}

	if curve.Verify(hash, r, s, x, y) {
		fmt.Println("signature is valid")
	} else {
		fmt.Println("signature is invalid")
	}
}

Test && Benchmark

// run tests
go test -v

// run benchmarks
go test -bench=.

Issues

If you find an issue/bug or have a feature request please submit an issue here Issues

Contributing

If you are looking to contribute, please head to the Contributing section.

About

Golang Library for StarkNet/Cairo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 96.8%
  • Cairo 2.9%
  • Shell 0.3%