-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_helpers.go
218 lines (169 loc) · 5.55 KB
/
test_helpers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package types
import (
"crypto/sha256"
"encoding/json"
fmt "fmt"
"strings"
sdkClient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/hd"
keyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
eciesgo "github.com/ecies/go/v2"
"github.com/google/uuid"
)
// generate a mock private key using mock keyring
func MakePrivateKey(fromName string) (*eciesgo.PrivateKey, error) {
var ctx sdkClient.Context
ctx.Keyring = keyring.NewInMemory()
ctx.FromName = fromName
algo := hd.Secp256k1
_, _, err := ctx.Keyring.NewMnemonic(fromName, keyring.English, sdkTypes.FullFundraiserPath, keyring.DefaultBIP39Passphrase, algo)
if err != nil {
return nil, err
}
signed, _, err := ctx.Keyring.Sign(ctx.FromName, []byte("jackal_init"))
if err != nil {
return nil, err
}
k := secp256k1.GenPrivKeyFromSecret(signed)
newKey := eciesgo.NewPrivateKeyFromBytes(k.Bytes())
return newKey, nil
}
func CreateMsgMakeRoot(creator string) (*MsgMakeRoot, error) {
merklePath := MerklePath("s/")
trackingNumber := uuid.NewString()
editorIds := strings.Split(creator, ",")
jsonEditors, err := MakeEditorAccessMap(trackingNumber, editorIds, "place holder key")
if err != nil {
return nil, err
}
accountHash := HashThenHex(creator)
msg := NewMsgMakeRoot(
creator,
accountHash,
merklePath,
"Contents",
string(jsonEditors),
"Viewers",
trackingNumber,
)
return msg, nil
}
func CreateRootFolder(creator string) (*Files, error) {
merklePath := MerklePath("s/")
trackingNumber := uuid.NewString()
editorIds := strings.Split(creator, ",")
jsonEditors, err := MakeEditorAccessMap(trackingNumber, editorIds, "place holder key")
if err != nil {
return nil, err
}
accountHash := HashThenHex(creator)
ownerAddress := MakeOwnerAddress(merklePath, accountHash)
rootFolder := Files{
Contents: "Contents", // This won't be used for now, but we're leaving it in as a stub in case it's needed
Owner: ownerAddress,
ViewingAccess: "NONE", // This won't be used for now, but we're leaving it in as a stub in case it's needed
EditAccess: string(jsonEditors),
Address: merklePath,
TrackingNumber: trackingNumber,
}
return &rootFolder, nil
}
func CreateFolderOrFile(creator string, editorIds []string, viewerIds []string, readablePath string) (*Files, error) {
merklePath := MerklePath(readablePath)
trackingNumber := uuid.NewString()
jsonEditors, err := MakeEditorAccessMap(trackingNumber, editorIds, "place holder key")
if err != nil {
return nil, err
}
jsonViewers, err := MakeViewerAccessMap(trackingNumber, viewerIds, "place holder key")
if err != nil {
return nil, err
}
accountHash := HashThenHex(creator)
ownerAddress := MakeOwnerAddress(merklePath, accountHash)
File := Files{
Contents: "Contents: FID goes here",
Owner: ownerAddress,
ViewingAccess: string(jsonViewers),
EditAccess: string(jsonEditors),
Address: merklePath,
TrackingNumber: trackingNumber,
}
return &File, nil
}
// Owner address is whoever owns this file/folder
func MakeOwnerAddress(merklePath string, user string) string {
// make sure that user was already hex(hashed) before it was passed into
// this function
h := sha256.New()
h.Write([]byte(fmt.Sprintf("o%s%s", merklePath, user)))
hash := h.Sum(nil)
ownerAddress := fmt.Sprintf("%x", hash)
return ownerAddress
}
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 := MerklePath(parentString)
h := sha256.New()
h.Write([]byte(childString))
childHash := fmt.Sprintf("%x", h.Sum(nil))
return parentHash, childHash
}
// helper function to hash then hex any given string
func HashThenHex(any string) string {
H := sha256.New()
H.Write([]byte(any))
hash := H.Sum(nil)
return fmt.Sprintf("%x", hash)
}
func MakeEditorAccessMap(trackingNumber string, editorIds []string, aesKey string) ([]byte, error) {
editors := make(map[string]string)
for _, v := range editorIds {
h := sha256.New()
h.Write([]byte(fmt.Sprintf("e%s%s", trackingNumber, v)))
hash := h.Sum(nil)
addressString := fmt.Sprintf("%x", hash)
editors[addressString] = fmt.Sprintf("%x", aesKey) // need helper function to generate a placeholder key
}
jsonEditors, err := json.Marshal(editors)
if err != nil {
return nil, ErrCantMarshall
}
return jsonEditors, nil
}
func MakeViewerAccessMap(trackingNumber string, viewerIds []string, aesKey string) ([]byte, error) {
viewers := make(map[string]string)
for _, v := range viewerIds {
h := sha256.New()
h.Write([]byte(fmt.Sprintf("v%s%s", trackingNumber, v)))
hash := h.Sum(nil)
addressString := fmt.Sprintf("%x", hash)
viewers[addressString] = fmt.Sprintf("%x", aesKey) // need helper function to generate a placeholder key
}
jsonViewers, err := json.Marshal(viewers)
if err != nil {
return nil, ErrCantMarshall
}
return jsonViewers, nil
}
func CreateMsgPostFile(creator string, readablePath string, jsonEditAccess []byte, trackingNumber string) (*MsgPostFile, error) {
accountHash := HashThenHex(creator)
parentHash, childHash := MerkleHelper(readablePath)
msg := NewMsgPostFile(
creator,
accountHash,
parentHash,
childHash,
"contents: FID goes here",
"viewers",
string(jsonEditAccess),
trackingNumber,
)
return msg, nil
}