utils

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: MIT Imports: 11 Imported by: 35

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigIntArrToFeltArr added in v0.7.1

func BigIntArrToFeltArr(bigArr []*big.Int) []*felt.Felt

BigIntArrToFeltArr converts an array of big.Int objects to an array of Felt objects.

Parameters:

  • bigArr: the array of big.Int objects to convert

Returns:

  • []*felt.Felt: the array of Felt objects

func BigIntToFelt

func BigIntToFelt(bigNum *big.Int) *felt.Felt

BigIntToFelt converts a big integer to a felt.Felt.

Parameters:

  • bigNum: the big integer to convert

Returns:

  • *felt.Felt: the converted value

func BigToHex added in v0.4.6

func BigToHex(in *big.Int) string

BigToHex converts a big integer to its hexadecimal representation.

Parameters:

  • in: the big integer to be converted

Returns:

  • string: the hexadecimal representation

func BuildDeclareTxn added in v0.8.0

func BuildDeclareTxn(
	senderAddress *felt.Felt,
	casmClass *contracts.CasmClass,
	contractClass *contracts.ContractClass,
	nonce *felt.Felt,
	resourceBounds *rpc.ResourceBoundsMapping,
	opts *TxnOptions,
) (*rpc.BroadcastDeclareTxnV3, error)

BuildDeclareTxn creates a new declare transaction (v3) for the StarkNet network. A declare transaction is used to declare a new contract class on the network.

Parameters:

  • senderAddress: The address of the account sending the transaction
  • casmClass: The casm class of the contract to be declared
  • contractClass: The contract class to be declared
  • nonce: The account's nonce
  • resourceBounds: Resource bounds for the transaction execution
  • opts: optional settings for the transaction

Returns:

  • rpc.BroadcastDeclareTxnV3: A broadcast declare transaction with default values for signature, paymaster data, etc. Needs to be signed before being sent.

func BuildDeployAccountTxn added in v0.8.0

func BuildDeployAccountTxn(
	nonce *felt.Felt,
	contractAddressSalt *felt.Felt,
	constructorCalldata []*felt.Felt,
	classHash *felt.Felt,
	resourceBounds *rpc.ResourceBoundsMapping,
	opts *TxnOptions,
) *rpc.BroadcastDeployAccountTxnV3

BuildDeployAccountTxn creates a new deploy account transaction (v3) for the StarkNet network. A deploy account transaction is used to deploy a new account contract on the network.

Parameters:

  • nonce: The account's nonce
  • contractAddressSalt: A value used to randomise the deployed contract address
  • constructorCalldata: The parameters for the constructor function
  • classHash: The hash of the contract class to deploy
  • resourceBounds: Resource bounds for the transaction execution
  • opts: optional settings for the transaction

Returns:

  • rpc.BroadcastDeployAccountTxnV3: A broadcast deploy account transaction with default values for signature, paymaster data, etc. Needs to be signed before being sent.

func BuildInvokeTxn added in v0.8.0

func BuildInvokeTxn(
	senderAddress *felt.Felt,
	nonce *felt.Felt,
	calldata []*felt.Felt,
	resourceBounds *rpc.ResourceBoundsMapping,
	opts *TxnOptions,
) *rpc.BroadcastInvokeTxnV3

BuildInvokeTxn creates a new invoke transaction (v3) for the StarkNet network.

Parameters:

  • senderAddress: The address of the account sending the transaction
  • nonce: The account's nonce
  • calldata: The data expected by the account's `execute` function (in most usecases, this includes the called contract address and a function selector)
  • resourceBounds: Resource bounds for the transaction execution
  • opts: optional settings for the transaction

Returns:

  • rpc.BroadcastInvokev3Txn: A broadcast invoke transaction with default values for signature, paymaster data, etc. Needs to be signed before being sent.

func BuildUDCCalldata added in v0.13.0

func BuildUDCCalldata(
	classHash *felt.Felt,
	constructorCalldata []*felt.Felt,
	opts *UDCOptions,
) (rpc.InvokeFunctionCall, *felt.Felt, error)

Builds the INVOKE txn function call to deploy a contract using the UDC.

Parameters:

  • classHash: the class hash of the contract to deploy
  • constructorCalldata: the calldata to pass to the constructor of the contract
  • opts: the options for the UDC deployment. If nil, the default options will be used.

