2015-10-25 13:45:13 -07:00
|
|
|
package crypto
|
2015-10-25 13:42:49 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-05-09 11:48:46 +01:00
|
|
|
"crypto/subtle"
|
|
|
|
|
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 {
|
2018-05-09 11:48:46 +01:00
|
|
|
return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 1
|
2016-07-24 13:21:57 -07:00
|
|
|
} 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 {
|
2018-05-09 11:48:46 +01:00
|
|
|
return subtle.ConstantTimeCompare(sig[:], otherSecp[:]) == 1
|
2016-07-24 13:21:57 -07:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 16:42:11 +02:00
|
|
|
|
|
|
|
func SignatureSecp256k1FromBytes(data []byte) Signature {
|
2018-04-30 19:34:19 +02:00
|
|
|
sig := make(SignatureSecp256k1, len(data))
|
2018-04-30 16:42:11 +02:00
|
|
|
copy(sig[:], data)
|
|
|
|
return sig
|
|
|
|
}
|