Skip to content

Account Package

The account package handles Starknet account management, transaction building, signing, and execution. Use it to create accounts, send transactions, and manage cryptographic keys.

What This Package Does

  • Create and manage Starknet accounts
  • Build, sign, and execute transactions (invoke, declare, deploy)
  • Estimate fees and manage resource bounds
  • Handle key storage through the Keystore interface
  • Support both Cairo 0 and Cairo 2 contracts

Quick Start

package main
 
import (
	"context"
	"fmt"
	"log"
 
	"github.com/NethermindEth/juno/core/felt"
	"github.com/NethermindEth/starknet.go/account"
	"github.com/NethermindEth/starknet.go/rpc"
)
 
func main() {
	// Create RPC provider
	provider, err := rpc.NewProvider("https://starknet-sepolia.public.blastapi.io/rpc/v0_8")
	if err != nil {
		log.Fatal(err)
	}
 
	// Generate random keys for testing
	ks, pubKey, privKey := account.GetRandomKeys()
	fmt.Printf("Public Key: %s\n", pubKey.String())
	fmt.Printf("Private Key: %s\n", privKey.String())
 
	// Create account
	accountAddress, _ := felt.SetString("0x1234...")
	acc, err := account.NewAccount(provider, accountAddress, pubKey.String(), ks, account.CairoV2)
	if err != nil {
		log.Fatal(err)
	}
 
	// Get nonce
	nonce, err := acc.Nonce(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Account nonce: %s\n", nonce.String())
}
See Also:

Package Structure

Functions

Standalone functions for calldata formatting, address computation, and key management:

  • FmtCallDataCairo0 / FmtCallDataCairo2 - Format calldata for Cairo versions
  • PrecomputeAccountAddress - Calculate address before deployment
  • GetRandomKeys - Generate key pairs for testing
  • NewMemKeystore / SetNewMemKeystore - Create in-memory keystores

Keystore

Key management interface and implementations:

  • Keystore interface - Define custom key storage
  • MemKeystore - In-memory keystore for development

Methods

Account methods for transactions and cryptographic operations:

  • BuildAndSendInvokeTxn - Execute contract functions
  • BuildAndSendDeclareTxn - Declare contract classes
  • Sign / SignInvokeTransaction / SignDeclareTransaction - Sign messages and transactions
  • TransactionHashInvoke / TransactionHashDeclare - Calculate transaction hashes
  • Nonce - Get account nonce
  • Verify - Verify signatures
  • WaitForTransactionReceipt - Wait for confirmation

Types

Type definitions and structures:

  • Account - Main account structure
  • AccountInterface - Account interface
  • CairoVersion - Cairo version constants
  • TxnOptions - Transaction configuration
  • UDCOptions - Universal Deployer Contract options

Key Features

Transaction Handling

Build, sign, and send transactions with automatic nonce management and fee estimation:

// Simple invoke
resp, err := accnt.BuildAndSendInvokeTxn(
    context.Background(),
    []rpc.InvokeFunctionCall{fnCall},
    nil,
)

See the Invoke example for both simple and detailed approaches.

Fee Estimation

Estimate fees before sending transactions:

feeEst, err := accnt.EstimateFee(
    context.Background(),
    []rpc.BroadcastTxn{invokeTxn},
    []rpc.SimulationFlag{},
    rpc.WithBlockTag("latest"),
)

Key Management

The Keystore interface allows custom key storage implementations. Use MemKeystore for development or implement your own for production:

// Create keystore
ks := account.NewMemKeystore()
 
// Store keys
ks.Put(publicKey, privateKey)
 
// Use with account
acc, err := account.NewAccount(provider, address, pubKey, ks, account.CairoV2)

Cairo Version Support

Supports both Cairo 0 and Cairo 2 contract formats with automatic calldata formatting:

// Cairo 2 account
acc, _ := account.NewAccount(provider, address, pubKey, ks, account.CairoV2)
 
// Cairo 0 account
acc, _ := account.NewAccount(provider, address, pubKey, ks, account.CairoV0)

Common Workflows

Deploy a New Account

// 1. Generate keys
ks, pubKey, _ := account.GetRandomKeys()
 
// 2. Precompute address
address := account.PrecomputeAccountAddress(salt, classHash, constructorCalldata)
 
// 3. Fund the address (use faucet or transfer)
 
// 4. Build and send deploy transaction
deployTx, precomputedAddr, err := acc.BuildAndEstimateDeployAccountTxn(
    context.Background(),
    pubKey,
    classHash,
    constructorCalldata,
    1.5,
)
resp, err := acc.SendTransaction(context.Background(), deployTx)

See the Deploy Account example for the complete flow.

Execute Contract Function

// Build function call
fnCall := rpc.InvokeFunctionCall{
    ContractAddress: contractAddr,
    FunctionName:    "transfer",
    CallData:        calldata,
}
 
// Send transaction
resp, err := acc.BuildAndSendInvokeTxn(
    context.Background(),
    []rpc.InvokeFunctionCall{fnCall},
    nil,
)
 
// Wait for confirmation
receipt, err := acc.WaitForTransactionReceipt(context.Background(), resp.Hash, time.Second)

Declare Contract Class

// Load contract files
casmClass, err := utils.UnmarshalJSONFileToType[contracts.CasmClass]("contract.casm.json", "")
contractClass, err := utils.UnmarshalJSONFileToType[contracts.ContractClass]("contract.sierra.json", "")
 
// Declare
resp, err := acc.BuildAndSendDeclareTxn(
    context.Background(),
    casmClass,
    contractClass,
    nil,
)

See the Declare example for details.

Installation

go get github.com/NethermindEth/starknet.go

Import

import "github.com/NethermindEth/starknet.go/account"

Related Packages

Examples

Reference

Full API reference: pkg.go.dev