2020-10-28 06:36:49 +01:00
|
|
|
package stake
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
|
|
|
legacy "github.com/lbryio/types/v1/go"
|
|
|
|
pb "github.com/lbryio/types/v2/go"
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/cockroachdb/errors"
|
2020-10-28 06:36:49 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) serialized() ([]byte, error) {
|
2020-10-28 06:36:49 +01:00
|
|
|
serialized := c.Claim.String() + c.Support.String()
|
|
|
|
if serialized == "" {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.New("not initialized"))
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.LegacyClaim != nil {
|
|
|
|
return proto.Marshal(c.getLegacyProtobuf())
|
|
|
|
} else if c.IsSupport() {
|
|
|
|
return proto.Marshal(c.getSupportProtobuf())
|
|
|
|
}
|
|
|
|
|
|
|
|
return proto.Marshal(c.getClaimProtobuf())
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) getClaimProtobuf() *pb.Claim {
|
2020-10-28 06:36:49 +01:00
|
|
|
claim := &pb.Claim{
|
|
|
|
Title: c.Claim.GetTitle(),
|
|
|
|
Description: c.Claim.GetDescription(),
|
|
|
|
Thumbnail: c.Claim.GetThumbnail(),
|
|
|
|
Tags: c.Claim.GetTags(),
|
|
|
|
Languages: c.Claim.GetLanguages(),
|
|
|
|
Locations: c.Claim.GetLocations(),
|
|
|
|
}
|
|
|
|
if c.Claim.GetChannel() != nil {
|
|
|
|
claim.Type = &pb.Claim_Channel{Channel: c.Claim.GetChannel()}
|
|
|
|
} else if c.GetStream() != nil {
|
|
|
|
claim.Type = &pb.Claim_Stream{Stream: c.GetStream()}
|
|
|
|
} else if c.Claim.GetCollection() != nil {
|
|
|
|
claim.Type = &pb.Claim_Collection{Collection: c.Claim.GetCollection()}
|
|
|
|
} else if c.Claim.GetRepost() != nil {
|
|
|
|
claim.Type = &pb.Claim_Repost{Repost: c.Claim.GetRepost()}
|
|
|
|
}
|
|
|
|
|
|
|
|
return claim
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) getSupportProtobuf() *pb.Support {
|
2020-10-28 06:36:49 +01:00
|
|
|
return &pb.Support{
|
|
|
|
Emoji: c.Support.GetEmoji(),
|
|
|
|
XXX_NoUnkeyedLiteral: struct{}{},
|
|
|
|
XXX_unrecognized: nil,
|
|
|
|
XXX_sizecache: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) getLegacyProtobuf() *legacy.Claim {
|
2020-10-28 06:36:49 +01:00
|
|
|
v := c.LegacyClaim.GetVersion()
|
|
|
|
t := c.LegacyClaim.GetClaimType()
|
|
|
|
return &legacy.Claim{
|
|
|
|
Version: &v,
|
|
|
|
ClaimType: &t,
|
|
|
|
Stream: c.LegacyClaim.GetStream(),
|
|
|
|
Certificate: c.LegacyClaim.GetCertificate(),
|
|
|
|
PublisherSignature: c.LegacyClaim.GetPublisherSignature()}
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) serializedHexString() (string, error) {
|
2020-10-28 06:36:49 +01:00
|
|
|
serialized, err := c.serialized()
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return "", errors.WithStack(err)
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
return hex.EncodeToString(serialized), nil
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) serializedNoSignature() ([]byte, error) {
|
2020-10-28 06:36:49 +01:00
|
|
|
if c.Claim.String() == "" && c.Support.String() == "" {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.New("not initialized"))
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
if c.Signature == nil {
|
|
|
|
serialized, err := c.serialized()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return serialized, nil
|
|
|
|
} else {
|
|
|
|
if c.LegacyClaim != nil {
|
|
|
|
clone := &legacy.Claim{}
|
|
|
|
proto.Merge(clone, c.getLegacyProtobuf())
|
|
|
|
proto.ClearAllExtensions(clone.PublisherSignature)
|
|
|
|
clone.PublisherSignature = nil
|
|
|
|
return proto.Marshal(clone)
|
|
|
|
} else if c.IsSupport() {
|
|
|
|
clone := &pb.Support{}
|
|
|
|
proto.Merge(clone, c.getSupportProtobuf())
|
|
|
|
return proto.Marshal(clone)
|
|
|
|
}
|
|
|
|
clone := &pb.Claim{}
|
|
|
|
proto.Merge(clone, c.getClaimProtobuf())
|
|
|
|
return proto.Marshal(clone)
|
|
|
|
}
|
|
|
|
}
|