keypair

package module
v0.0.0-...-5d2dd09 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

README

Here is an enhanced README.md for the Go ED25519 wallet library with BIP39 and BIP32 compatibility:

Go ED25519 Wallet

This is a Golang implementation of an ED25519 based asymmetric cryptography wallet library.

Features

  • Generate ED25519 public/private key pairs
  • Derive addresses from public keys
  • Sign and verify messages using keys
  • Recover public key from signatures
  • Implements HD wallet algorithms (seed, master keys, derivation etc.)
  • Provides mnemonic phrase generation and recovery (inspired by BIP39)
  • Supports encrypting/decrypting private keys (using passphrase and Scrypt)
  • Implements a complete asymmetric crypto account system
  • Compatible with BIP39 and BIP32 standards

Usage Example

	privk := keypair.NewPrivateKey(nil)
	pubk := privk.GetPubKey()
	addr := pubk.Address()
	fmt.Println(addr)

	fmt.Println("is valid addr: ", keypair.IsValidAddr(addr))

Mnemonic Phrases

The library implements a BIP39-like mnemonic phrase algorithm to generate seeds and recover wallets.

// Generate mnemonic phrase
mnemonic := keypair.GenerateMnemonic(12,"cn")

Encryption uses PBKDF2 and Scrypt key derivation to harden security.

This library can be used to build ED25519 based cryptocurrency projects and applications. Contributions via issues and PRs are welcome!

Documentation

Index

Constants

View Source
const (
	EntropyBits128 int = 128
	EntropyBits160 int = 160
	EntropyBits192 int = 192
	EntropyBits224 int = 224
	EntropyBits256 int = 256
)
View Source
const (

	// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptN = 1 << 18

	// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptP = 1

	// LightScryptN is the N parameter of Scrypt encryption algorithm, using 4MB
	// memory and taking approximately 100ms CPU time on a modern processor.
	LightScryptN = 1 << 12

	// LightScryptP is the P parameter of Scrypt encryption algorithm, using 4MB
	// memory and taking approximately 100ms CPU time on a modern processor.
	LightScryptP = 6
)
View Source
const PrivateKeyLength int = ed25519.PrivateKeySize
View Source
const PublicKeyLength int = ed25519.PublicKeySize

Variables

View Source
var (
	ErrDecrypt = errors.New("could not decrypt key with given password")
)

Functions

func DecryptCBC

func DecryptCBC(data, iv []byte, key SharedKey) ([]byte, error)

func EncryptCBC

func EncryptCBC(data, iv []byte, key SharedKey) ([]byte, error)

func GenerateMnemonic

func GenerateMnemonic(length int, language string) (string, error)

GenerateMnemonic 生成助记词 length 助记词长度 language 助记词语言 cn:中文简体,en:英语

func SignMsg

func SignMsg(privKey PrivateKey, msg []byte) []byte

func ToSeed

func ToSeed(mnemonic, password string) []byte

func VerifyMsg

func VerifyMsg(pubKey PublicKey, originMsg, signMsg []byte) bool

Types

type CryptoJson

type CryptoJson struct {
	Cipher       string                 `json:"cipher"`
	CipherParams cipherparamsJSON       `json:"cipher_params"`
	CipherText   string                 `json:"cipher_text"`
	KDF          string                 `json:"kdf"`
	KDFParams    map[string]interface{} `json:"kdf_params"`
	MAC          string                 `json:"mac"`
}

type Keypair

type Keypair struct {
	// contains filtered or unexported fields
}

func LoadFromPrivKeyBytes

func LoadFromPrivKeyBytes(privKey []byte) Keypair

func LoadFromPrivKeyHexString

func LoadFromPrivKeyHexString(s string) Keypair

func New

func New(seed []byte) Keypair

func (Keypair) PrivateKey

func (kp Keypair) PrivateKey() PrivateKey

func (Keypair) PublicKey

func (kp Keypair) PublicKey() PublicKey

func (Keypair) SaveAsKeystore

func (kp Keypair) SaveAsKeystore(password, datadir string, useLightweightKDF bool) (string, error)

func (Keypair) SharedSecret

func (kp Keypair) SharedSecret(pubKey PublicKey) (SharedKey, error)

func (Keypair) SignMsg

func (kp Keypair) SignMsg(msg []byte) []byte

type Keystore

type Keystore struct {
	PubKey string
	Crypto CryptoJson `json:"crtpto"`
	// contains filtered or unexported fields
}

func LoadKeystore

func LoadKeystore(filepath string) (*Keystore, error)

func (Keystore) Filepath

func (ks Keystore) Filepath() string

func (*Keystore) Persistence

func (ks *Keystore) Persistence() error

持久化

func (*Keystore) Unlock

func (ks *Keystore) Unlock(password string) (Keypair, error)

type PrivateKey

type PrivateKey [PrivateKeyLength]byte

func NewPrivateKey

func NewPrivateKey(seed []byte) PrivateKey

func (PrivateKey) Bytes

func (pk PrivateKey) Bytes() []byte

func (PrivateKey) GetPubKey

func (pk PrivateKey) GetPubKey() PublicKey

func (PrivateKey) HexString

func (pk PrivateKey) HexString() string

func (*PrivateKey) LoadFromBytes

func (pk *PrivateKey) LoadFromBytes(d []byte) error

func (*PrivateKey) LoadFromHex

func (pk *PrivateKey) LoadFromHex(s string) error

func (PrivateKey) SignMsg

func (pk PrivateKey) SignMsg(msg []byte) []byte

func (PrivateKey) ToEd25519PrivKey

func (pk PrivateKey) ToEd25519PrivKey() ed25519.PrivateKey

type PublicKey

type PublicKey [PublicKeyLength]byte

func (PublicKey) Bytes

func (pk PublicKey) Bytes() []byte

func (PublicKey) HexString

func (pk PublicKey) HexString() string

func (*PublicKey) LoadFromBytes

func (pk *PublicKey) LoadFromBytes(d []byte) error

func (PublicKey) VerifyMsg

func (pk PublicKey) VerifyMsg(originMsg, signMsg []byte) bool

type SharedKey

type SharedKey [32]byte

func SharedSecret

func SharedSecret(privK PrivateKey, pubK PublicKey) (SharedKey, error)

func (SharedKey) DecryptCBC

func (key SharedKey) DecryptCBC(data, iv []byte) ([]byte, error)

func (SharedKey) EncryptCBC

func (key SharedKey) EncryptCBC(data, iv []byte) ([]byte, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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