Skip to content

Commit

Permalink
keys package: fundraiser compatibility and HD keys (BIP 39 & BIP 32 /…
Browse files Browse the repository at this point in the history
… BIP 44) (cosmos#118)

- fundraiser compatibility for HD keys (BIP 39 & BIP 32 / BIP 44)
  • Loading branch information
liamsi authored Jun 20, 2018
1 parent fed8807 commit 4634063
Show file tree
Hide file tree
Showing 28 changed files with 590 additions and 10,086 deletions.
12 changes: 6 additions & 6 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
name = "github.com/btcsuite/btcutil"
branch = "master"

[[constraint]]
name = "github.com/howeyc/crc16"
branch = "master"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ check: check_tools
# Command to generate the workd list (kept here for documentation purposes only):
wordlist:
# To re-generate wordlist.go run:
# go-bindata -ignore ".*\.go" -o keys/words/wordlist/wordlist.go -pkg "wordlist" keys/words/wordlist/...
# go-bindata -ignore ".*\.go" -o keys/words/bip39/wordlist.go -pkg "wordlist" keys/bip39/wordlist/...

build: wordlist
# Nothing else to build!
Expand Down
1 change: 1 addition & 0 deletions amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
RegisterAmino(cdc)
}

// RegisterAmino registers all go-crypto related types in the given (amino) codec.
func RegisterAmino(cdc *amino.Codec) {
cdc.RegisterInterface((*PubKey)(nil), nil)
cdc.RegisterConcrete(PubKeyEd25519{},
Expand Down
8 changes: 4 additions & 4 deletions armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ package crypto

import (
"bytes"
"fmt"
"io/ioutil"

. "github.com/tendermint/tmlibs/common"
"golang.org/x/crypto/openpgp/armor"
)

func EncodeArmor(blockType string, headers map[string]string, data []byte) string {
buf := new(bytes.Buffer)
w, err := armor.Encode(buf, blockType, headers)
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
panic(fmt.Errorf("could not encode ascii armor: %s", err))
}
_, err = w.Write(data)
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
panic(fmt.Errorf("could not encode ascii armor: %s", err))
}
err = w.Close()
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
panic(fmt.Errorf("could not encode ascii armor: %s", err))
}
return buf.String()
}
Expand Down
2 changes: 1 addition & 1 deletion keys/bcrypt/bcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ type hashed struct {
// to compare the returned hashed password with its cleartext version.
func GenerateFromPassword(salt []byte, password []byte, cost int) ([]byte, error) {
if len(salt) != maxSaltSize {
return nil, fmt.Errorf("Salt len must be %v", maxSaltSize)
return nil, fmt.Errorf("salt len must be %v", maxSaltSize)
}
p, err := newFromPassword(salt, password, cost)
if err != nil {
Expand Down
62 changes: 62 additions & 0 deletions keys/bip39/wordcodec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package bip39

import (
"strings"

"github.com/bartekn/go-bip39"
)

// ValidSentenceLen defines the mnemonic sentence lengths supported by this BIP 39 library.
type ValidSentenceLen uint8

const (
// FundRaiser is the sentence length used during the cosmos fundraiser (12 words).
FundRaiser ValidSentenceLen = 12
// FreshKey is the sentence length used for newly created keys (24 words).
FreshKey ValidSentenceLen = 24
)

// NewMnemonic will return a string consisting of the mnemonic words for
// the given sentence length.
func NewMnemonic(len ValidSentenceLen) (words []string, err error) {
// len = (ENT + checksum) / 11
var ENT int
switch len {
case FundRaiser:
ENT = 128
case FreshKey:
ENT = 256
}
var entropy []byte
entropy, err = bip39.NewEntropy(ENT)
if err != nil {
return
}
var mnemonic string
mnemonic, err = bip39.NewMnemonic(entropy)
if err != nil {
return
}
words = strings.Split(mnemonic, " ")
return
}

// MnemonicToSeed creates a BIP 39 seed from the passed mnemonic (with an empty BIP 39 password).
// This method does not validate the mnemonics checksum.
func MnemonicToSeed(mne string) (seed []byte) {
// we do not checksum here...
seed = bip39.NewSeed(mne, "")
return
}

// MnemonicToSeedWithErrChecking returns the same seed as MnemonicToSeed.
// It creates a BIP 39 seed from the passed mnemonic (with an empty BIP 39 password).
//
// Different from MnemonicToSeed it validates the checksum.
// For details on the checksum see the BIP 39 spec.
func MnemonicToSeedWithErrChecking(mne string) (seed []byte, err error) {
seed, err = bip39.NewSeedWithErrorChecking(mne, "")
return
}


15 changes: 15 additions & 0 deletions keys/bip39/wordcodec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package bip39

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWordCodec_NewMnemonic(t *testing.T) {
_, err := NewMnemonic(FundRaiser)
assert.NoError(t, err, "unexpected error generating fundraiser mnemonic")

_, err = NewMnemonic(FreshKey)
assert.NoError(t, err, "unexpected error generating new 24-word mnemonic")
}
Loading

0 comments on commit 4634063

Please sign in to comment.