-
Notifications
You must be signed in to change notification settings - Fork 28
/
init_wallet.go
179 lines (146 loc) · 3.94 KB
/
init_wallet.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
/*
Copyright Gen Digital Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package cmd
import (
"crypto/tls"
"fmt"
"log/slog"
"github.com/piprate/json-gold/ld"
vdrapi "github.com/trustbloc/did-go/vdr/api"
"github.com/trustbloc/kms-go/kms"
"github.com/trustbloc/kms-go/secretlock/noop"
storageapi "github.com/trustbloc/kms-go/spi/storage"
"github.com/trustbloc/kms-go/wrapper/api"
"github.com/trustbloc/kms-go/wrapper/localsuite"
"github.com/trustbloc/vcs/component/wallet-cli/internal/ldutil"
"github.com/trustbloc/vcs/component/wallet-cli/internal/storage"
"github.com/trustbloc/vcs/component/wallet-cli/internal/vdrutil"
"github.com/trustbloc/vcs/component/wallet-cli/pkg/wallet"
)
type walletFlags struct {
levelDBPath string
mongoDBConnectionString string
contextProviderURL string
walletDIDIndex int
}
func initWallet(flags *walletFlags) (*wallet.Wallet, *services, error) {
svc, err := initServices(
flags.levelDBPath,
flags.mongoDBConnectionString,
flags.contextProviderURL,
)
if err != nil {
return nil, nil, err
}
keyCreator, err := svc.CryptoSuite().RawKeyCreator()
if err != nil {
return nil, nil, err
}
w, err := wallet.New(
&walletProvider{
storageProvider: svc.StorageProvider(),
documentLoader: svc.DocumentLoader(),
vdrRegistry: svc.VDR(),
keyCreator: keyCreator,
},
)
if err != nil {
return nil, nil, err
}
if len(w.DIDs()) == 0 {
return nil, nil, fmt.Errorf("wallet not initialized, please run 'create' command")
}
if len(w.DIDs()) < flags.walletDIDIndex {
return nil, nil, fmt.Errorf("--wallet-did-index is out of range")
}
if len(w.DIDs()) > 1 && flags.walletDIDIndex == -1 {
var dids []any
for i, did := range w.DIDs() {
dids = append(dids, fmt.Sprintf("%d", i), did.ID)
}
slog.Warn("wallet supports multiple DIDs",
slog.Group("did", dids...),
)
}
return w, svc, nil
}
type services struct {
storageProvider storageapi.Provider
documentLoader ld.DocumentLoader
vdrRegistry vdrapi.Registry
cryptoSuite api.Suite
tlsConfig *tls.Config
}
func initServices(
levelDBPath,
mongoDBConnectionString,
contextProviderURL string,
) (*services, error) {
var (
storageType string
opts []storage.Opt
)
if levelDBPath != "" {
storageType = "leveldb"
opts = append(opts, storage.WithDBPath(levelDBPath))
} else if mongoDBConnectionString != "" {
storageType = "mongodb"
opts = append(opts, storage.WithConnectionString(mongoDBConnectionString))
} else {
return nil, fmt.Errorf("either --leveldb-path or --mongodb-connection-string must be specified")
}
slog.Info("initializing storage provider",
"storage_type", storageType,
)
storageProvider, err := storage.NewProvider(storageType, opts...)
if err != nil {
return nil, err
}
var ldOpts []ldutil.Opt
if contextProviderURL != "" {
ldOpts = append(ldOpts, ldutil.WithRemoteProviderURL(contextProviderURL))
}
documentLoader, err := ldutil.DocumentLoader(storageProvider, ldOpts...)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
vdr, err := vdrutil.NewRegistry(tlsConfig)
if err != nil {
return nil, err
}
kmsStore, err := kms.NewAriesProviderWrapper(storageProvider)
if err != nil {
return nil, err
}
suite, err := localsuite.NewLocalCryptoSuite("local-lock://wallet-cli", kmsStore, &noop.NoLock{})
if err != nil {
return nil, err
}
return &services{
storageProvider: storageProvider,
documentLoader: documentLoader,
vdrRegistry: vdr,
cryptoSuite: suite,
tlsConfig: tlsConfig,
}, nil
}
func (p *services) StorageProvider() storageapi.Provider {
return p.storageProvider
}
func (p *services) DocumentLoader() ld.DocumentLoader {
return p.documentLoader
}
func (p *services) VDR() vdrapi.Registry {
return p.vdrRegistry
}
func (p *services) CryptoSuite() api.Suite {
return p.cryptoSuite
}
func (p *services) TLSConfig() *tls.Config {
return p.tlsConfig
}