2019-07-25 22:15:03 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-21 17:15:28 +00:00
|
|
|
"bytes"
|
2019-07-25 22:15:03 +00:00
|
|
|
|
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m *SignedMessage) ToStorageBlock() (block.Block, error) {
|
|
|
|
data, err := m.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:15:28 +00:00
|
|
|
pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31)
|
2019-07-25 22:15:03 +00:00
|
|
|
c, err := pref.Sum(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(data, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SignedMessage) Cid() cid.Cid {
|
2019-08-16 22:10:34 +00:00
|
|
|
if m.Signature.Type == KTBLS {
|
|
|
|
return m.Message.Cid()
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
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
|
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
|
|
|
}
|