Skip to content

SetNewMemKeystore

Creates a new in-memory keystore with a specified key pair. This is a convenience function equivalent to creating an empty keystore with NewMemKeystore() and then calling Put() to add the initial key pair.

Function Signature

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

Source: keystore.go

Parameters

  • pub - Public key as hex string
  • priv - Private key as big.Int

Returns

  • *MemKeystore - New MemKeystore with the specified key pair

Usage Example

package main
 
import (
	"fmt"
	"math/big"
 
	"github.com/NethermindEth/starknet.go/account"
)
 
func main() {
	pubKey1 := "0xabc123def456789"
	privKey1 := new(big.Int).SetUint64(111111)
 
	fmt.Printf("Public Key:  %s\n", pubKey1)
	fmt.Printf("Private Key: %s\n", privKey1)
 
	ks := account.SetNewMemKeystore(pubKey1, privKey1)
 
	retrieved1, err := ks.Get(pubKey1)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
	} else {
		fmt.Printf("Retrieved key: %s\n", retrieved1)
		fmt.Printf("Keys match: %v\n", retrieved1.String() == privKey1.String())
	}
 
	pubKey2 := "0xdef456789abc123"
	privKey2 := new(big.Int).SetUint64(222222)
	ks.Put(pubKey2, privKey2)
	fmt.Printf("Added key 2: Public=%s, Private=%s\n", pubKey2, privKey2)
 
	pubKey3 := "0x789abcdef123456"
	privKey3 := new(big.Int).SetUint64(333333)
	ks.Put(pubKey3, privKey3)
	fmt.Printf("Added key 3: Public=%s, Private=%s\n", pubKey3, privKey3)
 
	retrieved2, err := ks.Get(pubKey2)
	if err != nil {
		fmt.Printf("Error retrieving key 2: %v\n", err)
	} else {
		fmt.Printf("Retrieved key 2: %s\n", retrieved2)
	}
 
	retrieved3, err := ks.Get(pubKey3)
	if err != nil {
		fmt.Printf("Error retrieving key 3: %v\n", err)
	} else {
		fmt.Printf("Retrieved key 3: %s\n", retrieved3)
	}
 
	_, err = ks.Get("0xnonexistent")
	if err != nil {
		fmt.Printf("Error (expected): %v\n", err)
	}
 
	pubKey4 := "0xfedcba9876543210"
	privKey4 := new(big.Int).SetUint64(444444)
	ks2 := account.SetNewMemKeystore(pubKey4, privKey4)
 
	fmt.Printf("Created second keystore with key: Public=%s, Private=%s\n", pubKey4, privKey4)
 
	retrieved4, err := ks2.Get(pubKey4)
	if err != nil {
		fmt.Printf("Error: %v\n", err)
	} else {
		fmt.Printf("Retrieved from keystore 2: %s\n", retrieved4)
	}
 
	_, err1 := ks.Get(pubKey4)
	_, err2 := ks2.Get(pubKey1)
	fmt.Printf("Keystore 1 has key 4: %v\n", err1 == nil)
	fmt.Printf("Keystore 2 has key 1: %v\n", err2 == nil)
}

Error Handling

SetNewMemKeystore does not return errors. It always creates a valid keystore instance with the provided key pair.

Common Use Cases

  • Creating a keystore with a known key pair for testing
  • Initializing keystore with specific account credentials
  • Setting up keystore for development with predefined keys
  • Quick keystore creation when you already have a key pair