lotus/chain/types/signedmessage.go
2019-10-13 22:03:15 +09:00

78 lines
1.4 KiB
Go

package types
import (
"bytes"
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
)
func (m *SignedMessage) ToStorageBlock() (block.Block, error) {
if m.Signature.Type == KTBLS {
return m.Message.ToStorageBlock()
}
data, err := m.Serialize()
if err != nil {
return nil, err
}
pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31)
c, err := pref.Sum(data)
if err != nil {
return nil, err
}
return block.NewBlockWithCid(data, c)
}
func (m *SignedMessage) Cid() cid.Cid {
if m.Signature.Type == KTBLS {
return m.Message.Cid()
}
sb, err := m.ToStorageBlock()
if err != nil {
panic(err)
}
return sb.Cid()
}
type SignedMessage struct {
Message Message
Signature Signature
}
func DecodeSignedMessage(data []byte) (*SignedMessage, error) {
var msg SignedMessage
if err := msg.UnmarshalCBOR(bytes.NewReader(data)); err != nil {
return nil, err
}
return &msg, nil
}
func (sm *SignedMessage) Serialize() ([]byte, error) {
buf := new(bytes.Buffer)
if err := sm.MarshalCBOR(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (sm *SignedMessage) Size() int {
serdata, err := sm.Serialize()
if err != nil {
log.Errorf("serializing message failed: %s", err)
return 0
}
return len(serdata)
}
func (sm *SignedMessage) VMMessage() *Message {
return &sm.Message
}