2019-07-25 22:15:03 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-21 17:15:28 +00:00
|
|
|
"bytes"
|
2020-10-12 18:46:34 +00:00
|
|
|
"encoding/json"
|
2019-07-25 22:15:03 +00:00
|
|
|
|
2023-03-25 07:33:05 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2019-07-25 22:15:03 +00:00
|
|
|
)
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (sm *SignedMessage) ToStorageBlock() (block.Block, error) {
|
|
|
|
if sm.Signature.Type == crypto.SigTypeBLS {
|
|
|
|
return sm.Message.ToStorageBlock()
|
2019-10-09 12:57:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
data, err := sm.Serialize()
|
2019-07-25 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:05:11 +00:00
|
|
|
c, err := abi.CidBuilder.Sum(data)
|
2019-07-25 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(data, c)
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (sm *SignedMessage) Cid() cid.Cid {
|
|
|
|
if sm.Signature.Type == crypto.SigTypeBLS {
|
|
|
|
return sm.Message.Cid()
|
2019-08-16 22:10:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
sb, err := sm.ToStorageBlock()
|
2019-07-25 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
type SignedMessage struct {
|
|
|
|
Message Message
|
2020-02-12 23:52:36 +00:00
|
|
|
Signature crypto.Signature
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeSignedMessage(data []byte) (*SignedMessage, error) {
|
|
|
|
var msg SignedMessage
|
2019-08-21 17:15:28 +00:00
|
|
|
if err := msg.UnmarshalCBOR(bytes.NewReader(data)); err != nil {
|
2019-07-25 22:15:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *SignedMessage) Serialize() ([]byte, error) {
|
2019-08-21 17:15:28 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := sm.MarshalCBOR(buf); err != nil {
|
2019-07-25 22:15:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-21 17:15:28 +00:00
|
|
|
return buf.Bytes(), nil
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
2019-09-27 23:55:15 +00:00
|
|
|
|
2020-10-12 18:46:34 +00:00
|
|
|
type smCid struct {
|
|
|
|
*RawSignedMessage
|
|
|
|
CID cid.Cid
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawSignedMessage SignedMessage
|
|
|
|
|
|
|
|
func (sm *SignedMessage) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(&smCid{
|
|
|
|
RawSignedMessage: (*RawSignedMessage)(sm),
|
|
|
|
CID: sm.Cid(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
func (sm *SignedMessage) ChainLength() int {
|
2020-10-13 17:20:11 +00:00
|
|
|
var ser []byte
|
|
|
|
var err error
|
|
|
|
if sm.Signature.Type == crypto.SigTypeBLS {
|
|
|
|
// BLS chain message length doesn't include signature
|
|
|
|
ser, err = sm.Message.Serialize()
|
|
|
|
} else {
|
|
|
|
ser, err = sm.Serialize()
|
|
|
|
}
|
2020-03-25 11:29:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return len(ser)
|
|
|
|
}
|
|
|
|
|
2019-10-13 13:03:15 +00:00
|
|
|
func (sm *SignedMessage) Size() int {
|
|
|
|
serdata, err := sm.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("serializing message failed: %s", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(serdata)
|
|
|
|
}
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
func (sm *SignedMessage) VMMessage() *Message {
|
|
|
|
return &sm.Message
|
|
|
|
}
|