claim decoding example

This commit is contained in:
Alex Grintsvayg 2018-11-08 15:52:47 -05:00
parent 66b108751d
commit a24a1a6c5e
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 36 additions and 17 deletions

23
claim/decode.go Normal file
View file

@ -0,0 +1,23 @@
package claim
import (
"bytes"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
types "github.com/lbryio/types/go"
)
func ToJSON(value []byte) (string, error) {
c := &types.Claim{}
err := proto.Unmarshal(value, c)
if err != nil {
return "", err
}
b := bytes.NewBuffer(nil)
m := jsonpb.Marshaler{Indent: " "}
err = m.Marshal(b, c)
return b.String(), err
}

View file

@ -1,12 +1,11 @@
package cmd package cmd
import ( import (
"os" "encoding/hex"
"os/signal" "fmt"
"sync"
"syscall" "github.com/lbryio/lbry.go/claim"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -20,16 +19,13 @@ func init() {
} }
func test(cmd *cobra.Command, args []string) { func test(cmd *cobra.Command, args []string) {
var wg sync.WaitGroup value, err := hex.DecodeString("080110011ac10108011279080410011a0343617422002a00320d5075626c696320446f6d61696e38004a5568747470733a2f2f737065652e63682f373961653031353766356165356535336232326562646465666663326564623862363130666437372f68773978754137686d326b32645a35477479744a336448372e6a706752005a001a42080110011a30970015f05a30531c465a3f889ab516b972c57c529cd4b57b0bd1685a19c0caa8073f6d4f3db338c1034481fb3eb37241220a696d6167652f6a7065672a5c080110031a4088d15f554d64776f3b43bc63b50c16a69162eb256c9e7afe04505f88a36d7455069de25244834f6d14479b45064d4766fa359bd041886b612040c9dbc9d1d0ec221412bcd69bf6a7d503002f09d34c76f904253a4be2")
c := make(chan os.Signal, 1) if err != nil {
signal.Notify(c, os.Interrupt, syscall.SIGTERM) panic(err)
wg.Add(1) }
go func() { s, err := claim.ToJSON(value)
defer wg.Done() if err != nil {
<-c panic(err)
log.Println("got signal") }
}() fmt.Println(s)
log.Println("waiting for ctrl+c")
wg.Wait()
log.Println("done waiting")
} }