cckat

package module
v0.0.0-...-b3e7098 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 13 Imported by: 0

README

cckat - CryptoCurrency Keys and Addresses Tools

A lightweight golang library for generation, format conversion and other operations on private keys, public keys and cryptocurrency addresses using the Secp256k1 elliptic curve e.g. for Bitcoin and Ethereum.

  • Key generation with the possibility of using an additional source of entropy (e.g. file with random data) and mixing it with rand.Reader. See https://datatracker.ietf.org/doc/html/rfc4086#section-5.1;
  • supported private key formats: WIF, HEX, []byte, big.Int, BIP38 encrypt;
  • supported public key formats: compressed, uncompressed, X-only;
  • BIP38 encrypting, decrypting (no EC multiply).

Can be used in particular for cold wallets.

Now supported:
Bitcoin
  • P2PKH - Pay to pubkey hash
  • P2PKHUncomp - Pay to pubkey hash (uncompressed pubkey)
  • P2SH - Pay to script hash
  • P2WPKH - Pay to witness pubkey hash
  • P2TR - Pay to taproot
Ethereum
  • ETH - Ethereum address (mixed-case checksum)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PKeyOutOfR   = errors.New("private key out of range")
	CoordOutOfR  = errors.New("coordinate(s) out of range")
	NoSuchPoin   = errors.New("no such point exists")
	InvHexStr    = errors.New("invalid hex string")
	InvWIF       = errors.New("invalid WIF")
	InvPubKeyF   = errors.New("invalid public key format")
	InvWIFCSum   = errors.New("invalid WIF checksum")
	InvAddrType  = errors.New("invalid address type")
	BIP38InvCSum = errors.New("BIP38 checksum invalid")
	BIP38PassErr = errors.New("BIP38 wrong password")
)
View Source
var B58Alphabet = []byte("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")

Functions

func Base58Decode

func Base58Decode(inp []byte) ([]byte, error)

Base58Decode decodes Base58-encoded data

func Base58Encode

func Base58Encode(input []byte) []byte

Base58Encode encodes a byte array to Base58

func Bech32mencode

func Bech32mencode(data []byte, hrp string, ver int) string

Bech32mencode encodes a segwit (Bech32 BIP 0173 / Bech32m BIP 0350) address.

func Convbits85

func Convbits85(p []byte) []int

Convbits85 converts a byte slice (8 bits) to int slice (5 bits).

func Encrypt

func Encrypt(k PrKey, passphrase string) string

Encrypt returns the encrypted private key (BIP038) for the private key k. (no EC multiply).

func GetAddressETH

func GetAddressETH(pubKey []byte) (string, error)

GetAddressETH returns the Ethereum address (mixed-case checksum).

func GetAddressP2PKH

func GetAddressP2PKH(pubKey []byte) (string, error)

GetAddressP2PKH returns the Pay-to-pubkey-hash Bitcoin address (compressed pubkey).

func GetAddressP2PKHUncomp

func GetAddressP2PKHUncomp(pubKey []byte) (string, error)

GetAddressP2PKHUncomp returns the Pay-to-pubkey-hash Bitcoin address (uncompressed pubkey).

func GetAddressP2SH

func GetAddressP2SH(pubKey []byte) (string, error)

GetAddressP2SH returns the Pay-to-script-hash Bitcoin address.

func GetAddressP2TR

func GetAddressP2TR(pubKey []byte) (string, error)

GetAddressP2TR returns the Pay-to-taproot Bitcoin address with pubKey as internal key.

func GetAddressP2WPKH

func GetAddressP2WPKH(pubKey []byte) (string, error)

GetAddressP2WPKH returns the Pay-to-witness-pubkey-hash Bitcoin address.

func HashPubKey

func HashPubKey(pubKey []byte) []byte

HashPubKey returns the Hash160 hash of the pubkey.

func Keccak256Hash

func Keccak256Hash(data []byte) (h []byte)

Keccak256Hash returns the legacy Keccak256 hash of data

func PointFromX

func PointFromX(xb []byte, even bool) (x, y *big.Int)

PointFromX returns the x and y coordinates of the point for which x coordinate and even/odd of y coordinate are given. WARNING!!! This function does not check is the coordinate valid and if the point exists. Use PointFromXc if yoy want to check.

func PointFromXc

func PointFromXc(xb []byte, even bool) (*big.Int, *big.Int, error)

PointFromXc returns the x and y coordinates of the point for which x coordinate and even/odd of y coordinate are given. If the point does not exist, an error will be returned

func PubKey

func PubKey(k *big.Int, uncomp bool) []byte

PubKey returns the public key of the private key k in compressed format if comp == true or in uncompressed format if comp == false.

func PubKeyCompUncomp

func PubKeyCompUncomp(k []byte, comp bool) ([]byte, error)

PubKeyCompUncomp returns public key in compressed format if comp == true or in uncompressed format if comp == false. k - public key in compressed or uncompressed format. Returns nil and InvPubKeyF error if the public key format is invalid. WARNING!!! This function checks only the key format. If the point is not on the curve (or at infinity), the behavior is undefined. Use the IsOnCurve function to check this.

