2020-10-28 06:36:49 +01:00
|
|
|
package stake
|
2017-09-12 18:02:30 +02:00
|
|
|
|
|
|
|
import (
|
2017-11-20 19:44:19 +01:00
|
|
|
"encoding/hex"
|
2019-04-07 06:29:12 +02:00
|
|
|
"strconv"
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbry.go/v3/schema/address"
|
|
|
|
"github.com/lbryio/lbry.go/v3/schema/keys"
|
|
|
|
v1PB "github.com/lbryio/types/v1/go"
|
|
|
|
v2PB "github.com/lbryio/types/v2/go"
|
2018-11-09 23:04:05 +01:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/cockroachdb/errors"
|
2017-11-20 19:44:19 +01:00
|
|
|
"github.com/golang/protobuf/proto"
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbcd/btcec"
|
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
|
|
|
)
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
type Helper struct {
|
|
|
|
Claim *v2PB.Claim
|
|
|
|
Support *v2PB.Support
|
|
|
|
LegacyClaim *v1PB.Claim
|
2019-04-07 06:29:12 +02:00
|
|
|
ClaimID []byte
|
|
|
|
Version version
|
|
|
|
Signature []byte
|
2019-05-04 06:00:23 +02:00
|
|
|
Payload []byte
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) 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
|
|
|
}
|
2020-10-28 06:36:49 +01:00
|
|
|
} else if c.Claim.GetChannel() != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
return nil
|
2017-11-20 19:44:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
return errors.WithStack(errors.New("claim helper created with migrated v2 protobuf claim 'invalid state'"))
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func validateAddress(tmpAddr []byte, blockchainName string) error {
|
|
|
|
if len(tmpAddr) != 25 {
|
|
|
|
return errors.WithStack(errors.New("invalid address length: " + strconv.FormatInt(int64(len(tmpAddr)), 10)))
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
addr := [25]byte{}
|
|
|
|
for i := range addr {
|
2021-10-06 20:00:59 +02:00
|
|
|
addr[i] = tmpAddr[i]
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
_, err := address.EncodeAddress(addr, blockchainName)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return err
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) ValidateCertificate() error {
|
2020-10-28 06:36:49 +01:00
|
|
|
if !c.IsClaim() || c.Claim.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 {
|
2021-10-06 20:00:59 +02:00
|
|
|
return err
|
2017-11-27 16:22:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) IsClaim() bool {
|
2020-10-28 06:36:49 +01:00
|
|
|
return c.Claim != nil && c.Claim.String() != ""
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) IsSupport() bool {
|
2020-10-28 06:36:49 +01:00
|
|
|
return c.Support != nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) LoadFromBytes(rawClaim []byte, blockchainName string) error {
|
|
|
|
return c.loadFromBytes(rawClaim, false, blockchainName)
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) LoadSupportFromBytes(rawClaim []byte, blockchainName string) error {
|
|
|
|
return c.loadFromBytes(rawClaim, true, blockchainName)
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) loadFromBytes(rawClaim []byte, isSupport bool, blockchainName string) error {
|
2020-10-28 06:36:49 +01:00
|
|
|
if c.Claim.String() != "" && !isSupport {
|
2021-10-06 20:00:59 +02:00
|
|
|
return errors.WithStack(errors.New("already initialized"))
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
if len(rawClaim) < 1 {
|
|
|
|
return errors.WithStack(errors.New("there is nothing to decode"))
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2017-11-08 03:39:08 +01:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
var claimPb *v2PB.Claim
|
|
|
|
var legacyClaimPb *v1PB.Claim
|
|
|
|
var supportPb *v2PB.Support
|
2019-04-07 06:29:12 +02:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
version := getVersionFromByte(rawClaim[0]) //First byte = version
|
|
|
|
pbPayload := rawClaim[1:]
|
2019-04-07 06:29:12 +02:00
|
|
|
var claimID []byte
|
|
|
|
var signature []byte
|
|
|
|
if version == WithSig {
|
2021-10-06 20:00:59 +02:00
|
|
|
if len(rawClaim) < 85 {
|
|
|
|
return errors.WithStack(errors.New("signature version indicated by 1st byte but not enough bytes for valid format"))
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
claimID = rawClaim[1:21] // channel claimid = next 20 bytes
|
|
|
|
signature = rawClaim[21:85] // signature = next 64 bytes
|
|
|
|
pbPayload = rawClaim[85:] // protobuf payload = remaining bytes
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
var err error
|
|
|
|
if !isSupport {
|
2021-10-06 20:00:59 +02:00
|
|
|
claimPb = &v2PB.Claim{}
|
|
|
|
err = proto.Unmarshal(pbPayload, claimPb)
|
2020-10-28 06:36:49 +01:00
|
|
|
} else {
|
2021-10-06 20:00:59 +02:00
|
|
|
support := &v2PB.Support{}
|
2020-10-28 06:36:49 +01:00
|
|
|
err = proto.Unmarshal(pbPayload, support)
|
|
|
|
if err == nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
supportPb = support
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-12 18:02:30 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
legacyClaimPb = &v1PB.Claim{}
|
|
|
|
legacyErr := proto.Unmarshal(rawClaim, legacyClaimPb)
|
2019-04-07 06:29:12 +02:00
|
|
|
if legacyErr == nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
claimPb, err = migrateV1PBClaim(*legacyClaimPb)
|
2019-04-07 06:29:12 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return errors.WithMessage(err, "migration from v1 to v2 protobuf failed")
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
if legacyClaimPb.GetPublisherSignature() != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
version = WithSig
|
2021-10-06 20:00:59 +02:00
|
|
|
claimID = legacyClaimPb.GetPublisherSignature().GetCertificateId()
|
|
|
|
signature = legacyClaimPb.GetPublisherSignature().GetSignature()
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
if legacyClaimPb.GetCertificate() != nil {
|
2019-04-07 06:29:12 +02:00
|
|
|
version = NoSig
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
*c = Helper{
|
|
|
|
Claim: claimPb,
|
|
|
|
Support: supportPb,
|
|
|
|
LegacyClaim: legacyClaimPb,
|
2019-04-07 06:29:12 +02:00
|
|
|
ClaimID: claimID,
|
|
|
|
Version: version,
|
|
|
|
Signature: signature,
|
2019-05-04 06:00:23 +02:00
|
|
|
Payload: pbPayload,
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2019-04-07 06:29:12 +02:00
|
|
|
|
2019-06-02 19:32:30 +02:00
|
|
|
// Commenting out because of a bug in SDK release allowing empty addresses.
|
|
|
|
//err = c.ValidateAddresses(blockchainName)
|
|
|
|
//if err != nil {
|
|
|
|
// return err
|
|
|
|
//}
|
|
|
|
|
2017-11-27 16:22:57 +01:00
|
|
|
err = c.ValidateCertificate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:02:30 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) 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
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) LoadSupportFromHexString(claim_hex string, blockchainName string) error {
|
2020-10-28 06:36:49 +01:00
|
|
|
buf, err := hex.DecodeString(claim_hex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.LoadSupportFromBytes(buf, blockchainName)
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*Helper, error) {
|
|
|
|
claim := &Helper{&v2PB.Claim{}, &v2PB.Support{}, nil, nil, NoSig, nil, 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
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func DecodeSupportProtoBytes(serialized []byte, blockchainName string) (*Helper, error) {
|
|
|
|
claim := &Helper{nil, &v2PB.Support{}, nil, nil, NoSig, nil, nil}
|
2020-10-28 06:36:49 +01:00
|
|
|
err := claim.LoadSupportFromBytes(serialized, blockchainName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return claim, nil
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func DecodeClaimHex(serialized string, blockchainName string) (*Helper, error) {
|
|
|
|
claimBytes, err := hex.DecodeString(serialized)
|
2017-09-12 18:02:30 +02:00
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2017-09-12 18:02:30 +02:00
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
return DecodeClaimBytes(claimBytes, 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
|
2021-10-06 20:00:59 +02:00
|
|
|
func DecodeClaimBytes(serialized []byte, blockchainName string) (*Helper, error) {
|
2018-11-09 18:10:58 +01:00
|
|
|
helper, err := DecodeClaimProtoBytes(serialized, blockchainName)
|
|
|
|
if err == nil {
|
|
|
|
return helper, nil
|
|
|
|
}
|
2021-10-06 20:00:59 +02:00
|
|
|
helper = &Helper{}
|
2018-11-09 18:10:58 +01:00
|
|
|
//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 {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithMessage(err, "vlaim value has no matching version")
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
helper.Claim, err = migrateV3Claim(*v3Claim)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithMessage(err, "v3 metadata migration")
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
helper.Claim, err = migrateV2Claim(*v2Claim)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithMessage(err, "v2 metadata migration")
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.Claim, err = migrateV1Claim(*v1Claim)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithMessage(err, "v1 metadata migration")
|
2018-11-09 18:10:58 +01:00
|
|
|
}
|
|
|
|
return helper, nil
|
2017-11-27 16:23:24 +01:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
// DecodeSupportBytes take a byte array and tries to decode it to a protobuf support
|
2021-10-06 20:00:59 +02:00
|
|
|
func DecodeSupportBytes(serialized []byte, blockchainName string) (*Helper, error) {
|
|
|
|
return DecodeSupportProtoBytes(serialized, blockchainName)
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) GetStream() *v2PB.Stream {
|
2017-11-20 19:44:19 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) CompileValue() ([]byte, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
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
|
|
|
}
|
2020-10-28 06:36:49 +01:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) GetPublicKey() (*btcec.PublicKey, error) {
|
2020-10-28 06:36:49 +01:00
|
|
|
if c.IsClaim() {
|
|
|
|
if c.Claim.GetChannel() == nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.New("claim is not of type channel, so there is no public key to get"))
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if c.IsSupport() {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.New("stake is a support and does not come with a public key to get"))
|
2020-10-28 06:36:49 +01:00
|
|
|
}
|
|
|
|
return keys.GetPublicKeyFromBytes(c.Claim.GetChannel().PublicKey)
|
|
|
|
}
|