Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

Commit

Permalink
cleanup, renaming according to latest discussions
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Jun 8, 2018
1 parent bdff560 commit ca85172
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
25 changes: 17 additions & 8 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 @@ -29,10 +29,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
10 changes: 7 additions & 3 deletions keys/hd/hdpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func ExampleStringifyPathParams() {

func ExampleSomeBIP32TestVecs() {

seed := bip39.MnemonicToSeed("barrel original fuel morning among eternal filter ball stove pluck matrix mechanic")
seed := bip39.MnemonicToSeed("barrel original fuel morning among eternal " +
"filter ball stove pluck matrix mechanic")
master, ch := ComputeMastersFromSeed(seed)
fmt.Println("keys from fundraiser test-vector (cosmos, bitcoin, ether)")
fmt.Println()
Expand All @@ -32,12 +33,15 @@ func ExampleSomeBIP32TestVecs() {
fmt.Println("keys generated via https://coinomi.com/recovery-phrase-tool.html")
fmt.Println()

seed = bip39.MnemonicToSeed("advice process birth april short trust crater change bacon monkey medal garment gorilla ranch hour rival razor call lunar mention taste vacant woman sister")
seed = bip39.MnemonicToSeed(
"advice process birth april short trust crater change bacon monkey medal garment " +
"gorilla ranch hour rival razor call lunar mention taste vacant woman sister")
master, ch = ComputeMastersFromSeed(seed)
priv, _ = DerivePrivateKeyForPath(master, ch, "44'/1'/1'/0/4")
fmt.Println(hex.EncodeToString(priv[:]))

seed = bip39.MnemonicToSeed("idea naive region square margin day captain habit gun second farm pact pulse someone armed")
seed = bip39.MnemonicToSeed("idea naive region square margin day captain habit " +
"gun second farm pact pulse someone armed")
master, ch = ComputeMastersFromSeed(seed)
priv, _ = DerivePrivateKeyForPath(master, ch, "44'/0'/0'/0/420")
fmt.Println(hex.EncodeToString(priv[:]))
Expand Down
25 changes: 14 additions & 11 deletions keys/keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func New(db dbm.DB) dbKeybase {

var _ Keybase = dbKeybase{}

// Create generates a new key and persists it to storage, encrypted
// CreateMnemonic generates a new key and persists it to storage, encrypted
// using the provided password.
// It returns the generated mnemonic and the key Info.
// It returns an error if it fails to
// generate a key for the given algo type, or if another key is
// already stored under the same name.
func (kb dbKeybase) Create(name, language, passwd string, algo CryptoAlgo) (info *Info, mnemonic string, err error) {
func (kb dbKeybase) CreateMnemonic(name, language, passwd string, algo CryptoAlgo) (info *Info, mnemonic string, err error) {
if algo != AlgoSecp256k1 {
err = fmt.Errorf("currently only Secp256k1 are supported as required by bip39/bip44, requested %s", algo)
return
Expand All @@ -46,14 +46,14 @@ func (kb dbKeybase) Create(name, language, passwd string, algo CryptoAlgo) (info
// a helper function for that
mnemonic = strings.Join(mnemonicS, " ")
seed := bip39.MnemonicToSeed(mnemonic)
info = kb.persistDerivedKey(seed, passwd, name, hd.FullFundraiserPath)
info, err = kb.persistDerivedKey(seed, passwd, name, hd.FullFundraiserPath)
return
}

// Recover converts a seedphrase to a private key and persists it,
// encrypted with the given passphrase. Functions like Create, but
// CreateFundraiserKey converts a seedphrase to a private key and persists it,
// encrypted with the given passphrase. Functions like CreateMnemonic, but
// seedphrase is input not output.
func (kb dbKeybase) Recover(name, mnemonic, passwd string) (info *Info, err error) {
func (kb dbKeybase) CreateFundraiserKey(name, mnemonic, passwd string) (info *Info, err error) {
words := strings.Split(mnemonic, " ")
if len(words) != 12 {
err = fmt.Errorf("recovering only works with 12 word (fundraiser) mnemonics, got: %v words", len(words))
Expand All @@ -63,27 +63,30 @@ func (kb dbKeybase) Recover(name, mnemonic, passwd string) (info *Info, err erro
if err != nil {
return
}
info = kb.persistDerivedKey(seed, passwd, name, hd.FullFundraiserPath)
info, err = kb.persistDerivedKey(seed, passwd, name, hd.FullFundraiserPath)
return
}

func (kb dbKeybase) Derive(name, mnemonic, passwd string,
account uint32, change bool, addressIdx uint32) (info *Info, err error) {

params := hd.NewFundraiserParams(account, change, addressIdx)
params := hd.NewFundraiserParams(account, addressIdx)
seed, err := bip39.MnemonicToSeedWithErrChecking(mnemonic)
if err != nil {
return
}
info = kb.persistDerivedKey(seed, passwd, name, params.String())
info, err = kb.persistDerivedKey(seed, passwd, name, params.String())

return
}

func (kb *dbKeybase) persistDerivedKey(seed []byte, passwd, name, fullHdPath string) (info *Info) {
func (kb *dbKeybase) persistDerivedKey(seed []byte, passwd, name, fullHdPath string) (info *Info, err error) {
// create master key and derive first key:
masterPriv, ch := hd.ComputeMastersFromSeed(seed)
derivedPriv := hd.DerivePrivateKeyForPath(masterPriv, ch, fullHdPath)
derivedPriv, err := hd.DerivePrivateKeyForPath(masterPriv, ch, fullHdPath)
if err != nil {
return
}

// if we have a password, use it to encrypt the private key and store it
// else store the public key only
Expand Down
30 changes: 15 additions & 15 deletions keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func TestKeyManagement(t *testing.T) {
//i, err := cstore.Get(n1)
//fmt.Println(i)
//assert.Error(t, err)
//i, _, err = cstore.Create(n1, "english", p1, algo)
//i, _, err = cstore.CreateMnemonic(n1, "english", p1, algo)
//require.Equal(t, n1, i.Name)
//require.Nil(t, err)
//_, _, err = cstore.Create(n2, "english", p2, algo)
//_, _, err = cstore.CreateMnemonic(n2, "english", p2, algo)
//require.Nil(t, err)
//
//// we can get these keys
Expand Down Expand Up @@ -88,10 +88,10 @@ func TestSignVerify(t *testing.T) {
//p1, p2, p3 := "1234", "foobar", "foobar"
//
//// create two users and get their info
//i1, _, err := cstore.Create(n1, p1, algo)
//i1, _, err := cstore.CreateMnemonic(n1, p1, algo)
//require.Nil(t, err)
//
//i2, _, err := cstore.Create(n2, p2, algo)
//i2, _, err := cstore.CreateMnemonic(n2, p2, algo)
//require.Nil(t, err)
//
//// Import a public key
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestSignWithLedger(t *testing.T) {
p := "hard2hack"
// create a nano user
c, _, err := cstore.Create(n, p, nano.KeyLedgerEd25519)
c, _, err := cstore.CreateMnemonic(n, p, nano.KeyLedgerEd25519)
require.Nil(t, err, "%+v", err)
assert.Equal(t, c.Key, n)
_, ok := c.PubKey.Unwrap().(nano.PubKeyLedgerEd25519)
Expand Down Expand Up @@ -225,7 +225,7 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// bip39.MustLoadCodec("english"),
// )
//
// info, _, err := cstore.Create("john", "passphrase", "english", keys.AlgoEd25519)
// info, _, err := cstore.CreateMnemonic("john", "passphrase", "english", keys.AlgoEd25519)
// assert.Nil(t, err)
// assert.Equal(t, info.Name, "john")
// addr := info.PubKey.Address()
Expand Down Expand Up @@ -257,8 +257,8 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// bip39.MustLoadCodec("english"),
// )
//
// // Create a private-public key pair and ensure consistency
// info, _, err := cstore.Create("john", "passphrase", keys.AlgoEd25519)
// // CreateMnemonic a private-public key pair and ensure consistency
// info, _, err := cstore.CreateMnemonic("john", "passphrase", keys.AlgoEd25519)
// assert.Nil(t, err)
// assert.NotEqual(t, info.PrivKeyArmor, "")
// assert.Equal(t, info.Name, "john")
Expand Down Expand Up @@ -305,7 +305,7 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// p1, p2 := "1234", "foobar"
//
// // make sure key works with initial password
// _, _, err := cstore.Create(n1, p1, algo)
// _, _, err := cstore.CreateMnemonic(n1, p1, algo)
// require.Nil(t, err, "%+v", err)
// assertPassword(t, cstore, n1, p1, p2)
//
Expand Down Expand Up @@ -355,7 +355,7 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// p1, p2 := "1234", "foobar"
//
// // make sure key works with initial password
// info, seed, err := cstore.Create(n1, p1, algo)
// info, seed, err := cstore.CreateMnemonic(n1, p1, algo)
// require.Nil(t, err, "%+v", err)
// assert.Equal(t, n1, info.Name)
// assert.NotEmpty(t, seed)
Expand All @@ -367,7 +367,7 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// require.NotNil(t, err)
//
// // let us re-create it from the seed-phrase
// newInfo, err := cstore.Recover(n2, p2, seed)
// newInfo, err := cstore.CreateFundraiserKey(n2, p2, seed)
// require.Nil(t, err, "%+v", err)
// assert.Equal(t, n2, newInfo.Name)
// assert.Equal(t, info.Address(), newInfo.Address())
Expand All @@ -384,16 +384,16 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// sec := keys.AlgoSecp256k1
//
// // Add keys and see they return in alphabetical order
// bob, _, err := cstore.Create("Bob", "friend", ed)
// bob, _, err := cstore.CreateMnemonic("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)
// cstore.CreateMnemonic("Alice", "secret", sec)
// cstore.CreateMnemonic("Carl", "mitm", ed)
// info, _ := cstore.List()
// for _, i := range info {
// fmt.Println(i.Name)
Expand All @@ -409,7 +409,7 @@ func assertPassword(t *testing.T, cstore keys.Keybase, name, pass, badpass strin
// // 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")
// fmt.Println("Get and CreateMnemonic return different keys")
// }
//
// if pub.Equals(binfo.PubKey) {
Expand Down
8 changes: 4 additions & 4 deletions keys/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
type Keybase interface {
// Sign some bytes
Sign(name, passwd string, msg []byte) (crypto.Signature, crypto.PubKey, error)
// Create a new keypair
Create(name, language, passwd string, algo CryptoAlgo) (info *Info, seed string, err error)
// Recover takes a seedphrase and loads in the key
Recover(name, mnemonic, seedphrase string) (info *Info, err error)
// CreateMnemonic a new keypair
CreateMnemonic(name, language, passwd string, algo CryptoAlgo) (info *Info, seed string, err error)
// CreateFundraiserKey takes a seedphrase and loads in the key
CreateFundraiserKey(name, mnemonic, seedphrase string) (info *Info, err error)
Derive(name, mnemonic, passwd string, account uint32, change bool, addressIdx uint32) (*Info, error)
List() ([]Info, error)
Get(name string) (*Info, error)
Expand Down
6 changes: 3 additions & 3 deletions random.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ func (ri *randInfo) MixEntropy(seedBytes []byte) {
hashBytes32 := [32]byte{}
copy(hashBytes32[:], hashBytes)
ri.seedBytes = xorBytes32(ri.seedBytes, hashBytes32)
// Create new cipher.Block
// CreateMnemonic new cipher.Block
var err error
ri.cipherAES256, err = aes.NewCipher(ri.seedBytes[:])
if err != nil {
PanicSanity("Error creating AES256 cipher: " + err.Error())
}
// Create new stream
// CreateMnemonic new stream
ri.streamAES256 = cipher.NewCTR(ri.cipherAES256, randBytes(aes.BlockSize))
// Create new reader
// CreateMnemonic new reader
ri.reader = &cipher.StreamReader{S: ri.streamAES256, R: crand.Reader}
}

Expand Down

0 comments on commit ca85172

Please sign in to comment.