caigo

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2022 License: MIT Imports: 16 Imported by: 0

README

Golang Library for StarkNet

Go Reference test

Caigo is an MIT-licensed Go library for interacting with StarkNet.

Getting Started

Run Examples

starkcurve

cd examples/curve
go mod tidy
go run main.go

starknet contract

cd examples/contract
go mod tidy
go run main.go

starknet account

cd examples/account
go mod tidy
go run main.go

RPC

Caigo RPC implements the StarkNet RPC Spec:

Method Implemented (*)
starknet_getBlockByHash
starknet_getBlockByNumber
starknet_getTransactionByHash
starknet_getTransactionReceipt
starknet_getClass
starknet_getClassHashAt
starknet_getClassAt
starknet_call
starknet_blockNumber
starknet_chainId
starknet_syncing
starknet_getEvents
starknet_addInvokeTransaction
starknet_addDeployTransaction
starknet_addDeclareTransaction
starknet_estimateFee
starknet_getBlockTransactionCountByHash
starknet_getBlockTransactionCountByNumber
starknet_getTransactionByBlockNumberAndIndex
starknet_getTransactionByBlockHashAndIndex
starknet_getStorageAt
starknet_getNonce
starknet_getStateUpdate
*starknet_traceBlockTransactions
*starknet_traceTransaction

(*) some methods are not implemented because they are not yet available from eqlabs/pathfinder.

Run Tests

go test -v ./...

Run RPC Tests

go test -v ./rpc -env [mainnet|devnet|testnet|mock]

Run Benchmarks

go test -bench=.

Compatibility and stability

Caigo is currently under active development and will under go breaking changes until the initial stable(v1.0.0) release. The example directories and *_test.go files should always be applicable for the latest commitment on the main branch. NOTE: examples and tests may be out of sync with tagged versions and pkg.go.dev documentation

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.

Documentation

Index

Constants

View Source
const (
	TRANSACTION_PREFIX      = "invoke"
	EXECUTE_SELECTOR        = "__execute__"
	CONTRACT_ADDRESS_PREFIX = "STARKNET_CONTRACT_ADDRESS"
)

Variables

View Source
var (
	ErrUnsupportedAccount = errors.New("unsupported account implementation")
	MAX_FEE, _            = big.NewInt(0).SetString("0x20000000000", 0)
)
View Source
var PedersenParamsRaw []byte

Functions

func DivMod

func DivMod(n, m, p *big.Int) *big.Int

Finds a nonnegative integer 0 <= x < p such that (m * x) % p == n

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/math_utils.py)

func FmtKecBytes added in v0.2.0

func FmtKecBytes(in *big.Int, rolen int) (buf []byte)

format the bytes in Keccak hash

func MaskBits added in v0.2.0

func MaskBits(mask, wordSize int, slice []byte) (ret []byte)

mask excess bits

func MerkleHash added in v0.4.0

func MerkleHash(x, y *big.Int) (*big.Int, error)

func ProofMerklePath added in v0.4.0

func ProofMerklePath(root *big.Int, leaf *big.Int, path []*big.Int) bool

Types

type Account added in v0.3.0

type Account struct {
	AccountAddress string
	// contains filtered or unexported fields
}

func NewGatewayAccount added in v0.4.0

func NewGatewayAccount(private, address string, provider *gateway.GatewayProvider, options ...AccountOptionFunc) (*Account, error)

func NewRPCAccount added in v0.4.0

func NewRPCAccount(private, address string, provider *rpcv01.Provider, options ...AccountOptionFunc) (*Account, error)

func (*Account) Call added in v0.4.0

func (account *Account) Call(ctx context.Context, call types.FunctionCall) ([]string, error)

func (*Account) EstimateFee added in v0.3.0

func (account *Account) EstimateFee(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.FeeEstimate, error)

func (*Account) Execute added in v0.3.0

func (account *Account) Execute(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.AddInvokeTransactionOutput, error)

func (*Account) Nonce added in v0.4.0

func (account *Account) Nonce(ctx context.Context) (*big.Int, error)

func (*Account) Sign added in v0.3.0

func (account *Account) Sign(msgHash *big.Int) (*big.Int, *big.Int, error)

func (*Account) TransactionHash added in v0.4.0

func (account *Account) TransactionHash(calls []types.FunctionCall, details types.ExecuteDetails) (*big.Int, error)

type AccountOption added in v0.4.0

