ed25519

package
v0.0.0-...-0b9e0a5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: MIT Imports: 8 Imported by: 1

Documentation

Overview

Package ed25519 deals with SLIP-10/Ed25519 keys. Unlike SLIP-10/ECDSA ones these keys can be used for hardened derivation only.

Example
package main

import (
	"crypto"
	stded25519 "crypto/ed25519"
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"fmt"

	"github.com/ecadlabs/hdw"
	"github.com/ecadlabs/hdw/ed25519"
)

var seedData = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"

func main() {
	// alternatively use hdw.NewSeedFromMnemonic
	seed, err := hex.DecodeString(seedData)
	if err != nil {
		panic(err)
	}

	// generate the root key
	root := ed25519.NewKeyFromSeed(seed)

	path := hdw.Path{0 | hdw.Hard, 1 | hdw.Hard, 2 | hdw.Hard}
	// generate the derivative child private key
	priv, err := root.DerivePath(path)
	if err != nil {
		panic(err)
	}

	digest := sha256.Sum256([]byte("text"))
	sig, err := priv.Sign(rand.Reader, digest[:], crypto.Hash(0))
	if err != nil {
		panic(err)
	}

	// get the corresponding public key
	pub := priv.Public()

	// verify the signature
	ok := stded25519.Verify(pub.(stded25519.PublicKey), digest[:], sig)
	fmt.Printf("signature ok: %t\n", ok)

}
Output:

signature ok: true

Index

Examples

Constants

View Source
const (
	// MinSeedSize is the minimal allowed seed byte length
	MinSeedSize = 16
	// MaxSeedSize is the maximal allowed seed byte length
	MaxSeedSize = 64
)

Variables

View Source
var (
	// ErrNonHardened is returned when an attempt to use non hardened path was made
	ErrNonHardened = errors.New("ed25519: non hardened derivation")
	// ErrPublic is returned when an attempt to derive a public key's child was made
	ErrPublic = errors.New("ed25519: can't use public key for derivation")
)

Functions

This section is empty.

Types

type PrivateKey

type PrivateKey struct {
	ed25519.PrivateKey
	ChainCode []byte
}

PrivateKey is the extended Ed25519 private key. It implements hdw.PrivateKey

func NewKeyFromSeed

func NewKeyFromSeed(seed []byte) *PrivateKey

NewKeyFromSeed generates the root key from the seed as specified in SLIP-10

func (*PrivateKey) Chain

func (p *PrivateKey) Chain() []byte

Chain returns the chain code

func (*PrivateKey) Derive

func (s *PrivateKey) Derive(index uint32) (hdw.PrivateKey, error)

Derive returns a child key of the receiver using a single index

func (*PrivateKey) DerivePath

func (s *PrivateKey) DerivePath(path hdw.Path) (hdw.PrivateKey, error)

Derive returns a child key of the receiver using a full path

func (*PrivateKey) ExtendedPublic

func (p *PrivateKey) ExtendedPublic() hdw.PublicKey

ExtendedPublic returns the extended public key corresponding to the receiver

func (*PrivateKey) Naked

func (p *PrivateKey) Naked() crypto.PrivateKey

Naked returns the naked private key that can be used with the standard Go crypto library

type PublicKey

type PublicKey struct {
	ed25519.PublicKey
	ChainCode []byte
}

PublicKey is the extended Ed25519 public key. It implements hdw.PublicKey

func (*PublicKey) Chain

func (p *PublicKey) Chain() []byte

Chain returns the chain code

func (*PublicKey) Derive

func (p *PublicKey) Derive(index uint32) (hdw.PublicKey, error)

As SLIP10-Ed25519 doesn't support non hardened derivation this function always returns ErrPublic

func (*PublicKey) DerivePath

func (s *PublicKey) DerivePath(path hdw.Path) (hdw.PublicKey, error)

As SLIP10-Ed25519 doesn't support non hardened derivation this function always returns ErrPublic

func (*PublicKey) Naked

func (p *PublicKey) Naked() crypto.PublicKey

Naked returns the naked public key that can be used with the standard Go crypto library

Jump to

Keyboard shortcuts

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