forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keys package: fundraiser compatibility and HD keys (BIP 39 & BIP 32 /…
… BIP 44) (cosmos#118) - fundraiser compatibility for HD keys (BIP 39 & BIP 32 / BIP 44)
- Loading branch information
Showing
28 changed files
with
590 additions
and
10,086 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.