tendermint/signature.go

104 lines
2.5 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"
2015-10-25 13:45:13 -07:00
"github.com/tendermint/go-wire"
data "github.com/tendermint/go-wire/data"
. "github.com/tendermint/tmlibs/common"
2015-10-25 13:42:49 -07:00
)
2017-04-09 00:32:54 -07:00
func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
err = wire.ReadBinaryBytes(sigBytes, &sig)
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
2017-04-09 00:32:54 -07:00
// DO NOT USE THIS INTERFACE.
// You probably want to use Signature.
2017-05-16 07:59:40 -07:00
// +gen wrapper:"Signature,Impl[SignatureEd25519,SignatureSecp256k1],ed25519,secp256k1"
type SignatureInner interface {
2017-04-09 00:32:54 -07:00
AssertIsSignatureInner()
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
2017-04-09 00:32:54 -07:00
Wrap() Signature
2015-10-25 13:42:49 -07:00
}
//-------------------------------------
var _ SignatureInner = SignatureEd25519{}
2015-10-25 13:42:49 -07:00
// Implements Signature
type SignatureEd25519 [64]byte
2017-04-09 00:32:54 -07:00
func (sig SignatureEd25519) AssertIsSignatureInner() {}
2016-03-22 15:21:18 -07:00
func (sig SignatureEd25519) Bytes() []byte {
return wire.BinaryBytes(Signature{sig})
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.Unwrap().(SignatureEd25519); ok {
2016-07-24 13:21:57 -07:00
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
2017-04-09 00:32:54 -07:00
func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(sig[:])
2017-02-22 14:15:10 -08:00
}
2017-04-09 00:32:54 -07:00
func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
2017-02-22 14:15:10 -08:00
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
2017-04-09 00:32:54 -07:00
copy(sig[:], ref)
2017-02-22 14:15:10 -08:00
return err
}
2017-09-12 02:40:30 -07:00
func SignatureEd25519FromBytes(data []byte) Signature {
var sig SignatureEd25519
copy(sig[:], data)
return sig.Wrap()
}
2016-04-19 01:02:31 -07:00
//-------------------------------------
var _ SignatureInner = 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
2017-04-09 00:32:54 -07:00
func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
2016-04-19 01:02:31 -07:00
func (sig SignatureSecp256k1) Bytes() []byte {
return wire.BinaryBytes(Signature{sig})
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 otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
2016-07-24 13:21:57 -07:00
return bytes.Equal(sig[:], otherEd[:])
} else {
return false
}
}
2017-04-09 00:32:54 -07:00
func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(sig)
}
func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
return data.Encoder.Unmarshal((*[]byte)(sig), enc)
2017-02-22 14:43:26 -08:00
}