-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.go
103 lines (81 loc) · 2.59 KB
/
utils.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
package cli
import (
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
eciesgo "github.com/ecies/go/v2"
filetypes "github.com/jackalLabs/canine-chain/v3/x/filetree/types"
"github.com/spf13/cobra"
)
func MakePrivateKey(clientCtx client.Context) (*eciesgo.PrivateKey, error) {
signed, _, err := clientCtx.Keyring.Sign(clientCtx.GetFromName(), []byte("jackal_init"))
if err != nil {
return nil, err
}
k := secp256k1.GenPrivKeyFromSecret(signed)
newKey := eciesgo.NewPrivateKeyFromBytes(k.Bytes())
return newKey, nil
}
func merkleHelper(argHashpath string) (string, string) {
// Cut out the / at the end for compatibility with types/merkle-paths.go
trimPath := strings.TrimSuffix(argHashpath, "/")
chunks := strings.Split(trimPath, "/")
parentString := strings.Join(chunks[0:len(chunks)-1], "/")
childString := (chunks[len(chunks)-1])
parentHash := filetypes.MerklePath(parentString)
h := sha256.New()
h.Write([]byte(childString))
childHash := fmt.Sprintf("%x", h.Sum(nil))
return parentHash, childHash
}
// Owner address is whoever owns this file/folder
func MakeOwnerAddress(merklePath string, user string) string {
h := sha256.New()
h.Write([]byte(user))
hash := h.Sum(nil)
accountHash := fmt.Sprintf("%x", hash)
// h1 is so named as to differentiate it from h above--else compiler complains
h1 := sha256.New()
h1.Write([]byte(fmt.Sprintf("o%s%s", merklePath, accountHash)))
hash1 := h1.Sum(nil)
ownerAddress := fmt.Sprintf("%x", hash1)
return ownerAddress
}
func encryptFileAESKey(cmd *cobra.Command, key string, argKeys string) ([]byte, error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return nil, err
}
queryClient := filetypes.NewQueryClient(clientCtx)
res, err := queryClient.Pubkey(cmd.Context(), &filetypes.QueryPubkeyRequest{Address: key})
if err != nil {
return nil, filetypes.ErrPubKeyNotFound
}
pkey, err := eciesgo.NewPublicKeyFromHex(res.Pubkey.Key)
if err != nil {
return nil, err
}
encrypted, err := eciesgo.Encrypt(pkey, []byte(argKeys))
if err != nil {
return nil, err
}
return encrypted, nil
}
func getCallerAddress(ctx client.Context) *string {
fromAddress := ctx.GetFromAddress().String()
return &fromAddress
}
func JSONMarshalViewersAndEditors(viewers map[string]string, editors map[string]string) ([]byte, []byte, error) {
jsonViewers, err := json.Marshal(viewers)
if err != nil {
return nil, nil, err
}
jsonEditors, err := json.Marshal(editors)
if err != nil {
return nil, nil, err
}
return jsonViewers, jsonEditors, nil
}