lbryschema.go/claim/serialization.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-09-12 18:02:30 +02:00
package claim
import (
"errors"
"github.com/golang/protobuf/proto"
"encoding/hex"
2017-11-08 03:39:08 +01:00
"../pb"
2017-09-12 18:02:30 +02:00
)
2017-11-08 03:39:08 +01:00
func (claim *ClaimHelper) Serialized() ([]byte, error) {
serialized := claim.String()
2017-09-12 18:02:30 +02:00
if serialized == "" {
return nil, errors.New("not initialized")
}
2017-11-08 03:39:08 +01:00
v := claim.GetVersion()
t := claim.GetClaimType()
return proto.Marshal(
&pb.Claim{
Version: &v,
ClaimType: &t,
Stream: claim.GetStream(),
Certificate: claim.GetCertificate(),
PublisherSignature: claim.GetPublisherSignature()})
}
func (claim *ClaimHelper) GetProtobuf() (*pb.Claim) {
v := claim.GetVersion()
t := claim.GetClaimType()
return &pb.Claim{
Version: &v,
ClaimType: &t,
Stream: claim.GetStream(),
Certificate: claim.GetCertificate(),
PublisherSignature: claim.GetPublisherSignature()}
2017-09-12 18:02:30 +02:00
}
2017-11-08 03:39:08 +01:00
func (claim *ClaimHelper) SerializedHexString() (string, error) {
2017-09-12 18:02:30 +02:00
serialized, err := claim.Serialized()
if err != nil {
return "", err
}
serialized_hex := hex.EncodeToString(serialized)
return serialized_hex, nil
}
2017-11-08 03:39:08 +01:00
func (claim *ClaimHelper) SerializedNoSignature() ([]byte, error) {
if claim.String() == "" {
2017-09-12 18:02:30 +02:00
return nil, errors.New("not initialized")
}
2017-11-08 03:39:08 +01:00
if claim.GetPublisherSignature() == nil {
2017-09-12 18:02:30 +02:00
serialized, err := claim.Serialized()
if err != nil {
return nil, err
}
return serialized, nil
} else {
clone := &pb.Claim{}
2017-11-08 03:39:08 +01:00
proto.Merge(clone, claim.GetProtobuf())
2017-09-12 18:02:30 +02:00
proto.ClearAllExtensions(clone.PublisherSignature)
clone.PublisherSignature = nil
return proto.Marshal(clone)
}
}