tendermint/signature.go

114 lines
2.6 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/go-common"
2017-02-22 23:15:10 +01:00
data "github.com/tendermint/go-data"
2015-10-25 13:45:13 -07:00
"github.com/tendermint/go-wire"
2015-10-25 13:42:49 -07:00
)
// Signature is a part of Txs and consensus Votes.
type Signature interface {
2016-03-22 15:21:18 -07:00
Bytes() []byte
2015-10-25 13:42:49 -07:00
IsZero() bool
String() string
2016-07-24 13:21:57 -07:00
Equals(Signature) bool
2015-10-25 13:42:49 -07:00
}
2017-02-22 23:15:10 +01:00
var sigMapper data.Mapper
// register both public key types with go-data (and thus go-wire)
func init() {
sigMapper = data.NewMapper(SignatureS{}).
2017-02-24 00:31:43 +01:00
RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519).
RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
2017-02-22 23:15:10 +01:00
}
// SignatureS add json serialization to Signature
type SignatureS struct {
Signature
}
func (p SignatureS) MarshalJSON() ([]byte, error) {
return sigMapper.ToJSON(p.Signature)
}
func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
parsed, err := sigMapper.FromJSON(data)
if err == nil && parsed != nil {
2017-02-22 23:15:10 +01:00
p.Signature = parsed.(Signature)
}
return
}
2015-10-25 13:42:49 -07:00
func (p SignatureS) Empty() bool {
return p.Signature == nil
}
2016-04-21 18:05:15 -07:00
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
err = wire.ReadBinaryBytes(sigBytes, &sig)
return
}
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 {
return wire.BinaryBytes(struct{ Signature }{sig})
}
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 {
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
2017-02-22 23:15:10 +01:00
func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p[:])
}
func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(p[:], ref)
return err
}
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 {
return wire.BinaryBytes(struct{ Signature }{sig})
}
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 otherEd, ok := other.(SignatureSecp256k1); ok {
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
2017-02-22 23:43:26 +01:00
func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p)
}
func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
return data.Encoder.Unmarshal((*[]byte)(p), enc)
}