account

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 15 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTxnTypeUnSupported    = errors.New("unsupported transaction type")
	ErrTxnVersionUnSupported = errors.New("unsupported transaction version")
	BRAAVOS_WARNING_MESSAGE  = `` /* 206-byte string literal not displayed */

)
View Source
var ErrSenderNoExist = errors.New("sender does not exist")

Functions

func FmtCallDataCairo0 added in v0.6.1

func FmtCallDataCairo0(callArray []rpc.FunctionCall) []*felt.Felt

FmtCallDataCairo0 generates a slice of *felt.Felt that represents the calldata for the given function calls in Cairo 0 format.

Parameters:

  • fnCalls: a slice of rpc.FunctionCall containing the function calls.

Returns:

  • a slice of *felt.Felt representing the generated calldata.

https://github.com/project3fusion/StarkSharp/blob/main/StarkSharp/StarkSharp.Rpc/Modules/Transactions/Hash/TransactionHash.cs#L27

func FmtCallDataCairo2 added in v0.6.1

func FmtCallDataCairo2(callArray []rpc.FunctionCall) []*felt.Felt

FmtCallDataCairo2 generates the calldata for the given function calls for Cairo 2 contracts.

Parameters:

  • fnCalls: a slice of rpc.FunctionCall containing the function calls.

Returns:

  • a slice of *felt.Felt representing the generated calldata.

https://github.com/project3fusion/StarkSharp/blob/main/StarkSharp/StarkSharp.Rpc/Modules/Transactions/Hash/TransactionHash.cs#L22

func PrecomputeAccountAddress added in v0.8.0

func PrecomputeAccountAddress(salt, classHash *felt.Felt, constructorCalldata []*felt.Felt) *felt.Felt

PrecomputeAccountAddress calculates the precomputed address for an account. ref: https://docs.starknet.io/architecture-and-concepts/smart-contracts/contract-address/

Parameters:

  • salt: the salt for the address of the deployed contract
  • classHash: the class hash of the contract to be deployed
  • constructorCalldata: the parameters passed to the constructor

Returns:

  • *felt.Felt: the precomputed address as a *felt.Felt
  • error: an error if any

Types

type Account

type Account struct {
	Provider rpc.RpcProvider
	ChainId  *felt.Felt
	Address  *felt.Felt

	CairoVersion CairoVersion
	// contains filtered or unexported fields
}

func NewAccount

func NewAccount(
	provider rpc.RpcProvider,
	accountAddress *felt.Felt,
	publicKey string,
	keystore Keystore,
	cairoVersion CairoVersion,
) (*Account, error)

NewAccount creates a new Account instance.

Parameters:

  • provider: the provider to use
  • accountAddress: the account address
  • publicKey: the public key of the account
  • keystore: the keystore to use
  • cairoVersion: the cairo version of the account (CairoVersion0 or CairoVersion2)

It returns:

  • *Account: a pointer to newly created Account
  • error: an error if any

func (*Account) BuildAndEstimateDeployAccountTxn added in v0.8.0

func (account *Account) BuildAndEstimateDeployAccountTxn(
	ctx context.Context,
	salt *felt.Felt,
	classHash *felt.Felt,
	constructorCalldata []*felt.Felt,
	opts *TxnOptions,
) (*rpc.BroadcastDeployAccountTxnV3, *felt.Felt, error)

BuildAndEstimateDeployAccountTxn builds and signs a v3 deploy account transaction, estimates the fee, and computes the address.

This function doesn't send the transaction because the precomputed account address requires funding first. This address is calculated deterministically and returned by this function, and must be funded with the appropriate amount of STRK tokens. Without sufficient funds, the transaction will fail. See the 'examples/deployAccount/' for more details on how to do this.

Parameters:

  • ctx: The context.Context for the request.
  • salt: the salt for the address of the deployed contract
  • classHash: the class hash of the contract to be deployed
  • constructorCalldata: the parameters passed to the constructor
  • opts: options for building/estimating the transaction. Pass `nil` to use default values.

Returns:

  • *rpc.BroadcastDeployAccountTxnV3: the transaction to be broadcasted, signed and with the estimated fee based on the multiplier
  • *felt.Felt: the precomputed account address as a *felt.Felt, it needs to be funded with appropriate amount of tokens
  • error: an error if any

func (*Account) BuildAndSendDeclareTxn added in v0.8.0

func (account *Account) BuildAndSendDeclareTxn(
	ctx context.Context,
	casmClass *contracts.CasmClass,
	contractClass *contracts.ContractClass,
	opts *TxnOptions,
) (*rpc.AddDeclareTransactionResponse, error)

BuildAndSendDeclareTxn builds and sends a v3 declare transaction. It automatically calculates the nonce, formats the calldata, estimates fees, and signs the transaction with the account's private key.