Returns:

  • the INVOKE txn function call to deploy the contract, including the UDC address and the calldata
  • the salt used for the UDC deployment (either the provided one or the random one)
  • an error if any

func ByteArrFeltToString added in v0.7.1

func ByteArrFeltToString(arr []*felt.Felt) (string, error)

ByteArrFeltToString converts array of Felts to string. The input array of felts will be of the format

[number of felts with 31 characters in length, 31 byte felts..., pending word with max size of 30 bytes, pending words bytes size]

For further explanation, refer the article

Parameters:

  • []*felt.Felt: the array of felt.Felt objects

Returns:

  • s: string/bytearray

  • error: an error, if any

func BytesToBig added in v0.4.6

func BytesToBig(bytes []byte) *big.Int

BytesToBig converts a byte slice to a big.Int.

Parameters:

  • bytes: the byte slice to be converted

Returns:

  • *big.Int: the converted value

func ComputeFact added in v0.4.6

func ComputeFact(programHash *big.Int, programOutputs []*big.Int) *big.Int

ComputeFact computes the factorial of a given number.

Parameters:

  • programHash: a pointer to a big.Int representing the program hash
  • programOutputs: a slice of pointers to big.Int representing the program outputs

Returns:

  • *big.Int: a pointer to a big.Int representing the computed factorial

func ETHToWei added in v0.8.0

func ETHToWei(eth float64) *felt.Felt

ETHToWei converts an ETH amount to Wei Returns the Wei value as a *felt.Felt

func FRIToSTRK added in v0.8.0

func FRIToSTRK(fri *felt.Felt) float64

FRIToSTRK converts a FRI amount to STRK Returns the STRK value as a float64

func FeeEstToResBoundsMap added in v0.8.0

func FeeEstToResBoundsMap(
	feeEstimation rpc.FeeEstimation,
	multiplier float64,
) *rpc.ResourceBoundsMapping

FeeEstToResBoundsMap converts a FeeEstimation to ResourceBoundsMapping with applied multipliers. Parameters:

  • feeEstimation: The fee estimation to convert
  • multiplier: Multiplier for max amount and max price per unit. Recommended to be 1.5, but at least greater than 0. If multiplier < 0, all resources bounds will be set to 0. If resource bounds overflow, they will be set to the max allowed value (U64 or U128).

Returns:

  • rpc.ResourceBoundsMapping: Resource bounds with applied multipliers

func FeltArrToBigIntArr added in v0.4.5

func FeltArrToBigIntArr(f []*felt.Felt) []*big.Int

FeltArrToBigIntArr converts an array of Felt objects to an array of big.Int objects.

Parameters:

  • f: the array of Felt objects to convert

Returns:

  • []*big.Int: the array of big.Int objects

func FeltArrToStringArr added in v0.7.3

func FeltArrToStringArr(f []*felt.Felt) []string

FeltArrToStringArr converts an array of Felt objects to an array of string objects.

Parameters:

  • f: the array of Felt objects to convert

Returns:

  • []string: the array of string objects

func FeltToBigInt

func FeltToBigInt(f *felt.Felt) *big.Int

FeltToBigInt converts a Felt value to a *big.Int.

Parameters:

  • f: the Felt value to convert

Returns:

  • *big.Int: the converted value

func FillHexWithZeroes added in v0.12.0

func FillHexWithZeroes(hex string) string

FillHexWithZeroes normalises a hex string to have a '0x' prefix and pads it with leading zeros to a total length of 66 characters (including the '0x' prefix).

func FmtKecBytes added in v0.4.6

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

FmtKecBytes formats the given big.Int as a byte slice (Keccak hash) with a specified length.

The function appends the bytes of the big.Int to a buffer and returns it. If the length of the buffer is less than the specified length, the function pads the buffer with zeros.

Parameters:

  • in: the big.Int to be formatted
  • rolen: the length of the buffer

Returns: buf: the formatted buffer

func GetSelectorFromName added in v0.4.6

func GetSelectorFromName(funcName string) *big.Int

GetSelectorFromName generates a selector from a given function name.

Parameters:

  • funcName: the name of the function

Returns:

  • *big.Int: the selector

func GetSelectorFromNameFelt added in v0.4.6

func GetSelectorFromNameFelt(funcName string) *felt.Felt

GetSelectorFromNameFelt returns a *felt.Felt based on the given function name.

Parameters:

  • funcName: the name of the function

Returns:

  • *felt.Felt: the *felt.Felt

