tendermint/signature.go

82 lines
1.7 KiB
Go
Raw Normal View History

2015-10-25 13:45:13 -07:00
package crypto
2015-10-25 13:42:49 -07:00
import (
2016-07-24 13:21:57 -07:00
"bytes"
2015-10-25 13:42:49 -07:00
"fmt"
. "github.com/tendermint/tmlibs/common"
2015-10-25 13:42:49 -07:00
)
func SignatureFromBytes(pubKeyBytes []byte) (pubKey Signature, err error) {
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
2017-04-09 00:32:54 -07:00
return
}
2017-03-29 06:17:06 -07:00
2017-04-09 00:32:54 -07:00
//----------------------------------------
2017-03-29 06:17:06 -07:00
type Signature interface {
2016-03-22 15:21:18 -07:00
Bytes() []byte
2015-10-25 13:42:49 -07:00
IsZero() bool
2016-07-24 13:21:57 -07:00
Equals(Signature) bool
2015-10-25 13:42:49 -07:00
}
//-------------------------------------
var _ Signature = SignatureEd25519{}
2015-10-25 13:42:49 -07:00
// Implements Signature
type SignatureEd25519 [64]byte
2016-03-22 15:21:18 -07:00
func (sig SignatureEd25519) Bytes() []byte {
bz, err := cdc.MarshalBinaryBare(sig)
if err != nil {
panic(err)
}
return bz
2016-03-22 15:21:18 -07:00
}
2015-10-25 13:42:49 -07:00
func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
2016-04-19 01:02:31 -07:00
2016-07-24 13:21:57 -07:00
func (sig SignatureEd25519) Equals(other Signature) bool {
if otherEd, ok := other.(SignatureEd25519); ok {
2016-07-24 13:21:57 -07:00
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
2017-09-12 02:40:30 -07:00
func SignatureEd25519FromBytes(data []byte) Signature {
var sig SignatureEd25519
copy(sig[:], data)
return sig
2017-09-12 02:40:30 -07:00
}
2016-04-19 01:02:31 -07:00
//-------------------------------------
var _ Signature = SignatureSecp256k1{}
2016-04-19 01:02:31 -07:00
// Implements Signature
2017-02-22 14:43:26 -08:00
type SignatureSecp256k1 []byte
2016-04-19 01:02:31 -07:00
func (sig SignatureSecp256k1) Bytes() []byte {
bz, err := cdc.MarshalBinaryBare(sig)
if err != nil {
panic(err)
}
return bz
2016-04-19 01:02:31 -07:00
}
func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
2016-07-24 13:21:57 -07:00
func (sig SignatureSecp256k1) Equals(other Signature) bool {
if otherSecp, ok := other.(SignatureSecp256k1); ok {
2017-12-21 16:51:57 -08:00
return bytes.Equal(sig[:], otherSecp[:])
2016-07-24 13:21:57 -07:00
} else {
return false
}
}