Parameters:

  • ctx: The context.Context for the request.
  • casmClass: The casm class of the contract to be declared
  • contractClass: The sierra contract class of the contract to be declared
  • opts: options for building/estimating the transaction. Pass `nil` to use default values.

Returns:

  • *rpc.AddDeclareTransactionResponse: the response of the submitted transaction.
  • error: An error if the transaction building fails.

func (*Account) BuildAndSendInvokeTxn added in v0.8.0

func (account *Account) BuildAndSendInvokeTxn(
	ctx context.Context,
	functionCalls []rpc.InvokeFunctionCall,
	opts *TxnOptions,
) (*rpc.AddInvokeTransactionResponse, error)

BuildAndSendInvokeTxn builds and sends a v3 invoke transaction with the given function calls. It automatically calculates the nonce, formats the calldata, estimates fees, and signs the transaction with the account's private key.

Parameters:

  • ctx: The context.Context for the request.
  • functionCalls: A slice of rpc.InvokeFunctionCall representing the function calls for the transaction, allowing either single or multiple function calls in the same transaction.
  • opts: options for building/estimating the transaction. Pass `nil` to use default values.

Returns:

  • *rpc.AddInvokeTransactionResponse: the response of the submitted transaction.
  • error: An error if the transaction building fails.

func (*Account) DeployContractWithUDC added in v0.13.0

func (account *Account) DeployContractWithUDC(
	ctx context.Context,
	classHash *felt.Felt,
	constructorCalldata []*felt.Felt,
	txnOpts *TxnOptions,
	udcOpts *UDCOptions,
) (*rpc.AddInvokeTransactionResponse, *felt.Felt, error)

A helper to deploy a contract from an existing class using UDC.

Parameters:

  • ctx: The context.Context for the request.
  • classHash: The class hash of the contract to be deployed.
  • constructorCalldata: The parameters passed to the constructor. Pass `nil` if the constructor has no arguments.
  • txnOpts: The options for building/estimating the transaction. Pass `nil` to use default values.
  • udcOpts: The options for building the UDC calldata. Pass `nil` to use default values.

Returns:

  • *rpc.AddInvokeTransactionResponse: the response of the submitted UDC transaction.
  • *felt.Felt: the salt used for the UDC deployment (either the provided one or the random one)
  • error: An error if any.

func (*Account) FmtCalldata

func (account *Account) FmtCalldata(fnCalls []rpc.FunctionCall) ([]*felt.Felt, error)

FmtCalldata generates the formatted calldata for the given function calls and Cairo version.

Parameters:

  • fnCalls: a slice of rpc.FunctionCall representing the function calls.

Returns:

  • a slice of *felt.Felt representing the formatted calldata.
  • an error if Cairo version is not supported.

func (*Account) Nonce

func (account *Account) Nonce(ctx context.Context) (*felt.Felt, error)

Nonce retrieves the nonce for the account's contract address.

func (*Account) SendTransaction added in v0.7.3

func (account *Account) SendTransaction(ctx context.Context, txn rpc.BroadcastTxn) (*rpc.TransactionResponse, error)

SendTransaction can send Invoke, Declare, and Deploy transactions. It provides a unified way to send different transactions. It can only send v3 transactions.

Parameters:

  • ctx: the context.Context object for the transaction.
  • txn: the Broadcast V3 Transaction to be sent.

Returns:

  • *rpc.TransactionResponse: the transaction response.
  • error: an error if any.

func (*Account) Sign

func (account *Account) Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt, error)

Sign signs the given felt message using the account's private key.

Parameters:

  • ctx: is the context used for the signing operation
  • msg: is the felt message to be signed

Returns:

  • []*felt.Felt: an array of signed felt messages
  • error: an error, if any

func (*Account) SignDeclareTransaction

func (account *Account) SignDeclareTransaction(ctx context.Context, tx rpc.DeclareTxnType) error

SignDeclareTransaction signs a declare transaction using the provided Account.

Parameters:

  • ctx: the context.Context
  • tx: the pointer to a Declare or BroadcastDeclare txn

Returns:

  • error: an error if any

func (*Account) SignDeployAccountTransaction

func (account *Account) SignDeployAccountTransaction(ctx context.Context, tx rpc.DeployAccountType, precomputeAddress *felt.Felt) error

SignDeployAccountTransaction signs a deploy account transaction.

Parameters:

  • ctx: the context.Context for the function execution
  • tx: the *rpc.DeployAccountTxnV3 pointer representing the transaction to be signed
  • precomputeAddress: the precomputed address for the transaction

Returns:

  • error: an error if any

func (*Account) SignInvokeTransaction

func (account *Account) SignInvokeTransaction(ctx context.Context, invokeTx rpc.InvokeTxnType) error

