gokeystorev4

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 14 Imported by: 3

README

Go KeystoreV4

Golang implementation of EIP-2335 BLS12-381 KeystoreV4

Example

package main

import (
	"encoding/hex"
	"log"
	"strings"

	keystore "github.com/viwet/GoKeystoreV4"
)

const (
	SecretKey     = "0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
	PublicKey     = "0x9612d7a727c9d0a22e185a1c768478dfe919cada9266988cb32359c11f2b7b27f4ae4040902382ae2910c15e2b420d07"
	SecretKeyPath = "m/12381/60/0/0"
	Description   = "Keystore description"
	Password      = "𝔱𝔢𝔰𝔱𝔭𝔞𝔰𝔰𝔴𝔬𝔯𝔡🔑"
)

func main() {
	secret, err := hex.DecodeString(strings.TrimPrefix(SecretKey, "0x"))
	if err != nil {
		log.Fatal(err)
	}
	pubkey, err := hex.DecodeString(strings.TrimPrefix(PublicKey, "0x"))
	if err != nil {
		log.Fatal(err)
	}

	keystore, err := keystore.Encrypt(
		secret,
		Password,
		SecretKeyPath,
		// Optional params
		keystore.WithPublicKey(pubkey),
		keystore.WithDescription(Description),
		keystore.WithCrypto(
			keystore.WithKDF(keystore.NewScrypt()), // or keystore.WithKDF(keystore.NewPBKDF2())
			keystore.WithCipher(keystore.NewAES128()),
			keystore.WithChecksum(keystore.NewSha256()),
		),
	)
	if err != nil {
		log.Fatal(err)
	}

	...
}

Documentation

Index

Constants

View Source
const (
	AES128KeyLen = 16

	AES128Name = "aes-128-ctr"
)

AES128 params

View Source
const (
	ScryptDKLen = 32
	ScryptN     = 1 << 18
	ScryptR     = 8
	ScryptP     = 1

	ScryptName = "scrypt"
)

Default Scrypt params

View Source
const (
	PBKDF2DKLen = 32
	PBKDF2C     = 1 << 18
	PBKDF2PRF   = "hmac-sha256"

	PBKDF2Name = "pbkdf2"
)

Default Scrypt params

View Source
const KeystoreVersion = 4

KeystoreVersion

View Source
const Sha256Name = "sha256"

Sha256 params

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(keystore *Keystore, password string) ([]byte, error)

Decrypt secret key from keystore using password

func ProcessPassword

func ProcessPassword(password string) []byte

ProcessPassword encodes password in NFKD form and removes all control characters

func RandomBytes

func RandomBytes(length int) []byte

RandomBytse generates random bytes sequence of given length

Types

type AES128

type AES128 struct {
	InitialValue IV `json:"iv"`
}

AES-128 params

func NewAES128

func NewAES128() *AES128

NewAES128 with new initial value

func (*AES128) Decrypt

func (c *AES128) Decrypt(key, data []byte) ([]byte, error)

Decrypt data using key

func (*AES128) Encrypt

func (c *AES128) Encrypt(data, key []byte) ([]byte, error)

Encrypt data using key

func (*AES128) Function

func (c *AES128) Function() string

Function name

type ChecksumFunction

type ChecksumFunction interface {
	Function() string
	Checksum(key, cipher []byte) []byte
}

Checksum function interface

type CipherFunction

type CipherFunction interface {
	Function() string
	Encrypt(data, key []byte) ([]byte, error)
	Decrypt(key, data []byte) ([]byte, error)
}

Cipher function interface

type Crypto

type Crypto struct {
	KDF      *Module[KDFunction]       `json:"kdf"`
	Cipher   *Module[CipherFunction]   `json:"cipher"`
	Checksum *Module[ChecksumFunction] `json:"checksum"`
}

Crypto modules of keystore

func DefaultCrypto

func DefaultCrypto() *Crypto

Default Crypto with Scrypt, AES128 and Sha256

func NewCrypto

func NewCrypto(options ...CryptoOption) (*Crypto, error)

NewCrypto with options or default

func (*Crypto) Decrypt

func (c *Crypto) Decrypt(password []byte) ([]byte, error)

Decrypt secret key using password

func (*Crypto) Encrypt

func (c *Crypto) Encrypt(secret, password []byte) error

Encrypt secret key using password

func (*Crypto) UnmarshalJSON

func (c *Crypto) UnmarshalJSON(data []byte) error

impl json.Unmarshaler for Crypto

type CryptoJSON

