-
Notifications
You must be signed in to change notification settings - Fork 45
/
getter.go
243 lines (206 loc) · 6.99 KB
/
getter.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
package getter
import (
"fmt"
"os"
"path"
"strings"
"github.com/desmos-labs/desmos/v5/app/desmos/cmd/chainlink/types"
"github.com/manifoldco/promptui"
)
// ChainLinkReferenceGetter allows to get all the data needed to generate a ChainLinkJSON instance
type ChainLinkReferenceGetter interface {
// IsSingleSignatureAccount returns if the target account is single signature account
IsSingleSignatureAccount() (bool, error)
// GetChain returns Chain instance
GetChain() (types.Chain, error)
// GetFilename returns filename to save
GetFilename() (string, error)
// GetOwner returns the owner of the link
GetOwner() (string, error)
}
// SingleSignatureAccountReferenceGetter allows to get all the data needed to generate a ChainLinkJSON interface for single signature account
type SingleSignatureAccountReferenceGetter interface {
// GetMnemonic returns the mnemonic
GetMnemonic() (string, error)
}
// MultiSignatureAccountReferenceGetter allows to get all the data needed to generate a ChainLinkJSON interface for multi signature account
type MultiSignatureAccountReferenceGetter interface {
// GetSignedChainID returns the chain id which is used to sign the multisigned tx file
GetSignedChainID() (string, error)
// GetMultiSignedTxFilePath returns the path of multisigned transaction file
GetMultiSignedTxFilePath() (string, error)
}
// --------------------------------------------------------------------------------------------------------------------
// ChainLinkReferencePrompt is a ChainLinkReferenceGetter implemented with an interactive prompt
type ChainLinkReferencePrompt struct {
ChainLinkReferenceGetter
cfg types.Config
}
// NewChainLinkReferencePrompt returns an instance implementing ChainLinkReferencePrompt
func NewChainLinkReferencePrompt() *ChainLinkReferencePrompt {
return &ChainLinkReferencePrompt{
cfg: types.DefaultConfig(),
}
}
// IsSingleSignatureAccount implements ChainLinkReferenceGetter
func (cp ChainLinkReferencePrompt) IsSingleSignatureAccount() (bool, error) {
return cp.isSingleSignatureAccount()
}
// GetChain implements ChainLinkReferenceGetter
func (cp ChainLinkReferencePrompt) GetChain() (types.Chain, error) {
chain, err := cp.selectChain()
if err != nil {
return types.Chain{}, err
}
if chain.ID == "Other" {
newChain, err := cp.getCustomChain(chain)
if err != nil {
return types.Chain{}, err
}
chain = newChain
}
return chain, nil
}
// GetFilename implements ChainLinkReferenceGetter
func (cp ChainLinkReferencePrompt) GetFilename() (string, error) {
filename, err := cp.getFilename()
if err != nil {
return "", err
}
if filename == "" {
return "", fmt.Errorf("file name cannot be empty")
}
return filename, nil
}
// GetOwner implements ChainLinkReferenceGetter
func (cp *ChainLinkReferencePrompt) GetOwner() (string, error) {
owner, err := cp.getOwner()
if err != nil {
return "", err
}
return owner, nil
}
// --------------------------------------------------------------------------------------------------------------------
// isSingleSignatureAccount asks the user if the target of the account is single signature account, and then returns it
func (cp ChainLinkReferencePrompt) isSingleSignatureAccount() (bool, error) {
prompt := promptui.Select{
Label: "Please select if the target account is a single signature account. (select no if it is multi signature account)",
Items: []string{"Yes", "No"},
Templates: &promptui.SelectTemplates{
Active: "\U00002713 {{ . | cyan }}",
Inactive: " {{ . | cyan }}",
Selected: "Module: \U00002713 {{ . | cyan }}",
},
}
_, result, err := prompt.Run()
if err != nil {
return false, err
}
return result == "Yes", nil
}
// selectChain asks the user to select a predefined Chain instance, and returns it
func (cp ChainLinkReferencePrompt) selectChain() (types.Chain, error) {
cfg := cp.cfg
prompt := promptui.Select{
Label: "Select a target chain",
Items: cfg.Chains,
Templates: &promptui.SelectTemplates{
Active: "\U00002713 {{ .ID | cyan }}",
Inactive: " {{ .ID | cyan }}",
Selected: "Module: \U00002713 {{ .ID | cyan }}",
},
}
index, _, err := prompt.Run()
if err != nil {
return types.Chain{}, err
}
return cfg.Chains[index], nil
}
// getCustomChain asks the user to input the data to build a custom Chain instance, and then returns it
func (cp ChainLinkReferencePrompt) getCustomChain(chain types.Chain) (types.Chain, error) {
chainName, err := cp.getChainName()
if err != nil {
return types.Chain{}, err
}
prefix, err := cp.getBech32Prefix()
if err != nil {
return types.Chain{}, err
}
derivationPath, err := cp.getDerivationPath()
if err != nil {
return types.Chain{}, err
}
chain.Name = chainName
chain.Prefix = prefix
chain.DerivationPath = derivationPath
return chain, nil
}
// getChainName asks the user to input a chain name, and returns it
func (cp ChainLinkReferencePrompt) getChainName() (string, error) {
prompt := promptui.Prompt{
Label: "Please input the name of the chain you want to link with",
Validate: func(s string) error {
if strings.TrimSpace(s) == "" {
return fmt.Errorf("chain name cannot be empty or blank")
}
if strings.ToLower(s) != s {
return fmt.Errorf("chain name should be lowercase")
}
return nil
},
}
return prompt.Run()
}
// getBech32Prefix asks the user to input the Bech32 prefix of the address, and then returns it
func (cp ChainLinkReferencePrompt) getBech32Prefix() (string, error) {
prompt := promptui.Prompt{
Label: "Please input the Bech32 account address prefix used inside the the chain",
Validate: func(s string) error {
if strings.TrimSpace(s) == "" {
return fmt.Errorf("bech32 prefix cannot be empty or blank")
}
return nil
},
}
return prompt.Run()
}
// getDerivationPath asks the user to input the derivation path of the account, and then returns it
func (cp ChainLinkReferencePrompt) getDerivationPath() (string, error) {
prompt := promptui.Prompt{
Label: "Please input the derivation path used by the chain to generate the accounts",
Default: "m/44'/118'/0'/0/0",
Validate: func(s string) error {
if strings.TrimSpace(s) == "" {
return fmt.Errorf("derivation path cannot be empty or blank")
}
return nil
},
}
return prompt.Run()
}
// getFilename asks the user to input the filename where to store the chain link, and then returns it
func (cp ChainLinkReferencePrompt) getFilename() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
prompt := promptui.Prompt{
Label: "Please insert where the chain link JSON object should be stored (fully qualified path)",
Default: path.Join(wd, "data.json"),
}
return prompt.Run()
}
// getOwner asks the owner of the link and then returns it
func (cp *ChainLinkReferencePrompt) getOwner() (string, error) {
prompt := promptui.Prompt{
Label: "Please enter the owner that should be used to link",
HideEntered: true,
Validate: func(s string) error {
if strings.TrimSpace(s) == "" {
return fmt.Errorf("owner cannot be empty or blank")
}
return nil
},
}
return prompt.Run()
}