2020-10-28 06:36:49 +01:00
|
|
|
package stake
|
2018-11-09 18:10:58 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/cockroachdb/errors"
|
2019-04-07 06:29:12 +02:00
|
|
|
v1pb "github.com/lbryio/types/v1/go"
|
|
|
|
pb "github.com/lbryio/types/v2/go"
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbcutil/base58"
|
2018-11-09 18:10:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const lbrySDHash = "lbry_sd_hash"
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
func newStreamClaim() *pb.Claim {
|
|
|
|
claimStream := new(pb.Claim_Stream)
|
2018-11-09 18:10:58 +01:00
|
|
|
stream := new(pb.Stream)
|
2019-04-07 06:29:12 +02:00
|
|
|
|
|
|
|
pbClaim := new(pb.Claim)
|
|
|
|
pbClaim.Type = claimStream
|
|
|
|
claimStream.Stream = stream
|
2018-11-09 18:10:58 +01:00
|
|
|
|
|
|
|
return pbClaim
|
|
|
|
}
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
func newChannelClaim() *pb.Claim {
|
|
|
|
claimChannel := new(pb.Claim_Channel)
|
|
|
|
channel := new(pb.Channel)
|
|
|
|
|
|
|
|
pbClaim := new(pb.Claim)
|
|
|
|
pbClaim.Type = claimChannel
|
|
|
|
claimChannel.Channel = channel
|
|
|
|
|
|
|
|
return pbClaim
|
|
|
|
}
|
|
|
|
|
2019-04-22 05:06:48 +02:00
|
|
|
func setMetaData(claim *pb.Claim, author string, description string, language pb.Language_Language, license string,
|
2018-11-09 18:10:58 +01:00
|
|
|
licenseURL *string, title string, thumbnail *string, nsfw bool) {
|
2019-04-22 05:06:48 +02:00
|
|
|
claim.Title = title
|
|
|
|
claim.Description = description
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
claim.GetStream().Author = author
|
2019-04-22 05:06:48 +02:00
|
|
|
claim.Languages = []*pb.Language{{Language: language}}
|
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
if thumbnail != nil {
|
2019-04-22 05:06:48 +02:00
|
|
|
source := new(pb.Source)
|
|
|
|
source.Url = *thumbnail
|
|
|
|
claim.Thumbnail = source
|
|
|
|
}
|
|
|
|
if nsfw {
|
|
|
|
claim.Tags = []string{"mature"}
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
claim.GetStream().License = license
|
|
|
|
if licenseURL != nil {
|
|
|
|
claim.GetStream().LicenseUrl = *licenseURL
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 18:10:58 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
func migrateV1PBClaim(vClaim v1pb.Claim) (*pb.Claim, error) {
|
|
|
|
if *vClaim.ClaimType == v1pb.Claim_streamType {
|
|
|
|
return migrateV1PBStream(vClaim)
|
|
|
|
}
|
|
|
|
if *vClaim.ClaimType == v1pb.Claim_certificateType {
|
|
|
|
return migrateV1PBChannel(vClaim)
|
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.Newf("Could not migrate v1 protobuf claim due to unknown type '%s'.", vClaim.ClaimType.String()))
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func migrateV1PBStream(vClaim v1pb.Claim) (*pb.Claim, error) {
|
|
|
|
claim := newStreamClaim()
|
2019-04-22 05:06:48 +02:00
|
|
|
source := new(pb.Source)
|
|
|
|
source.MediaType = vClaim.GetStream().GetSource().GetContentType()
|
|
|
|
source.SdHash = vClaim.GetStream().GetSource().GetSource()
|
|
|
|
claim.GetStream().Source = source
|
2019-04-07 06:29:12 +02:00
|
|
|
md := vClaim.GetStream().GetMetadata()
|
|
|
|
if md.GetFee() != nil {
|
|
|
|
claim.GetStream().Fee = new(pb.Fee)
|
|
|
|
claim.GetStream().GetFee().Amount = uint64(*md.GetFee().Amount * 100000000)
|
|
|
|
claim.GetStream().GetFee().Address = md.GetFee().GetAddress()
|
|
|
|
claim.GetStream().GetFee().Currency = pb.Fee_Currency(pb.Fee_Currency_value[md.GetFee().GetCurrency().String()])
|
|
|
|
}
|
|
|
|
if vClaim.GetStream().GetMetadata().GetNsfw() {
|
2019-04-22 05:06:48 +02:00
|
|
|
claim.Tags = []string{"mature"}
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2019-04-22 05:06:48 +02:00
|
|
|
thumbnailSource := new(pb.Source)
|
|
|
|
thumbnailSource.Url = md.GetThumbnail()
|
|
|
|
claim.Thumbnail = thumbnailSource
|
2019-04-07 06:29:12 +02:00
|
|
|
language := pb.Language_Language(pb.Language_Language_value[md.GetLanguage().String()])
|
2019-04-22 05:06:48 +02:00
|
|
|
claim.Languages = []*pb.Language{{Language: language}}
|
2019-04-07 06:29:12 +02:00
|
|
|
claim.GetStream().LicenseUrl = md.GetLicenseUrl()
|
|
|
|
claim.GetStream().License = md.GetLicense()
|
2019-04-22 05:06:48 +02:00
|
|
|
claim.Title = md.GetTitle()
|
|
|
|
claim.Description = md.GetDescription()
|
2019-04-07 06:29:12 +02:00
|
|
|
claim.GetStream().Author = md.GetAuthor()
|
|
|
|
|
|
|
|
return claim, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateV1PBChannel(vClaim v1pb.Claim) (*pb.Claim, error) {
|
|
|
|
claim := newChannelClaim()
|
|
|
|
claim.GetChannel().PublicKey = vClaim.GetCertificate().PublicKey
|
|
|
|
|
|
|
|
return claim, nil
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func migrateV1Claim(vClaim V1Claim) (*pb.Claim, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
pbClaim := newStreamClaim()
|
2018-11-09 18:10:58 +01:00
|
|
|
//Stream
|
|
|
|
// -->Universal
|
|
|
|
setFee(vClaim.Fee, pbClaim)
|
|
|
|
// -->MetaData
|
2019-04-07 06:29:12 +02:00
|
|
|
language := pb.Language_Language(pb.Language_Language_value[vClaim.Language])
|
2019-04-22 05:06:48 +02:00
|
|
|
setMetaData(pbClaim, vClaim.Author, vClaim.Description, language,
|
2018-11-09 18:10:58 +01:00
|
|
|
vClaim.License, nil, vClaim.Title, vClaim.Thumbnail, false)
|
|
|
|
// -->Source
|
2019-04-22 05:06:48 +02:00
|
|
|
source := new(pb.Source)
|
|
|
|
source.MediaType = vClaim.ContentType
|
|
|
|
|
2018-11-09 18:10:58 +01:00
|
|
|
src, err := hex.DecodeString(vClaim.Sources.LbrySDHash)
|
2019-04-07 06:29:12 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2019-04-22 05:06:48 +02:00
|
|
|
source.SdHash = src
|
|
|
|
pbClaim.GetStream().Source = source
|
2018-11-09 18:10:58 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
return pbClaim, nil
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func migrateV2Claim(vClaim V2Claim) (*pb.Claim, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
pbClaim := newStreamClaim()
|
2018-11-09 18:10:58 +01:00
|
|
|
//Stream
|
|
|
|
// -->Fee
|
|
|
|
setFee(vClaim.Fee, pbClaim)
|
|
|
|
// -->MetaData
|
2019-04-07 06:29:12 +02:00
|
|
|
language := pb.Language_Language(pb.Language_Language_value[vClaim.Language])
|
2019-04-22 05:06:48 +02:00
|
|
|
setMetaData(pbClaim, vClaim.Author, vClaim.Description, language,
|
2018-11-09 18:10:58 +01:00
|
|
|
vClaim.License, vClaim.LicenseURL, vClaim.Title, vClaim.Thumbnail, vClaim.NSFW)
|
|
|
|
// -->Source
|
2019-04-22 05:06:48 +02:00
|
|
|
source := new(pb.Source)
|
|
|
|
source.MediaType = vClaim.ContentType
|
2018-11-09 18:10:58 +01:00
|
|
|
src, err := hex.DecodeString(vClaim.Sources.LbrySDHash)
|
2019-04-07 06:29:12 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2019-04-22 05:06:48 +02:00
|
|
|
source.SdHash = src
|
|
|
|
pbClaim.GetStream().Source = source
|
2018-11-09 18:10:58 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
return pbClaim, nil
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func migrateV3Claim(vClaim V3Claim) (*pb.Claim, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
pbClaim := newStreamClaim()
|
2018-11-09 18:10:58 +01:00
|
|
|
//Stream
|
|
|
|
// -->Fee
|
|
|
|
setFee(vClaim.Fee, pbClaim)
|
|
|
|
// -->MetaData
|
2019-04-07 06:29:12 +02:00
|
|
|
language := pb.Language_Language(pb.Language_Language_value[vClaim.Language])
|
2019-04-22 05:06:48 +02:00
|
|
|
setMetaData(pbClaim, vClaim.Author, vClaim.Description, language,
|
2018-11-09 18:10:58 +01:00
|
|
|
vClaim.License, vClaim.LicenseURL, vClaim.Title, vClaim.Thumbnail, vClaim.NSFW)
|
|
|
|
// -->Source
|
2019-04-22 05:06:48 +02:00
|
|
|
source := new(pb.Source)
|
|
|
|
source.MediaType = vClaim.ContentType
|
2018-11-09 18:10:58 +01:00
|
|
|
src, err := hex.DecodeString(vClaim.Sources.LbrySDHash)
|
2019-04-07 06:29:12 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2019-04-22 05:06:48 +02:00
|
|
|
source.SdHash = src
|
|
|
|
pbClaim.GetStream().Source = source
|
2018-11-09 18:10:58 +01:00
|
|
|
|
2019-04-07 06:29:12 +02:00
|
|
|
return pbClaim, nil
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func setFee(fee *Fee, pbClaim *pb.Claim) {
|
|
|
|
if fee != nil {
|
|
|
|
amount := float32(0.0)
|
|
|
|
currency := pb.Fee_LBC
|
|
|
|
address := ""
|
|
|
|
if fee.BTC != nil {
|
|
|
|
amount = fee.BTC.Amount
|
|
|
|
currency = pb.Fee_BTC
|
|
|
|
address = fee.BTC.Address
|
|
|
|
} else if fee.LBC != nil {
|
|
|
|
amount = fee.LBC.Amount
|
|
|
|
currency = pb.Fee_LBC
|
|
|
|
address = fee.LBC.Address
|
|
|
|
} else if fee.USD != nil {
|
|
|
|
amount = fee.USD.Amount
|
|
|
|
currency = pb.Fee_USD
|
|
|
|
address = fee.USD.Address
|
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
pbClaim.GetStream().Fee = new(pb.Fee)
|
2018-11-09 18:10:58 +01:00
|
|
|
//Fee Settings
|
2019-04-07 06:29:12 +02:00
|
|
|
pbClaim.GetStream().GetFee().Amount = uint64(amount * 100000000)
|
|
|
|
pbClaim.GetStream().GetFee().Currency = currency
|
|
|
|
pbClaim.GetStream().GetFee().Address = base58.Decode(address)
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
}
|