subkey

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2022 License: MIT Imports: 10 Imported by: 13

README

go-subkey

Go Reference

Subkey port in Go

Usage

Generate Key pair
Sr25519
    kr, err := sr25519.Scheme{}.Generate()
Ed25519
    kr, err := ed25519.Scheme{}.Generate()
Ecdsa
    kr, err := ecdsa.Scheme{}.Generate()
Deriving keypair from a mnemonic or seed
Mnemonic
    uri := "crowd swamp sniff machine grid pretty client emotion banana cricket flush soap//foo//42///password"
    scheme := sr25519.Scheme{}
    kr, err := subkey.DeriveKeyPair(scheme, uri)
Hex encoded Seed
    uri := "0x6ea8835d60351a39a1e2293b2902d7bd6e12e526e72c46f4fda4a233809c4379"
    scheme := sr25519.Scheme{}
    kr, err := subkey.DeriveKeyPair(scheme, uri)
Hex encoded Seed with derivation
    uri := "0x6ea8835d60351a39a1e2293b2902d7bd6e12e526e72c46f4fda4a233809c4379//foo//42///password"
    scheme := sr25519.Scheme{}
    kr, err := subkey.DeriveKeyPair(scheme, uri)
Sign and verify using Keypair
    kr, err := ed25519.Scheme{}.Generate()
    msg := []byte("test message")
    sig, err := kr.Sign(msg)
    ok := kr.Verify(msg, sig)

Documentation

Index

Constants

View Source
const (
	// DevPhrase is default phrase used for dev test accounts
	DevPhrase = "bottom drive obey lake curtain smoke basket hold race lonely fit walk"
)

Variables

This section is empty.

Functions

func DecodeHex

func DecodeHex(uri string) ([]byte, bool)

DecodeHex decodes the hex string to bytes. `0x` prefix is accepted.

func EncodeHex

func EncodeHex(b []byte) string

EncodeHex encodes bytes to hex `0x` prefix is added.

func SS58Decode

func SS58Decode(address string) (uint16, []byte, error)

SS58Decode decodes an SS58 checksumed value into its data and format.

func SS58Encode

func SS58Encode(pubkey []byte, format uint16) string

SS58Encode encodes data and format identifier to an SS58 checksumed string.

Types

type DeriveJunction

type DeriveJunction struct {
	ChainCode [32]byte
	IsHard    bool
}

type KeyPair

type KeyPair interface {
	Signer
	PublicKey

	// Seed returns the seed of the pair
	Seed() []byte
}

KeyPair can sign, verify using a seed and public key

func DeriveKeyPair

func DeriveKeyPair(scheme Scheme, uri string) (kp KeyPair, err error)

DeriveKeyPair derives the Keypair from the URI using the provided cryptography scheme.

type PublicKey

type PublicKey interface {
	Verifier

	// Public returns the pub key in bytes.
	Public() []byte

	// AccountID returns the accountID for this key
	AccountID() []byte

	// SS58Address returns the Base58 public key with checksum and network identifier.
	SS58Address(network uint16) string
}

PublicKey can verify and be converted to SS58 addresses

type Scheme

type Scheme interface {
	fmt.Stringer
	Generate() (KeyPair, error)
	FromSeed(seed []byte) (KeyPair, error)
	FromPhrase(phrase, password string) (KeyPair, error)
	Derive(pair KeyPair, djs []DeriveJunction) (KeyPair, error)
	FromPublicKey([]byte) (PublicKey, error)
}

Scheme represents a cryptography scheme.

type Signer

type Signer interface {
	Sign(msg []byte) ([]byte, error)
}

Signer signs the message and returns the signature.

type Verifier

type Verifier interface {
	Verify(msg []byte, signature []byte) bool
}

Verifier verifies the signature.

Directories

Path Synopsis
cmd
nolint
nolint

Jump to

Keyboard shortcuts

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