-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_provider_url.go
224 lines (200 loc) · 8.21 KB
/
update_provider_url.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/go-playground/validator/v10"
"github.com/rsksmart/liquidity-provider-server/cmd/utils/defaults"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/rootstock"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/rootstock/bindings"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap/wallet"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment/secrets"
"golang.org/x/term"
"net/url"
"os"
"syscall"
)
type UpdateProviderScriptInput struct {
ProviderName string `validate:"required"`
ProviderUrl string `validate:"required,http_url"`
Network string `validate:"required,oneof=regtest testnet mainnet"`
RskEndpoint string `validate:"required,http_url"`
CustomLbcAddress string `validate:"omitempty,eth_addr"`
AwsLocalEndpoint string `validate:"http_url"`
SecretSource string `validate:"required,oneof=aws env"`
EncryptedJsonSecret string
EncryptedJsonPasswordSecret string
KeystoreFile string `validate:"omitempty,filepath"`
KeystorePassword string
}
type PasswordReader = func(int) ([]byte, error)
type UpdateProviderArgs struct {
Name string
url *url.URL
network string
}
func NewUpdateProviderArgs(name string, rawUrl string, network string) (UpdateProviderArgs, error) {
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return UpdateProviderArgs{}, err
}
return UpdateProviderArgs{Name: name, url: parsedUrl, network: network}, nil
}
func (args UpdateProviderArgs) Validate() error {
if args.Name == "" {
return errors.New("empty name")
}
if args.url.Scheme == "" || args.url.Host == "" {
return errors.New("invalid url")
}
if args.network != "regtest" && args.url.Scheme != "https" {
return errors.New("invalid url, not using https")
}
return nil
}
func (args UpdateProviderArgs) Url() string {
return fmt.Sprintf("%s://%s", args.url.Scheme, args.url.Host)
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "This script is used to update the provider information displayed in the Liquidity Bridge Contract when the discovery function is executed.\n")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
scriptInput := new(UpdateProviderScriptInput)
ReadUpdateProviderScriptInput(scriptInput)
env, err := ParseUpdateProviderScriptInput(scriptInput, term.ReadPassword)
if err != nil {
ExitWithError(2, "Error reading input", err)
}
ctx := context.Background()
rskClient, err := bootstrap.Rootstock(ctx, env.Rsk)
if err != nil {
ExitWithError(2, "Error connecting to RSK node", err)
}
rskWallet, err := GetWallet(ctx, env, rskClient)
if err != nil {
ExitWithError(2, "Error accessing to wallet", err)
}
updateArgs, err := NewUpdateProviderArgs(scriptInput.ProviderName, scriptInput.ProviderUrl, scriptInput.Network)
if err != nil {
ExitWithError(2, "Invalid provider information", err)
}
err = updateArgs.Validate()
if err != nil {
ExitWithError(2, "Invalid provider information", err)
}
err = ExecuteUpdateProvider(ctx, env, rskWallet, rskClient, updateArgs)
if err != nil {
ExitWithError(2, "Error on transaction execution", err)
}
}
func ReadUpdateProviderScriptInput(scriptInput *UpdateProviderScriptInput) {
flag.StringVar(&scriptInput.Network, "network", "", "The network to execute the script. Must be one of the following: regtest, testnet, mainnet")
flag.StringVar(&scriptInput.ProviderName, "provider-name", "", "The liquidity provider name to display")
flag.StringVar(&scriptInput.ProviderUrl, "provider-url", "", "The URL of the liquidity provider to be accessible by the users")
flag.StringVar(&scriptInput.AwsLocalEndpoint, "aws-endpoint", "http://localhost:4566", "AWS endpoint for localstack")
flag.StringVar(&scriptInput.SecretSource, "secret-src", "", "The source of the secrets to execute the transaction. Must be one of the following: env, aws")
flag.StringVar(&scriptInput.RskEndpoint, "rsk-endpoint", "", "The URL of the RSK RPC server. E.g. http://localhost:4444")
flag.StringVar(&scriptInput.CustomLbcAddress, "lbc-address", "", "Custom address of the liquidity bridge contract. If not provided will use the network default.")
flag.StringVar(&scriptInput.KeystoreFile, "keystore-file", "", "Path to the keystore file. Only required if the secret source is env")
flag.StringVar(&scriptInput.EncryptedJsonSecret, "keystore-secret", "", "Name of the secret storing the keystore. Only required if the secret source is aws")
flag.StringVar(&scriptInput.EncryptedJsonPasswordSecret, "password-secret", "", "Name of the secret storing the keystore password. Only required if the secret source is aws")
}
func ParseUpdateProviderScriptInput(scriptInput *UpdateProviderScriptInput, pwdReader PasswordReader) (environment.Environment, error) {
var env environment.Environment
flag.Parse()
validate := validator.New(validator.WithRequiredStructEnabled())
err := validate.Struct(scriptInput)
if err != nil {
return environment.Environment{}, fmt.Errorf("invalid input: %w", err)
}
if scriptInput.SecretSource == "env" {
var password []byte
fmt.Println("Insert keystore password:")
if password, err = pwdReader(syscall.Stdin); err != nil {
return environment.Environment{}, fmt.Errorf("error reading password: %w", err)
}
scriptInput.KeystorePassword = string(password)
}
rskEnvDefaults, err := defaults.GetRsk(scriptInput.Network)
if err != nil {
return environment.Environment{}, fmt.Errorf("invalid input: %w", err)
}
var lbcAddress string
if scriptInput.CustomLbcAddress != "" {
lbcAddress = scriptInput.CustomLbcAddress
} else {
lbcAddress = rskEnvDefaults.LbcAddress
}
env.LpsStage = scriptInput.Network
env.AwsLocalEndpoint = scriptInput.AwsLocalEndpoint
env.SecretSource = scriptInput.SecretSource
env.WalletManagement = "native"
env.Rsk = environment.RskEnv{
Endpoint: scriptInput.RskEndpoint,
ChainId: rskEnvDefaults.ChainId,
LbcAddress: lbcAddress,
BridgeAddress: rskEnvDefaults.BridgeAddress,
AccountNumber: rskEnvDefaults.AccountNumber,
EncryptedJsonSecret: scriptInput.EncryptedJsonSecret,
EncryptedJsonPasswordSecret: scriptInput.EncryptedJsonPasswordSecret,
KeystoreFile: scriptInput.KeystoreFile,
KeystorePassword: scriptInput.KeystorePassword,
}
env.Btc = environment.BtcEnv{Network: scriptInput.Network}
return env, nil
}
func GetWallet(
ctx context.Context,
env environment.Environment,
rskClient *rootstock.RskClient,
) (rootstock.RskSignerWallet, error) {
secretLoader, err := secrets.GetSecretLoader(ctx, env)
if err != nil {
return nil, err
}
walletFactory, err := wallet.NewFactory(env, wallet.FactoryCreationArgs{
Ctx: ctx, Env: env, SecretLoader: secretLoader, RskClient: rskClient,
})
if err != nil {
return nil, err
}
return walletFactory.RskWallet()
}
func ExecuteUpdateProvider(
ctx context.Context,
env environment.Environment,
rskWallet rootstock.RskSignerWallet,
rskClient *rootstock.RskClient,
args UpdateProviderArgs,
) error {
lbc, err := bindings.NewLiquidityBridgeContract(common.HexToAddress(env.Rsk.LbcAddress), rskClient.Rpc())
if err != nil {
return err
}
opts := &bind.TransactOpts{From: rskWallet.Address(), Signer: rskWallet.Sign}
tx, err := lbc.UpdateProvider(opts, args.Name, args.Url())
if err != nil {
return err
}
receipt, err := bind.WaitMined(ctx, rskClient.Rpc(), tx)
if err != nil {
return err
}
if receipt.Status == 1 {
fmt.Println("Provider information updated successfully. Transaction hash: ", receipt.TxHash.Hex())
return nil
} else {
return fmt.Errorf("transaction %s failed", receipt.TxHash.Hex())
}
}
func ExitWithError(code int, message string, err error) {
fmt.Println(fmt.Sprintf("%s: %s", message, err.Error()))
os.Exit(code)
}