keys

package
v0.7.0-rc0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2018 License: Apache-2.0 Imports: 10 Imported by: 51

Documentation

Index

Examples

Constants

View Source
const (
	AlgoEd25519   = CryptoAlgo("ed25519")
	AlgoSecp256k1 = CryptoAlgo("secp256k1")
)

Variables

This section is empty.

Functions

func New added in v0.5.0

func New(db dbm.DB, codec words.Codec) dbKeybase
Example
package main

import (
	"fmt"

	dbm "github.com/tendermint/tmlibs/db"

	"github.com/tendermint/go-crypto/keys"
	"github.com/tendermint/go-crypto/keys/words"
)

func main() {
	// Select the encryption and storage for your cryptostore
	cstore := keys.New(
		dbm.NewMemDB(),
		words.MustLoadCodec("english"),
	)
	ed := keys.AlgoEd25519
	sec := keys.AlgoSecp256k1

	// Add keys and see they return in alphabetical order
	bob, _, err := cstore.Create("Bob", "friend", ed)
	if err != nil {
		// this should never happen
		fmt.Println(err)
	} else {
		// return info here just like in List
		fmt.Println(bob.Name)
	}
	cstore.Create("Alice", "secret", sec)
	cstore.Create("Carl", "mitm", ed)
	info, _ := cstore.List()
	for _, i := range info {
		fmt.Println(i.Name)
	}

	// We need to use passphrase to generate a signature
	tx := []byte("deadbeef")
	sig, pub, err := cstore.Sign("Bob", "friend", tx)
	if err != nil {
		fmt.Println("don't accept real passphrase")
	}

	// and we can validate the signature with publically available info
	binfo, _ := cstore.Get("Bob")
	if !binfo.PubKey.Equals(bob.PubKey) {
		fmt.Println("Get and Create return different keys")
	}

	if pub.Equals(binfo.PubKey) {
		fmt.Println("signed by Bob")
	}
	if !pub.VerifyBytes(tx, sig) {
		fmt.Println("invalid signature")
	}

}
Output:

Bob
Alice
Bob
Carl
signed by Bob

Types

type CryptoAlgo added in v0.5.0

type CryptoAlgo string

type Info

type Info struct {
	Name         string        `json:"name"`
	PubKey       crypto.PubKey `json:"pubkey"`
	PrivKeyArmor string        `json:"privkey.armor"`
}

Info is the public information about a key

func (Info) Address

func (i Info) Address() []byte

Address is a helper function to calculate the address from the pubkey

type Keybase added in v0.5.0

type Keybase interface {
	// Sign some bytes
	Sign(name, passphrase string, msg []byte) (crypto.Signature, crypto.PubKey, error)
	// Create a new keypair
	Create(name, passphrase string, algo CryptoAlgo) (info Info, seed string, err error)
	// Recover takes a seedphrase and loads in the key
	Recover(name, passphrase, seedphrase string) (info Info, erro error)
	List() ([]Info, error)
	Get(name string) (Info, error)
	Update(name, oldpass, newpass string) error
	Delete(name, passphrase string) error

	Import(name string, armor string) (err error)
	ImportPubKey(name string, armor string) (err error)
	Export(name string) (armor string, err error)
	ExportPubKey(name string) (armor string, err error)
}

Keybase allows simple CRUD on a keystore, as an aid to signing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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