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())
}- Deploy Account example - Deploy a new account
- Invoke Contract example - Send transactions
- Deploy with UDC example - Deploy contracts
Package Structure
Functions
Standalone functions for calldata formatting, address computation, and key management:
FmtCallDataCairo0/FmtCallDataCairo2- Format calldata for Cairo versionsPrecomputeAccountAddress- Calculate address before deploymentGetRandomKeys- Generate key pairs for testingNewMemKeystore/SetNewMemKeystore- Create in-memory keystores
Keystore
Key management interface and implementations:
Keystoreinterface - Define custom key storageMemKeystore- In-memory keystore for development
Methods
Account methods for transactions and cryptographic operations:
BuildAndSendInvokeTxn- Execute contract functionsBuildAndSendDeclareTxn- Declare contract classesSign/SignInvokeTransaction/SignDeclareTransaction- Sign messages and transactionsTransactionHashInvoke/TransactionHashDeclare- Calculate transaction hashesNonce- Get account nonceVerify- Verify signaturesWaitForTransactionReceipt- Wait for confirmation
Types
Type definitions and structures:
Account- Main account structureAccountInterface- Account interfaceCairoVersion- Cairo version constantsTxnOptions- Transaction configurationUDCOptions- 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.goImport
import "github.com/NethermindEth/starknet.go/account"Related Packages
- RPC Package - Low-level RPC operations (accounts are built on RPC)
- Curve Package - Cryptographic operations for signing
- Hash Package - Transaction hash calculations
- Utils Package - Helper functions for conversions
Examples
- Deploy Account - Deploy a new account
- Invoke Contract - Execute contract functions
- Deploy with UDC - Deploy contracts using UDC
- Declare Contract - Declare contract classes
- Paymaster - Use paymasters for fee abstraction
Reference
Full API reference: pkg.go.dev