type AccountOption struct {
	AccountPlugin AccountPlugin
	// contains filtered or unexported fields
}

func AccountVersion0 added in v0.4.0

func AccountVersion0(string, string) (AccountOption, error)

func AccountVersion1 added in v0.4.0

func AccountVersion1(string, string) (AccountOption, error)

type AccountOptionFunc added in v0.4.0

type AccountOptionFunc func(string, string) (AccountOption, error)

type AccountPlugin added in v0.4.0

type AccountPlugin interface {
	PluginCall(calls []types.FunctionCall) (types.FunctionCall, error)
}

type CurveOption added in v0.3.0

type CurveOption interface {
	// contains filtered or unexported methods
}

func WithConstants added in v0.3.0

func WithConstants(paramsPath ...string) CurveOption

functions that require pedersen hashes must be run on a curve initialized with constant points

type Definition added in v0.2.0

type Definition struct {
	Name string
	Type string
}

type Domain added in v0.2.0

type Domain struct {
	Name    string
	Version string
	ChainId string
}

func (Domain) FmtDefinitionEncoding added in v0.2.0

func (dm Domain) FmtDefinitionEncoding(field string) (fmtEnc []*big.Int)

encoding definition for standard StarkNet Domain messages

type FixedSizeMerkleTree added in v0.4.0

type FixedSizeMerkleTree struct {
	Leaves   []*big.Int
	Branches [][]*big.Int
	Root     *big.Int
}

func NewFixedSizeMerkleTree added in v0.4.0

func NewFixedSizeMerkleTree(leaves ...*big.Int) (*FixedSizeMerkleTree, error)

func (*FixedSizeMerkleTree) Proof added in v0.4.0

func (mt *FixedSizeMerkleTree) Proof(leaf *big.Int) ([]*big.Int, error)

type ProviderType added in v0.4.0

type ProviderType string
const (
	ProviderRPCv01  ProviderType = "rpcv01"
	ProviderGateway ProviderType = "gateway"
)

type StarkCurve

type StarkCurve struct {
	*elliptic.CurveParams
	EcGenX           *big.Int
	EcGenY           *big.Int
	MinusShiftPointX *big.Int
	MinusShiftPointY *big.Int
	Max              *big.Int
	Alpha            *big.Int
	ConstantPoints   [][]*big.Int
}

Returned stark curve includes several values above and beyond what the 'elliptic' interface calls for to facilitate common starkware functions

var Curve StarkCurve

func (StarkCurve) Add

func (sc StarkCurve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int)

Gets two points on an elliptic curve mod p and returns their sum. Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int)

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/math_utils.py)

func (StarkCurve) ComputeHashOnElements added in v0.4.0

func (sc StarkCurve) ComputeHashOnElements(elems []*big.Int) (hash *big.Int, err error)

Hashes the contents of a given array with its size using a golang Pedersen Hash implementation.

(ref: https://github.com/starkware-libs/cairo-lang/blob/13cef109cd811474de114925ee61fd5ac84a25eb/src/starkware/cairo/common/hash_state.py#L6)

func (StarkCurve) Double

func (sc StarkCurve) Double(x1, y1 *big.Int) (x, y *big.Int)

Doubles a point on an elliptic curve with the equation y^2 = x^3 + alpha*x + beta mod p. Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int)

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/math_utils.py)

func (StarkCurve) EcMult

func (sc StarkCurve) EcMult(m, x1, y1 *big.Int) (x, y *big.Int)

Multiplies by m a point on the elliptic curve with equation y^2 = x^3 + alpha*x + beta mod p. Assumes affine form (x, y) is spread (x1 *big.Int, y1 *big.Int) and that 0 < m < order(point).

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/math_utils.py)

func (StarkCurve) GenerateSecret

func (sc StarkCurve) GenerateSecret(msgHash, privKey, seed *big.Int) (secret *big.Int)

implementation based on https://github.com/codahale/rfc6979/blob/master/rfc6979.go for the specification, see https://tools.ietf.org/html/rfc6979#section-3.2

func (StarkCurve) GetRandomPrivateKey

func (sc StarkCurve) GetRandomPrivateKey() (priv *big.Int, err error)

obtain random primary key on stark curve NOTE: to be used for testing purposes

func (StarkCurve) GetYCoordinate

func (sc StarkCurve) GetYCoordinate(starkX *big.Int) *big.Int

