-
Notifications
You must be signed in to change notification settings - Fork 3
/
wallet.go
118 lines (101 loc) · 2.97 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
// Copyright 2018 The uranus Authors
// This file is part of the uranus library.
//
// The uranus library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The uranus library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the uranus library. If not, see <http://www.gnu.org/licenses/>.
package rpcapi
import (
"errors"
"github.com/UranusBlockStack/uranus/common/utils"
"github.com/UranusBlockStack/uranus/wallet"
)
// WalletAPI exposes methods for the RPC interface
type WalletAPI struct {
b Backend
}
// NewWalletAPI creates a new RPC service with methods specific for the wallet.
func NewWalletAPI(b Backend) *WalletAPI {
return &WalletAPI{b}
}
// NewAccount create a new account, and generate keyfile in keystore dir.
func (w *WalletAPI) NewAccount(passphrase string, reply *utils.Address) error {
account, err := w.b.NewAccount(passphrase)
if err != nil {
return err
}
*reply = account.Address
return nil
}
type DeleteArgs struct {
Address utils.Address
Passphrase string
}
// Delete delete a account by address and passphrase, at the same time, delete keyfile.
func (w *WalletAPI) Delete(args DeleteArgs, reply *bool) error {
if err := w.b.Delete(args.Address, args.Passphrase); err != nil {
return err
}
*reply = true
return nil
}
type UpdateArgs struct {
Address utils.Address
Passphrase string
NewPassphrase string
}
// Update a account passphrase, at the same time, update keyfile.
func (w *WalletAPI) Update(args UpdateArgs, reply *bool) error {
if err := w.b.Update(args.Address, args.Passphrase, args.NewPassphrase); err != nil {
return err
}
*reply = true
return nil
}
// Accounts list all wallet account.
func (w *WalletAPI) Accounts(ignore string, reply *wallet.Accounts) error {
accounts, err := w.b.Accounts()
if err != nil {
return err
}
if len(accounts) == 0 {
return errors.New("wallet is nil")
}
*reply = accounts
return nil
}
type ImportRawKeyArgs struct {
PrivKeyHex string
Passphrase string
}
// ImportRawKey import raw PrivateKey into walet.
func (w *WalletAPI) ImportRawKey(args ImportRawKeyArgs, reply *utils.Address) error {
addr, err := w.b.ImportRawKey(args.PrivKeyHex, args.Passphrase)
if err != nil {
return nil
}
*reply = addr
return nil
}
type ExportRawKeyArgs struct {
Address utils.Address
Passphrase string
}
// ExportRawKey returns key hex.
func (w *WalletAPI) ExportRawKey(args ExportRawKeyArgs, reply *string) error {
hex, err := w.b.ExportRawKey(args.Address, args.Passphrase)
if err != nil {
return nil
}
*reply = hex
return nil
}