Skip to content

Commit

Permalink
feat: crypto custom marshaller (#177)
Browse files Browse the repository at this point in the history
* feat: crypto custom marshaller

* chore: revise code
  • Loading branch information
jinsan-line authored Feb 3, 2021
1 parent 362e19c commit 15a5a84
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 15 deletions.
19 changes: 14 additions & 5 deletions crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ func init() {
PrivKeyAminoName, nil)
}

var _, PubKeyPrefix = amino.NameToDisfix(PubKeyAminoName)
var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName)

// PrivKeyEd25519 implements crypto.PrivKey.
type PrivKeyEd25519 [64]byte

// Bytes marshals the privkey using amino encoding.
func (privKey PrivKeyEd25519) Bytes() []byte {
return cdc.MustMarshalBinaryBare(privKey)
buf := bytes.NewBuffer(nil)
buf.Write(privKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil {
panic(err.Error())
}
return buf.Bytes()
}

// Sign produces a signature on the provided message.
Expand Down Expand Up @@ -141,11 +149,12 @@ func (pubKey PubKeyEd25519) Address() crypto.Address {

// Bytes marshals the PubKey using amino encoding.
func (pubKey PubKeyEd25519) Bytes() []byte {
bz, err := cdc.MarshalBinaryBare(pubKey)
if err != nil {
panic(err)
buf := bytes.NewBuffer(nil)
buf.Write(PubKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil {
panic(err.Error())
}
return bz
return buf.Bytes()
}

func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig []byte) bool {
Expand Down
13 changes: 13 additions & 0 deletions crypto/ed25519/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
)

func TestSignAndValidateEd25519(t *testing.T) {
Expand All @@ -28,3 +30,14 @@ func TestSignAndValidateEd25519(t *testing.T) {

assert.False(t, pubKey.VerifyBytes(msg, sig))
}

func TestMarshal(t *testing.T) {
cdc := amino.NewCodec()
cryptoamino.RegisterAmino(cdc)

privKey := ed25519.GenPrivKey()
pubKey := privKey.PubKey()

require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes())
require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes())
}
4 changes: 4 additions & 0 deletions crypto/multisig/threshold_pubkey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package multisig

import (
amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
)

Expand All @@ -12,6 +13,8 @@ type PubKeyMultisigThreshold struct {

var _ crypto.PubKey = PubKeyMultisigThreshold{}

var _, PubKeyPrefix = amino.NameToDisfix(PubKeyMultisigThresholdAminoRoute)

// NewPubKeyMultisigThreshold returns a new PubKeyMultisigThreshold.
// Panics if len(pubkeys) < k or 0 >= k.
func NewPubKeyMultisigThreshold(k int, pubkeys []crypto.PubKey) crypto.PubKey {
Expand Down Expand Up @@ -69,6 +72,7 @@ func (pk PubKeyMultisigThreshold) VerifyBytes(msg []byte, marshalledSig []byte)

// Bytes returns the amino encoded version of the PubKeyMultisigThreshold
func (pk PubKeyMultisigThreshold) Bytes() []byte {
// TODO custom marshaller
return cdc.MustMarshalBinaryBare(pk)
}

Expand Down
19 changes: 14 additions & 5 deletions crypto/secp256k1/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func init() {
PrivKeyAminoName, nil)
}

var _, PubKeyPrefix = amino.NameToDisfix(PubKeyAminoName)
var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName)

//-------------------------------------

var _ crypto.PrivKey = PrivKeySecp256k1{}
Expand All @@ -43,7 +46,12 @@ type PrivKeySecp256k1 [32]byte

// Bytes marshalls the private key using amino encoding.
func (privKey PrivKeySecp256k1) Bytes() []byte {
return cdc.MustMarshalBinaryBare(privKey)
buf := bytes.NewBuffer(nil)
buf.Write(privKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil {
panic(err.Error())
}
return buf.Bytes()
}

// PubKey performs the point-scalar multiplication from the privKey on the
Expand Down Expand Up @@ -151,11 +159,12 @@ func (pubKey PubKeySecp256k1) Address() crypto.Address {

// Bytes returns the pubkey marshalled with amino encoding.
func (pubKey PubKeySecp256k1) Bytes() []byte {
bz, err := cdc.MarshalBinaryBare(pubKey)
if err != nil {
panic(err)
buf := bytes.NewBuffer(nil)
buf.Write(PubKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil {
panic(err.Error())
}
return bz
return buf.Bytes()
}

func (pubKey PubKeySecp256k1) String() string {
Expand Down
13 changes: 13 additions & 0 deletions crypto/secp256k1/secp256k1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/crypto/secp256k1"

underlyingSecp256k1 "github.com/btcsuite/btcd/btcec"
Expand Down Expand Up @@ -115,3 +117,14 @@ func TestGenPrivKeySecp256k1(t *testing.T) {
})
}
}

func TestMarshal(t *testing.T) {
cdc := amino.NewCodec()
cryptoamino.RegisterAmino(cdc)

privKey := secp256k1.GenPrivKey()
pubKey := privKey.PubKey()

require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes())
require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes())
}
11 changes: 10 additions & 1 deletion crypto/sr25519/privkey.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package sr25519

import (
"bytes"
"crypto/subtle"
"fmt"
"io"

amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"

schnorrkel "github.com/ChainSafe/go-schnorrkel"
Expand All @@ -16,9 +18,16 @@ const PrivKeySr25519Size = 32
// PrivKeySr25519 implements crypto.PrivKey.
type PrivKeySr25519 [PrivKeySr25519Size]byte

var _, privKeyPrefix = amino.NameToDisfix(PrivKeyAminoName)

// Bytes marshals the privkey using amino encoding.
func (privKey PrivKeySr25519) Bytes() []byte {
return cdc.MustMarshalBinaryBare(privKey)
buf := bytes.NewBuffer(nil)
buf.Write(privKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, privKey[:]); err != nil {
panic(err.Error())
}
return buf.Bytes()
}

// Sign produces a signature on the provided message.
Expand Down
12 changes: 8 additions & 4 deletions crypto/sr25519/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"

amino "github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"

Expand All @@ -18,18 +19,21 @@ const PubKeySr25519Size = 32
// PubKeySr25519 implements crypto.PubKey for the Sr25519 signature scheme.
type PubKeySr25519 [PubKeySr25519Size]byte

var _, pubKeyPrefix = amino.NameToDisfix(PubKeyAminoName)

// Address is the SHA256-20 of the raw pubkey bytes.
func (pubKey PubKeySr25519) Address() crypto.Address {
return crypto.Address(tmhash.SumTruncated(pubKey[:]))
}

// Bytes marshals the PubKey using amino encoding.
func (pubKey PubKeySr25519) Bytes() []byte {
bz, err := cdc.MarshalBinaryBare(pubKey)
if err != nil {
panic(err)
buf := bytes.NewBuffer(nil)
buf.Write(pubKeyPrefix[:])
if err := amino.EncodeByteSlice(buf, pubKey[:]); err != nil {
panic(err.Error())
}
return bz
return buf.Bytes()
}

func (pubKey PubKeySr25519) VerifyBytes(msg []byte, sig []byte) bool {
Expand Down
13 changes: 13 additions & 0 deletions crypto/sr25519/sr25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
cryptoamino "github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/crypto/sr25519"
)

Expand All @@ -29,3 +31,14 @@ func TestSignAndValidateSr25519(t *testing.T) {

assert.False(t, pubKey.VerifyBytes(msg, sig))
}

func TestMarshal(t *testing.T) {
cdc := amino.NewCodec()
cryptoamino.RegisterAmino(cdc)

privKey := sr25519.GenPrivKey()
pubKey := privKey.PubKey()

require.Equal(t, cdc.MustMarshalBinaryBare(privKey), privKey.Bytes())
require.Equal(t, cdc.MustMarshalBinaryBare(pubKey), pubKey.Bytes())
}

0 comments on commit 15a5a84

Please sign in to comment.