-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.go
224 lines (190 loc) · 5.69 KB
/
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package kms
import (
"context"
"encoding/hex"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/perme-io/vault-plugin-secrets-kms/chains"
)
const (
walletStoragePath = "wallet"
publicKeyHashOffset = 20
)
type kmsWallet struct {
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
Address string `json:"address"`
}
func pathWallet(b *kmsBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "wallet",
Fields: map[string]*framework.FieldSchema{
"username": {
Type: framework.TypeString,
Description: "username of wallet",
Required: true,
},
"address": {
Type: framework.TypeString,
Description: "address of wallet",
Required: false,
},
"chainName": {
Type: framework.TypeString,
Description: "name of blockchain",
Required: false,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathWalletRead,
},
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathWalletCreate,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathWalletCreate,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathWalletDelete,
},
},
HelpSynopsis: pathWalletHelpSynopsis,
HelpDescription: pathWalletHelpDescription,
},
}
}
func getWalletPath(username string, address string) string {
userPath := username + "/" + address
return walletStoragePath + "/" + userPath
}
func getWallet(ctx context.Context, req *logical.Request, walletPath string) (*kmsWallet, error) {
// Decode the data
entry, err := req.Storage.Get(ctx, walletPath)
if err != nil {
return nil, fmt.Errorf("error reading wallet: %w", err)
}
if entry == nil {
return nil, fmt.Errorf("error not found wallet")
}
wallet := new(kmsWallet)
if err := entry.DecodeJSON(&wallet); err != nil {
return nil, fmt.Errorf("error decode wallet: %w", err)
}
return wallet, nil
}
func (b *kmsBackend) pathWalletRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var username string
if un, ok := d.GetOk("username"); ok {
username = un.(string)
} else if !ok {
return nil, fmt.Errorf("missing username in wallet")
}
var address string
if addr, ok := d.GetOk("address"); ok {
address = addr.(string)
} else if !ok {
return nil, fmt.Errorf("missing address in wallet")
}
walletPath := getWalletPath(username, address)
wallet, err := getWallet(ctx, req, walletPath)
if err != nil {
return nil, err
}
// Generate the response
resp := &logical.Response{
Data: map[string]interface{}{
"address": wallet.Address,
"public_key": wallet.PublicKey,
},
}
return resp, nil
}
func createWallet(chainName chains.ChainName) (*kmsWallet, error) {
wallet := &kmsWallet{}
var chain chains.Chain
if privateKey, err := secp256k1.GeneratePrivateKey(); err == nil {
switch chainName {
case chains.ICON:
chain = chains.IconChain{PrivateKey: privateKey}
case chains.AERGO:
chain = chains.AergoChain{PrivateKey: privateKey}
default:
return nil, fmt.Errorf("unknown chain name: %v", chainName)
}
} else {
return nil, err
}
pubKeySerialized := chain.GetPublicKeySerialized()
wallet.PrivateKey = hex.EncodeToString(chain.GetPrivateKeySerialized())
wallet.PublicKey = hex.EncodeToString(pubKeySerialized)
wallet.Address = chain.GetPublicKeyAddress(pubKeySerialized)
return wallet, nil
}
func (b *kmsBackend) pathWalletCreate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var username string
if un, ok := d.GetOk("username"); ok {
if username = un.(string); username == "" {
return nil, fmt.Errorf("empty username in wallet")
}
} else if !ok {
return nil, fmt.Errorf("missing username in wallet")
}
var chainName chains.ChainName
if wtype, ok := d.GetOk("chainName"); ok {
chainName = chains.ChainName(wtype.(string))
} else {
return nil, fmt.Errorf("missing chainName in wallet")
}
b.Logger().Debug("chainName:", chainName)
wallet, err := createWallet(chainName)
if err != nil {
return nil, fmt.Errorf("failed to create wallet. err=%v", err)
}
walletPath := getWalletPath(username, wallet.Address)
entry, err := logical.StorageEntryJSON(walletPath, wallet)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
resp := &logical.Response{
Data: map[string]interface{}{
"address": wallet.Address,
},
}
return resp, nil
}
func (b *kmsBackend) pathWalletDelete(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var username string
if un, ok := d.GetOk("username"); ok {
if username = un.(string); username == "" {
return nil, fmt.Errorf("empty username in wallet")
}
} else if !ok {
return nil, fmt.Errorf("missing username in wallet")
}
var address string
if addr, ok := d.GetOk("address"); ok {
address = addr.(string)
} else if !ok {
return nil, fmt.Errorf("missing address in wallet")
}
walletPath := getWalletPath(username, address)
err := req.Storage.Delete(ctx, walletPath)
if err != nil {
return nil, fmt.Errorf("error deleting wallet: %w", err)
}
return nil, nil
}
const (
pathWalletHelpSynopsis = `Manages the Vault wallet for generating transaction signature.`
pathWalletHelpDescription = `
This path allows you to read and write wallet used to generate transaction signature.
You can create wallet to generate a user's transaction signature by setting the username field.
`
)