func RandFieldElement

func RandFieldElement(rand io.Reader) (k *big.Int, err error)

RandFieldElement returns a random element of the field underlying the given curve using the procedure given in [NSA] A.2.1.

Implementation copied from Go's crypto/ecdsa package since the function wasn't public.

func RandFieldElementEx

func RandFieldElementEx(rand io.Reader, ex []byte) (k *big.Int, err error)

RandFieldElementEx returns a random element of the field. It obtains input from two of sources (rand io.Reader and ex []byte) and mixes them. See https://datatracker.ietf.org/doc/html/rfc4086#section-5.1

Returned RandFieldElement(rand) if len(ex) == 0

Types

type AddressType

type AddressType uint
const (
	P2PKH       AddressType = iota // Pay-to-pubkey-hash
	P2PKHUncomp                    // Pay-to-pubkey-hash (uncompressed pubkey)
	P2SH                           // Pay-to-script-hash
	P2WPKH                         // Pay-to-witness-pubkey-hash
	P2TR                           // Pay-to-taproot
	ETH                            // Ethereum address (mixed-case checksum)
	MaxType
)

Possible types of the addresses

type PrKey

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

func Decrypt

func Decrypt(b, password string) (*PrKey, error)

Decrypt BIP38 string which does not have the ECMultiply flag set

func (*PrKey) Address

func (k *PrKey) Address() (a string)

Address returns the address of the type k.a

func (*PrKey) AddressT

func (k *PrKey) AddressT(at AddressType) (a string)

AddressT returns an address of the given type

func (*PrKey) BIP38

func (k *PrKey) BIP38(p string) string

BIP38 returns the BIP38 encoded private key k.k.

func (*PrKey) Bytes

func (k *PrKey) Bytes() []byte

Bytes returns the private key as 32 byte slice.

func (*PrKey) GetAddressType

func (k *PrKey) GetAddressType() AddressType

func (*PrKey) Hex

func (k *PrKey) Hex() string

Hex returns the private key in HEX format.

func (*PrKey) IsUncomp

func (k *PrKey) IsUncomp() bool

func (*PrKey) PubK

func (k *PrKey) PubK() []byte

PubK returns the uncompressed public key

func (*PrKey) Set

func (k *PrKey) Set(key big.Int) (*PrKey, error)

Set set k.k. to the value of key and returns k, nil. nil, PKeyOutOfR will be returned if k < 1 or k greater than the order of the base point - 1.

func (*PrKey) SetAddressType

func (k *PrKey) SetAddressType(t AddressType) (*PrKey, error)

SetAddressType sets k.a to t and returns k, error. If error != nil, (nil, error) returned.

func (*PrKey) SetBIP38

func (k *PrKey) SetBIP38(w, p string) (*PrKey, error)

SetBIP38 tries to decrypt the BIP38 encrypted private key w with a password p, sets k.k to its value and returns k, error. If error != nil, (nil, error) returned.

func (*PrKey) SetBytes

func (k *PrKey) SetBytes(b []byte) (*PrKey, error)

SetBytes interprets b as the bytes of a big-endian unsigned integer, sets k.k to that value, and returns k, error. If error != nil, (nil, error) returned.

func (*PrKey) SetHex

func (k *PrKey) SetHex(khex string) (*PrKey, error)

SetHex set k.k to the value of khex, interpreted as base 16 and returns k, error. khex must be 64 hex digits long and may be prefixed with "0X" or "0x". If error != nil, (nil, error) returned.

func (*PrKey) SetUncomp

func (k *PrKey) SetUncomp(u bool) *PrKey

func (*PrKey) SetWIF

func (k *PrKey) SetWIF(w string) (*PrKey, error)

SetWIF interprets w as the WIF private key, sets k.k to that value, and returns k, error. If error != nil, (nil, error) returned.

func (*PrKey) WIF

func (k *PrKey) WIF() string

WIF returns the private key in WIF format.

type Secp256k1C

type Secp256k1C struct {
	P       *big.Int // the order of the underlying field
	N       *big.Int // the order of the base point
	B       *big.Int // the constant of the Secp256k1C equation
	Gx, Gy  *big.Int // (x,y) of the base point
	BitSize int      // the size of the underlying field
}

func Secp256k1

func Secp256k1() *Secp256k1C

func (*Secp256k1C) Add

func (curve *Secp256k1C) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int)

func (*Secp256k1C) Double

func (curve *Secp256k1C) Double(x1, y1 *big.Int) (*big.Int, *big.Int)

func (*Secp256k1C) IsOnCurve

func (curve *Secp256k1C) IsOnCurve(x, y *big.Int) bool

IsOnCurve returns true if the given (x,y) lies on the BitCurve.

func (*Secp256k1C) Params

func (curve *Secp256k1C) Params() *Secp256k1C

func (*Secp256k1C) ScalarBaseMult

func (curve *Secp256k1C) ScalarBaseMult(k []byte) (*big.Int, *big.Int)

func (*Secp256k1C) ScalarMult

func (curve *Secp256k1C) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int)

Jump to

Keyboard shortcuts

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