lbryschema.go/claim/claim.go

77 lines
1.4 KiB
Go
Raw Normal View History

2017-09-12 18:02:30 +02:00
package claim
import (
"github.com/golang/protobuf/proto"
"encoding/hex"
"../pb"
"errors"
)
2017-11-08 03:39:08 +01:00
type ClaimHelper struct {
*pb.Claim
2017-09-12 18:02:30 +02:00
}
2017-11-08 03:39:08 +01:00
func (claim *ClaimHelper) LoadFromBytes(raw_claim []byte) (error) {
if claim.String() != "" {
2017-09-12 18:02:30 +02:00
return errors.New("already initialized")
}
if len(raw_claim) < 1 {
return errors.New("there is nothing to decode")
}
2017-11-08 03:39:08 +01:00
2017-09-12 18:02:30 +02:00
claim_pb := &pb.Claim{}
err := proto.Unmarshal(raw_claim, claim_pb)
if err != nil {
return err
}
2017-11-08 03:39:08 +01:00
*claim = ClaimHelper{claim_pb}
2017-09-12 18:02:30 +02:00
return nil
}
2017-11-08 03:39:08 +01:00
func (claim *ClaimHelper) LoadFromHexString(claim_hex string) (error) {
2017-09-12 18:02:30 +02:00
buf, err := hex.DecodeString(claim_hex)
if err != nil {
return err
}
return claim.LoadFromBytes(buf)
}
2017-11-08 03:39:08 +01:00
func DecodeClaimBytes(serialized []byte) (*ClaimHelper, error) {
claim := &ClaimHelper{&pb.Claim{}}
2017-09-12 18:02:30 +02:00
err := claim.LoadFromBytes(serialized)
if err != nil {
return nil, err
}
return claim, nil
}
2017-11-08 03:39:08 +01:00
func DecodeClaimHex(serialized string) (*ClaimHelper, error) {
2017-09-12 18:02:30 +02:00
claim_bytes, err := hex.DecodeString(serialized)
if err != nil {
return nil, err
}
return DecodeClaimBytes(claim_bytes)
}
2017-11-08 03:39:08 +01:00
func (m *ClaimHelper) GetStream() *pb.Stream {
if m != nil {
return m.Stream
}
return nil
}
func (m *ClaimHelper) GetCertificate() *pb.Certificate {
if m != nil {
return m.Certificate
}
return nil
}
func (m *ClaimHelper) GetPublisherSignature() *pb.Signature {
if m != nil {
return m.PublisherSignature
}
return nil
}