tendermint/signature.go

78 lines
1.8 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"
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
}
// Types of Signature implementations
const (
2016-04-19 01:02:31 -07:00
SignatureTypeEd25519 = byte(0x01)
SignatureTypeSecp256k1 = byte(0x02)
2015-10-25 13:42:49 -07:00
)
// for wire.readReflect
var _ = wire.RegisterInterface(
struct{ Signature }{},
wire.ConcreteType{SignatureEd25519{}, SignatureTypeEd25519},
2016-04-19 01:02:31 -07:00
wire.ConcreteType{SignatureSecp256k1{}, SignatureTypeSecp256k1},
2015-10-25 13:42:49 -07:00
)
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
}
}
2016-04-19 01:02:31 -07:00
//-------------------------------------
// Implements Signature
type SignatureSecp256k1 []byte
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
}
}