From a24a1a6c5e69006f30ab7e8d6aec9ed5a5a889d2 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Thu, 8 Nov 2018 15:52:47 -0500 Subject: [PATCH] claim decoding example --- claim/decode.go | 23 +++++++++++++++++++++++ cmd/test.go | 30 +++++++++++++----------------- 2 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 claim/decode.go diff --git a/claim/decode.go b/claim/decode.go new file mode 100644 index 0000000..86fc103 --- /dev/null +++ b/claim/decode.go @@ -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 +} diff --git a/cmd/test.go b/cmd/test.go index bc9533f..997c2a0 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -1,12 +1,11 @@ package cmd import ( - "os" - "os/signal" - "sync" - "syscall" + "encoding/hex" + "fmt" + + "github.com/lbryio/lbry.go/claim" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -20,16 +19,13 @@ func init() { } func test(cmd *cobra.Command, args []string) { - var wg sync.WaitGroup - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt, syscall.SIGTERM) - wg.Add(1) - go func() { - defer wg.Done() - <-c - log.Println("got signal") - }() - log.Println("waiting for ctrl+c") - wg.Wait() - log.Println("done waiting") + value, err := hex.DecodeString("080110011ac10108011279080410011a0343617422002a00320d5075626c696320446f6d61696e38004a5568747470733a2f2f737065652e63682f373961653031353766356165356535336232326562646465666663326564623862363130666437372f68773978754137686d326b32645a35477479744a336448372e6a706752005a001a42080110011a30970015f05a30531c465a3f889ab516b972c57c529cd4b57b0bd1685a19c0caa8073f6d4f3db338c1034481fb3eb37241220a696d6167652f6a7065672a5c080110031a4088d15f554d64776f3b43bc63b50c16a69162eb256c9e7afe04505f88a36d7455069de25244834f6d14479b45064d4766fa359bd041886b612040c9dbc9d1d0ec221412bcd69bf6a7d503002f09d34c76f904253a4be2") + if err != nil { + panic(err) + } + s, err := claim.ToJSON(value) + if err != nil { + panic(err) + } + fmt.Println(s) }