forked from filecoin-project/boost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
156 lines (129 loc) · 3 KB
/
node.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
package node
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
crand "crypto/rand"
"github.com/filecoin-project/boost/lib/keystore"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/mitchellh/go-homedir"
)
type Node struct {
Host host.Host
Wallet *wallet.LocalWallet
}
func Setup(cfgdir string) (*Node, error) {
cfgdir, err := homedir.Expand(cfgdir)
if err != nil {
return nil, fmt.Errorf("getting homedir: %w", err)
}
_, err = os.Stat(cfgdir)
if err != nil && errors.Is(err, os.ErrNotExist) {
return nil, errors.New("repo dir doesn't exist. run `boost init` first.")
}
peerkey, err := loadOrInitPeerKey(keyPath(cfgdir))
if err != nil {
return nil, err
}
h, err := libp2p.New(
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0"),
libp2p.Identity(peerkey),
)
if err != nil {
return nil, err
}
wallet, err := setupWallet(walletPath(cfgdir))
if err != nil {
return nil, err
}
return &Node{
Host: h,
Wallet: wallet,
}, nil
}
func loadOrInitPeerKey(kf string) (crypto.PrivKey, error) {
data, err := os.ReadFile(kf)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
k, _, err := crypto.GenerateEd25519Key(crand.Reader)
if err != nil {
return nil, err
}
data, err := crypto.MarshalPrivateKey(k)
if err != nil {
return nil, err
}
if err := os.WriteFile(kf, data, 0600); err != nil {
return nil, err
}
return k, nil
}
return crypto.UnmarshalPrivateKey(data)
}
func setupWallet(dir string) (*wallet.LocalWallet, error) {
kstore, err := keystore.OpenOrInitKeystore(dir)
if err != nil {
return nil, err
}
wallet, err := wallet.NewWallet(kstore)
if err != nil {
return nil, err
}
addrs, err := wallet.WalletList(context.TODO())
if err != nil {
return nil, err
}
if len(addrs) == 0 {
_, err := wallet.WalletNew(context.TODO(), types.KTBLS)
if err != nil {
return nil, err
}
}
return wallet, nil
}
func keyPath(baseDir string) string {
return filepath.Join(baseDir, "libp2p.key")
}
func walletPath(baseDir string) string {
return filepath.Join(baseDir, "wallet")
}
func (n *Node) GetProvidedOrDefaultWallet(ctx context.Context, provided string) (address.Address, error) {
var walletAddr address.Address
if provided == "" {
var err error
walletAddr, err = n.Wallet.GetDefault()
if err != nil {
return address.Address{}, err
}
} else {
w, err := address.NewFromString(provided)
if err != nil {
return address.Address{}, err
}
addrs, err := n.Wallet.WalletList(ctx)
if err != nil {
return address.Address{}, err
}
found := false
for _, a := range addrs {
if bytes.Equal(a.Bytes(), w.Bytes()) {
walletAddr = w
found = true
}
}
if !found {
return address.Address{}, fmt.Errorf("couldn't find wallet %s locally", provided)
}
}
return walletAddr, nil
}