forked from kaleido-io/kaleido-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
335 lines (280 loc) · 9.42 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package registry
import (
"encoding/json"
"errors"
"fmt"
"log"
"math/big"
"os"
"strings"
"syscall"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
ethclient "github.com/ethereum/go-ethereum/ethclient"
kaleido "github.com/kaleido-io/kaleido-sdk-go/common"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh/terminal"
resty "gopkg.in/resty.v1"
)
type utilsInterface interface {
GetServiceDefinition() (*ServiceDefinitionType, error)
getRegistryURL() string
getAPIClient() *resty.Client
GetNetworkManagerClient() *resty.Client
getDirectoryAddress() string
getDirectoryClient() *resty.Client
getProfilesAddress() string
getProfilesClient() *resty.Client
getNodeClient() *ethclient.Client
ValidateGetResponse(res *resty.Response, err error, resourceName string) error
validateCreateResponse(res *resty.Response, err error, resourceName string) error
GenerateNodeID(path string) string
generateUserID(path string, email string) string
newKeyStoreTransactor(from *accounts.Account, keystore *keystore.KeyStore, chainID *big.Int) *bind.TransactOpts
getAccountForAddress(ks *keystore.KeyStore, hexAddress string) (*accounts.Account, error)
readPassword(envVarName string, prompt string) (string, error)
}
var instance *utilsImpl
func Utils() utilsInterface {
if instance == nil {
instance = &utilsImpl{}
err := instance.initialize()
if err != nil {
fmt.Println(err.Error())
os.Exit(1) // hacky but will do instead of adding an err check on all utils() call
}
}
return instance
}
type utilsImpl struct {
registryURL string
apiClient *resty.Client
networkManagerClient *resty.Client
directoryAddress string
directoryClient *resty.Client
profilesAddress string
profilesClient *resty.Client
nodeClient *ethclient.Client
serviceID string
}
func (u *utilsImpl) newClient(url, authToken string) *resty.Client {
client := resty.New().SetHostURL(url).SetAuthToken(authToken)
client.SetDebug(viper.GetBool("api.debug"))
return client
}
func (u *utilsImpl) initAPIClient() error {
u.registryURL = viper.GetString("api.url") + "/idregistry/" + u.serviceID
u.apiClient = u.newClient(u.registryURL, viper.GetString("api.key"))
return nil
}
func (u *utilsImpl) initNetworkManagerClient() error {
u.networkManagerClient = u.newClient(viper.GetString("api.url"), viper.GetString("api.key"))
return nil
}
func (u *utilsImpl) initDirectoryClient() error {
directory, profiles, err := u.fetchOnChainAddresses()
if err != nil {
return err
}
u.directoryAddress = directory
u.profilesAddress = profiles
directoryURL := Utils().getRegistryURL() + "/directories/" + u.directoryAddress
profilesURL := Utils().getRegistryURL() + "/properties/" + u.profilesAddress
u.directoryClient = u.newClient(directoryURL, viper.GetString("api.key"))
u.profilesClient = u.newClient(profilesURL, viper.GetString("api.key"))
return nil
}
func (u *utilsImpl) initEthClient() error {
url := viper.GetString("networks.url")
if url == "" {
return errors.New("Unable to load node url. Have you setup your configuration (~/.kld.yaml) properly?")
}
var err error
if u.nodeClient, err = ethclient.Dial(url); err != nil {
log.Fatalf("Unable to connect to node at %s", url)
return err
}
return nil
}
func (u *utilsImpl) initialize() error {
u.serviceID = viper.GetString("service.id")
if err := u.initAPIClient(); err != nil {
return err
}
if err := u.initNetworkManagerClient(); err != nil {
return err
}
if err := u.initDirectoryClient(); err != nil {
return err
}
if err := u.initEthClient(); err != nil {
return err
}
return nil
}
func (u *utilsImpl) getRegistryURL() string {
return u.registryURL
}
var client *resty.Client
func (u *utilsImpl) getAPIClient() *resty.Client {
return u.apiClient
}
func (u *utilsImpl) GetNetworkManagerClient() *resty.Client {
return u.networkManagerClient
}
func (u *utilsImpl) getDirectoryAddress() string {
return u.directoryAddress
}
func (u *utilsImpl) getDirectoryClient() *resty.Client {
return u.directoryClient
}
func (u *utilsImpl) getProfilesAddress() string {
return u.profilesAddress
}
func (u *utilsImpl) getProfilesClient() *resty.Client {
return u.profilesClient
}
func (u *utilsImpl) getNodeClient() *ethclient.Client {
return u.nodeClient
}
func (u *utilsImpl) ValidateGetResponse(res *resty.Response, err error, resourceName string) error {
if res.StatusCode() != 200 {
if res.StatusCode() >= 400 && res.StatusCode() < 500 {
type photicError struct {
ErrorMessage string `json:"errorMessage,omitempty"`
}
var e photicError
unmarshalError := json.Unmarshal(res.Body(), &e)
if unmarshalError != nil {
fmt.Println(unmarshalError)
} else {
return errors.New(e.ErrorMessage)
}
}
return fmt.Errorf("could not retrieve %s. status code: %d", resourceName, res.StatusCode())
}
return err
}
func (u *utilsImpl) validateCreateResponse(res *resty.Response, err error, resourceName string) error {
if res.StatusCode() != 201 && res.StatusCode() != 200 {
if res.StatusCode() >= 400 && res.StatusCode() < 500 {
type photicError struct {
ErrorMessage string `json:"errorMessage,omitempty"`
}
var e photicError
unmarshalError := json.Unmarshal(res.Body(), &e)
if unmarshalError != nil {
fmt.Println(unmarshalError)
} else {
return errors.New(e.ErrorMessage)
}
}
return fmt.Errorf("could not create %s. status code: %d", resourceName, res.StatusCode())
}
return err
}
func (u *utilsImpl) fetchOnChainAddresses() (string, string, error) {
type responseBody struct {
ID string `json:"_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Directory string `json:"directory,omitempty"`
Profiles string `json:"profiles,omitempty"`
Claims string `json:"claims,omitempty"`
}
var directories []responseBody
client := Utils().getAPIClient()
response, err := client.R().SetResult(&directories).Get("/directories")
err = u.ValidateGetResponse(response, err, "directories")
if err != nil {
return "", "", err
}
if len(directories) < 1 {
return "", "", errors.New("Unexpected error: no directories available")
}
return directories[0].Directory, directories[0].Profiles, nil
}
type ServiceDefinitionType struct {
Consortium string `json:"consortia_id,omitempty"`
Environment string `json:"environment_id,omitempty"`
MemberID string `json:"membership_id,omitempty"`
}
func (u *utilsImpl) GetServiceDefinition() (*ServiceDefinitionType, error) {
client := u.newClient(viper.GetString("api.url"), viper.GetString("api.key"))
url := fmt.Sprintf("/services?_id=%s", u.serviceID)
var services []ServiceDefinitionType
response, err := client.R().SetResult(&services).Get(url)
if err = u.ValidateGetResponse(response, err, "services"); err != nil {
return nil, err
}
return &services[0], nil
}
func (u *utilsImpl) GenerateNodeID(path string) string {
nodeID := path
if path[:2] != "0x" {
nodeID, _ = kaleido.PathHash(path)
}
return nodeID
}
func (u *utilsImpl) generateUserID(path string, email string) string {
userID := email
if userID[:2] != "0x" {
nodeID := u.GenerateNodeID(path)
userID = kaleido.ChildHash(nodeID, email)
}
return userID
}
func (u *utilsImpl) readPassword(envVarName string, prompt string) (string, error) {
// read a passphrase from the enviroment or the terminal and sign with the passphrase
var passphrase string
if passphrase = os.Getenv(envVarName); passphrase == "" {
fmt.Printf(prompt)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Printf("\n") // advance to the next line and don't screw someone else up
if err != nil {
return "", err
}
passphrase = string(bytePassword)
}
return passphrase, nil
}
func (u *utilsImpl) newKeyStoreTransactor(from *accounts.Account, keystore *keystore.KeyStore, chainID *big.Int) *bind.TransactOpts {
return &bind.TransactOpts{
From: from.Address,
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
var txSigned *types.Transaction
var err error
// attempt to sign without a passphrase
if txSigned, err = keystore.SignTx(*from, tx, chainID); err != nil {
// if it fails with authentication needed
if strings.HasPrefix(err.Error(), "authentication needed") {
// read a passphrase from the enviroment or the terminal and sign with the passphrase
var passphrase string
if passphrase, err = u.readPassword("KLD_GETH_KEYSTORE_PASSWORD", "Password needed to unlock keystore: "); err != nil {
return nil, err
}
return keystore.SignTxWithPassphrase(*from, passphrase, tx, chainID)
}
// failed for some other reason other than passphrase, just return whatever was returned to us
return txSigned, err
}
// aha, we didn't fail, you should think about securing your keystore
fmt.Println(">>>> warning: keystore is not secured with a passphrase <<<<")
return txSigned, nil
},
}
}
func (u *utilsImpl) getAccountForAddress(ks *keystore.KeyStore, hexAddress string) (*accounts.Account, error) {
signerAccount := common.HexToAddress(hexAddress)
if ks.HasAddress(signerAccount) {
for _, account := range ks.Accounts() {
if account.Address == signerAccount {
return &account, nil
}
}
}
return nil, fmt.Errorf("Account for address %s not found", hexAddress)
}