-
Notifications
You must be signed in to change notification settings - Fork 15
/
wallet_api_plugin.go
347 lines (296 loc) · 10 KB
/
wallet_api_plugin.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package wallet_api_plugin
import (
"encoding/json"
"fmt"
"github.com/eosspark/eos-go/common"
"github.com/eosspark/eos-go/crypto/ecc"
. "github.com/eosspark/eos-go/exception"
. "github.com/eosspark/eos-go/exception/try"
"github.com/eosspark/eos-go/log"
. "github.com/eosspark/eos-go/plugins/appbase/app"
"github.com/eosspark/eos-go/plugins/http_plugin"
"github.com/eosspark/eos-go/plugins/wallet_plugin"
"github.com/urfave/cli"
)
const WalletApiPlug = PluginTypeName("WalletApiPlugin")
var templatePlugin = App().RegisterPlugin(WalletApiPlug, NewWalletApiPlugin())
type WalletApiPlugin struct {
AbstractPlugin
log log.Logger
}
type WalletApiPluginImpl struct {
}
func NewWalletApiPlugin() *WalletApiPlugin {
plugin := &WalletApiPlugin{}
plugin.log = log.New("WalletApiPlugin")
plugin.log.SetHandler(log.TerminalHandler)
return plugin
}
func (w *WalletApiPlugin) SetProgramOptions(options *[]cli.Flag) {
}
func (w *WalletApiPlugin) PluginInitialize(options *cli.Context) {
Try(func() {
httpPlugin := App().GetPlugin(http_plugin.HttpPlug).(*http_plugin.HttpPlugin)
if !httpPlugin.IsOnLoopBack() {
if !httpPlugin.IsSecure() {
w.log.Error("\n" +
"********!!!SECURITY ERROR!!!********\n" +
"* *\n" +
"* -- Wallet API -- *\n" +
"* - EXPOSED to the LOCAL NETWORK - *\n" +
"* - HTTP RPC is NOT encrypted - *\n" +
"* - Password and/or Private Keys - *\n" +
"* - are at HIGH risk of exposure - *\n" +
"* *\n" +
"************************************\n")
} else {
w.log.Warn("\n" +
"**********SECURITY WARNING**********\n" +
"* *\n" +
"* -- Wallet API -- *\n" +
"* - EXPOSED to the LOCAL NETWORK - *\n" +
"* - Password and/or Private Keys - *\n" +
"* - are at risk of exposure - *\n" +
"* *\n" +
"************************************\n")
}
}
}).FcLogAndRethrow().End()
}
func (w *WalletApiPlugin) PluginStartup() {
w.log.Info("starting wallet_api_plugin")
walletMgr := App().GetPlugin(wallet_plugin.WalletPlug).(*wallet_plugin.WalletPlugin).GetWalletManager()
h := App().GetPlugin(http_plugin.HttpPlug).(*http_plugin.HttpPlugin)
h.AddHandler("/v1/wallet/set_timeout", func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param int64
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal set_time params: %s", err.Error())
}
walletMgr.SetTimeOut(param)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "set_timeout", string(body), cb)
}).End()
})
h.AddHandler("/v1/wallet/sign_digest", func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
type signDigest struct {
Digest common.DigestType `json:"digest"`
Key ecc.PublicKey `json:"key"`
}
var param signDigest
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal sign_digest params: %s", err.Error())
}
result := walletMgr.SignDigest(param.Digest, param.Key)
byte, _ := json.Marshal(result)
cb(201, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "sign_digest", string(body), cb)
}).End()
})
h.AddHandler(common.WalletSignTrx, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.SignTrxParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal sign_transaction params: %s", err.Error())
}
result := walletMgr.SignTransaction(param.Txn, param.Keys, param.ChainID)
byte, _ := json.Marshal(result)
cb(201, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "sign_transaction", string(body), cb)
}).End()
})
h.AddHandler(common.WalletCreate, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param string
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal create params: %s", err.Error())
}
result := walletMgr.Create(param)
byte, _ := json.Marshal(result)
cb(201, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "create", string(body), cb)
}).End()
})
h.AddHandler(common.WalletOpen, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param string
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal open params: %s", err.Error())
}
walletMgr.Open(param)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "open", string(body), cb)
}).End()
})
h.AddHandler(common.WalletLockAll, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
walletMgr.LockAllwallets()
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "lock_all", string(body), cb)
}).End()
})
h.AddHandler(common.WalletLock, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param string
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal lock params: %s", err.Error())
}
walletMgr.Lock(param)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "lock", string(body), cb)
}).End()
})
h.AddHandler(common.WalletUnlock, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.UnlockParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal unlock params: %s", err.Error())
}
walletMgr.Unlock(param.Name, param.Password)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "unlock", string(body), cb)
}).End()
})
h.AddHandler(common.WalletImportKey, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.ImportKeyParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal import_key params: %s", err.Error())
}
walletMgr.ImportKey(param.Name, param.Key)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "import_key", string(body), cb)
}).End()
})
h.AddHandler(common.WalletRemoveKey, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.RemoveKeyParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal remove_key params: %s", err.Error())
}
walletMgr.RemoveKey(param.Name, param.Password, param.Key)
byte, _ := json.Marshal(walletApiPluginEmpty{})
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "remove_key", string(body), cb)
}).End()
})
h.AddHandler(common.WalletCreateKey, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.CreateKeyParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal create_key params: %s", err.Error())
}
result := walletMgr.CreateKey(param.Name, param.KeyType)
byte, _ := json.Marshal(result)
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "create_key", string(body), cb)
}).End()
})
h.AddHandler(common.WalletList, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
result := walletMgr.ListWallets()
byte, _ := json.Marshal(result)
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "list_wallets", string(body), cb)
}).End()
})
h.AddHandler(common.WalletListKeys, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
var param wallet_plugin.ListKeysParams
err := json.Unmarshal(body, ¶m)
if err != nil {
EosThrow(&EofException{}, "unmarshal list_keys params: %s", err.Error())
}
result := walletMgr.ListKeys(param.Name, param.Password)
byte, err := json.Marshal(result)
if err != nil {
fmt.Println(err)
EosThrow(&EofException{}, "marshal list_keys result: %s", err.Error())
}
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "list_keys", string(body), cb)
}).End()
})
h.AddHandler(common.WalletPublicKeys, func(source string, body []byte, cb http_plugin.UrlResponseCallback) {
Try(func() {
if len(body) == 0 {
body = []byte("{}")
}
result := walletMgr.GetPublicKeys()
byte, err := json.Marshal(result)
if err != nil {
EosThrow(&EofException{}, "marshal get_public_keys result: %s", err.Error())
}
cb(200, byte)
}).Catch(func(e interface{}) {
http_plugin.HandleException(e, "wallet", "get_public_keys", string(body), cb)
}).End()
})
}
func (w *WalletApiPlugin) PluginShutdown() {
}
type walletApiPluginEmpty struct {
}