SignInvokeTransaction signs and invokes a transaction.

Parameters:

  • ctx: the context.Context for the function execution.
  • invokeTx: the InvokeTxnV3 pointer representing the transaction to be invoked.

Returns:

  • error: an error if there was an error in the signing or invoking process

func (*Account) TransactionHashDeclare

func (account *Account) TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error)

TransactionHashDeclare calculates the transaction hash for declaring a transaction type.

Parameters:

  • tx: The `tx` parameter of type `rpc.DeclareTxnType`. Can be one of the types DeclareTxnV1/V2/V3, and BroadcastDeclareTxnV3

Returns:

  • *felt.Felt: the calculated transaction hash as `*felt.Felt` value
  • error: an error, if any

If the `tx` parameter is not one of the supported types, the function returns an error `ErrTxnTypeUnSupported`.

func (*Account) TransactionHashDeployAccount

func (account *Account) TransactionHashDeployAccount(tx rpc.DeployAccountType, contractAddress *felt.Felt) (*felt.Felt, error)

TransactionHashDeployAccount calculates the transaction hash for a deploy account transaction.

Parameters:

  • tx: The deploy account transaction to calculate the hash for. Can be of type DeployAccountTxn or DeployAccountTxnV3.
  • contractAddress: The contract address as parameters as a *felt.Felt

Returns:

  • *felt.Felt: the calculated transaction hash
  • error: an error if any

func (*Account) TransactionHashInvoke

func (account *Account) TransactionHashInvoke(tx rpc.InvokeTxnType) (*felt.Felt, error)

TransactionHashInvoke calculates the transaction hash for the given invoke transaction.

Parameters:

  • tx: The invoke transaction to calculate the hash for. Can be of type InvokeTxnV0, InvokeTxnV1, or InvokeTxnV3.

Returns:

  • *felt.Felt: The calculated transaction hash as a *felt.Felt
  • error: an error, if any

If the transaction type is unsupported, the function returns an error.

func (*Account) Verify added in v0.13.0

func (account *Account) Verify(msgHash *felt.Felt, signature []*felt.Felt) (bool, error)

Verifies the validity of the signature for a given message hash using the account's public key.

Parameters:

  • msgHash: The message hash to be verified
  • signature: A slice of felt.Felt containing the two signature components

Returns:

  • bool: true if the signature is valid, false otherwise
  • error: An error if any occurred during the verification process

func (*Account) WaitForTransactionReceipt

func (account *Account) WaitForTransactionReceipt(
	ctx context.Context,
	transactionHash *felt.Felt,
	pollInterval time.Duration,
) (*rpc.TransactionReceiptWithBlockInfo, error)

WaitForTransactionReceipt waits for the transaction receipt of the given transaction hash to succeed or fail.

Parameters:

  • ctx: The context
  • transactionHash: The hash
  • pollInterval: The time interval to poll the transaction receipt

It returns:

  • *rpc.TransactionReceipt: the transaction receipt
  • error: an error

type AccountInterface

type AccountInterface interface {
	BuildAndEstimateDeployAccountTxn(
		ctx context.Context,
		salt *felt.Felt,
		classHash *felt.Felt,
		constructorCalldata []*felt.Felt,
		opts *TxnOptions,
	) (*rpc.BroadcastDeployAccountTxnV3, *felt.Felt, error)
	BuildAndSendInvokeTxn(
		ctx context.Context,
		functionCalls []rpc.InvokeFunctionCall,
		opts *TxnOptions,
	) (*rpc.AddInvokeTransactionResponse, error)
	BuildAndSendDeclareTxn(
		ctx context.Context,
		casmClass *contracts.CasmClass,
		contractClass *contracts.ContractClass,
		opts *TxnOptions,
	) (*rpc.AddDeclareTransactionResponse, error)
	DeployContractWithUDC(
		ctx context.Context,
		classHash *felt.Felt,
		constructorCalldata []*felt.Felt,
		txnOpts *TxnOptions,
		udcOpts *UDCOptions,
	) (*rpc.AddInvokeTransactionResponse, *felt.Felt, error)
	Nonce(ctx context.Context) (*felt.Felt, error)
	SendTransaction(ctx context.Context, txn rpc.BroadcastTxn) (*rpc.TransactionResponse, error)
	Sign(ctx context.Context, msg *felt.Felt) ([]*felt.Felt, error)
	SignInvokeTransaction(ctx context.Context, tx rpc.InvokeTxnType) error
	SignDeployAccountTransaction(ctx context.Context, tx rpc.DeployAccountType, precomputeAddress *felt.Felt) error
	SignDeclareTransaction(ctx context.Context, tx rpc.DeclareTxnType) error
	TransactionHashInvoke(invokeTxn rpc.InvokeTxnType) (*felt.Felt, error)
	TransactionHashDeployAccount(tx rpc.DeployAccountType, contractAddress *felt.Felt) (*felt.Felt, error)
	TransactionHashDeclare(tx rpc.DeclareTxnType) (*felt.Felt, error)
	Verify(msgHash *felt.Felt, signature []*felt.Felt) (bool, error)
	WaitForTransactionReceipt(
		ctx context.Context,
		transactionHash *felt.Felt,
		pollInterval time.Duration,
	) (*rpc.TransactionReceiptWithBlockInfo, error)
}

