lbry.go/claim/serialization.go

79 lines
1.9 KiB
Go
Raw Normal View History

2017-09-12 18:02:30 +02:00
package claim
import (
2017-11-27 16:22:04 +01:00
"encoding/hex"
"github.com/lbryio/lbry.go/extras/errors"
legacy "github.com/lbryio/types/v1/go"
pb "github.com/lbryio/types/v2/go"
2018-11-09 23:04:05 +01:00
2017-09-12 18:02:30 +02:00
"github.com/golang/protobuf/proto"
)
func (c *ClaimHelper) serialized() ([]byte, error) {
2017-11-27 16:22:04 +01:00
serialized := c.String()
2017-09-12 18:02:30 +02:00
if serialized == "" {
return nil, errors.Err("not initialized")
}
if c.LegacyClaim != nil {
return proto.Marshal(c.getLegacyProtobuf())
2017-09-12 18:02:30 +02:00
}
2017-11-08 03:39:08 +01:00
return proto.Marshal(c.getProtobuf())
2017-11-08 03:39:08 +01:00
}
func (c *ClaimHelper) getProtobuf() *pb.Claim {
if c.GetChannel() != nil {
return &pb.Claim{Type: &pb.Claim_Channel{Channel: c.GetChannel()}}
} else if c.GetStream() != nil {
return &pb.Claim{Type: &pb.Claim_Stream{Stream: c.GetStream()}}
}
2017-11-08 03:39:08 +01:00
return nil
}
func (c *ClaimHelper) getLegacyProtobuf() *legacy.Claim {
v := c.LegacyClaim.GetVersion()
t := c.LegacyClaim.GetClaimType()
return &legacy.Claim{
2017-11-27 16:22:04 +01:00
Version: &v,
ClaimType: &t,
Stream: c.LegacyClaim.GetStream(),
Certificate: c.LegacyClaim.GetCertificate(),
PublisherSignature: c.LegacyClaim.GetPublisherSignature()}
2017-09-12 18:02:30 +02:00
}
func (c *ClaimHelper) serializedHexString() (string, error) {
serialized, err := c.serialized()
2017-09-12 18:02:30 +02:00
if err != nil {
return "", err
}
serialized_hex := hex.EncodeToString(serialized)
return serialized_hex, nil
}
func (c *ClaimHelper) serializedNoSignature() ([]byte, error) {
2017-11-27 16:22:04 +01:00
if c.String() == "" {
return nil, errors.Err("not initialized")
2017-09-12 18:02:30 +02:00
}
if c.Signature == nil {
serialized, err := c.serialized()
2017-09-12 18:02:30 +02:00
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)
}
2017-09-12 18:02:30 +02:00
clone := &pb.Claim{}
proto.Merge(clone, c.getProtobuf())
2017-09-12 18:02:30 +02:00
return proto.Marshal(clone)
}
}