subkey

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: MIT Imports: 10 Imported by: 0

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 DerivePath

func DerivePath(path string) (parts []string)

func EncodeHex

func EncodeHex(b []byte) string

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

func SS58Address

func SS58Address(accountID []byte, network uint8) (string, error)

SS58Address derives ss58 address from the accountID and network uses SS58Checksum checksum type SS58Checksum uses the concat(network, accountID) as blake2b hash pre-image More here: https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)#checksum-types

func SS58AddressWithAccountIDChecksum

func SS58AddressWithAccountIDChecksum(accountID []byte, network uint8) (string, error)

SS58AddressWithAccountIDChecksum derives ss58 address from the accountID, network uses AccountID checksum type AccountIDChecksum uses the accountID as the blake2b hash pre-image More here: https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)#checksum-types

Types

type DeriveJunction

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

func DeriveJunctions

func DeriveJunctions(codes []string) (djs []DeriveJunction, err error)

type KeyPair

type KeyPair interface {
	Signer
	Verifier

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

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

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

	// SS58Address returns the Base58 string.
	// uses SS58Checksum checksum type
	// SS58Checksum uses the concat(network, accountID) as blake2b hash pre-image
	// More here: https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)#checksum-types
	SS58Address(network uint8) (string, error)

	// SS58AddressWithAccountIDChecksum returns the Base58 string.
	// uses AccountID checksum type
	// AccountIDChecksum uses the accountID as the blake2b hash pre-image
	// More here: https://github.com/paritytech/substrate/wiki/External-Address-Format-(SS58)#checksum-types
	SS58AddressWithAccountIDChecksum(network uint8) (string, error)
}

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 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)
}

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