-
Notifications
You must be signed in to change notification settings - Fork 18
/
wallet.go
184 lines (156 loc) · 5.83 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
package btc
import (
"encoding/json"
"github.com/hiromaily/go-bitcoin/pkg/enum"
"github.com/pkg/errors"
)
//{
//"filename": "/Applications/sample01.dat"
//}
const (
dumpWallet = "dumpwallet"
importWallet = "importwallet"
)
// WalletResult dumpwallet/dumpwallet RPC のレスポンス
type WalletResult struct {
FileName string `json:"filename"`
}
// LoadWalletResult loadwallet/createwallet RPCのレスポンス
type LoadWalletResult struct {
WalletName string `json:"name"`
Warning string `json:"warning"`
}
// BackupWallet walletファイルをunloadする
// Safely copies current wallet file to destination, which can be a directory or a path with filename
func (b *Bitcoin) BackupWallet(fileName string) error {
//backupwallet
bFileName, err := json.Marshal(fileName)
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
_, err = b.client.RawRequest("backupwallet", []json.RawMessage{bFileName})
if err != nil {
return errors.Errorf("json.RawRequest(backupwallet): error: %s", err)
}
return nil
}
// DumpWallet walletを指定したファイル名でdumpする (これをすることでkeyの存在しないwalletが作られるはず)
// fileNameは絶対パス
// This does not allow overwriting existing files.
func (b *Bitcoin) DumpWallet(fileName string) error {
return b.dumpImportWallet(fileName, dumpWallet)
}
// ImportWallet walletファイルをimportする
func (b *Bitcoin) ImportWallet(fileName string) error {
return b.dumpImportWallet(fileName, importWallet)
}
func (b *Bitcoin) dumpImportWallet(fileName, method string) error {
bFileName, err := json.Marshal(string(fileName))
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
rawResult, err := b.client.RawRequest(method, []json.RawMessage{bFileName})
if err != nil {
return errors.Errorf("json.RawRequest(%s): error: %s", method, err)
}
var walletResult WalletResult
err = json.Unmarshal([]byte(rawResult), &walletResult)
if err != nil {
return errors.Errorf("json.Unmarshal(): error: %s", err)
}
return nil
}
// EncryptWallet Walletの初回パスフレーズを設定
// 0.16.2の場合、`encryptwallet`というmethod名になっている。これは古いのかも
func (b *Bitcoin) EncryptWallet(passphrase string) error {
return b.client.CreateEncryptedWallet(passphrase)
}
// WalletLock walletにロックをかける(walletpassphraseによってロックを解除するまで利用できない)
func (b *Bitcoin) WalletLock() error {
return b.client.WalletLock()
}
// WalletPassphrase ロックを解除する
func (b *Bitcoin) WalletPassphrase(passphrase string, timeoutSecs int64) error {
return b.client.WalletPassphrase(passphrase, timeoutSecs)
}
// WalletPassphraseChange パスフレーズを変更する
func (b *Bitcoin) WalletPassphraseChange(old, new string) error {
return b.client.WalletPassphraseChange(old, new)
}
// LoadWallet walletファイルをimportする
// Loads a wallet from a wallet file or directory.
// Note that all wallet command-line options used when starting bitcoind will be
// applied to the new wallet (eg -zapwallettxes, upgradewallet, rescan, etc).
// e.g. bitcoin-cli loadwallet "test.dat"
func (b *Bitcoin) LoadWallet(fileName string) error {
if b.Version() < enum.BTCVer17 {
return errors.New("`loadwallet` is available from bitcoin version 0.17")
}
//loadwallet "filename"
bFileName, err := json.Marshal(fileName)
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
rawResult, err := b.client.RawRequest("loadwallet", []json.RawMessage{bFileName})
if err != nil {
return errors.Errorf("json.RawRequest(loadwallet): error: %s", err)
}
loadWalletResult := LoadWalletResult{}
err = json.Unmarshal([]byte(rawResult), &loadWalletResult)
if err != nil {
return errors.Errorf("json.Unmarshal(): error: %s", err)
}
if loadWalletResult.Warning != "" {
//TODO:この扱いをどうするか
return errors.Errorf("json.RawRequest(loadwallet): warning: %s", loadWalletResult.Warning)
}
return nil
}
// UnLoadWallet walletファイルをunloadする
// Unloads the wallet referenced by the request endpoint otherwise unloads the wallet specified in the argument.
// Specifying the wallet name on a wallet endpoint is invalid.
// e.g. bitcoin-cli unloadwallet wallet_name
func (b *Bitcoin) UnLoadWallet(fileName string) error {
if b.Version() < enum.BTCVer17 {
return errors.New("`unloadwallet` is available from bitcoin version 0.17")
}
//unloadwallet ( "wallet_name" )
bFileName, err := json.Marshal(fileName)
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
_, err = b.client.RawRequest("unloadwallet", []json.RawMessage{bFileName})
if err != nil {
return errors.Errorf("json.RawRequest(unloadwallet): error: %s", err)
}
return nil
}
// CreateWallet Creates and loads a new wallet
func (b *Bitcoin) CreateWallet(fileName string, disablePrivKey bool) error {
if b.Version() < enum.BTCVer17 {
return errors.New("`createwallet` is available from bitcoin version 0.17")
}
//createwallet "wallet_name" ( disable_private_keys )
bFileName, err := json.Marshal(fileName)
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
bDisablePrivKey, err := json.Marshal(disablePrivKey)
if err != nil {
return errors.Errorf("json.Marchal(): error: %s", err)
}
rawResult, err := b.client.RawRequest("createwallet", []json.RawMessage{bFileName, bDisablePrivKey})
if err != nil {
return errors.Errorf("json.RawRequest(createwallet): error: %s", err)
}
loadWalletResult := LoadWalletResult{}
err = json.Unmarshal([]byte(rawResult), &loadWalletResult)
if err != nil {
return errors.Errorf("json.Unmarshal(): error: %s", err)
}
if loadWalletResult.Warning != "" {
//TODO:この扱いをどうするか
return errors.Errorf("json.RawRequest(loadwallet): warning: %s", loadWalletResult.Warning)
}
return nil
}