keystore

package
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCommand = &cli.Command{
	Name:  "create",
	Usage: "Generates a BLS or ECDSA keystore JSON file for a private key",
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:     "key",
			Usage:    "Private key (BLS private key in large number format or ECDSA private key in hex format)",
			Required: true,
		},
		&cli.StringFlag{
			Name:     "path",
			Usage:    "Full path to save keystore file, including filename (e.g., ./operator_keys/operator1.json)",
			Required: true,
		},
		&cli.StringFlag{
			Name:     "type",
			Usage:    "Curve type ('bn254' for BLS or 'ecdsa' for ECDSA)",
			Required: true,
		},
		&cli.StringFlag{
			Name:  "password",
			Usage: `Password to encrypt the keystore file. Default password is "" `,
			Value: "",
		},
	}, common.GlobalFlags...),
	Action: func(cCtx *cli.Context) error {
		logger := common.LoggerFromContext(cCtx.Context)

		privateKey := cCtx.String("key")
		path := cCtx.String("path")
		curve := cCtx.String("type")
		password := cCtx.String("password")

		logger.Debug("🔐 Starting keystore creation")
		logger.Debug("• Curve: %s", curve)
		logger.Debug("• Output Path: %s", path)

		switch curve {
		case "bn254":
			return CreateBLSKeystore(logger, privateKey, path, password, curve)
		case "ecdsa":
			return CreateECDSAKeystore(logger, privateKey, path, password)
		default:
			return fmt.Errorf("unsupported curve type: %s (supported: bn254, ecdsa)", curve)
		}
	},
}
View Source
var KeystoreCommand = &cli.Command{
	Name:  "keystore",
	Usage: "Manage keystore operations",
	Subcommands: []*cli.Command{
		CreateCommand,
		ReadCommand,
	},
}
View Source
var ReadCommand = &cli.Command{
	Name:  "read",
	Usage: "Print the private key from a given keystore file, password",
	Flags: append([]cli.Flag{
		&cli.StringFlag{
			Name:     "path",
			Usage:    "Path to the keystore JSON",
			Required: true,
		},
		&cli.StringFlag{
			Name:     "password",
			Usage:    "Password to decrypt the keystore file",
			Required: true,
		},
	}, common.GlobalFlags...),
	Action: func(cCtx *cli.Context) error {
		path := cCtx.String("path")
		password := cCtx.String("password")

		fileContent, err := os.ReadFile(path)
		if err != nil {
			return fmt.Errorf("failed to read keystore file: %w", err)
		}

		// Check if it's an ECDSA keystore (has "address" field)
		var jsonData map[string]interface{}
		if err := json.Unmarshal(fileContent, &jsonData); err != nil {
			return fmt.Errorf("failed to parse keystore JSON: %w", err)
		}

		if _, hasAddress := jsonData["address"]; hasAddress {

			key, err := ethkeystore.DecryptKey(fileContent, password)
			if err != nil {
				return fmt.Errorf("failed to decrypt ECDSA keystore: %w", err)
			}

			privateKeyHex := hex.EncodeToString(key.PrivateKey.D.Bytes())
			log.Println("✅ ECDSA Keystore decrypted successfully")
			log.Println("")
			log.Println("🔑 Save this ECDSA private key in a secure location:")
			log.Printf("    0x%s\n", privateKeyHex)
			log.Println("")
		} else if _, hasPubkey := jsonData["pubkey"]; hasPubkey {

			scheme := bn254.NewScheme()
			keystoreData, err := blskeystore.LoadKeystoreFile(path)
			if err != nil {
				return fmt.Errorf("failed to load BLS keystore file: %w", err)
			}

			privateKeyData, err := keystoreData.GetPrivateKey(password, scheme)
			if err != nil {
				return fmt.Errorf("failed to extract BLS private key: %w", err)
			}
			log.Println("✅ BLS Keystore decrypted successfully")
			log.Println("")
			log.Println("🔑 Save this BLS private key in a secure location:")
			log.Printf("    %s\n", privateKeyData.Bytes())
			log.Println("")
		} else {
			return fmt.Errorf("unknown keystore format")
		}

		return nil
	},
}

Functions

func CreateBLSKeystore

func CreateBLSKeystore(logger iface.Logger, privateKey, path, password, curve string) error

func CreateECDSAKeystore added in v0.0.10

func CreateECDSAKeystore(logger iface.Logger, privateKeyHex, path, password string) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL