-
Notifications
You must be signed in to change notification settings - Fork 17
/
query_files.go
127 lines (99 loc) · 3.06 KB
/
query_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cli
import (
"context"
"crypto/sha256"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/jackalLabs/canine-chain/v3/x/filetree/types"
"github.com/spf13/cobra"
)
func CmdListFiles() *cobra.Command {
cmd := &cobra.Command{
Use: "list-files",
Short: "list all files",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryAllFilesRequest{
Pagination: pageReq,
}
res, err := queryClient.FilesAll(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// Need to input the full hex formatted merkleHash address and the ownerAddress for this to work
func CmdShowFiles() *cobra.Command {
cmd := &cobra.Command{
Use: "show-files [address] [ownerAddress]",
Short: "shows a files",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
argAddress := args[0]
argOwnerAddress := args[1]
params := &types.QueryFileRequest{
Address: argAddress,
OwnerAddress: argOwnerAddress,
}
res, err := queryClient.Files(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// Query using human readable path to show that merklePath() function works as intended
// Should really be called "CmdShowFileFromPathAndOwner", but I opted to not make the name so long given that this is just a testing function
func CmdShowFileFromPath() *cobra.Command {
cmd := &cobra.Command{
Use: "show-file-from-path [path] [owner]",
Short: "shows a file given human readable path and owner's bech32 address",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
queryClient := types.NewQueryClient(clientCtx)
argAddress := args[0]
argOwnerAddress := args[1]
trimMerklePath := strings.TrimSuffix(argAddress, "/")
merklePath := types.MerklePath(trimMerklePath)
// hash the owner address alone
h := sha256.New()
h.Write([]byte(argOwnerAddress))
hash := h.Sum(nil)
accountHash := fmt.Sprintf("%x", hash)
// make the full OwnerAddress
H := sha256.New()
H.Write([]byte(fmt.Sprintf("o%s%s", merklePath, accountHash)))
Hash := H.Sum(nil)
ownerAddress := fmt.Sprintf("%x", Hash)
params := &types.QueryFileRequest{
Address: merklePath,
OwnerAddress: ownerAddress,
}
res, err := queryClient.Files(context.Background(), params)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}