func HexArrToBNArr added in v0.7.1

func HexArrToBNArr(hexArr []string) []*big.Int

HexArrToBNArr converts a hexadecimal string array to a *big.Int array. Trim "0x" prefix(if exists)

Parameters:

  • hexArr: the hexadecimal string array to be converted

Returns:

  • *big.Int: the converted array

func HexArrToFelt

func HexArrToFelt(hexArr []string) ([]*felt.Felt, error)

HexArrToFelt converts an array of hexadecimal strings to an array of felt objects.

The function iterates over each element in the hexArr array and calls the HexToFelt function to convert each hexadecimal value to a felt object. If any error occurs during the conversion, the function will return nil and the corresponding error. Otherwise, it appends the converted felt object to the feltArr array. Finally, the function returns the feltArr array containing all the converted felt objects.

Parameters:

  • hexArr: an array of strings representing hexadecimal values

Returns:

  • []*felt.Felt: an array of *felt.Felt objects, or nil if there was
  • error: an error if any

func HexToBN added in v0.4.6

func HexToBN(hexString string) *big.Int

HexToBN converts a hexadecimal string to a big.Int. trim "0x" prefix(if exists)

Parameters:

  • hexString: the hexadecimal string to be converted

Returns:

  • *big.Int: the converted value

func HexToBytes added in v0.4.6

func HexToBytes(hexString string) ([]byte, error)

HexToBytes converts a hexadecimal string to a byte slice. trim "0x" prefix(if exists)

Parameters:

  • hexString: the hexadecimal string to be converted

Returns:

  • []byte: the converted value
  • error: an error if any

func HexToFelt

func HexToFelt(hex string) (*felt.Felt, error)

HexToFelt converts a hexadecimal string to a *felt.Felt object.

Parameters:

  • hex: the input hexadecimal string to be converted.

Returns:

  • *felt.Felt: a *felt.Felt object
  • error: if conversion fails

func HexToShortStr added in v0.4.6

func HexToShortStr(hexStr string) string

HexToShortStr converts a hexadecimal string to a short string (Starknet) representation.

Parameters:

  • hexStr: the hexadecimal string to convert to a short string

Returns:

  • string: a short string

func HexToU256Felt added in v0.8.0

func HexToU256Felt(hexStr string) ([]*felt.Felt, error)

HexToU256Felt converts a hexadecimal string to a Cairo u256 representation. The Cairo u256 is represented as two felt.Felt values:

  • The first felt.Felt contains the 128 least significant bits (low part)
  • The second felt.Felt contains the 128 most significant bits (high part)

Parameters:

  • hexStr: the hexadecimal string to convert to a Cairo u256

Returns:

  • []*felt.Felt: a slice containing two felt.Felt values [low, high]
  • error: if conversion fails

func InvokeFuncCallsToFunctionCalls added in v0.8.0

func InvokeFuncCallsToFunctionCalls(invokeFuncCalls []rpc.InvokeFunctionCall) []rpc.FunctionCall

InvokeFuncCallsToFunctionCalls converts a slice of InvokeFunctionCall to a slice of FunctionCall.

Parameters:

  • invokeFuncCalls: The invoke function calls to convert

Returns:

  • []*rpc.FunctionCall: A new function calls

func Keccak256 added in v0.4.6

func Keccak256(data ...[]byte) []byte