Given the x coordinate of a stark_key, returns a possible y coordinate such that together the point (x,y) is on the curve. Note: the real y coordinate is either y or -y.

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/signature.py)

func (StarkCurve) HashElements

func (sc StarkCurve) HashElements(elems []*big.Int) (hash *big.Int, err error)

Hashes the contents of a given array using a golang Pedersen Hash implementation.

(ref: https://github.com/seanjameshan/starknet.js/blob/main/src/utils/ellipticCurve.ts)

func (StarkCurve) IsOnCurve

func (sc StarkCurve) IsOnCurve(x, y *big.Int) bool

func (StarkCurve) MimicEcMultAir

func (sc StarkCurve) MimicEcMultAir(mout, x1, y1, x2, y2 *big.Int) (x *big.Int, y *big.Int, err error)

Computes m * point + shift_point using the same steps like the AIR and throws an exception if and only if the AIR errors.

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/signature.py)

func (StarkCurve) PedersenHash

func (sc StarkCurve) PedersenHash(elems []*big.Int) (hash *big.Int, err error)

Provides the pedersen hash of given array of big integers. NOTE: This function assumes the curve has been initialized with contant points

(ref: https://github.com/seanjameshan/starknet.js/blob/main/src/utils/ellipticCurve.ts)

func (StarkCurve) PrivateToPoint

func (sc StarkCurve) PrivateToPoint(privKey *big.Int) (x, y *big.Int, err error)

obtain public key coordinates from stark curve given the private key

func (StarkCurve) ScalarBaseMult

func (sc StarkCurve) ScalarBaseMult(k []byte) (x, y *big.Int)

func (StarkCurve) ScalarMult

func (sc StarkCurve) ScalarMult(x1, y1 *big.Int, k []byte) (x, y *big.Int)

func (StarkCurve) Sign

func (sc StarkCurve) Sign(msgHash, privKey *big.Int, seed ...*big.Int) (x, y *big.Int, err error)

Signs the hash value of contents with the provided private key. Secret is generated using a golang implementation of RFC 6979. Implementation does not yet include "extra entropy" or "retry gen".

(ref: https://datatracker.ietf.org/doc/html/rfc6979)

func (StarkCurve) Verify

func (sc StarkCurve) Verify(msgHash, r, s, pubX, pubY *big.Int) bool

Verifies the validity of the stark curve signature given the message hash, and public key (x, y) coordinates used to sign the message.

(ref: https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/starkware/crypto/signature/signature.py)

type StarkCurvePayload

type StarkCurvePayload struct {
	License        []string     `json:"_license"`
	Comment        string       `json:"_comment"`
	FieldPrime     *big.Int     `json:"FIELD_PRIME"`
	FieldGen       int          `json:"FIELD_GEN"`
	EcOrder        *big.Int     `json:"EC_ORDER"`
	Alpha          int64        `json:"ALPHA"`
	Beta           *big.Int     `json:"BETA"`
	ConstantPoints [][]*big.Int `json:"CONSTANT_POINTS"`
}

struct definition for parsing 'pedersen_params.json'

var PedersenParams StarkCurvePayload

type TypeDef added in v0.2.0

type TypeDef struct {
	Encoding    *big.Int
	Definitions []Definition
}

type TypedData added in v0.2.0

type TypedData struct {
	Types       map[string]TypeDef
	PrimaryType string
	Domain      Domain
	Message     TypedMessage
}

func NewTypedData added in v0.2.0

func NewTypedData(types map[string]TypeDef, pType string, dom Domain) (td TypedData, err error)

'typedData' interface for interacting and signing typed data in accordance with https://github.com/0xs34n/starknet.js/tree/develop/src/utils/typedData

func (TypedData) EncodeType added in v0.2.0

func (td TypedData) EncodeType(inType string) (enc string, err error)

func (TypedData) GetTypeHash added in v0.2.0

func (td TypedData) GetTypeHash(inType string) (ret *big.Int, err error)

func (TypedData) GetTypedMessageHash added in v0.2.0

func (td TypedData) GetTypedMessageHash(inType string, msg TypedMessage, sc StarkCurve) (hash *big.Int, err error)

type TypedMessage added in v0.2.0

type TypedMessage interface {
	FmtDefinitionEncoding(string) []*big.Int
}

Directories

Path Synopsis
examples
account Module
contract Module
curve Module
starkcurve Module
starknet Module
plugins

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL