2020-10-28 06:36:49 +01:00
|
|
|
package stake
|
2019-04-07 06:29:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbry.go/v3/schema/address"
|
|
|
|
"github.com/lbryio/lbry.go/v3/schema/keys"
|
2019-04-07 06:29:12 +02:00
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
"github.com/lbryio/lbcd/btcec"
|
2021-10-20 18:23:55 +02:00
|
|
|
|
|
|
|
"github.com/cockroachdb/errors"
|
2019-04-07 06:29:12 +02:00
|
|
|
)
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func Sign(privKey btcec.PrivateKey, channel Helper, claim Helper, k string) (*keys.Signature, error) {
|
2020-10-28 06:36:49 +01:00
|
|
|
if channel.Claim.GetChannel() == nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(errors.New("claim as channel is not of type channel"))
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
if claim.LegacyClaim != nil {
|
|
|
|
return claim.signV1(privKey, channel, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return claim.sign(privKey, channel, k)
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) sign(privKey btcec.PrivateKey, channel Helper, firstInputTxID string) (*keys.Signature, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
txidBytes, err := hex.DecodeString(firstInputTxID)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
metadataBytes, err := c.serialized()
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, err
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var digest []byte
|
|
|
|
digest = append(digest, txidBytes...)
|
|
|
|
digest = append(digest, c.ClaimID...)
|
|
|
|
digest = append(digest, metadataBytes...)
|
|
|
|
hash := sha256.Sum256(digest)
|
|
|
|
hashBytes := make([]byte, len(hash))
|
|
|
|
for i, b := range hash {
|
|
|
|
hashBytes[i] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := privKey.Sign(hashBytes)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 01:00:44 +01:00
|
|
|
return &keys.Signature{*sig}, nil
|
2019-04-07 06:29:12 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:00:59 +02:00
|
|
|
func (c *Helper) signV1(privKey btcec.PrivateKey, channel Helper, claimAddress string) (*keys.Signature, error) {
|
2019-04-07 06:29:12 +02:00
|
|
|
metadataBytes, err := c.serializedNoSignature()
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addressBytes, err := address.DecodeAddress(claimAddress, "lbrycrd_main")
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithMessage(err, "V1 signing requires claim address and the decode failed")
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var digest []byte
|
|
|
|
|
|
|
|
address := make([]byte, len(addressBytes))
|
|
|
|
for i, b := range addressBytes {
|
|
|
|
address[i] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
digest = append(digest, address...)
|
|
|
|
digest = append(digest, metadataBytes...)
|
|
|
|
digest = append(digest, channel.ClaimID...)
|
|
|
|
|
|
|
|
hash := sha256.Sum256(digest)
|
|
|
|
hashBytes := make([]byte, len(hash))
|
|
|
|
for i, b := range hash {
|
|
|
|
hashBytes[i] = b
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := privKey.Sign(hashBytes)
|
|
|
|
if err != nil {
|
2021-10-06 20:00:59 +02:00
|
|
|
return nil, errors.WithStack(err)
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 01:00:44 +01:00
|
|
|
return &keys.Signature{Signature: *sig}, nil
|
2019-04-07 06:29:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// rev reverses a byte slice. useful for switching endian-ness
|
|
|
|
func reverseBytes(b []byte) []byte {
|
|
|
|
r := make([]byte, len(b))
|
|
|
|
for left, right := 0, len(b)-1; left < right; left, right = left+1, right-1 {
|
|
|
|
r[left], r[right] = b[right], b[left]
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|