Skip to content

Get

Retrieves a private key from the MemKeystore by its identifier (typically the public key as a hex string). The method is thread-safe and returns an error if the key doesn't exist.

Method Signature

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

Source: keystore.go

Parameters

  • senderAddress - Identifier for the key (typically public key)

Returns

  • *big.Int - Private key
  • error - Error if key doesn't exist

Usage Example

package main
 
import (
	"context"
	"fmt"
	"log"
	"math/big"
 
	"github.com/NethermindEth/starknet.go/account"
)
 
func main() {
	ctx := context.Background()
 
	pubKey := "0x3b8abf14ab2632589812dd0ca906cc314afe293e8d69a5852af0178859ab053"
	privKey, _ := new(big.Int).SetString("0x1e7fcb94882a17e2313ddb9d5f037c33cc17601703f7e41c47bbbcc426d0b10", 0)
 
	ks := account.NewMemKeystore()
	ks.Put(pubKey, privKey)
	fmt.Printf("Put: Added key to keystore\n")
	fmt.Printf("  Public Key:  %s\n\n", pubKey)
 
	retrievedKey, err := ks.Get(pubKey)
	if err != nil {
		log.Fatal("Get failed:", err)
	}
	fmt.Printf("Get: Retrieved private key\n")
	fmt.Printf("  Match: %v\n\n", retrievedKey.Cmp(privKey) == 0)
 
	msgHash := new(big.Int).SetUint64(12345)
	r, s, err := ks.Sign(ctx, pubKey, msgHash)
	if err != nil {
		log.Fatal("Sign failed:", err)
	}
 
	fmt.Printf("Sign: Signed message hash\n")
	fmt.Printf("  Message: %s\n", msgHash.String())
	fmt.Printf("  Signature r: %s\n", r.String())
	fmt.Printf("  Signature s: %s\n", s.String())
}

Error Handling

key, err := ks.Get(publicKey)
if err != nil {
	// Handle key not found error
	return err
}

Common Use Cases

  • Retrieving private keys for signing operations
  • Verifying that a key exists in the keystore before use
  • Loading keys from keystore for account operations
  • Validating key storage after Put operations
  • Managing multiple account keys from a single keystore