Compare commits
13
Commits
mod-updates
...
cors
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7e84c6b97 | ||
|
|
4580a95b74 | ||
|
|
29773829af | ||
|
|
ef1b43ac62 | ||
|
|
39e5821760 | ||
|
|
cb68cb004e | ||
|
|
eb6bb93500 | ||
|
|
d0df93ebac | ||
|
|
8c41d8ccd9 | ||
|
|
e9753ffdc7 | ||
|
|
69e03da94a | ||
|
|
b3f7657c1b | ||
|
|
29574578c1 |
No files matched your search
+1
-1
@@ -2,7 +2,7 @@ os: linux
|
||||
dist: xenial
|
||||
language: go
|
||||
go:
|
||||
- 1.11.x
|
||||
- 1.15.x
|
||||
|
||||
env:
|
||||
global:
|
||||
|
||||
@@ -17,6 +17,12 @@ import (
|
||||
// ResponseHeaders are returned with each response
|
||||
var ResponseHeaders map[string]string
|
||||
|
||||
// CorsDomains Allowed domains for CORS Policy
|
||||
var CorsDomains []string
|
||||
|
||||
// CorsAllowLocalhost if true localhost connections are always allowed
|
||||
var CorsAllowLocalhost bool
|
||||
|
||||
// Log allows logging of events and errors
|
||||
var Log = func(*http.Request, *Response, error) {}
|
||||
|
||||
@@ -77,6 +83,32 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(key, value)
|
||||
}
|
||||
}
|
||||
origin := r.Header.Get("origin")
|
||||
for _, d := range CorsDomains {
|
||||
if d == origin {
|
||||
w.Header().Set("Access-Control-Allow-Origin", d)
|
||||
vary := w.Header().Get("Vary")
|
||||
if vary != "*" {
|
||||
if vary != "" {
|
||||
vary += ", "
|
||||
}
|
||||
vary += "Origin"
|
||||
}
|
||||
w.Header().Set("Vary", vary)
|
||||
}
|
||||
}
|
||||
|
||||
if CorsAllowLocalhost && strings.HasPrefix(origin, "http://localhost:") {
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
vary := w.Header().Get("Vary")
|
||||
if vary != "*" {
|
||||
if vary != "" {
|
||||
vary += ", "
|
||||
}
|
||||
vary += "Origin"
|
||||
}
|
||||
w.Header().Set("Vary", vary)
|
||||
}
|
||||
|
||||
// Stop here if its a preflighted OPTIONS request
|
||||
if r.Method == "OPTIONS" {
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
"github.com/lbryio/lbry.go/v2/stream"
|
||||
|
||||
schema "github.com/lbryio/lbry.go/v2/schema/claim"
|
||||
schema "github.com/lbryio/lbry.go/v2/schema/stake"
|
||||
lbryschema "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
@@ -46,6 +46,15 @@ type APIResponse struct {
|
||||
Data *ResponseData `json:"data"`
|
||||
}
|
||||
|
||||
// APIError wraps errors returned by LBRY API server to discern them from other kinds (like http errors).
|
||||
type APIError struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e APIError) Error() string {
|
||||
return fmt.Sprintf("api error: %v", e.Err)
|
||||
}
|
||||
|
||||
// ResponseData is a map containing parsed json response.
|
||||
type ResponseData map[string]interface{}
|
||||
|
||||
@@ -110,6 +119,9 @@ func (c Client) doCall(url string, payload string) ([]byte, error) {
|
||||
if err != nil {
|
||||
return body, err
|
||||
}
|
||||
if r.StatusCode >= 500 {
|
||||
return body, fmt.Errorf("server returned non-OK status: %v", r.StatusCode)
|
||||
}
|
||||
defer r.Body.Close()
|
||||
return ioutil.ReadAll(r.Body)
|
||||
}
|
||||
@@ -133,16 +145,17 @@ func (c Client) Call(object, method string, params map[string]interface{}) (Resp
|
||||
return rd, err
|
||||
}
|
||||
if !ar.Success {
|
||||
return rd, errors.New(*ar.Error)
|
||||
return rd, APIError{errors.New(*ar.Error)}
|
||||
}
|
||||
return *ar.Data, err
|
||||
}
|
||||
|
||||
// UserMe returns user details for the user associated with the current auth_token
|
||||
// UserMe returns user details for the user associated with the current auth_token.
|
||||
func (c Client) UserMe() (ResponseData, error) {
|
||||
return c.Call(userObjectPath, userMeMethod, map[string]interface{}{})
|
||||
}
|
||||
|
||||
// UserHasVerifiedEmail calls has_verified_email method.
|
||||
func (c Client) UserHasVerifiedEmail() (ResponseData, error) {
|
||||
return c.Call(userObjectPath, userHasVerifiedEmailMethod, map[string]interface{}{})
|
||||
}
|
||||
@@ -7,42 +7,26 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUserMeWrongToken(t *testing.T) {
|
||||
c := NewClient("abc", nil)
|
||||
r, err := c.UserMe()
|
||||
require.NotNil(t, err)
|
||||
assert.Equal(t, "could not authenticate user", err.Error())
|
||||
assert.Nil(t, r)
|
||||
}
|
||||
|
||||
func TestUserHasVerifiedEmailWrongToken(t *testing.T) {
|
||||
c := NewClient("abc", nil)
|
||||
r, err := c.UserHasVerifiedEmail()
|
||||
require.NotNil(t, err)
|
||||
assert.Equal(t, "could not authenticate user", err.Error())
|
||||
assert.Nil(t, r)
|
||||
}
|
||||
|
||||
func launchDummyServer(lastReq **http.Request, path, response string) *httptest.Server {
|
||||
func launchDummyServer(lastReq **http.Request, path, response string, status int) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
*lastReq = &*r
|
||||
if lastReq != nil {
|
||||
*lastReq = &*r
|
||||
}
|
||||
if r.URL.Path != path {
|
||||
fmt.Printf("path doesn't match: %v != %v", r.URL.Path, path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(response))
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func TestUserMe(t *testing.T) {
|
||||
var req *http.Request
|
||||
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
|
||||
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
||||
@@ -52,8 +36,7 @@ func TestUserMe(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUserHasVerifiedEmail(t *testing.T) {
|
||||
var req *http.Request
|
||||
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse)
|
||||
ts := launchDummyServer(nil, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), userHasVerifiedEmailResponse, http.StatusOK)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL})
|
||||
@@ -65,7 +48,7 @@ func TestUserHasVerifiedEmail(t *testing.T) {
|
||||
|
||||
func TestRemoteIP(t *testing.T) {
|
||||
var req *http.Request
|
||||
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse)
|
||||
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userMeMethod), userMeResponse, http.StatusOK)
|
||||
defer ts.Close()
|
||||
|
||||
c := NewClient("realToken", &ClientOpts{ServerAddress: ts.URL, RemoteIP: "8.8.8.8"})
|
||||
@@ -74,6 +57,34 @@ func TestRemoteIP(t *testing.T) {
|
||||
assert.Equal(t, []string{"8.8.8.8"}, req.Header["X-Forwarded-For"])
|
||||
}
|
||||
|
||||
func TestWrongToken(t *testing.T) {
|
||||
c := NewClient("zcasdasc", nil)
|
||||
|
||||
r, err := c.UserHasVerifiedEmail()
|
||||
assert.Nil(t, r)
|
||||
assert.EqualError(t, err, "api error: could not authenticate user")
|
||||
assert.ErrorAs(t, err, &APIError{})
|
||||
}
|
||||
|
||||
func TestHTTPError(t *testing.T) {
|
||||
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: "http://lolcathost"})
|
||||
|
||||
r, err := c.UserHasVerifiedEmail()
|
||||
assert.Nil(t, r)
|
||||
assert.EqualError(t, err, `Post "http://lolcathost/user/has_verified_email": dial tcp: lookup lolcathost: no such host`)
|
||||
}
|
||||
|
||||
func TestGatewayError(t *testing.T) {
|
||||
var req *http.Request
|
||||
ts := launchDummyServer(&req, makeMethodPath(userObjectPath, userHasVerifiedEmailMethod), "", http.StatusBadGateway)
|
||||
defer ts.Close()
|
||||
c := NewClient("zcasdasc", &ClientOpts{ServerAddress: ts.URL})
|
||||
|
||||
r, err := c.UserHasVerifiedEmail()
|
||||
assert.Nil(t, r)
|
||||
assert.EqualError(t, err, `server returned non-OK status: 502`)
|
||||
}
|
||||
|
||||
const userMeResponse = `{
|
||||
"success": true,
|
||||
"error": null,
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package util
|
||||
|
||||
import "strings"
|
||||
|
||||
func StringSplitArg(stringToSplit, separator string) []interface{} {
|
||||
split := strings.Split(stringToSplit, separator)
|
||||
splitInterface := make([]interface{}, len(split))
|
||||
for i, s := range split {
|
||||
splitInterface[i] = s
|
||||
}
|
||||
return splitInterface
|
||||
}
|
||||
@@ -20,7 +20,7 @@ require (
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
github.com/kr/pretty v0.1.0 // indirect
|
||||
github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04
|
||||
github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8
|
||||
github.com/lbryio/types v0.0.0-20201019032447-f0b4476ef386
|
||||
github.com/lyoshenka/bencode v0.0.0-20180323155644-b7abd7672df5
|
||||
github.com/mitchellh/mapstructure v1.1.2
|
||||
github.com/nlopes/slack v0.6.0
|
||||
@@ -34,7 +34,7 @@ require (
|
||||
github.com/smartystreets/assertions v1.0.1 // indirect
|
||||
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
|
||||
github.com/spf13/cast v1.3.0
|
||||
github.com/stretchr/testify v1.4.0
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/ybbus/jsonrpc v0.0.0-20180411222309-2a548b7d822d
|
||||
go.uber.org/atomic v1.4.0
|
||||
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc
|
||||
@@ -51,4 +51,4 @@ require (
|
||||
gotest.tools v2.2.0+incompatible
|
||||
)
|
||||
|
||||
go 1.13
|
||||
go 1.15
|
||||
@@ -67,8 +67,8 @@ github.com/lbryio/lbrycrd.go v0.0.0-20200203050410-e1076f12bf19 h1:/zWD8dVIl7bV1
|
||||
github.com/lbryio/lbrycrd.go v0.0.0-20200203050410-e1076f12bf19/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04 h1:Nze+C2HbeKvhjI/kVn+9Poj/UuEW5sOQxcsxqO7L3GI=
|
||||
github.com/lbryio/ozzo-validation v0.0.0-20170323141101-d1008ad1fd04/go.mod h1:fbG/dzobG8r95KzMwckXiLMHfFjZaBRQqC9hPs2XAQ4=
|
||||
github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8 h1:jSNW/rK6DQsz7Zh+iv1zR384PeQdHt0gS4hKY17tkuM=
|
||||
github.com/lbryio/types v0.0.0-20191009145016-1bb8107e04f8/go.mod h1:CG3wsDv5BiVYQd5i1Jp7wGsaVyjZTJshqXeWMVKsISE=
|
||||
github.com/lbryio/types v0.0.0-20201019032447-f0b4476ef386 h1:JOQkGpeCM9FWkEHRx+kRPqySPCXElNW1em1++7tVS4M=
|
||||
github.com/lbryio/types v0.0.0-20201019032447-f0b4476ef386/go.mod h1:CG3wsDv5BiVYQd5i1Jp7wGsaVyjZTJshqXeWMVKsISE=
|
||||
github.com/lyoshenka/bencode v0.0.0-20180323155644-b7abd7672df5 h1:mG83tLXWSRdcXMWfkoumVwhcCbf3jHF9QKv/m37BkM0=
|
||||
github.com/lyoshenka/bencode v0.0.0-20180323155644-b7abd7672df5/go.mod h1:H0aPCWffGOaDcjkw1iB7W9DVLp6GXmfcJY/7YZCWPA4=
|
||||
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
|
||||
@@ -106,8 +106,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/ybbus/jsonrpc v0.0.0-20180411222309-2a548b7d822d h1:tQo6hjclyv3RHUgZOl6iWb2Y44A/sN9bf9LAYfuioEg=
|
||||
github.com/ybbus/jsonrpc v0.0.0-20180411222309-2a548b7d822d/go.mod h1:XJrh1eMSzdIYFbM08flv0wp5G35eRniyeGut1z+LSiE=
|
||||
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
|
||||
@@ -170,9 +170,10 @@ gopkg.in/nullbio/null.v6 v6.0.0-20161116030900-40264a2e6b79/go.mod h1:gWkaRU7CoX
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
+11
-10
@@ -3,11 +3,12 @@ package lbrycrd
|
||||
import (
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/claim"
|
||||
"github.com/lbryio/lbry.go/v2/schema/keys"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/stake"
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
)
|
||||
|
||||
func NewChannel() (*c.ClaimHelper, *btcec.PrivateKey, error) {
|
||||
func NewChannel() (*c.StakeHelper, *btcec.PrivateKey, error) {
|
||||
claimChannel := new(pb.Claim_Channel)
|
||||
channel := new(pb.Channel)
|
||||
claimChannel.Channel = channel
|
||||
@@ -19,21 +20,21 @@ func NewChannel() (*c.ClaimHelper, *btcec.PrivateKey, error) {
|
||||
if err != nil {
|
||||
return nil, nil, errors.Err(err)
|
||||
}
|
||||
pubkeyBytes, err := c.PublicKeyToDER(privateKey.PubKey())
|
||||
pubkeyBytes, err := keys.PublicKeyToDER(privateKey.PubKey())
|
||||
if err != nil {
|
||||
return nil, nil, errors.Err(err)
|
||||
}
|
||||
|
||||
helper := c.ClaimHelper{Claim: pbClaim}
|
||||
helper := c.StakeHelper{Claim: pbClaim}
|
||||
helper.Version = c.NoSig
|
||||
helper.GetChannel().PublicKey = pubkeyBytes
|
||||
helper.Tags = []string{}
|
||||
helper.Claim.GetChannel().PublicKey = pubkeyBytes
|
||||
helper.Claim.Tags = []string{}
|
||||
coverSrc := new(pb.Source)
|
||||
helper.GetChannel().Cover = coverSrc
|
||||
helper.Languages = []*pb.Language{}
|
||||
helper.Claim.GetChannel().Cover = coverSrc
|
||||
helper.Claim.Languages = []*pb.Language{}
|
||||
thumbnailSrc := new(pb.Source)
|
||||
helper.Thumbnail = thumbnailSrc
|
||||
helper.Locations = []*pb.Location{}
|
||||
helper.Claim.Thumbnail = thumbnailSrc
|
||||
helper.Claim.Locations = []*pb.Location{}
|
||||
|
||||
return &helper, privateKey, nil
|
||||
}
|
||||
+10
-10
@@ -4,14 +4,14 @@ import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/claim"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/stake"
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
func NewImageStreamClaim() (*c.ClaimHelper, error) {
|
||||
func NewImageStreamClaim() (*c.StakeHelper, error) {
|
||||
streamClaim := new(pb.Claim_Stream)
|
||||
stream := new(pb.Stream)
|
||||
image := new(pb.Stream_Image)
|
||||
@@ -23,12 +23,12 @@ func NewImageStreamClaim() (*c.ClaimHelper, error) {
|
||||
pbClaim := new(pb.Claim)
|
||||
pbClaim.Type = streamClaim
|
||||
|
||||
helper := c.ClaimHelper{Claim: pbClaim}
|
||||
helper := c.StakeHelper{Claim: pbClaim}
|
||||
|
||||
return &helper, nil
|
||||
}
|
||||
|
||||
func NewVideoStreamClaim() (*c.ClaimHelper, error) {
|
||||
func NewVideoStreamClaim() (*c.StakeHelper, error) {
|
||||
streamClaim := new(pb.Claim_Stream)
|
||||
stream := new(pb.Stream)
|
||||
video := new(pb.Stream_Video)
|
||||
@@ -39,12 +39,12 @@ func NewVideoStreamClaim() (*c.ClaimHelper, error) {
|
||||
pbClaim := new(pb.Claim)
|
||||
pbClaim.Type = streamClaim
|
||||
|
||||
helper := c.ClaimHelper{Claim: pbClaim}
|
||||
helper := c.StakeHelper{Claim: pbClaim}
|
||||
|
||||
return &helper, nil
|
||||
}
|
||||
|
||||
func NewStreamClaim(title, description string) (*c.ClaimHelper, error) {
|
||||
func NewStreamClaim(title, description string) (*c.StakeHelper, error) {
|
||||
streamClaim := new(pb.Claim_Stream)
|
||||
stream := new(pb.Stream)
|
||||
streamClaim.Stream = stream
|
||||
@@ -52,14 +52,14 @@ func NewStreamClaim(title, description string) (*c.ClaimHelper, error) {
|
||||
pbClaim := new(pb.Claim)
|
||||
pbClaim.Type = streamClaim
|
||||
|
||||
helper := c.ClaimHelper{Claim: pbClaim}
|
||||
helper.Title = title
|
||||
helper.Description = description
|
||||
helper := c.StakeHelper{Claim: pbClaim}
|
||||
helper.Claim.Title = title
|
||||
helper.Claim.Description = description
|
||||
|
||||
return &helper, nil
|
||||
}
|
||||
|
||||
func SignClaim(rawTx *wire.MsgTx, privKey btcec.PrivateKey, claim, channel *c.ClaimHelper, channelClaimID string) error {
|
||||
func SignClaim(rawTx *wire.MsgTx, privKey btcec.PrivateKey, claim, channel *c.StakeHelper, channelClaimID string) error {
|
||||
claimIDHexBytes, err := hex.DecodeString(channelClaimID)
|
||||
if err != nil {
|
||||
return errors.Err(err)
|
||||
|
||||
+3
-3
@@ -7,7 +7,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/claim"
|
||||
c "github.com/lbryio/lbry.go/v2/schema/stake"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/btcsuite/btcd/btcjson"
|
||||
@@ -271,7 +271,7 @@ const (
|
||||
ClaimSupport
|
||||
)
|
||||
|
||||
func (c *Client) AddStakeToTx(rawTx *wire.MsgTx, claim *c.ClaimHelper, name string, claimAmount float64, scriptType ScriptType) error {
|
||||
func (c *Client) AddStakeToTx(rawTx *wire.MsgTx, claim *c.StakeHelper, name string, claimAmount float64, scriptType ScriptType) error {
|
||||
|
||||
address, err := c.GetNewAddress("")
|
||||
if err != nil {
|
||||
@@ -314,7 +314,7 @@ func (c *Client) AddStakeToTx(rawTx *wire.MsgTx, claim *c.ClaimHelper, name stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateChannel(name string, amount float64) (*c.ClaimHelper, *btcec.PrivateKey, error) {
|
||||
func (c *Client) CreateChannel(name string, amount float64) (*c.StakeHelper, *btcec.PrivateKey, error) {
|
||||
channel, key, err := NewChannel()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ dist: trusty
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.10.3
|
||||
- 1.15.x
|
||||
|
||||
script:
|
||||
- ./build_and_test.sh
|
||||
@@ -24,9 +24,9 @@ const lbrycrdTestnet = "lbrycrd_testnet"
|
||||
const lbrycrdRegtest = "lbrycrd_regtest"
|
||||
|
||||
var addressPrefixes = map[string][2]byte{
|
||||
lbrycrdMain: [2]byte{lbrycrdMainPubkeyPrefix, lbrycrdMainScriptPrefix},
|
||||
lbrycrdTestnet: [2]byte{lbrycrdTestnetPubkeyPrefix, lbrycrdTestnetScriptPrefix},
|
||||
lbrycrdRegtest: [2]byte{lbrycrdRegtestPubkeyPrefix, lbrycrdRegtestScriptPrefix},
|
||||
lbrycrdMain: {lbrycrdMainPubkeyPrefix, lbrycrdMainScriptPrefix},
|
||||
lbrycrdTestnet: {lbrycrdTestnetPubkeyPrefix, lbrycrdTestnetScriptPrefix},
|
||||
lbrycrdRegtest: {lbrycrdRegtestPubkeyPrefix, lbrycrdRegtestScriptPrefix},
|
||||
}
|
||||
|
||||
func PrefixIsValid(address [addressLength]byte, blockchainName string) bool {
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
package claim
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
legacy "github.com/lbryio/types/v1/go"
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
func (c *ClaimHelper) serialized() ([]byte, error) {
|
||||
serialized := c.String()
|
||||
if serialized == "" {
|
||||
return nil, errors.Err("not initialized")
|
||||
}
|
||||
|
||||
if c.LegacyClaim != nil {
|
||||
return proto.Marshal(c.getLegacyProtobuf())
|
||||
}
|
||||
|
||||
return proto.Marshal(c.getProtobuf())
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) getProtobuf() *pb.Claim {
|
||||
claim := &pb.Claim{
|
||||
Title: c.GetTitle(),
|
||||
Description: c.GetDescription(),
|
||||
Thumbnail: c.GetThumbnail(),
|
||||
Tags: c.GetTags(),
|
||||
Languages: c.GetLanguages(),
|
||||
Locations: c.GetLocations(),
|
||||
}
|
||||
if c.GetChannel() != nil {
|
||||
claim.Type = &pb.Claim_Channel{Channel: c.GetChannel()}
|
||||
} else if c.GetStream() != nil {
|
||||
claim.Type = &pb.Claim_Stream{Stream: c.GetStream()}
|
||||
} else if c.GetCollection() != nil {
|
||||
claim.Type = &pb.Claim_Collection{Collection: c.GetCollection()}
|
||||
} else if c.GetRepost() != nil {
|
||||
claim.Type = &pb.Claim_Repost{Repost: c.GetRepost()}
|
||||
}
|
||||
|
||||
return claim
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) getLegacyProtobuf() *legacy.Claim {
|
||||
v := c.LegacyClaim.GetVersion()
|
||||
t := c.LegacyClaim.GetClaimType()
|
||||
return &legacy.Claim{
|
||||
Version: &v,
|
||||
ClaimType: &t,
|
||||
Stream: c.LegacyClaim.GetStream(),
|
||||
Certificate: c.LegacyClaim.GetCertificate(),
|
||||
PublisherSignature: c.LegacyClaim.GetPublisherSignature()}
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) serializedHexString() (string, error) {
|
||||
serialized, err := c.serialized()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
serialized_hex := hex.EncodeToString(serialized)
|
||||
return serialized_hex, nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) serializedNoSignature() ([]byte, error) {
|
||||
if c.String() == "" {
|
||||
return nil, errors.Err("not initialized")
|
||||
}
|
||||
if c.Signature == nil {
|
||||
serialized, err := c.serialized()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return serialized, nil
|
||||
} else {
|
||||
if c.LegacyClaim != nil {
|
||||
clone := &legacy.Claim{}
|
||||
proto.Merge(clone, c.getLegacyProtobuf())
|
||||
proto.ClearAllExtensions(clone.PublisherSignature)
|
||||
clone.PublisherSignature = nil
|
||||
return proto.Marshal(clone)
|
||||
}
|
||||
clone := &pb.Claim{}
|
||||
proto.Merge(clone, c.getProtobuf())
|
||||
return proto.Marshal(clone)
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,14 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/schema/claim"
|
||||
"github.com/lbryio/lbry.go/v2/schema/stake"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 1 {
|
||||
claimBytes := []byte(args[0])
|
||||
decoded, err := claim.DecodeClaimBytes(claimBytes, "lbrycrd_main")
|
||||
decoded, err := stake.DecodeClaimBytes(claimBytes, "lbrycrd_main")
|
||||
if err != nil {
|
||||
fmt.Println("Decoding error:", err)
|
||||
return
|
||||
@@ -25,7 +25,7 @@ func main() {
|
||||
return
|
||||
} else if (len(args) == 2) && (args[1] == "--decode_hex") {
|
||||
claimHex := args[0]
|
||||
decoded, err := claim.DecodeClaimHex(claimHex, "lbrycrd_main")
|
||||
decoded, err := stake.DecodeClaimHex(claimHex, "lbrycrd_main")
|
||||
if err != nil {
|
||||
fmt.Println("Decoding error:", err)
|
||||
return
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
package claim
|
||||
package keys
|
||||
|
||||
import (
|
||||
"crypto/elliptic"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/asn1"
|
||||
"encoding/pem"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
type publicKeyInfo struct {
|
||||
Raw asn1.RawContent
|
||||
Algorithm pkix.AlgorithmIdentifier
|
||||
PublicKey asn1.BitString
|
||||
}
|
||||
|
||||
//This type provides compatibility with the btcec package
|
||||
type ecPrivateKey struct {
|
||||
Version int
|
||||
PrivateKey []byte
|
||||
NamedCurveOID asn1.ObjectIdentifier `asn1:"optional,explicit,tag:0"`
|
||||
PublicKey asn1.BitString `asn1:"optional,explicit,tag:1"`
|
||||
}
|
||||
|
||||
func PublicKeyToDER(publicKey *btcec.PublicKey) ([]byte, error) {
|
||||
var publicKeyBytes []byte
|
||||
var publicKeyAlgorithm pkix.AlgorithmIdentifier
|
||||
@@ -35,16 +50,18 @@ func PublicKeyToDER(publicKey *btcec.PublicKey) ([]byte, error) {
|
||||
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) GetPublicKey() (*btcec.PublicKey, error) {
|
||||
if c.GetChannel() == nil {
|
||||
return nil, errors.Err("claim is not of type channel, so there is no public key to get")
|
||||
}
|
||||
return getPublicKeyFromBytes(c.GetChannel().PublicKey)
|
||||
}
|
||||
|
||||
func getPublicKeyFromBytes(pubKeyBytes []byte) (*btcec.PublicKey, error) {
|
||||
func GetPublicKeyFromBytes(pubKeyBytes []byte) (*btcec.PublicKey, error) {
|
||||
PKInfo := publicKeyInfo{}
|
||||
asn1.Unmarshal(pubKeyBytes, &PKInfo)
|
||||
pubkeyBytes1 := []byte(PKInfo.PublicKey.Bytes)
|
||||
return btcec.ParsePubKey(pubkeyBytes1, btcec.S256())
|
||||
}
|
||||
|
||||
//Returns a btec.Private key object if provided a correct secp256k1 encoded pem.
|
||||
func ExtractKeyFromPem(pm string) (*btcec.PrivateKey, *btcec.PublicKey) {
|
||||
byta := []byte(pm)
|
||||
blck, _ := pem.Decode(byta)
|
||||
var ecp ecPrivateKey
|
||||
asn1.Unmarshal(blck.Bytes, &ecp)
|
||||
return btcec.PrivKeyFromBytes(btcec.S256(), ecp.PrivateKey)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package claim
|
||||
package keys
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
@@ -10,12 +11,13 @@ import (
|
||||
// lbry SDK does as this is the bytes that are put into protobuf and the same bytes are used for verify signatures.
|
||||
// Making sure these
|
||||
func TestPublicKeyToDER(t *testing.T) {
|
||||
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||
cert_claim, err := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
|
||||
publicKeyHex := "3056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||
pubKeyBytes, err := hex.DecodeString(publicKeyHex)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
p1, err := getPublicKeyFromBytes(cert_claim.Claim.GetChannel().PublicKey)
|
||||
|
||||
p1, err := GetPublicKeyFromBytes(pubKeyBytes)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -24,11 +26,11 @@ func TestPublicKeyToDER(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
for i, b := range cert_claim.Claim.GetChannel().PublicKey {
|
||||
for i, b := range pubKeyBytes {
|
||||
assert.Assert(t, b == pubkeyBytes2[i], "DER format in bytes must match!")
|
||||
}
|
||||
|
||||
p2, err := getPublicKeyFromBytes(pubkeyBytes2)
|
||||
p2, err := GetPublicKeyFromBytes(pubkeyBytes2)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/schema/keys"
|
||||
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
@@ -71,20 +73,20 @@ func TestCreateChannelClaim(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
pubKeyBytes, err := PublicKeyToDER(private.PubKey())
|
||||
pubKeyBytes, err := keys.PublicKeyToDER(private.PubKey())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
claim := &ClaimHelper{Claim: newChannelClaim(), Version: NoSig}
|
||||
claim.GetChannel().PublicKey = pubKeyBytes
|
||||
claim.Title = "Test Channel Title"
|
||||
claim.Description = "Test Channel Description"
|
||||
claim.GetChannel().Cover = &pb.Source{Url: "http://testcoverurl.com"}
|
||||
claim.Tags = []string{"TagA", "TagB", "TagC"}
|
||||
claim.Languages = []*pb.Language{{Language: pb.Language_en}, {Language: pb.Language_es}}
|
||||
claim.Thumbnail = &pb.Source{Url: "http://thumbnailurl.com"}
|
||||
claim.GetChannel().WebsiteUrl = "http://homepageurl.com"
|
||||
claim.Locations = []*pb.Location{{Country: pb.Location_AD}, {Country: pb.Location_US, State: "NJ", City: "some city"}}
|
||||
claim := &StakeHelper{Claim: newChannelClaim(), Version: NoSig}
|
||||
claim.Claim.GetChannel().PublicKey = pubKeyBytes
|
||||
claim.Claim.Title = "Test Channel Title"
|
||||
claim.Claim.Description = "Test Channel Description"
|
||||
claim.Claim.GetChannel().Cover = &pb.Source{Url: "http://testcoverurl.com"}
|
||||
claim.Claim.Tags = []string{"TagA", "TagB", "TagC"}
|
||||
claim.Claim.Languages = []*pb.Language{{Language: pb.Language_en}, {Language: pb.Language_es}}
|
||||
claim.Claim.Thumbnail = &pb.Source{Url: "http://thumbnailurl.com"}
|
||||
claim.Claim.GetChannel().WebsiteUrl = "http://homepageurl.com"
|
||||
claim.Claim.Locations = []*pb.Location{{Country: pb.Location_AD}, {Country: pb.Location_US, State: "NJ", City: "some city"}}
|
||||
|
||||
rawClaim, err := claim.CompileValue()
|
||||
if err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -232,13 +232,13 @@ func TestMigrationFromV1YTSync(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Assert(t, claim.GetTitle() == "Here are 5 Reasons I ❤️ Nextcloud | TLG")
|
||||
assert.Assert(t, claim.GetDescription() == "Find out more about Nextcloud: https://nextcloud.com/\n\nYou can find me on these socials:\n * Forums: https://forum.heavyelement.io/\n * Podcast: https://offtopical.net\n * Patreon: https://patreon.com/thelinuxgamer\n * Merch: https://teespring.com/stores/official-linux-gamer\n * Twitch: https://twitch.tv/xondak\n * Twitter: https://twitter.com/thelinuxgamer\n\n...\nhttps://www.youtube.com/watch?v=FrTdBCOS_fc")
|
||||
assert.Assert(t, claim.Claim.GetTitle() == "Here are 5 Reasons I ❤️ Nextcloud | TLG")
|
||||
assert.Assert(t, claim.Claim.GetDescription() == "Find out more about Nextcloud: https://nextcloud.com/\n\nYou can find me on these socials:\n * Forums: https://forum.heavyelement.io/\n * Podcast: https://offtopical.net\n * Patreon: https://patreon.com/thelinuxgamer\n * Merch: https://teespring.com/stores/official-linux-gamer\n * Twitch: https://twitch.tv/xondak\n * Twitter: https://twitter.com/thelinuxgamer\n\n...\nhttps://www.youtube.com/watch?v=FrTdBCOS_fc")
|
||||
assert.Assert(t, claim.GetStream().GetLicense() == "Copyrighted (contact author)")
|
||||
assert.Assert(t, claim.GetStream().GetAuthor() == "The Linux Gamer")
|
||||
//?assert.Assert(t, claim.GetStream().GetLanguages()[0])
|
||||
assert.Assert(t, claim.GetStream().GetSource().GetMediaType() == "video/mp4")
|
||||
assert.Assert(t, claim.GetThumbnail().GetUrl() == "https://berk.ninja/thumbnails/FrTdBCOS_fc")
|
||||
assert.Assert(t, claim.Claim.GetThumbnail().GetUrl() == "https://berk.ninja/thumbnails/FrTdBCOS_fc")
|
||||
sdHashBytes, err := hex.DecodeString("040e8ac6e89c061f982528c23ad33829fd7146435bf7a4cc22f0bff70c4fe0b91fd36da9a375e3e1c171db825bf5d1f3")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@@ -254,7 +254,7 @@ func TestMigrationFromV1YTSync(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Assert(t, bytes.Equal(pubKeyBytes, channel.GetChannel().GetPublicKey()))
|
||||
assert.Assert(t, bytes.Equal(pubKeyBytes, channel.Claim.GetChannel().GetPublicKey()))
|
||||
}
|
||||
|
||||
func TestMigrationFromV1UnsignedWithFee(t *testing.T) {
|
||||
@@ -263,8 +263,8 @@ func TestMigrationFromV1UnsignedWithFee(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Assert(t, claim.GetTitle() == "rpg midi")
|
||||
assert.Assert(t, claim.GetDescription() == "midi")
|
||||
assert.Assert(t, claim.Claim.GetTitle() == "rpg midi")
|
||||
assert.Assert(t, claim.Claim.GetDescription() == "midi")
|
||||
assert.Assert(t, claim.GetStream().GetLicense() == "Creative Commons Attribution 4.0 International")
|
||||
assert.Assert(t, claim.GetStream().GetAuthor() == "rpg midi")
|
||||
//assert.Assert(t, claim.GetStream().GetLanguage() == "en")
|
||||
@@ -1,17 +1,21 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
)
|
||||
|
||||
func marshalToString(c *ClaimHelper) (string, error) {
|
||||
func marshalToString(c *StakeHelper) (string, error) {
|
||||
m_pb := &jsonpb.Marshaler{}
|
||||
return m_pb.MarshalToString(c)
|
||||
if c.IsSupport() {
|
||||
return m_pb.MarshalToString(c.Support)
|
||||
}
|
||||
return m_pb.MarshalToString(c.Claim)
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) RenderJSON() (string, error) {
|
||||
func (c *StakeHelper) RenderJSON() (string, error) {
|
||||
r, err := marshalToString(c)
|
||||
if err != nil {
|
||||
fmt.Println("err")
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -0,0 +1,105 @@
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
legacy "github.com/lbryio/types/v1/go"
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
func (c *StakeHelper) serialized() ([]byte, error) {
|
||||
serialized := c.Claim.String() + c.Support.String()
|
||||
if serialized == "" {
|
||||
return nil, errors.Err("not initialized")
|
||||
}
|
||||
|
||||
if c.LegacyClaim != nil {
|
||||
return proto.Marshal(c.getLegacyProtobuf())
|
||||
} else if c.IsSupport() {
|
||||
return proto.Marshal(c.getSupportProtobuf())
|
||||
}
|
||||
|
||||
return proto.Marshal(c.getClaimProtobuf())
|
||||
}
|
||||
|
||||
func (c *StakeHelper) getClaimProtobuf() *pb.Claim {
|
||||
claim := &pb.Claim{
|
||||
Title: c.Claim.GetTitle(),
|
||||
Description: c.Claim.GetDescription(),
|
||||
Thumbnail: c.Claim.GetThumbnail(),
|
||||
Tags: c.Claim.GetTags(),
|
||||
Languages: c.Claim.GetLanguages(),
|
||||
Locations: c.Claim.GetLocations(),
|
||||
}
|
||||
if c.Claim.GetChannel() != nil {
|
||||
claim.Type = &pb.Claim_Channel{Channel: c.Claim.GetChannel()}
|
||||
} else if c.GetStream() != nil {
|
||||
claim.Type = &pb.Claim_Stream{Stream: c.GetStream()}
|
||||
} else if c.Claim.GetCollection() != nil {
|
||||
claim.Type = &pb.Claim_Collection{Collection: c.Claim.GetCollection()}
|
||||
} else if c.Claim.GetRepost() != nil {
|
||||
claim.Type = &pb.Claim_Repost{Repost: c.Claim.GetRepost()}
|
||||
}
|
||||
|
||||
return claim
|
||||
}
|
||||
|
||||
func (c *StakeHelper) getSupportProtobuf() *pb.Support {
|
||||
return &pb.Support{
|
||||
Emoji: c.Support.GetEmoji(),
|
||||
XXX_NoUnkeyedLiteral: struct{}{},
|
||||
XXX_unrecognized: nil,
|
||||
XXX_sizecache: 0,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *StakeHelper) getLegacyProtobuf() *legacy.Claim {
|
||||
v := c.LegacyClaim.GetVersion()
|
||||
t := c.LegacyClaim.GetClaimType()
|
||||
return &legacy.Claim{
|
||||
Version: &v,
|
||||
ClaimType: &t,
|
||||
Stream: c.LegacyClaim.GetStream(),
|
||||
Certificate: c.LegacyClaim.GetCertificate(),
|
||||
PublisherSignature: c.LegacyClaim.GetPublisherSignature()}
|
||||
}
|
||||
|
||||
func (c *StakeHelper) serializedHexString() (string, error) {
|
||||
serialized, err := c.serialized()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
serialized_hex := hex.EncodeToString(serialized)
|
||||
return serialized_hex, nil
|
||||
}
|
||||
|
||||
func (c *StakeHelper) serializedNoSignature() ([]byte, error) {
|
||||
if c.Claim.String() == "" && c.Support.String() == "" {
|
||||
return nil, errors.Err("not initialized")
|
||||
}
|
||||
if c.Signature == nil {
|
||||
serialized, err := c.serialized()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return serialized, nil
|
||||
} else {
|
||||
if c.LegacyClaim != nil {
|
||||
clone := &legacy.Claim{}
|
||||
proto.Merge(clone, c.getLegacyProtobuf())
|
||||
proto.ClearAllExtensions(clone.PublisherSignature)
|
||||
clone.PublisherSignature = nil
|
||||
return proto.Marshal(clone)
|
||||
} else if c.IsSupport() {
|
||||
clone := &pb.Support{}
|
||||
proto.Merge(clone, c.getSupportProtobuf())
|
||||
return proto.Marshal(clone)
|
||||
}
|
||||
clone := &pb.Claim{}
|
||||
proto.Merge(clone, c.getClaimProtobuf())
|
||||
return proto.Marshal(clone)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
func Sign(privKey btcec.PrivateKey, channel ClaimHelper, claim ClaimHelper, k string) (*Signature, error) {
|
||||
if channel.GetChannel() == nil {
|
||||
func Sign(privKey btcec.PrivateKey, channel StakeHelper, claim StakeHelper, k string) (*Signature, error) {
|
||||
if channel.Claim.GetChannel() == nil {
|
||||
return nil, errors.Err("claim as channel is not of type channel")
|
||||
}
|
||||
if claim.LegacyClaim != nil {
|
||||
@@ -21,7 +21,7 @@ func Sign(privKey btcec.PrivateKey, channel ClaimHelper, claim ClaimHelper, k st
|
||||
return claim.sign(privKey, channel, k)
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) sign(privKey btcec.PrivateKey, channel ClaimHelper, firstInputTxID string) (*Signature, error) {
|
||||
func (c *StakeHelper) sign(privKey btcec.PrivateKey, channel StakeHelper, firstInputTxID string) (*Signature, error) {
|
||||
|
||||
txidBytes, err := hex.DecodeString(firstInputTxID)
|
||||
if err != nil {
|
||||
@@ -52,7 +52,7 @@ func (c *ClaimHelper) sign(privKey btcec.PrivateKey, channel ClaimHelper, firstI
|
||||
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) signV1(privKey btcec.PrivateKey, channel ClaimHelper, claimAddress string) (*Signature, error) {
|
||||
func (c *StakeHelper) signV1(privKey btcec.PrivateKey, channel StakeHelper, claimAddress string) (*Signature, error) {
|
||||
metadataBytes, err := c.serializedNoSignature()
|
||||
if err != nil {
|
||||
return nil, errors.Err(err)
|
||||
@@ -1,9 +1,13 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/schema/keys"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
@@ -14,13 +18,13 @@ func TestSign(t *testing.T) {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
channel := &ClaimHelper{newChannelClaim(), nil, nil, NoSig, nil, nil}
|
||||
pubkeyBytes, err := PublicKeyToDER(privateKey.PubKey())
|
||||
channel := &StakeHelper{newChannelClaim(), nil, nil, nil, NoSig, nil, nil}
|
||||
pubkeyBytes, err := keys.PublicKeyToDER(privateKey.PubKey())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
channel.GetChannel().PublicKey = pubkeyBytes
|
||||
channel.Claim.GetChannel().PublicKey = pubkeyBytes
|
||||
claimID := "cf3f7c898af87cc69b06a6ac7899efb9a4878fdb" //Fake
|
||||
txid := "4c1df9e022e396859175f9bfa69b38e444db10fb53355fa99a0989a83bcdb82f" //Fake
|
||||
claimIDHexBytes, err := hex.DecodeString(claimID)
|
||||
@@ -29,7 +33,7 @@ func TestSign(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
|
||||
claim := &StakeHelper{newStreamClaim(), nil, nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
|
||||
claim.Claim.Title = "Test title"
|
||||
claim.Claim.Description = "Test description"
|
||||
sig, err := Sign(*privateKey, *channel, *claim, txid)
|
||||
@@ -78,6 +82,61 @@ func TestSign(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestSignSupportWithChannel(t *testing.T) {
|
||||
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||
channel, err := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
privateKey, err := btcec.NewPrivateKey(btcec.S256())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
pubkeyBytes, err := keys.PublicKeyToDER(privateKey.PubKey())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
channel.Claim.GetChannel().PublicKey = pubkeyBytes
|
||||
|
||||
claimID := "251305ca93d4dbedb50dceb282ebcb7b07b7ac64"
|
||||
txid := "4c1df9e022e396859175f9bfa69b38e444db10fb53355fa99a0989a83bcdb82f" //Fake
|
||||
claimIDHexBytes, err := hex.DecodeString(claimID)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
support := &StakeHelper{nil, &pb.Support{}, nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
|
||||
sig, err := Sign(*privateKey, *channel, *support, txid)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
signatureBytes, err := sig.LBRYSDKEncode()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
support.Signature = signatureBytes
|
||||
compiledSupport, err := support.CompileValue()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
support, err = DecodeSupportBytes(compiledSupport, "lbrycrd_main")
|
||||
|
||||
valid, err := support.ValidateClaimSignature(channel, txid, claimID, "lbrycrd_main")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Assert(t, valid, "could not verify signature")
|
||||
}
|
||||
|
||||
func TestSignWithV1Channel(t *testing.T) {
|
||||
cert_claim_hex := "08011002225e0801100322583056301006072a8648ce3d020106052b8104000a03420004d015365a40f3e5c03c87227168e5851f44659837bcf6a3398ae633bc37d04ee19baeb26dc888003bd728146dbea39f5344bf8c52cedaf1a3a1623a0166f4a367"
|
||||
channel, err := DecodeClaimHex(cert_claim_hex, "lbrycrd_main")
|
||||
@@ -89,12 +148,12 @@ func TestSignWithV1Channel(t *testing.T) {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
pubkeyBytes, err := PublicKeyToDER(privateKey.PubKey())
|
||||
pubkeyBytes, err := keys.PublicKeyToDER(privateKey.PubKey())
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
channel.GetChannel().PublicKey = pubkeyBytes
|
||||
channel.Claim.GetChannel().PublicKey = pubkeyBytes
|
||||
|
||||
claimID := "251305ca93d4dbedb50dceb282ebcb7b07b7ac64"
|
||||
txid := "4c1df9e022e396859175f9bfa69b38e444db10fb53355fa99a0989a83bcdb82f" //Fake
|
||||
@@ -104,7 +163,7 @@ func TestSignWithV1Channel(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
claim := &ClaimHelper{newStreamClaim(), nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
|
||||
claim := &StakeHelper{newStreamClaim(), nil, nil, reverseBytes(claimIDHexBytes), WithSig, nil, nil}
|
||||
claim.Claim.Title = "Test title"
|
||||
claim.Claim.Description = "Test description"
|
||||
sig, err := Sign(*privateKey, *channel, *claim, txid)
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
@@ -6,9 +6,11 @@ import (
|
||||
|
||||
"github.com/lbryio/lbry.go/v2/extras/errors"
|
||||
"github.com/lbryio/lbry.go/v2/schema/address"
|
||||
"github.com/lbryio/lbry.go/v2/schema/keys"
|
||||
legacy_pb "github.com/lbryio/types/v1/go"
|
||||
pb "github.com/lbryio/types/v2/go"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
@@ -25,8 +27,9 @@ const (
|
||||
UNKNOWN = version(byte(2))
|
||||
)
|
||||
|
||||
type ClaimHelper struct {
|
||||
*pb.Claim
|
||||
type StakeHelper struct {
|
||||
Claim *pb.Claim
|
||||
Support *pb.Support
|
||||
LegacyClaim *legacy_pb.Claim
|
||||
ClaimID []byte
|
||||
Version version
|
||||
@@ -36,7 +39,7 @@ type ClaimHelper struct {
|
||||
|
||||
const migrationErrorMessage = "migration from v1 to v2 protobuf failed with: "
|
||||
|
||||
func (c *ClaimHelper) ValidateAddresses(blockchainName string) error {
|
||||
func (c *StakeHelper) ValidateAddresses(blockchainName string) error {
|
||||
if c.Claim != nil { // V2
|
||||
// check the validity of a fee address
|
||||
if c.Claim.GetStream() != nil {
|
||||
@@ -46,7 +49,7 @@ func (c *ClaimHelper) ValidateAddresses(blockchainName string) error {
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
} else if c.GetChannel() != nil {
|
||||
} else if c.Claim.GetChannel() != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -80,8 +83,8 @@ func getVersionFromByte(versionByte byte) version {
|
||||
return UNKNOWN
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) ValidateCertificate() error {
|
||||
if c.GetChannel() == nil {
|
||||
func (c *StakeHelper) ValidateCertificate() error {
|
||||
if !c.IsClaim() || c.Claim.GetChannel() == nil {
|
||||
return nil
|
||||
}
|
||||
_, err := c.GetPublicKey()
|
||||
@@ -91,8 +94,24 @@ func (c *ClaimHelper) ValidateCertificate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) error {
|
||||
if c.String() != "" {
|
||||
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 {
|
||||
return errors.Err("already initialized")
|
||||
}
|
||||
if len(raw_claim) < 1 {
|
||||
@@ -101,13 +120,14 @@ func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) err
|
||||
|
||||
var claim_pb *pb.Claim
|
||||
var legacy_claim_pb *legacy_pb.Claim
|
||||
var support_pb *pb.Support
|
||||
|
||||
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 {
|
||||
if len(raw_claim) < 85 {
|
||||
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
|
||||
@@ -115,8 +135,17 @@ func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) err
|
||||
pbPayload = raw_claim[85:] // protobuf payload = remaining bytes
|
||||
}
|
||||
|
||||
claim_pb = &pb.Claim{}
|
||||
err := proto.Unmarshal(pbPayload, claim_pb)
|
||||
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
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
legacy_claim_pb = &legacy_pb.Claim{}
|
||||
legacyErr := proto.Unmarshal(raw_claim, legacy_claim_pb)
|
||||
@@ -138,8 +167,9 @@ func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) err
|
||||
}
|
||||
}
|
||||
|
||||
*c = ClaimHelper{
|
||||
*c = StakeHelper{
|
||||
Claim: claim_pb,
|
||||
Support: support_pb,
|
||||
LegacyClaim: legacy_claim_pb,
|
||||
ClaimID: claimID,
|
||||
Version: version,
|
||||
@@ -161,7 +191,7 @@ func (c *ClaimHelper) LoadFromBytes(raw_claim []byte, blockchainName string) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) LoadFromHexString(claim_hex string, blockchainName string) error {
|
||||
func (c *StakeHelper) LoadFromHexString(claim_hex string, blockchainName string) error {
|
||||
buf, err := hex.DecodeString(claim_hex)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -169,8 +199,16 @@ func (c *ClaimHelper) LoadFromHexString(claim_hex string, blockchainName string)
|
||||
return c.LoadFromBytes(buf, blockchainName)
|
||||
}
|
||||
|
||||
func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
|
||||
claim := &ClaimHelper{&pb.Claim{}, nil, nil, NoSig, nil, nil}
|
||||
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}
|
||||
err := claim.LoadFromBytes(serialized, blockchainName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -178,7 +216,16 @@ func DecodeClaimProtoBytes(serialized []byte, blockchainName string) (*ClaimHelp
|
||||
return claim, nil
|
||||
}
|
||||
|
||||
func DecodeClaimHex(serialized string, blockchainName string) (*ClaimHelper, error) {
|
||||
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) {
|
||||
claim_bytes, err := hex.DecodeString(serialized)
|
||||
if err != nil {
|
||||
return nil, errors.Err(err)
|
||||
@@ -187,12 +234,12 @@ func DecodeClaimHex(serialized string, blockchainName string) (*ClaimHelper, err
|
||||
}
|
||||
|
||||
// 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
|
||||
func DecodeClaimBytes(serialized []byte, blockchainName string) (*ClaimHelper, error) {
|
||||
func DecodeClaimBytes(serialized []byte, blockchainName string) (*StakeHelper, error) {
|
||||
helper, err := DecodeClaimProtoBytes(serialized, blockchainName)
|
||||
if err == nil {
|
||||
return helper, nil
|
||||
}
|
||||
helper = &ClaimHelper{}
|
||||
helper = &StakeHelper{}
|
||||
//If protobuf fails, try json versions before returning an error.
|
||||
v1Claim := new(V1Claim)
|
||||
err = v1Claim.Unmarshal(serialized)
|
||||
@@ -225,14 +272,23 @@ func DecodeClaimBytes(serialized []byte, blockchainName string) (*ClaimHelper, e
|
||||
return helper, nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) GetStream() *pb.Stream {
|
||||
// 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 {
|
||||
if c != nil {
|
||||
return c.Claim.GetStream()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) CompileValue() ([]byte, error) {
|
||||
func (c *StakeHelper) CompileValue() ([]byte, error) {
|
||||
payload, err := c.serialized()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -247,3 +303,15 @@ func (c *ClaimHelper) CompileValue() ([]byte, error) {
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import "testing"
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/asn1"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"math/big"
|
||||
@@ -13,12 +11,6 @@ import (
|
||||
"github.com/lbryio/lbry.go/v2/schema/address"
|
||||
)
|
||||
|
||||
type publicKeyInfo struct {
|
||||
Raw asn1.RawContent
|
||||
Algorithm pkix.AlgorithmIdentifier
|
||||
PublicKey asn1.BitString
|
||||
}
|
||||
|
||||
const SECP256k1 = "SECP256k1"
|
||||
|
||||
//const NIST256p = "NIST256p"
|
||||
@@ -34,7 +26,7 @@ func getClaimSignatureDigest(bytes ...[]byte) [32]byte {
|
||||
return [32]byte(digest)
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) VerifyDigest(certificate *ClaimHelper, signature [64]byte, digest [32]byte) bool {
|
||||
func (c *StakeHelper) VerifyDigest(certificate *StakeHelper, signature [64]byte, digest [32]byte) bool {
|
||||
if certificate == nil {
|
||||
return false
|
||||
}
|
||||
@@ -50,7 +42,7 @@ func (c *ClaimHelper) VerifyDigest(certificate *ClaimHelper, signature [64]byte,
|
||||
return ecdsa.Verify(pk.ToECDSA(), digest[:], R, S)
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, k string, certificateId string, blockchainName string) (bool, error) {
|
||||
func (c *StakeHelper) ValidateClaimSignature(certificate *StakeHelper, k string, certificateId string, blockchainName string) (bool, error) {
|
||||
if c.LegacyClaim != nil {
|
||||
return c.validateV1ClaimSignature(certificate, k, certificateId, blockchainName)
|
||||
}
|
||||
@@ -58,7 +50,7 @@ func (c *ClaimHelper) ValidateClaimSignature(certificate *ClaimHelper, k string,
|
||||
return c.validateClaimSignature(certificate, k, certificateId, blockchainName)
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) validateClaimSignature(certificate *ClaimHelper, firstInputTxHash, certificateId string, blockchainName string) (bool, error) {
|
||||
func (c *StakeHelper) validateClaimSignature(certificate *StakeHelper, firstInputTxHash, certificateId string, blockchainName string) (bool, error) {
|
||||
certificateIdSlice, err := hex.DecodeString(certificateId)
|
||||
if err != nil {
|
||||
return false, errors.Err(err)
|
||||
@@ -82,7 +74,7 @@ func (c *ClaimHelper) validateClaimSignature(certificate *ClaimHelper, firstInpu
|
||||
return c.VerifyDigest(certificate, signatureBytes, claimDigest), nil
|
||||
}
|
||||
|
||||
func (c *ClaimHelper) validateV1ClaimSignature(certificate *ClaimHelper, claimAddy string, certificateId string, blockchainName string) (bool, error) {
|
||||
func (c *StakeHelper) validateV1ClaimSignature(certificate *StakeHelper, claimAddy string, certificateId string, blockchainName string) (bool, error) {
|
||||
addressBytes, err := address.DecodeAddress(claimAddy, blockchainName)
|
||||
if err != nil {
|
||||
return false, err
|
||||
@@ -1,4 +1,4 @@
|
||||
package claim
|
||||
package stake
|
||||
|
||||
import (
|
||||
"testing"
|
||||
+347
@@ -0,0 +1,347 @@
|
||||
package url
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const regexPartProtocol = "^((?:lbry://|https://)?)"
|
||||
const regexPartHost = "((?:open.lbry.com/|lbry.tv/|lbry.lat/|lbry.fr/|lbry.in/)?)"
|
||||
const regexPartStreamOrChannelName = "([^:$#/]*)"
|
||||
const regexPartModifierSeparator = "([:$#]?)([^/]*)"
|
||||
const regexQueryStringBreaker = "^([\\S]+)([?][\\S]*)"
|
||||
const urlComponentsSize = 9
|
||||
|
||||
const ChannelNameMinLength = 1
|
||||
const ClaimIdMaxLength = 40
|
||||
const ProtoDefault = "lbry://"
|
||||
const RegexClaimId = "(?i)^[0-9a-f]+$"
|
||||
const RegexInvalidUri = "(?i)[ =&#:$@%?;/\\\\\\\\\\\"<>%\\\\{\\\\}|^~\\\\[\\\\]`\\u0000-\\u0008\\u000b-\\u000c\\u000e-\\u001F\\uD800-\\uDFFF\\uFFFE-\\uFFFF]"
|
||||
|
||||
type LbryUri struct {
|
||||
Path string
|
||||
IsChannel bool
|
||||
StreamName string
|
||||
StreamClaimId string
|
||||
ChannelName string
|
||||
ChannelClaimId string
|
||||
PrimaryClaimSequence int
|
||||
SecondaryClaimSequence int
|
||||
PrimaryBidPosition int
|
||||
SecondaryBidPosition int
|
||||
ClaimName string
|
||||
ClaimId string
|
||||
ContentName string
|
||||
QueryString string
|
||||
}
|
||||
|
||||
type UriModifier struct {
|
||||
ClaimId string
|
||||
ClaimSequence int
|
||||
BidPosition int
|
||||
}
|
||||
|
||||
func (uri LbryUri) IsChannelUrl() bool {
|
||||
return (!isEmpty(uri.ChannelName) && isEmpty(uri.StreamName)) || (!isEmpty(uri.ClaimName) && strings.HasPrefix(uri.ClaimName, "@"))
|
||||
}
|
||||
|
||||
func (uri LbryUri) IsNameValid(name string) bool {
|
||||
return !regexp.MustCompile(RegexInvalidUri).MatchString(name)
|
||||
}
|
||||
|
||||
func (uri LbryUri) String() string {
|
||||
return uri.Build(true, ProtoDefault, false)
|
||||
}
|
||||
|
||||
func (uri LbryUri) VanityString() string {
|
||||
return uri.Build(true, ProtoDefault, true)
|
||||
}
|
||||
|
||||
func (uri LbryUri) TvString() string {
|
||||
return uri.Build(true, "https://lbry.tv/", false)
|
||||
}
|
||||
|
||||
func (uri LbryUri) Build(includeProto bool, protocol string, vanity bool) string {
|
||||
formattedChannelName := ""
|
||||
if !isEmpty(uri.ChannelName) {
|
||||
formattedChannelName = uri.ChannelName
|
||||
if !strings.HasPrefix(formattedChannelName, "@") {
|
||||
formattedChannelName = fmt.Sprintf("@%s", formattedChannelName)
|
||||
}
|
||||
}
|
||||
primaryClaimName := uri.ClaimName
|
||||
if isEmpty(primaryClaimName) {
|
||||
primaryClaimName = uri.ContentName
|
||||
}
|
||||
if isEmpty(primaryClaimName) {
|
||||
primaryClaimName = formattedChannelName
|
||||
}
|
||||
if isEmpty(primaryClaimName) {
|
||||
primaryClaimName = uri.StreamName
|
||||
}
|
||||
|
||||
primaryClaimId := uri.ClaimId
|
||||
if isEmpty(primaryClaimId) {
|
||||
if !isEmpty(formattedChannelName) {
|
||||
primaryClaimId = uri.ChannelClaimId
|
||||
} else {
|
||||
primaryClaimId = uri.StreamClaimId
|
||||
}
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
if includeProto {
|
||||
sb.WriteString(protocol)
|
||||
}
|
||||
sb.WriteString(primaryClaimName)
|
||||
if vanity {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
secondaryClaimName := ""
|
||||
if isEmpty(uri.ClaimName) && !isEmpty(uri.ContentName) {
|
||||
secondaryClaimName = uri.ContentName
|
||||
}
|
||||
if isEmpty(secondaryClaimName) {
|
||||
if !isEmpty(formattedChannelName) {
|
||||
secondaryClaimName = uri.StreamName
|
||||
}
|
||||
}
|
||||
secondaryClaimId := ""
|
||||
if !isEmpty(secondaryClaimName) {
|
||||
secondaryClaimId = uri.StreamClaimId
|
||||
}
|
||||
|
||||
if !isEmpty(primaryClaimId) {
|
||||
sb.WriteString("#")
|
||||
sb.WriteString(primaryClaimId)
|
||||
} else if uri.PrimaryClaimSequence > 0 {
|
||||
sb.WriteString(":")
|
||||
sb.WriteString(strconv.Itoa(uri.PrimaryClaimSequence))
|
||||
} else if uri.PrimaryBidPosition > 0 {
|
||||
sb.WriteString("$")
|
||||
sb.WriteString(strconv.Itoa(uri.PrimaryBidPosition))
|
||||
}
|
||||
|
||||
if !isEmpty(secondaryClaimName) {
|
||||
sb.WriteString("/")
|
||||
sb.WriteString(secondaryClaimName)
|
||||
}
|
||||
|
||||
if !isEmpty(secondaryClaimId) {
|
||||
sb.WriteString("#")
|
||||
sb.WriteString(secondaryClaimId)
|
||||
} else if uri.SecondaryClaimSequence > 0 {
|
||||
sb.WriteString(":")
|
||||
sb.WriteString(strconv.Itoa(uri.SecondaryClaimSequence))
|
||||
} else if uri.SecondaryBidPosition > 0 {
|
||||
sb.WriteString("$")
|
||||
sb.WriteString(strconv.Itoa(uri.SecondaryBidPosition))
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func Parse(url string, requireProto bool) (*LbryUri, error) {
|
||||
if isEmpty(url) {
|
||||
return nil, errors.New("invalid url parameter")
|
||||
}
|
||||
|
||||
reComponents := regexp.MustCompile(
|
||||
fmt.Sprintf("(?i)%s%s%s%s(/?)%s%s",
|
||||
regexPartProtocol,
|
||||
regexPartHost,
|
||||
regexPartStreamOrChannelName,
|
||||
regexPartModifierSeparator,
|
||||
regexPartStreamOrChannelName,
|
||||
regexPartModifierSeparator))
|
||||
reSeparateQueryString := regexp.MustCompile(regexQueryStringBreaker)
|
||||
|
||||
cleanUrl := url
|
||||
queryString := ""
|
||||
|
||||
qsMatches := reSeparateQueryString.FindStringSubmatch(url)
|
||||
if len(qsMatches) == 3 {
|
||||
cleanUrl = qsMatches[1]
|
||||
queryString = qsMatches[2][1:]
|
||||
}
|
||||
|
||||
var components []string
|
||||
componentMatches := reComponents.FindStringSubmatch(cleanUrl)
|
||||
for _, component := range componentMatches[1:] {
|
||||
components = append(components, component)
|
||||
}
|
||||
if len(components) != urlComponentsSize {
|
||||
return nil, errors.New("regular expression error occurred while trying to Parse the value")
|
||||
}
|
||||
|
||||
/*
|
||||
* components[0] = proto
|
||||
* components[1] = host
|
||||
* components[2] = streamName or channelName
|
||||
* components[3] = primaryModSeparator
|
||||
* components[4] = primaryModValue
|
||||
* components[5] = path separator
|
||||
* components[6] = possibleStreamName
|
||||
* components[7] = secondaryModSeparator
|
||||
* components[8] = secondaryModValue
|
||||
*/
|
||||
if requireProto && isEmpty(components[0]) {
|
||||
return nil, errors.New("url must include a protocol prefix (lbry://)")
|
||||
}
|
||||
if isEmpty(components[2]) {
|
||||
return nil, errors.New("url does not include a name")
|
||||
}
|
||||
for _, component := range components[2:] {
|
||||
if strings.Index(component, " ") > -1 {
|
||||
return nil, errors.New("url cannot include a space")
|
||||
}
|
||||
}
|
||||
|
||||
streamOrChannelName := components[2]
|
||||
primaryModSeparator := components[3]
|
||||
primaryModValue := components[4]
|
||||
possibleStreamName := components[6]
|
||||
secondaryModSeparator := components[7]
|
||||
secondaryModValue := components[8]
|
||||
primaryClaimId := ""
|
||||
primaryClaimSequence := -1
|
||||
primaryBidPosition := -1
|
||||
secondaryClaimSequence := -1
|
||||
secondaryBidPosition := -1
|
||||
|
||||
includesChannel := strings.HasPrefix(streamOrChannelName, "@")
|
||||
isChannel := includesChannel && isEmpty(possibleStreamName)
|
||||
channelName := ""
|
||||
if includesChannel && len(streamOrChannelName) > 1 {
|
||||
channelName = streamOrChannelName[1:]
|
||||
}
|
||||
|
||||
// Convert the mod separators when parsing with protocol https://lbry.tv/ or similar
|
||||
// [https://] uses ':', [lbry://] expects #
|
||||
if !isEmpty(components[1]) {
|
||||
if primaryModSeparator == ":" {
|
||||
primaryModSeparator = "#"
|
||||
}
|
||||
if secondaryModSeparator == ":" {
|
||||
secondaryModSeparator = "#"
|
||||
}
|
||||
}
|
||||
|
||||
if includesChannel {
|
||||
if isEmpty(channelName) {
|
||||
// I wonder if this check is really necessary, considering the subsequent min length check
|
||||
return nil, errors.New("no channel name after @")
|
||||
}
|
||||
if len(channelName) < ChannelNameMinLength {
|
||||
return nil, errors.New(fmt.Sprintf("Channel names must be at least %d character long.", ChannelNameMinLength))
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
var primaryMod *UriModifier
|
||||
var secondaryMod *UriModifier
|
||||
if !isEmpty(primaryModSeparator) && !isEmpty(primaryModValue) {
|
||||
primaryMod, err = parseModifier(primaryModSeparator, primaryModValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
primaryClaimId = primaryMod.ClaimId
|
||||
primaryClaimSequence = primaryMod.ClaimSequence
|
||||
primaryBidPosition = primaryMod.BidPosition
|
||||
}
|
||||
if !isEmpty(secondaryModSeparator) && !isEmpty(secondaryModValue) {
|
||||
secondaryMod, err = parseModifier(secondaryModSeparator, secondaryModValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
secondaryClaimSequence = secondaryMod.ClaimSequence
|
||||
secondaryBidPosition = secondaryMod.BidPosition
|
||||
}
|
||||
|
||||
streamName := streamOrChannelName
|
||||
if includesChannel {
|
||||
streamName = possibleStreamName
|
||||
}
|
||||
|
||||
streamClaimId := ""
|
||||
if includesChannel && secondaryMod != nil {
|
||||
streamClaimId = secondaryMod.ClaimId
|
||||
} else if primaryMod != nil {
|
||||
streamClaimId = primaryMod.ClaimId
|
||||
}
|
||||
channelClaimId := ""
|
||||
if includesChannel && primaryMod != nil {
|
||||
channelClaimId = primaryMod.ClaimId
|
||||
}
|
||||
|
||||
return &LbryUri{
|
||||
Path: strings.Join(components[2:], ""),
|
||||
IsChannel: isChannel,
|
||||
StreamName: streamName,
|
||||
StreamClaimId: streamClaimId,
|
||||
ChannelName: channelName,
|
||||
ChannelClaimId: channelClaimId,
|
||||
PrimaryClaimSequence: primaryClaimSequence,
|
||||
SecondaryClaimSequence: secondaryClaimSequence,
|
||||
PrimaryBidPosition: primaryBidPosition,
|
||||
SecondaryBidPosition: secondaryBidPosition,
|
||||
ClaimName: streamOrChannelName,
|
||||
ClaimId: primaryClaimId,
|
||||
ContentName: streamName,
|
||||
QueryString: queryString,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseModifier(modSeparator string, modValue string) (*UriModifier, error) {
|
||||
claimId := ""
|
||||
claimSequence := 0
|
||||
bidPosition := 0
|
||||
|
||||
if !isEmpty(modSeparator) {
|
||||
if isEmpty(modValue) {
|
||||
return nil, errors.New(fmt.Sprintf("No modifier provided after separator %s", modSeparator))
|
||||
}
|
||||
|
||||
if modSeparator == "#" {
|
||||
claimId = modValue
|
||||
} else if modSeparator == ":" {
|
||||
claimSequence = parseInt(modValue, -1)
|
||||
} else if modSeparator == "$" {
|
||||
bidPosition = parseInt(modValue, -1)
|
||||
}
|
||||
}
|
||||
|
||||
if !isEmpty(claimId) && (len(claimId) > ClaimIdMaxLength || !regexp.MustCompile(RegexClaimId).MatchString(claimId)) {
|
||||
return nil, errors.New(fmt.Sprintf("Invalid claim ID %s", claimId))
|
||||
}
|
||||
if claimSequence == -1 {
|
||||
return nil, errors.New("claim sequence must be a number")
|
||||
}
|
||||
if bidPosition == -1 {
|
||||
return nil, errors.New("bid position must be a number")
|
||||
}
|
||||
|
||||
return &UriModifier{
|
||||
ClaimId: claimId,
|
||||
ClaimSequence: claimSequence,
|
||||
BidPosition: bidPosition,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func parseInt(value string, defaultValue int) int {
|
||||
v, err := strconv.ParseInt(value, 10, 32)
|
||||
if err != nil {
|
||||
return defaultValue
|
||||
}
|
||||
return int(v)
|
||||
}
|
||||
|
||||
func isEmpty(s string) bool {
|
||||
return len(strings.TrimSpace(s)) == 0
|
||||
}
|
||||
Reference in new issue
Block a user