2017-09-12 18:02:30 +02:00
|
|
|
package claim
|
|
|
|
|
|
|
|
import (
|
2017-11-20 19:44:19 +01:00
|
|
|
"encoding/hex"
|
2019-04-07 06:29:12 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
|
|
|
"github.com/lbryio/lbryschema.go/address"
|
|
|
|
legacy_pb "github.com/lbryio/types/v1/go"
|
|
|
|
pb "github.com/lbryio/types/v2/go"
|
2018-11-09 23:04:05 +01:00
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-04-07 06:29:12 +02:00
|
|
|
)
|
2018-11-09 18:10:58 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
type version byte
|
|
|
|
|
|
|
|
func (v version) byte() byte {
|
|
|
|
return byte(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoSig = version(byte(0))
|
|
|
|
//Signature using ECDSA SECP256k1 key and SHA-256 hash.
|
|
|
|
WithSig = version(byte(1))
|
|
|
|
UNKNOWN = version(byte(2))
|
2017-09-12 18:02:30 +02:00
|
|
|
)
|
|
|
|
|
2017-11-08 03:39:08 +01:00
|
|
|
type ClaimHelper struct {
|
|
|
|
*pb.Claim
|
2019-04-07 06:29:12 +02:00
|
|
|
LegacyClaim *legacy_pb.Claim
|
|
|
|
ClaimID []byte
|
|
|
|
Version version
|
|
|
|
Signature []byte
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
const migrationErrorMessage = "migration from v1 to v2 protobuf failed with: "
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
func (c *ClaimHelper) ValidateAddresses(blockchainName string) error {
|
2019-04-07 06:29:12 +02:00
|
|
|
if c.Claim != nil { // V2
|
|
|
|
// check the validity of a fee address
|
|
|
|
if c.Claim.GetStream() != nil {
|
|
|
|
fee := c.GetStream().GetFee()
|
|
|
|
if fee != nil {
|
|
|
|
return validateAddress(fee.GetAddress(), blockchainName)
|
|
|
|
} else {
|
|
|
|
return nil
|
2017-11-20 19:44:19 +01:00
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
} else if c.GetChannel() != nil {
|
|
|
|
return nil
|
2017-11-20 19:44:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
|
|
|
|
return errors.Err("claim helper created with migrated v2 protobuf claim 'invalid state'")
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateAddress(tmp_addr []byte, blockchainName string) error {
|
|
|
|
if len(tmp_addr) != 25 {
|
|
|
|
return errors.Err("invalid address length: " + strconv.FormatInt(int64(len(tmp_addr)), 10) + "!")
|
|
|
|
}
|
|
|
|
addr := [25]byte{}
|
|
|
|
for i := range addr {
|
|
|
|
addr[i] = tmp_addr[i]
|
|
|
|
}
|
|
|
|
_, err := address.EncodeAddress(addr, blockchainName)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
func getVersionFromByte(versionByte byte) version {
|
|
|
|
if versionByte == byte(0) {
|
|
|
|
return NoSig
|
|
|
|
} else if versionByte == byte(1) {
|
|
|
|
return WithSig
|
|
|
|
}
|
|
|
|
|
|
|
|
return UNKNOWN
|
|
|
|
}
|
|
|
|
|
2017-11-27 16:22:57 +01:00
|
|
|
func (c *ClaimHelper) ValidateCertificate() error {
|
2019-04-07 06:29:12 +02:00
|
|
|
if c.GetChannel() == nil {
|
2017-11-27 16:22:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
_, err := c.GetPublicKey()
|
2017-11-27 16:22:57 +01:00
|
|
|
if err != nil {
|
2018-11-09 18:10:58 +01:00
|
|
|
return errors.Err(err)
|
2017-11-27 16:22:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) error {
|
|
|
|
if c.String() != "" {
|
2018-11-09 18:10:58 +01:00
|
|
|
return errors.Err("already initialized")
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
|
|
|
if len(raw_claim) < 1 {
|
2018-11-09 18:10:58 +01:00
|
|
|
return errors.Err("there is nothing to decode")
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2017-11-08 03:39:08 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
var claim_pb *pb.Claim
|
|
|
|
var legacy_claim_pb *legacy_pb.Claim
|
|
|
|
|
|
|
|
version := getVersionFromByte(raw_claim[0]) //First byte = version
|
|
|
|
pbPayload := raw_claim[1:]
|
|
|
|
var claimID []byte
|
|
|
|
var signature []byte
|
|
|
|
if version == WithSig {
|
|
|
|
if len(raw_claim) < 86 {
|
|
|
|
return errors.Err("signature version indicated by 1st byte but not enough bytes for valid format")
|
|
|
|
}
|
|
|
|
claimID = raw_claim[1:21] // channel claimid = next 20 bytes
|
|
|
|
signature = raw_claim[21:85] // signature = next 64 bytes
|
|
|
|
pbPayload = raw_claim[85:] // protobuf payload = remaining bytes
|
|
|
|
}
|
|
|
|
|
|
|
|
claim_pb = &pb.Claim{}
|
|
|
|
err := proto.Unmarshal(pbPayload, claim_pb)
|
2017-09-12 18:02:30 +02:00
|
|
|
if err != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
legacy_claim_pb = &legacy_pb.Claim{}
|
|
|
|
legacyErr := proto.Unmarshal(raw_claim, legacy_claim_pb)
|
|
|
|
if legacyErr == nil {
|
|
|
|
claim_pb, err = migrateV1PBClaim(*legacy_claim_pb)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Prefix(migrationErrorMessage, err)
|
|
|
|
}
|
|
|
|
if legacy_claim_pb.GetPublisherSignature() != nil {
|
|
|
|
version = WithSig
|
2019-04-14 05:23:41 +02:00
|
|
|
claimID = legacy_claim_pb.GetPublisherSignature().GetCertificateId()
|
2019-04-07 06:29:12 +02:00
|
|
|
signature = legacy_claim_pb.GetPublisherSignature().GetSignature()
|
|
|
|
}
|
|
|
|
if legacy_claim_pb.GetCertificate() != nil {
|
|
|
|
version = NoSig
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*c = ClaimHelper{
|
|
|
|
Claim: claim_pb,
|
|
|
|
LegacyClaim: legacy_claim_pb,
|
|
|
|
ClaimID: claimID,
|
|
|
|
Version: version,
|
|
|
|
Signature: signature,
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
|
2017-11-27 16:22:57 +01:00
|
|
|
err = c.ValidateAddresses(blockchainName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.ValidateCertificate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:02:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
func (c *ClaimHelper) LoadFromHexString(claim_hex string, blockchainName string) error {
|
2017-09-12 18:02:30 +02:00
|
|
|
buf, err := hex.DecodeString(claim_hex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-20 19:44:19 +01:00
|
|
|
return c.LoadFromBytes(buf, blockchainName)
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 18:10:58 +01:00
|
|
|
func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
claim := &ClaimHelper{&pb.Claim{}, nil, nil, NoSig, nil}
|
2017-11-20 19:44:19 +01:00
|
|
|
err := claim.LoadFromBytes(serialized, blockchainName)
|
2017-09-12 18:02:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return claim, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
func DecodeClaimHex(serialized string, blockchainName string) (*ClaimHelper, error) {
|
2017-09-12 18:02:30 +02:00
|
|
|
claim_bytes, err := hex.DecodeString(serialized)
|
|
|
|
if err != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
return nil, errors.Err(err)
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2017-11-20 19:44:19 +01:00
|
|
|
return DecodeClaimBytes(claim_bytes, blockchainName)
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2017-11-08 03:39:08 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
// DecodeClaimBytes take a byte array and tries to decode it to a protobuf claim or migrate it from either json v1,2,3 or pb v1
|
2018-11-09 18:10:58 +01:00
|
|
|
func DecodeClaimBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
|
|
|
|
helper, err := DecodeClaimProtoBytes(serialized, blockchainName)
|
|
|
|
if err == nil {
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
helper = &ClaimHelper{}
|
|
|
|
//If protobuf fails, try json versions before returning an error.
|
|
|
|
v1Claim := new(V1Claim)
|
|
|
|
err = v1Claim.Unmarshal(serialized)
|
|
|
|
if err != nil {
|
|
|
|
v2Claim := new(V2Claim)
|
|
|
|
err := v2Claim.Unmarshal(serialized)
|
|
|
|
if err != nil {
|
|
|
|
v3Claim := new(V3Claim)
|
|
|
|
err := v3Claim.Unmarshal(serialized)
|
|
|
|
if err != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
return nil, errors.Prefix("Claim value has no matching version", err)
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
helper.Claim, err = migrateV3Claim(*v3Claim)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Prefix("V3 Metadata Migration Error", err)
|
|
|
|
}
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
helper.Claim, err = migrateV2Claim(*v2Claim)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Prefix("V2 Metadata Migration Error ", err)
|
|
|
|
}
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.Claim, err = migrateV1Claim(*v1Claim)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Prefix("V1 Metadata Migration Error ", err)
|
|
|
|
}
|
|
|
|
return helper, nil
|
2017-11-27 16:23:24 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 19:44:19 +01:00
|
|
|
func (c *ClaimHelper) GetStream() *pb.Stream {
|
|
|
|
if c != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
return c.Claim.GetStream()
|
2017-11-08 03:39:08 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
func (c *ClaimHelper) CompileValue() ([]byte, error) {
|
|
|
|
payload, err := c.serialized()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-11-08 03:39:08 +01:00
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
var value []byte
|
|
|
|
value = append(value, c.Version.byte())
|
|
|
|
if c.Version == WithSig {
|
|
|
|
value = append(value, c.ClaimID...)
|
|
|
|
value = append(value, c.Signature...)
|
2017-11-08 03:39:08 +01:00
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
value = append(value, payload...)
|
|
|
|
|
|
|
|
return value, nil
|
2017-11-08 03:39:08 +01:00
|
|
|
}
|