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

Commit

Permalink
document things
Browse files Browse the repository at this point in the history
  • Loading branch information
liamsi committed Jun 12, 2018
1 parent af28f3a commit 54b9bd5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
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 @@ -4,23 +4,23 @@ import (
"bytes"
"io/ioutil"

. "github.com/tendermint/tmlibs/common"
cmn "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())
cmn.PanicSanity("Error encoding ascii armor: " + err.Error())
}
_, err = w.Write(data)
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
cmn.PanicSanity("Error encoding ascii armor: " + err.Error())
}
err = w.Close()
if err != nil {
PanicSanity("Error encoding ascii armor: " + err.Error())
cmn.PanicSanity("Error encoding ascii armor: " + err.Error())
}
return buf.String()
}
Expand Down
9 changes: 9 additions & 0 deletions keys/bip39/wordcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"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
)

Expand Down Expand Up @@ -39,12 +42,18 @@ func NewMnemonic(len ValidSentenceLen) (words []string, err error) {
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 is completely equivalent to 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) {
// we do not checksum here...
seed, err = bip39.NewSeedWithErrorChecking(mne, "")
Expand Down
14 changes: 12 additions & 2 deletions keys/hd/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const (
FullFundraiserPath = BIP44Prefix + "0'/0/0"
)

// TODO(ismail): add a constructor that takes a string for bip32
// BIP44Params wraps BIP 44 params (5 level BIP 32 path).
// To receive a canonical string representation ala
// m / purpose' / coin_type' / account' / change / address_index
// call String() on a BIP44Params instance.
type BIP44Params struct {
purpose uint32
coinType uint32
Expand All @@ -32,6 +35,8 @@ type BIP44Params struct {
addressIdx uint32
}

// NewParams creates a BIP 44 parameter object from the params:
// m / purpose' / coin_type' / account' / change / address_index
func NewParams(purpose, coinType, account uint32, change bool, addressIdx uint32) *BIP44Params {
return &BIP44Params{
purpose: purpose,
Expand All @@ -42,6 +47,9 @@ func NewParams(purpose, coinType, account uint32, change bool, addressIdx uint32
}
}

// NewFundraiserParams creates a BIP 44 parameter object from the params:
// m / 44' / 118' / account' / 0 / address_index
// The fixed parameters (purpose', coin_type', and change) are determined by what was used in the fundraiser.
func NewFundraiserParams(account uint32, addressIdx uint32) *BIP44Params {
return &BIP44Params{
// the following 2 params are fixed:
Expand Down Expand Up @@ -79,6 +87,7 @@ func ComputeMastersFromSeed(seed []byte) (secret [32]byte, chainCode [32]byte) {
// using the given chainCode.
func DerivePrivateKeyForPath(privKeyBytes [32]byte, chainCode [32]byte, path string) (derivedKey [32]byte, err error) {
data := privKeyBytes
// TODO(ismail): refactor this out and provide a constructor for BIP44
parts := strings.Split(path, "/")
for _, part := range parts {
// do we have an apostrophe?
Expand Down Expand Up @@ -149,7 +158,8 @@ func uint32ToBytes(i uint32) []byte {
// i64 returns the two halfs of the SHA512 HMAC of key and data.
func i64(key []byte, data []byte) (IL [32]byte, IR [32]byte) {
mac := hmac.New(sha512.New, key)
mac.Write(data)
// sha512 does not err
_, _ = mac.Write(data)
I := mac.Sum(nil)
copy(IL[:], I[:32])
copy(IR[:], I[32:])
Expand Down
28 changes: 7 additions & 21 deletions keys/keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@ func TestKeyManagement(t *testing.T) {
keyS, err = cstore.List()
require.NoError(t, err)
require.Equal(t, 1, len(keyS))

// make sure that it only signs with the right password
// tx := mock.NewSig([]byte("mytransactiondata"))
// err = cstore.Sign(n2, p1, tx)
// assert.NotNil(t, err)
// err = cstore.Sign(n2, p2, tx)
// assert.Nil(t, err, "%+v", err)
// sigs, err := tx.Signers()
// assert.Nil(t, err, "%+v", err)
// if assert.Equal(t, 1, len(sigs)) {
// assert.Equal(t, i2.PubKey, sigs[0])
// }
}

// TestSignVerify does some detailed checks on how we sign and validate
Expand All @@ -125,10 +113,10 @@ func TestSignVerify(t *testing.T) {
armor, err := cstore.ExportPubKey(n2)
require.Nil(t, err)
cstore.ImportPubKey(n3, armor)
// TODO(ismail): fix this ...
// i3, err := cstore.Get(n3)
// require.Nil(t, err)
// require.Equal(t, i3.PrivKeyArmor, "")
i3, err := cstore.Get(n3)
require.NoError(t, err)
require.Equal(t, i3.GetName(), n3)


// let's try to sign some messages
d1 := []byte("my first message")
Expand Down Expand Up @@ -197,16 +185,14 @@ func TestExportImport(t *testing.T) {
db,
)

info, _, err := cstore.CreateMnemonic("john", keys.English,"passphrase", keys.Secp256k1)
info, _, err := cstore.CreateMnemonic("john", keys.English,"secretcpw", keys.Secp256k1)
assert.NoError(t, err)
assert.Equal(t, info.GetName(), "john")
addr := info.GetPubKey().Address()
// TODO(ismail): why is addr not used above ?

john, err := cstore.Get("john")
assert.NoError(t, err)
assert.Equal(t, info.GetName(), "john")
addr = info.GetPubKey().Address()
johnAddr := info.GetPubKey().Address()

armor, err := cstore.Export("john")
assert.NoError(t, err)
Expand All @@ -217,7 +203,7 @@ func TestExportImport(t *testing.T) {
john2, err := cstore.Get("john2")
assert.NoError(t, err)

assert.Equal(t, john.GetPubKey().Address(), addr)
assert.Equal(t, john.GetPubKey().Address(), johnAddr)
assert.Equal(t, john.GetName(), "john")
assert.Equal(t, john, john2)
}
Expand Down

0 comments on commit 54b9bd5

Please sign in to comment.