-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.go
More file actions
159 lines (150 loc) · 5.11 KB
/
utils.go
File metadata and controls
159 lines (150 loc) · 5.11 KB
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
package scripts
import (
"context"
"flag"
"fmt"
"github.com/awnumar/memguard"
"github.com/ethereum/go-ethereum/common"
"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/wallet"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment/secrets"
"github.com/rsksmart/liquidity-provider-server/internal/entities/blockchain"
"os"
)
type ParseFunc = func()
type PasswordReader = func(int) ([]byte, error)
type FileReader = func(string) ([]byte, error)
type RskClientFactory = func(context.Context, environment.Environment) (*rootstock.RskClient, error)
func ExitWithError(code int, message string, err error) {
fmt.Printf("%s: %s\n", message, err.Error())
memguard.Purge() // We add it here because exit doesn't execute deferred functions
memguard.SafeExit(code)
}
func GetWallet(
ctx context.Context,
env environment.Environment,
timeouts environment.ApplicationTimeouts,
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, Timeouts: timeouts,
})
if err != nil {
return nil, err
}
return walletFactory.RskWallet()
}
func CreatePeginContract(
ctx context.Context,
factory RskClientFactory,
env environment.Environment,
timeouts environment.ApplicationTimeouts,
) (blockchain.PeginContract, error) {
rskClient, err := factory(ctx, env)
if err != nil {
return nil, fmt.Errorf("error connecting to RSK node: %w", err)
}
rskWallet, err := GetWallet(ctx, env, timeouts, rskClient)
if err != nil {
return nil, fmt.Errorf("error accessing to wallet: %w", err)
}
peginBinding, err := bindings.NewIPegIn(common.HexToAddress(env.Rsk.PeginContractAddress), rskClient.Rpc())
if err != nil {
return nil, err
}
return rootstock.NewPeginContractImpl(
rskClient,
env.Rsk.PeginContractAddress,
rootstock.NewPeginContractAdapter(peginBinding),
rskWallet,
rootstock.RetryParams{Retries: 0, Sleep: 0},
environment.DefaultTimeouts().MiningWait.Seconds(),
rootstock.MustLoadFlyoverABIs(),
), nil
}
func CreatePegoutContract(
ctx context.Context,
factory RskClientFactory,
env environment.Environment,
timeouts environment.ApplicationTimeouts,
) (blockchain.PegoutContract, error) {
rskClient, err := factory(ctx, env)
if err != nil {
return nil, fmt.Errorf("error connecting to RSK node: %w", err)
}
rskWallet, err := GetWallet(ctx, env, timeouts, rskClient)
if err != nil {
return nil, fmt.Errorf("error accessing to wallet: %w", err)
}
pegoutContract, err := bindings.NewIPegOut(common.HexToAddress(env.Rsk.PegoutContractAddress), rskClient.Rpc())
if err != nil {
return nil, err
}
return rootstock.NewPegoutContractImpl(
rskClient,
env.Rsk.PegoutContractAddress,
rootstock.NewPegoutContractAdapter(pegoutContract),
rskWallet,
rootstock.RetryParams{Retries: 0, Sleep: 0},
environment.DefaultTimeouts().MiningWait.Seconds(),
rootstock.MustLoadFlyoverABIs(),
), nil
}
func CreateDiscoveryContract(
ctx context.Context,
factory RskClientFactory,
env environment.Environment,
timeouts environment.ApplicationTimeouts,
) (blockchain.DiscoveryContract, error) {
rskClient, err := factory(ctx, env)
if err != nil {
return nil, fmt.Errorf("error connecting to RSK node: %w", err)
}
rskWallet, err := GetWallet(ctx, env, timeouts, rskClient)
if err != nil {
return nil, fmt.Errorf("error accessing to wallet: %w", err)
}
discoveryContract, err := bindings.NewIFlyoverDiscovery(common.HexToAddress(env.Rsk.DiscoveryAddress), rskClient.Rpc())
if err != nil {
return nil, err
}
return rootstock.NewDiscoveryContractImpl(
rskClient,
env.Rsk.DiscoveryAddress,
discoveryContract,
rskWallet,
rootstock.RetryParams{Retries: 0, Sleep: 0},
environment.DefaultTimeouts().MiningWait.Seconds(),
rootstock.MustLoadFlyoverABIs(),
), nil
}
func SetUsageMessage(msg string) {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "%s\n", msg)
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
}
// EnableSecureBuffers is a function that sets up secure memory buffers by calling memguard.CatchInterrupt().
// This function returns another function that must be called at the end of the program to purge the secure memory buffers.
// Every LPS script that interacts with a wallet must call this function at the beginning of the script.
// An example of the correct way of calling this function is:
//
// func main() {
// defer scripts.EnableSecureBuffers()()
// // Your script logic here
// }
func EnableSecureBuffers() func() {
memguard.CatchInterrupt()
fmt.Println("Secure buffers enabled")
return func() {
memguard.Purge()
fmt.Println("Sensitive buffers were destroyed successfully")
}
}