Keccak256 returns the Keccak-256 hash of the input data. (ref: https://github.com/ethereum/go-ethereum/blob/master/crypto/crypto.go)

Parameters:

  • data: a variadic parameter of type []byte representing the input data

Returns:

  • []byte: a 32-byte hash output

func MaskBits added in v0.4.6

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

MaskBits masks (excess) bits in a slice of bytes based on the given mask and wordSize.

Parameters:

  • mask: an integer representing the number of bits to mask
  • wordSize: an integer representing the size of each word in bits
  • slice: a slice of bytes to mask

Returns:

  • ret: a slice of bytes with the masked bits

func PrecomputeAddressForUDC added in v0.13.0

func PrecomputeAddressForUDC(
	classHash *felt.Felt,
	salt *felt.Felt,
	constructorCalldata []*felt.Felt,
	udcVersion UDCVersion,
	originAccAddress *felt.Felt,
) *felt.Felt

Precomputes the address for a UDC deployment.

Parameters:

  • classHash: the class hash of the contract to deploy
  • salt: the salt to be used for the UDC deployment
  • constructorCalldata: the calldata to pass to the constructor of the contract
  • udcVersion: the UDC version to be used
  • originAccAddress: the address of the account that will deploy the contract. It must be `nil` if `OriginIndependent` is `true`.

Returns:

  • the precomputed address for the UDC deployment

func ResBoundsMapToOverallFee added in v0.8.0

func ResBoundsMapToOverallFee(
	resBounds *rpc.ResourceBoundsMapping,
	multiplier float64,
) (*felt.Felt, error)

ResBoundsMapToOverallFee calculates the overall fee for a ResourceBoundsMapping with applied multipliers. Parameters:

  • resBounds: The resource bounds to calculate the fee for
  • multiplier: Multiplier for max amount and max price per unit. Recommended to be 1.5, but at least greater than 0

Returns:

  • *felt.Felt: The overall fee in FRI
  • error: An error if any

func SNValToBN added in v0.4.6

func SNValToBN(str string) *big.Int

SNValToBN converts a given string to a *big.Int by checking if the string contains "0x" prefix. used in string conversions when interfacing with the APIs

Parameters:

  • str: a string to be converted to *big.Int

Returns:

  • *big.Int: a pointer to a big.Int representing the converted value

func STRKToFRI added in v0.8.0

func STRKToFRI(strk float64) *felt.Felt

STRKToFRI converts a STRK amount to FRI Returns the FRI value as a *felt.Felt

func SplitFactStr added in v0.4.6

func SplitFactStr(fact string) (fact_low, fact_high string, err error)

SplitFactStr splits a given fact, with maximum 256 bits size, into two parts (felts): fact_low and fact_high.

The function takes a fact string as input and converts it to a big number using the HexToBN function. It then converts the big number to bytes using the Bytes method. If the length of the bytes is less than 32, it pads the bytes with zeros using the bytes.Repeat method. The padded bytes are then appended to the original bytes. The function then extracts the low part of the bytes by taking the last 16 bytes and converts it to a big number using the BytesToBig function. It also extracts the high part of the bytes by taking the first 16 bytes and converts it to a big number using the BytesToBig function. Finally, it converts the low and high big numbers to hexadecimal strings using the BigToHex function and returns them.

Parameters:

  • fact: The fact string to be split

Return types:

  • fact_low: The low part of the fact string in hexadecimal format
  • fact_high: The high part of the fact string in hexadecimal format
  • err: An error if any

func StrToBig added in v0.4.6

func StrToBig(str string) *big.Int

StrToBig generates a *big.Int from a string representation.

Parameters:

  • str: The string to convert to a *big.Int

Returns:

  • *big.Int: a pointer to a big.Int representing the converted value

func StrToHex added in v0.7.3

func StrToHex(str string) string

StrToBig generates a hexadecimal from a string/number representation.

Parameters:

  • str: The string to convert to a hexadecimal

Returns:

  • hex: a string representing the converted value

func StringToByteArrFelt added in v0.7.1

func StringToByteArrFelt(s string) ([]*felt.Felt, error)

StringToByteArrFelt converts string to array of Felt objects. The returned array of felts will be of the format

[number of felts with 31 characters in length, 31 byte felts..., pending word with max size of 30 bytes, pending words bytes size]

For further explanation, refer the article

Parameters:

  • s: string/bytearray to convert

Returns:

  • []*felt.Felt: the array of felt.Felt objects

  • error: an error, if any

func U256FeltToHex added in v0.8.0

func U256FeltToHex(u256 []*felt.Felt) (string, error)

U256FeltToHex converts a Cairo u256 representation (two felt.Felt values) back to a hexadecimal string. The Cairo u256 is represented as two felt.Felt values:

  • The first felt.Felt contains the 128 least significant bits (low part)
  • The second felt.Felt contains the 128 most significant bits (high part)

Parameters:

  • u256: a slice containing two felt.Felt values [low, high]

Returns:

  • string: the hexadecimal representation of the combined value
  • error: if conversion fails

func UTF8StrToBig added in v0.4.6

func UTF8StrToBig(str string) *big.Int

UTF8StrToBig converts a UTF-8 string to a big integer.

Parameters:

  • str: The UTF-8 string to convert to a big integer

Returns:

  • *big.Int: a pointer to a big.Int representing the converted value

func Uint64ToFelt

func Uint64ToFelt(num uint64) *felt.Felt

Uint64ToFelt generates a new *felt.Felt from a given uint64 number.

Parameters:

  • num: the uint64 number to convert to a *felt.Felt

Returns:

  • *felt.Felt: a *felt.Felt

func UnmarshalJSONFileToType added in v0.8.0

func UnmarshalJSONFileToType[T any](filePath, subfield string) (*T, error)

UnmarshalJSONFileToType reads a JSON file at the given path and unmarshals it into the specified type T. If any error occurs during file reading or unmarshalling, it returns an error.

Parameters:

  • filePath: string path to the JSON file
  • subfield: string subfield to unmarshal from the JSON file

Returns:

  • *T: pointer to the unmarshalled data of type T
  • error: error if file reading or unmarshalling fails

func WeiToETH added in v0.8.0

func WeiToETH(wei *felt.Felt) float64

WeiToETH converts a Wei amount to ETH Returns the ETH value as a float64

Types

type KeccakState added in v0.4.6

type KeccakState interface {
	hash.Hash
	Read([]byte) (int, error)
}

KeccakState wraps sha3.state. In addition to the usual hash methods, it also supports Read to get a variable amount of data from the hash state. Read is faster than Sum because it doesn't copy the internal state, but also modifies the internal state.

func NewKeccakState added in v0.4.6

func NewKeccakState() KeccakState

NewKeccakState returns a new instance of KeccakState. (ref: https://github.com/ethereum/go-ethereum/blob/master/crypto/crypto.go)

Parameters:

none

Returns:

  • KeccakState: a new instance of KeccakState

type TxnOptions added in v0.13.0

type TxnOptions struct {
	// Tip amount in FRI for the transaction. Default: `"0x0"`.
	// Note: only ready to be used after Starknet v0.14.0 upgrade.
	Tip rpc.U64
	// A boolean flag indicating whether the transaction version should have
	// the query bit when estimating fees. If true, the transaction version
	// will be `rpc.TransactionV3WithQueryBit` (0x100000000000000000000000000000003).
	// If false, the transaction version will be `rpc.TransactionV3` (0x3).
	// In case of doubt, set to `false`. Default: `false`.
	UseQueryBit bool
}

Optional settings when building a transaction.

func (*TxnOptions) SafeTip added in v0.13.0

func (opts *TxnOptions) SafeTip() rpc.U64

SafeTip returns the tip amount in FRI for the transaction. If the tip is not set or invalid, returns "0x0".

func (*TxnOptions) TxnVersion added in v0.13.0

func (opts *TxnOptions) TxnVersion() rpc.TransactionVersion

TxnVersion returns `rpc.TransactionV3WithQueryBit` when UseQueryBit is true, and `rpc.TransactionV3` if false.

type UDCOptions added in v0.13.0

type UDCOptions struct {
	// The salt to be used for the UDC deployment. If not provided, a random value will be used.
	Salt *felt.Felt
	// This parameter is used to determine if the deployer’s address will be included in the contract address calculation.
	// By making deployments dependent upon the origin address, users can reserve a whole address
	// space to prevent someone else from taking ownership of the address. Keep it `false` to include
	// the deployer’s address, and `true` to make it origin independent.
	//
	// This parameter is agnostic to the UDC version. That means that, with `OriginIndependent` set to `true`:
	//   - UDCCairoV0: `unique` will be set to `false`.
	// See more at: https://github.com/starknet-io/starknet-docs/blob/aa1772da8eb42dbc8e6b26ebc37cf898c207f54e/components/Starknet/modules/architecture_and_concepts/pages/Smart_Contracts/universal-deployer.adoc#deployment-types
	//   - UDCCairoV2: `from_zero` will be set to `true`.
	// See more at: https://docs.openzeppelin.com/contracts-cairo/1.0.0/udc#deployment_types
	OriginIndependent bool
	// The UDC version to be used. If not provided, UDCCairoV0 will be used.
	UDCVersion UDCVersion
}

The options for building the UDC calldata

type UDCVersion added in v0.13.0

type UDCVersion int

Enum representing the UDC version to be used

const (
	// Represents the UDC version with Cairo v0 code, with the address 0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf
	UDCCairoV0 UDCVersion = iota
	// Represents the UDC version with Cairo v2 code, with the address 0x04a64cd09a853868621d94cae9952b106f2c36a3f81260f85de6696c6b050221
	UDCCairoV2
)

Jump to

Keyboard shortcuts

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