type CryptoJSON struct {
	KDF      *ModuleJSON `json:"kdf"`
	Cipher   *ModuleJSON `json:"cipher"`
	Checksum *ModuleJSON `json:"checksum"`
}

CryptoJSON representation

type CryptoOption

type CryptoOption = Option[*Crypto]

Type aliases

func WithChecksum

func WithChecksum(checksum ChecksumFunction) CryptoOption

WithChecksum sets ChecksumFunction

func WithCipher

func WithCipher(cipher CipherFunction) CryptoOption

WithCipher sets CipherFunction

func WithKDF

func WithKDF(kdf KDFunction) CryptoOption

WithKDF sets KDFunction

type CryptoOptions

type CryptoOptions = Options[*Crypto]

Type aliases

type Hex

type Hex []byte

Hex string

func (Hex) MarshalJSON

func (h Hex) MarshalJSON() ([]byte, error)

imlp json.Marshaler for Hex

func (*Hex) UnmarshalJSON

func (h *Hex) UnmarshalJSON(data []byte) error

imlp json.Unmarshaler for Hex

type IV

type IV = Hex

Type aliases

type KDFunction

type KDFunction interface {
	Function() string
	DeriveKey(password []byte) ([]byte, error)
}

Key derivation function interface

type Keystore

type Keystore struct {
	Crypto      *Crypto   `json:"crypto"`
	Description string    `json:"description,omitempty"`
	PublicKey   PublicKey `json:"pubkey,omitempty"`
	Path        string    `json:"path"`
	UUID        uuid.UUID `json:"uuid"`
	Version     uint64    `json:"version"`
}

KeystoreV4

func Encrypt

func Encrypt(secret []byte, password, path string, opts ...KeystoreOption) (*Keystore, error)

Encrypt secret key at path using password and options

type KeystoreOption

type KeystoreOption = Option[*Keystore]

Type aliases

func WithCrypto

func WithCrypto(opts ...CryptoOption) KeystoreOption

WithCrypto creates and sets new Crypto with given options

func WithDescription

func WithDescription(description string) KeystoreOption

WithDescription sets Description

func WithPublicKey

func WithPublicKey(key PublicKey) KeystoreOption

WithPublicKey sets PublicKey

type KeystoreOptions

type KeystoreOptions = Options[*Keystore]

Type aliases

type Module

type Module[T any] struct {
	Function string `json:"function"`
	Params   T      `json:"params"`
	Message  Hex    `json:"message"`
}

Generic crypto module

type ModuleJSON

type ModuleJSON struct {
	Function string          `json:"function"`
	Params   json.RawMessage `json:"params"`
	Message  Hex             `json:"message"`
}

ModuleJSON representation

type Option

type Option[T any] func(T) error

Generic option

type Options

type Options[T any] []Option[T]

Generic option slice

func (Options[T]) Apply

func (opts Options[T]) Apply(obj T) error

Apply all the options

type PBKDF2

type PBKDF2 struct {
	DKLen int    `json:"dklen"`
	C     int    `json:"c"`
	PRF   string `json:"prf"`
	Salt  Salt   `json:"salt"`
}

PBKDF2 params

func NewPBKDF2

func NewPBKDF2() *PBKDF2

NewPBKDF2 with default params and new salt

func (*PBKDF2) DeriveKey

func (kdf *PBKDF2) DeriveKey(password []byte) ([]byte, error)

DeriveKey from password using provided params

func (*PBKDF2) Function

func (kdf *PBKDF2) Function() string

Function name

type PublicKey

type PublicKey = Hex

Type aliases

type Salt

type Salt = Hex

Type aliases

type Scrypt

type Scrypt struct {
	DKLen int  `json:"dklen"`
	N     int  `json:"n"`
	R     int  `json:"r"`
	P     int  `json:"p"`
	Salt  Salt `json:"salt"`
}

Scrypt params

func NewScrypt

func NewScrypt() *Scrypt

NewScrypt with default params and new salt

func (*Scrypt) DeriveKey

func (kdf *Scrypt) DeriveKey(password []byte) ([]byte, error)

DeriveKey from password using provided params

func (*Scrypt) Function

func (kdf *Scrypt) Function() string

Function name

type Sha256

type Sha256 struct{}

Sha256 function

func NewSha256

func NewSha256() *Sha256

NewSha256

func (*Sha256) Checksum

func (c *Sha256) Checksum(key, cipher []byte) []byte

Checksum key and cipher

func (*Sha256) Function

func (c *Sha256) Function() string

Function name

Jump to

Keyboard shortcuts

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