type CairoVersion added in v0.13.0

type CairoVersion int

CairoVersion represents the version of Cairo used by the account contract.

const (
	// CairoV0 represents Cairo 0 contracts
	CairoV0 CairoVersion = 0
	// CairoV2 represents Cairo 2 contracts
	CairoV2 CairoVersion = 2
)

Cairo version constants

type Keystore

type Keystore interface {
	Sign(ctx context.Context, id string, msgHash *big.Int) (x *big.Int, y *big.Int, err error)
}

type MemKeystore

type MemKeystore struct {
	// contains filtered or unexported fields
}

MemKeystore implements the Keystore interface and is intended for example and test code.

func GetRandomKeys

func GetRandomKeys() (*MemKeystore, *felt.Felt, *felt.Felt)

GetRandomKeys gets a random set of pub-priv keys.

Returns:

  • *MemKeystore: a pointer to a MemKeystore instance
  • *felt.Felt: a pointer to a public key as a felt.Felt
  • *felt.Felt: a pointer to a private key as a felt.Felt

func NewMemKeystore

func NewMemKeystore() *MemKeystore

NewMemKeystore initialises and returns a new instance of MemKeystore.

Parameters:

none

Returns:

  • *MemKeystore: a pointer to MemKeystore.

func SetNewMemKeystore

func SetNewMemKeystore(pub string, priv *big.Int) *MemKeystore

SetNewMemKeystore returns a new instance of MemKeystore and sets the given public key and private key in it.

Parameters:

  • pub: a string representing the public key
  • priv: a pointer to a big.Int representing the private key

Returns:

  • *MemKeystore: a pointer to the newly created MemKeystore instance

func (*MemKeystore) Get

func (ks *MemKeystore) Get(senderAddress string) (*big.Int, error)

Get retrieves the value associated with the senderAddress from the MemKeystore.

Parameter:

  • senderAddress: The address of the sender

Returns:

  • *big.Int: The value associated with the senderAddress
  • error: An error if the senderAddress does not exist in the keystore

func (*MemKeystore) Put

func (ks *MemKeystore) Put(senderAddress string, k *big.Int)

Put stores the given key in the keystore for the specified sender address.

Parameters:

  • senderAddress: the address of the sender
  • k: the key to be stored

func (*MemKeystore) Sign

func (ks *MemKeystore) Sign(ctx context.Context, id string, msgHash *big.Int) (*big.Int, *big.Int, error)

Sign signs a message hash using the given key in the MemKeystore.

Parameters:

  • ctx: the context of the operation.
  • id: is the identifier of the key.
  • msgHash: is the message hash to be signed.

Returns:

  • *big.Int: the R component of the signature as *big.Int
  • *big.Int: the S component of the signature as *big.Int
  • error: an error if any

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

	// A safety factor for fee estimation that helps prevent transaction
	// failures due to fee fluctuations. It multiplies both the max amount
	// and max price per unit by this value.
	// A value of 1.5 (estimated fee + 50%) is recommended to balance between
	// transaction success rate and avoiding excessive fees. Higher values
	// provide more safety margin but may result in overpayment.
	// If multiplier <= 0, it'll be set to 1.5.
	Multiplier float64

	// A boolean flag indicating whether to use the latest block tag
	// when estimating fees instead of the pending block. Default: `false`.
	UseLatest bool
	// The simulation flag to be used when estimating fees. Default: none.
	SimulationFlag rpc.SimulationFlag
}

Optional settings for building/sending/estimating a transaction in the BuildAndSend* account methods.

func (*TxnOptions) BlockID added in v0.13.0

func (opts *TxnOptions) BlockID() rpc.BlockID

BlockID returns the block ID for fee estimation based on the UseLatest flag. If UseLatest is `true`, returns the latest block ID. Otherwise, returns the pending block ID.

func (*TxnOptions) SimulationFlags added in v0.13.0

func (opts *TxnOptions) SimulationFlags() []rpc.SimulationFlag

Returns a `[]rpc.SimulationFlag` containing the SimulationFlag. If the flag is not set, returns an empty slice.

type UDCOptions added in v0.13.0

type UDCOptions = utils.UDCOptions

Jump to

Keyboard shortcuts

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