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"
|
|
|
|
|
2020-09-01 19:58:08 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
2020-09-01 19:45:55 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/schema/address"
|
2020-10-28 06:36:49 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/schema/keys"
|
2019-04-07 06:29:12 +02:00
|
|
|
legacy_pb "github.com/lbryio/types/v1/go"
|
|
|
|
pb "github.com/lbryio/types/v2/go"
|
2018-11-09 23:04:05 +01:00
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
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
|
|
|
)
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
type StakeHelper struct {
|
|
|
|
Claim *pb.Claim
|
|
|
|
Support *pb.Support
|
2019-04-07 06:29:12 +02:00
|
|
|
LegacyClaim *legacy_pb.Claim
|
|
|
|
ClaimID []byte
|
|
|
|
Version version
|
|
|
|
Signature []byte
|
2019-05-04 06:00:23 +02:00
|
|
|
Payload []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: "
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) 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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) ValidateCertificate() error {
|
|
|
|
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 {
|
2018-11-09 18:10:58 +01:00
|
|
|
return errors.Err(err)
|
2017-11-27 16:22:57 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) IsClaim() bool {
|
|
|
|
return c.Claim != nil && c.Claim.String() != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StakeHelper) IsSupport() bool {
|
|
|
|
return c.Support != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StakeHelper) LoadFromBytes(raw_claim []byte, blockchainName string) error {
|
|
|
|
return c.loadFromBytes(raw_claim, false, blockchainName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StakeHelper) LoadSupportFromBytes(raw_claim []byte, blockchainName string) error {
|
|
|
|
return c.loadFromBytes(raw_claim, true, blockchainName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StakeHelper) loadFromBytes(raw_claim []byte, isSupport bool, blockchainName string) error {
|
|
|
|
if c.Claim.String() != "" && !isSupport {
|
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
|
2020-10-28 06:36:49 +01:00
|
|
|
var support_pb *pb.Support
|
2019-04-07 06:29:12 +02:00
|
|
|
|
|
|
|
version := getVersionFromByte(raw_claim[0]) //First byte = version
|
|
|
|
pbPayload := raw_claim[1:]
|
|
|
|
var claimID []byte
|
|
|
|
var signature []byte
|
|
|
|
if version == WithSig {
|
2020-10-28 06:36:49 +01:00
|
|
|
if len(raw_claim) < 85 {
|
2019-04-07 06:29:12 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
var err error
|
|
|
|
if !isSupport {
|
|
|
|
claim_pb = &pb.Claim{}
|
|
|
|
err = proto.Unmarshal(pbPayload, claim_pb)
|
|
|
|
} else {
|
|
|
|
support := &pb.Support{}
|
|
|
|
err = proto.Unmarshal(pbPayload, support)
|
|
|
|
if err == nil {
|
|
|
|
support_pb = support
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
*c = StakeHelper{
|
2019-04-07 06:29:12 +02:00
|
|
|
Claim: claim_pb,
|
2020-10-28 06:36:49 +01:00
|
|
|
Support: support_pb,
|
2019-04-07 06:29:12 +02:00
|
|
|
LegacyClaim: legacy_claim_pb,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) 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
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) LoadSupportFromHexString(claim_hex string, blockchainName string) error {
|
|
|
|
buf, err := hex.DecodeString(claim_hex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.LoadSupportFromBytes(buf, blockchainName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*StakeHelper, error) {
|
|
|
|
claim := &StakeHelper{&pb.Claim{}, &pb.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
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func DecodeSupportProtoBytes(serialized []byte, blockchainName string) (*StakeHelper, error) {
|
|
|
|
claim := &StakeHelper{nil, &pb.Support{}, nil, nil, NoSig, nil, nil}
|
|
|
|
err := claim.LoadSupportFromBytes(serialized, blockchainName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return claim, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeClaimHex(serialized string, blockchainName string) (*StakeHelper, 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
|
2020-10-28 06:36:49 +01:00
|
|
|
func DecodeClaimBytes(serialized []byte, blockchainName string) (*StakeHelper, error) {
|
2018-11-09 18:10:58 +01:00
|
|
|
helper, err := DecodeClaimProtoBytes(serialized, blockchainName)
|
|
|
|
if err == nil {
|
|
|
|
return helper, nil
|
|
|
|
}
|
2020-10-28 06:36:49 +01:00
|
|
|
helper = &StakeHelper{}
|
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 {
|
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
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
// DecodeSupportBytes take a byte array and tries to decode it to a protobuf support
|
|
|
|
func DecodeSupportBytes(serialized []byte, blockchainName string) (*StakeHelper, error) {
|
|
|
|
helper, err := DecodeSupportProtoBytes(serialized, blockchainName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
return helper, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StakeHelper) GetStream() *pb.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
|
|
|
|
}
|
|
|
|
|
2020-10-28 06:36:49 +01:00
|
|
|
func (c *StakeHelper) 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
|
|
|
|
|
|
|
func (c *StakeHelper) GetPublicKey() (*btcec.PublicKey, error) {
|
|
|
|
if c.IsClaim() {
|
|
|
|
if c.Claim.GetChannel() == nil {
|
|
|
|
return nil, errors.Err("claim is not of type channel, so there is no public key to get")
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if c.IsSupport() {
|
|
|
|
return nil, errors.Err("stake is a support and does not come with a public key to get")
|
|
|
|
}
|
|
|
|
return keys.GetPublicKeyFromBytes(c.Claim.GetChannel().PublicKey)
|
|
|
|
}
|