ecdsa

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: 10 Imported by: 0

Documentation

Overview

Example
package main

import (
	stdecdsa "crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"crypto/sha256"
	"encoding/hex"
	"fmt"

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

var seedData = "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542"

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

	// generate the root key
	root, err := ecdsa.NewKeyFromSeed(seed, elliptic.P256())
	if err != nil {
		panic(err)
	}

	path := hdw.Path{0, 1, 2}
	// 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[:], nil)
	if err != nil {
		panic(err)
	}

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

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

	// derive the public key from the root's public
	pub2, err := root.ExtendedPublic().DerivePath(path)
	if err != nil {
		panic(err)
	}
	// verify the signature
	ok = stdecdsa.VerifyASN1(pub2.Naked().(*stdecdsa.PublicKey), digest[:], sig)
	fmt.Printf("signature ok: %t\n", ok)
}
Output:

signature ok: true
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

This section is empty.

Functions

This section is empty.

Types

type PrivateKey

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

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

func NewKeyFromSeed

func NewKeyFromSeed(seed []byte, curve elliptic.Curve) (*PrivateKey, error)

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

func NewKeyFromSeedWithHMACKey

func NewKeyFromSeedWithHMACKey(seed []byte, curve elliptic.Curve, key string) *PrivateKey

NewKeyFromSeed generates the root key from the seed using a custom HMAC key. Can be used with custom curves.

func (*PrivateKey) Chain

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

Chain returns the chain code

func (*PrivateKey) Derive

func (p *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 {
	ecdsa.PublicKey
	ChainCode []byte
}

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

func (*PublicKey) Bytes

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

Bytes returns the serialized public key data in a compressed form

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)

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

func (*PublicKey) DerivePath

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

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

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