-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialize.go
104 lines (89 loc) · 3.22 KB
/
initialize.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
package wallet
import (
"errors"
"fmt"
"net/url"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/gorilla/mux"
"github.com/nodeset-org/hyperdrive-daemon/shared/types/api"
"github.com/rocket-pool/node-manager-core/api/server"
"github.com/rocket-pool/node-manager-core/api/types"
nodewallet "github.com/rocket-pool/node-manager-core/node/wallet"
"github.com/rocket-pool/node-manager-core/utils/input"
"github.com/rocket-pool/node-manager-core/wallet"
)
// ===============
// === Factory ===
// ===============
type walletInitializeContextFactory struct {
handler *WalletHandler
}
func (f *walletInitializeContextFactory) Create(args url.Values) (*walletInitializeContext, error) {
c := &walletInitializeContext{
handler: f.handler,
}
server.GetOptionalStringFromVars("derivation-path", args, &c.derivationPath)
inputErrs := []error{
server.ValidateOptionalArg("index", args, input.ValidateUint, &c.index, nil),
server.ValidateArg("password", args, input.ValidateNodePassword, &c.password),
server.ValidateArg("save-wallet", args, input.ValidateBool, &c.saveWallet),
server.ValidateArg("save-password", args, input.ValidateBool, &c.savePassword),
}
return c, errors.Join(inputErrs...)
}
func (f *walletInitializeContextFactory) RegisterRoute(router *mux.Router) {
server.RegisterQuerylessGet[*walletInitializeContext, api.WalletInitializeData](
router, "initialize", f, f.handler.logger.Logger, f.handler.serviceProvider.ServiceProvider,
)
}
// ===============
// === Context ===
// ===============
type walletInitializeContext struct {
handler *WalletHandler
derivationPath string
index uint64
password string
savePassword bool
saveWallet bool
}
func (c *walletInitializeContext) PrepareData(data *api.WalletInitializeData, opts *bind.TransactOpts) (types.ResponseStatus, error) {
sp := c.handler.serviceProvider
// Parse the derivation path
path, err := nodewallet.GetDerivationPath(wallet.DerivationPath(c.derivationPath))
if err != nil {
return types.ResponseStatus_InvalidArguments, err
}
var w *nodewallet.Wallet
var mnemonic string
if !c.saveWallet {
// Make a dummy wallet for the sake of creating a mnemonic and derived address
mnemonic, err = nodewallet.GenerateNewMnemonic()
if err != nil {
return types.ResponseStatus_Error, fmt.Errorf("error generating new mnemonic: %w", err)
}
w, err = nodewallet.TestRecovery(path, uint(c.index), mnemonic, 0)
if err != nil {
return types.ResponseStatus_Error, fmt.Errorf("error generating wallet from new mnemonic: %w", err)
}
} else {
// Initialize the daemon wallet
w = sp.GetWallet()
// Requirements
status, err := w.GetStatus()
if err != nil {
return types.ResponseStatus_Error, fmt.Errorf("error getting wallet status: %w", err)
}
if status.Wallet.IsOnDisk {
return types.ResponseStatus_ResourceConflict, fmt.Errorf("a wallet is already present")
}
// Create the new wallet
mnemonic, err = w.CreateNewLocalWallet(path, uint(c.index), c.password, c.savePassword)
if err != nil {
return types.ResponseStatus_Error, fmt.Errorf("error initializing new wallet: %w", err)
}
}
data.Mnemonic = mnemonic
data.AccountAddress, _ = w.GetAddress()
return types.ResponseStatus_Success, nil
}