NewMemKeystore
Creates a new empty in-memory keystore.
Function Signature
func NewMemKeystore() *MemKeystoreParameters
None
Returns
*MemKeystore- New empty MemKeystore instance
Usage Example
package main
import (
"fmt"
"math/big"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/account"
)
func main() {
// Create a new empty MemKeystore
ks := account.NewMemKeystore()
fmt.Println("Created new empty MemKeystore")
// Generate a test key pair (simplified for demo)
privKeyFelt, _ := new(felt.Felt).SetRandom()
bytes := privKeyFelt.Bytes()
privKey := new(big.Int).SetBytes(bytes[:])
// For this demo, we'll use a simple public key representation
pubKey := "0x1234567890abcdef"
fmt.Printf("\nTest key pair:")
fmt.Printf("Public Key: %s\n", pubKey)
fmt.Printf("Private Key: %s\n", privKey)
// Try to get a key that doesn't exist
fmt.Println("\nAttempting to get non-existent key:")
_, err := ks.Get("0x9999")
if err != nil {
fmt.Printf("Error (expected): %v\n", err)
}
// Add the key to the keystore
fmt.Println("\nAdding key to keystore:")
ks.Put(pubKey, privKey)
fmt.Println("Key added successfully")
// Retrieve the key
fmt.Println("\nRetrieving key from keystore:")
retrieved, err := ks.Get(pubKey)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Retrieved key: %s\n", retrieved)
fmt.Printf("Key retrieved successfully: %v\n", retrieved != nil)
}
// Add another key
fmt.Println("\nAdding another key:")
pubKey2 := "0xfedcba0987654321"
privKey2 := new(big.Int).SetUint64(999999)
ks.Put(pubKey2, privKey2)
fmt.Printf("Added key with public key: %s\n", pubKey2)
// Retrieve the second key
retrieved2, err := ks.Get(pubKey2)
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Retrieved second key: %s\n", retrieved2)
}
}Expected Output
Created new empty MemKeystore
Test key pair:Public Key: 0x1234567890abcdef
Private Key: 2311629583598576741398893932306457942980556261936134184524512141092578034099
Attempting to get non-existent key:
Error (expected): error getting key for sender 0x9999: sender does not exist
Adding key to keystore:
Key added successfully
Retrieving key from keystore:
Retrieved key: 2311629583598576741398893932306457942980556261936134184524512141092578034099
Key retrieved successfully: true
Adding another key:
Added key with public key: 0xfedcba0987654321
Retrieved second key: 999999Description
NewMemKeystore creates an in-memory keystore with no keys. You can add keys using the Put method.
MemKeystore is:
- Thread-safe (uses mutex)
- Intended for testing and development
- Not persistent (keys are lost when program exits)
For production, implement a custom Keystore that securely persists keys.
Related Functions
- SetNewMemKeystore - Create keystore with initial keys
- GetRandomKeys - Generate random keys with keystore
- Put - Add keys to keystore
- Get - Retrieve keys from keystore

