-
Notifications
You must be signed in to change notification settings - Fork 15
/
tx_make_root.go
88 lines (69 loc) · 2.16 KB
/
tx_make_root.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
package cli
import (
"crypto/sha256"
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
uuid "github.com/google/uuid"
"github.com/jackalLabs/canine-chain/x/filetree/types"
"github.com/spf13/cobra"
)
var _ = strconv.Itoa(0)
func CmdMakeRoot() *cobra.Command {
cmd := &cobra.Command{
Use: "make-root [account] [root-hash-path]",
Short: "Broadcast message make root",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argAccount := args[0]
argRootHashPath := args[1]
if err != nil {
return err
}
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
// In the keeper, the merklePath function will trim the trailing slash for us but let's just do it anyways to be safe.
trimMerklePath := strings.TrimSuffix(argRootHashPath, "/")
merklePath := types.MerklePath(trimMerklePath)
editors := make(map[string]string)
trackingNumber := uuid.NewString()
// This root folder is the master root and has no file key, so there is nothing to encrypt.
// We include the creator of this root as an editor so that they can add children--folders or files
h := sha256.New()
h.Write([]byte(fmt.Sprintf("e%s%s", trackingNumber, clientCtx.GetFromAddress().String())))
hash := h.Sum(nil)
addressString := fmt.Sprintf("%x", hash)
editors[addressString] = fmt.Sprintf("%x", "Placeholder key") // Determine if we need a place holder key
jsonEditors, err := json.Marshal(editors)
if err != nil {
return err
}
h1 := sha256.New()
h1.Write([]byte(argAccount))
hash1 := h1.Sum(nil)
accountHash := fmt.Sprintf("%x", hash1)
// FE will init their own root folders
msg := types.NewMsgMakeRoot(
clientCtx.GetFromAddress().String(),
accountHash,
merklePath,
"Contents",
string(jsonEditors),
"Viewers",
trackingNumber,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}