Skip to content

Commit

Permalink
PubKey proto types (#7147)
Browse files Browse the repository at this point in the history
* WIP on protobuf keys

* Use Type() and Bytes() in sr25519 pub key Equals

* Add tests

* Add few more tests

* Update other pub/priv key types Equals

* Fix PrivKey's Sign method

* Rename variables in tests

* Fix infinite recursive calls

* Use tm ed25519 keys

* Add Sign and VerifySignature tests

* Remove ed25519 and sr25519 references

* proto linting

* Add proto crypto file

* Implement some of the new multisig proto type methods

* Add tests for MultisigThresholdPubKey

* Add tests for pubkey pb/amino conversion functions

* Move crypto types.go and register new proto pubkeys

* Add missing pointer ref

* Address review comments

* panic in MultisigThresholdPubKey VerifySignature

* Use internal crypto.PubKey in multisig

* Add tests for MultisigThresholdPubKey VerifyMultisignature

* Only keep LegacyAminoMultisigThresholdPubKey and move to proto keys to v1

* Remove conversion functions and introduce internal PubKey type

* Remove old secp256k1 PubKey and PrivKey

* Uncomment test case

* Fix linting issues

* More linting

* Revert tests keys values

* Add Amino overrides to proto keys

* Add pubkey test

* Fix tests

* Use threshold isntead of K

* Standardize Type

* Revert standardize types commit

* Add comment

* Simplify proto names

* Add comment about multisig codec

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored Sep 16, 2020
1 parent d4266dc commit 320a852
Show file tree
Hide file tree
Showing 26 changed files with 2,205 additions and 677 deletions.
9 changes: 9 additions & 0 deletions codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@ type (
Size() int
Unmarshal(data []byte) error
}

// AminoMarshaler defines an interface where Amino marshalling can be
// overridden by custom marshalling.
AminoMarshaler interface {
MarshalAmino() ([]byte, error)
UnmarshalAmino([]byte) error
MarshalAminoJSON() ([]byte, error)
UnmarshalAminoJSON([]byte) error
}
)
10 changes: 8 additions & 2 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/tendermint/tendermint/crypto/sr25519"

"github.com/cosmos/cosmos-sdk/codec"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
)
Expand All @@ -25,17 +26,22 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
ed25519.PubKeyName, nil)
cdc.RegisterConcrete(sr25519.PubKey{},
sr25519.PubKeyName, nil)
cdc.RegisterConcrete(secp256k1.PubKey{},
cdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName, nil)
// TODO Follow-up in https://github.com/cosmos/cosmos-sdk/pull/7284
// Remove `multisig.PubKeyMultisigThreshold{}`, and register instead
// kmultisig.LegacyAminoPubKey{} on `PubKeyAminoRoute`.
cdc.RegisterConcrete(multisig.PubKeyMultisigThreshold{},
multisig.PubKeyAminoRoute, nil)
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
"cosmos-sdk/LegacyAminoPubKey", nil)

cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
cdc.RegisterConcrete(ed25519.PrivKey{},
ed25519.PrivKeyName, nil)
cdc.RegisterConcrete(sr25519.PrivKey{},
sr25519.PrivKeyName, nil)
cdc.RegisterConcrete(secp256k1.PrivKey{},
cdc.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName, nil)
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/hd/algo.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ func (s secp256k1Algo) Generate() GenerateFn {
var bzArr = make([]byte, secp256k1.PrivKeySize)
copy(bzArr, bz)

return secp256k1.PrivKey(bzArr)
return &secp256k1.PrivKey{Key: bzArr}
}
}
5 changes: 3 additions & 2 deletions crypto/hd/fundraiser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func TestFundraiserCompatibility(t *testing.T) {
master, ch := hd.ComputeMastersFromSeed(seed)
priv, err := hd.DerivePrivateKeyForPath(master, ch, "44'/118'/0'/0/0")
require.NoError(t, err)
pub := secp256k1.PrivKey(priv).PubKey()
privKey := &secp256k1.PrivKey{Key: priv}
pub := privKey.PubKey()

t.Log("\tNODEJS GOLANG\n")
t.Logf("SEED \t%X %X\n", seedB, seed)
Expand All @@ -78,7 +79,7 @@ func TestFundraiserCompatibility(t *testing.T) {
require.Equal(t, priv[:], privB, "Expected priv keys to match")
pubBFixed := make([]byte, secp256k1.PubKeySize)
copy(pubBFixed, pubB)
require.Equal(t, pub, secp256k1.PubKey(pubBFixed), fmt.Sprintf("Expected pub keys to match for %d", i))
require.Equal(t, pub, &secp256k1.PubKey{Key: pubBFixed}, fmt.Sprintf("Expected pub keys to match for %d", i))

addr := pub.Address()
t.Logf("ADDR \t%X %X\n", addrB, addr)
Expand Down
4 changes: 2 additions & 2 deletions crypto/keyring/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
)

func Test_writeReadLedgerInfo(t *testing.T) {
tmpKey := make(secp256k1.PubKey, secp256k1.PubKeySize)
tmpKey := make([]byte, secp256k1.PubKeySize)
bz, _ := hex.DecodeString("035AD6810A47F073553FF30D2FCC7E0D3B1C0B74B61A1AAA2582344037151E143A")
copy(tmpKey[:], bz)

lInfo := newLedgerInfo("some_name", tmpKey, *hd.NewFundraiserParams(5, sdk.CoinType, 1), hd.Secp256k1Type)
lInfo := newLedgerInfo("some_name", &secp256k1.PubKey{Key: tmpKey}, *hd.NewFundraiserParams(5, sdk.CoinType, 1), hd.Secp256k1Type)
assert.Equal(t, TypeLedger, lInfo.GetType())

path, err := lInfo.GetPath()
Expand Down
Loading

0 comments on commit 320a852

Please sign in to comment.