/
fs.go
122 lines (112 loc) · 2.89 KB
/
fs.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
package filemgr
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"github.com/gbrlsnchs/jwt/v3"
"github.com/google/uuid"
"github.com/ipfs-force-community/venus-wallet/common"
"github.com/ipfs-force-community/venus-wallet/config"
"github.com/ipfs-force-community/venus-wallet/core"
"github.com/ipfs-force-community/venus-wallet/crypto/aes"
"github.com/mitchellh/go-homedir"
"github.com/multiformats/go-multiaddr"
"os"
"path/filepath"
"strings"
)
var (
ErrNoAPIEndpoint = errors.New("API not running (no endpoint)")
ErrNoAPIToken = errors.New("API token not set")
ErrRepoAlreadyLocked = errors.New("repo is already locked")
ErrClosedRepo = errors.New("repo is no longer open")
)
// FsRepo is struct for repo, use NewFS to create
type FsRepo struct {
path string
cnf *config.Config
}
type OverrideParams struct {
API string
}
var _ Repo = &FsRepo{}
// NewFS creates a repo instance based on a path on file system
func NewFS(path string, op *OverrideParams) (Repo, error) {
path, err := homedir.Expand(path)
if err != nil {
return nil, err
}
fs := &FsRepo{
path: path,
}
err = fs.init()
if err != nil {
return nil, err
}
err = fs.checkConfig(op)
if err != nil {
return nil, err
}
return fs, nil
}
func (fsr *FsRepo) APISecret() (*common.APIAlg, error) {
sec, err := hex.DecodeString(fsr.cnf.JWT.Secret)
if err != nil {
return nil, err
}
return (*common.APIAlg)(jwt.NewHS256(sec)), nil
}
func (fsr *FsRepo) init() error {
exist, err := fsr.exists()
if err != nil {
return err
}
if exist {
return nil
}
err = os.Mkdir(fsr.path, 0755) //nolint: gosec
if err != nil && !os.IsExist(err) {
return err
}
return nil
}
func (fsr *FsRepo) exists() (bool, error) {
_, err := os.Stat(filepath.Join(fsr.path, skKeyStore))
notexist := os.IsNotExist(err)
if notexist {
err = nil
}
return !notexist, err
}
func (fsr *FsRepo) Config() *config.Config {
return fsr.cnf
}
// APIEndpoint returns endpoint of API in this repo
func (fsr *FsRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
strma := strings.TrimSpace(fsr.cnf.API.ListenAddress)
apima, err := multiaddr.NewMultiaddr(strma)
if err != nil {
return nil, err
}
return apima, nil
}
func (fsr *FsRepo) APIToken() ([]byte, error) {
return hex.DecodeString(fsr.cnf.JWT.Token)
}
func (fsr *FsRepo) APIStrategyToken(password string) (string, error) {
hashPasswd := aes.Keccak256([]byte(password))
rootKey, err := aes.EncryptData(hashPasswd, []byte("root"), fsr.cnf.Factor.ScryptN, fsr.cnf.Factor.ScryptP)
if err != nil {
return core.StringEmpty, errors.New("failed to gen token seed")
}
rootKB, err := json.Marshal(rootKey)
if err != nil {
return core.StringEmpty, errors.New("failed to marshal token seed")
}
rootk, err := uuid.NewRandomFromReader(bytes.NewBuffer(rootKB))
if err != nil {
return core.StringEmpty, errors.New("failed to convert token seed to uuid")
}
return rootk.String(), nil
}