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"
|
|
|
|
|
2017-05-15 02:51:52 +02:00
|
|
|
. "github.com/tendermint/tmlibs/common"
|
2015-10-25 13:42:49 -07:00
|
|
|
)
|
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
func SignatureFromBytes(pubKeyBytes []byte) (pubKey Signature, err error) {
|
2018-03-26 10:41:04 +02:00
|
|
|
err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
|
2017-04-09 00:32:54 -07:00
|
|
|
return
|
|
|
|
}
|
2017-03-29 15:17:06 +02:00
|
|
|
|
2017-04-09 00:32:54 -07:00
|
|
|
//----------------------------------------
|
2017-03-29 15:17:06 +02:00
|
|
|
|
2018-01-14 00:31:39 -08: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
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
var _ Signature = SignatureEd25519{}
|
2017-04-10 13:05:52 +02:00
|
|
|
|
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 {
|
2018-03-26 10:41:04 +02:00
|
|
|
bz, err := cdc.MarshalBinaryBare(sig)
|
2018-01-14 00:31:39 -08:00
|
|
|
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 {
|
2018-01-14 00:31:39 -08:00
|
|
|
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 11:40:30 +02:00
|
|
|
func SignatureEd25519FromBytes(data []byte) Signature {
|
|
|
|
var sig SignatureEd25519
|
|
|
|
copy(sig[:], data)
|
2018-01-14 00:31:39 -08:00
|
|
|
return sig
|
2017-09-12 11:40:30 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 01:02:31 -07:00
|
|
|
//-------------------------------------
|
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
var _ Signature = SignatureSecp256k1{}
|
2017-04-10 13:05:52 +02:00
|
|
|
|
2016-04-19 01:02:31 -07:00
|
|
|
// Implements Signature
|
2017-02-22 23:43:26 +01:00
|
|
|
type SignatureSecp256k1 []byte
|
2016-04-19 01:02:31 -07:00
|
|
|
|
|
|
|
func (sig SignatureSecp256k1) Bytes() []byte {
|
2018-03-26 10:41:04 +02:00
|
|
|
bz, err := cdc.MarshalBinaryBare(sig)
|
2018-01-14 00:31:39 -08:00
|
|
|
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 {
|
2018-01-14 00:31:39 -08:00
|
|
|
if otherSecp, ok := other.(SignatureSecp256k1); ok {
|
2017-12-21 19:51:57 -05:00
|
|
|
return bytes.Equal(sig[:], otherSecp[:])
|
2016-07-24 13:21:57 -07:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|