2019-07-24 05:38:16 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-20 04:37:53 +00:00
|
|
|
"bytes"
|
2019-07-24 05:38:16 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2019-08-20 04:37:53 +00:00
|
|
|
"io"
|
2019-07-24 05:38:16 +00:00
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2019-08-20 04:37:53 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-24 05:38:16 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 04:37:53 +00:00
|
|
|
const SignatureMaxLength = 200
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
type Signature crypto.Signature
|
2019-07-24 05:38:16 +00:00
|
|
|
|
|
|
|
func SignatureFromBytes(x []byte) (Signature, error) {
|
|
|
|
val, nr := binary.Uvarint(x)
|
|
|
|
if nr != 1 {
|
|
|
|
return Signature{}, fmt.Errorf("signatures with type field longer than one byte are invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Signature{
|
2020-02-12 23:52:36 +00:00
|
|
|
Type: crypto.SigType(val),
|
2019-07-24 05:38:16 +00:00
|
|
|
Data: x[1:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 04:37:53 +00:00
|
|
|
func (s *Signature) MarshalCBOR(w io.Writer) error {
|
2019-09-10 23:03:17 +00:00
|
|
|
if s == nil {
|
|
|
|
_, err := w.Write(cbg.CborNull)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 04:37:53 +00:00
|
|
|
header := cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(s.Data)+1))
|
|
|
|
|
|
|
|
if _, err := w.Write(header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
if _, err := w.Write([]byte{byte(s.Type)}); err != nil {
|
2019-08-20 04:37:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(s.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:29:19 +00:00
|
|
|
func (s *Signature) UnmarshalCBOR(br io.Reader) error {
|
2019-08-20 04:37:53 +00:00
|
|
|
maj, l, err := cbg.CborReadHeader(br)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if maj != cbg.MajByteString {
|
|
|
|
return fmt.Errorf("cbor input for signature was not a byte string")
|
|
|
|
}
|
|
|
|
|
|
|
|
if l > SignatureMaxLength {
|
|
|
|
return fmt.Errorf("cbor byte array for signature was too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, l)
|
|
|
|
if _, err := io.ReadFull(br, buf); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:52:36 +00:00
|
|
|
if buf[0] != byte(crypto.SigTypeSecp256k1) || buf[0] != byte(crypto.SigTypeBLS) {
|
2019-08-20 04:37:53 +00:00
|
|
|
return fmt.Errorf("invalid signature type in cbor input: %d", buf[0])
|
|
|
|
}
|
2020-02-12 23:52:36 +00:00
|
|
|
|
|
|
|
s.Type = crypto.SigType(buf[0])
|
2019-08-20 04:37:53 +00:00
|
|
|
s.Data = buf[1:]
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Signature) Equals(o *Signature) bool {
|
2020-01-14 15:51:33 +00:00
|
|
|
if s == nil || o == nil {
|
|
|
|
return s == o
|
|
|
|
}
|
2019-08-20 04:37:53 +00:00
|
|
|
return s.Type == o.Type && bytes.Equal(s.Data, o.Data)
|
|
|
|
}
|