This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alephium_wallets.go
322 lines (247 loc) · 10.2 KB
/
alephium_wallets.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
package alephium
import (
"strings"
)
// GetWallets returns the list of wallet present on the full node
func (a *Client) GetWallets() ([]WalletInfo, error) {
var wallets []WalletInfo
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets").
Receive(&wallets, &errorDetail)
return wallets, relevantError(err, errorDetail)
}
type CreateWalletRequestBody struct {
Password string `json:"password"`
IsMiner bool `json:"isMiner"`
WalletName string `json:"walletName"`
MnemonicPassphrase string `json:"mnemonicPassphrase,omitempty"`
MnemonicSize int `json:"mnemonicSize,omitempty"`
}
// CreateWallet creates a new wallet, generating mnemonic while doing so
func (a *Client) CreateWallet(walletName string, password string, isMiner bool, mnemonicPassphrase string) (WalletCreate, error) {
body := CreateWalletRequestBody{
Password: password,
IsMiner: isMiner,
WalletName: walletName,
MnemonicPassphrase: mnemonicPassphrase,
}
var wallet WalletCreate
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets").
BodyJSON(body).Receive(&wallet, &errorDetail)
return wallet, relevantError(err, errorDetail)
}
type RestoreWalletRequestBody struct {
Password string `json:"password"`
Mnemonic string `json:"mnemonic"`
IsMiner bool `json:"isMiner,omitempty"`
WalletName string `json:"walletName,omitempty"`
MnemonicPassphrase string `json:"mnemonicPassphrase,omitempty"`
}
// RestoreWallet creates a wallet with provided mnemonics (unlike CreateWallet which generates new mnemonics)
func (a *Client) RestoreWallet(password string, mnemonic string, walletName string,
isMiner bool, mnemonicPassphrase string) (Wallet, error) {
body := RestoreWalletRequestBody{
Password: password,
Mnemonic: mnemonic,
WalletName: walletName,
IsMiner: isMiner,
MnemonicPassphrase: mnemonicPassphrase,
}
var wallet Wallet
var errorDetail ErrorDetail
_, err := a.slingClient.New().Put("wallets").
BodyJSON(body).Receive(&wallet, &errorDetail)
return wallet, relevantError(err, errorDetail)
}
// GetWalletStatus returns the status of a given wallet
func (a *Client) GetWalletStatus(walletName string) (WalletInfo, error) {
var walletInfo WalletInfo
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets/"+walletName).
Receive(&walletInfo, &errorDetail)
return walletInfo, relevantError(err, errorDetail)
}
// LockWallet locks a given wallet. Returns false if the wallet was already locked.
func (a *Client) LockWallet(walletName string) (bool, error) {
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/lock").
Receive(nil, &errorDetail)
return true, relevantError(err, errorDetail)
}
type WalletPasswordRequestBody struct {
Password string `json:"password"`
MnemonicPassphrase string `json:"mnemonicPassphrase,omitempty"`
}
// UnlockWallet unlocks wallet with the provided password and optional passphrase.
// Returns true if the wallet got successfully unlocked, false when the wallet was already unlocked
func (a *Client) UnlockWallet(walletName string, password string, mnemonicPassphrase string) (bool, error) {
body := WalletPasswordRequestBody{
Password: password,
MnemonicPassphrase: mnemonicPassphrase,
}
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/unlock").
BodyJSON(body).Receive(nil, &errorDetail)
return true, relevantError(err, errorDetail)
}
// GetWalletBalances returns the balance of all the addresses inside the wallet.
func (a *Client) GetWalletBalances(walletName string) (WalletBalances, error) {
var walletBalances WalletBalances
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets/"+walletName+"/balances").
Receive(&walletBalances, &errorDetail)
return walletBalances, relevantError(err, errorDetail)
}
// GetWalletAddresses lists all the addresses from a wallet
func (a *Client) GetWalletAddresses(walletName string) (WalletAddresses, error) {
var walletAddresses WalletAddresses
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets/"+walletName+"/addresses").
Receive(&walletAddresses, &errorDetail)
return walletAddresses, relevantError(err, errorDetail)
}
// GetWalletAddressDetail returns detailed info about a specific address of a wallet
func (a *Client) GetWalletAddressDetail(walletName string, address string) (AddressDetailResponse, error) {
var addressDetailResponse AddressDetailResponse
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets/"+walletName+"/addresses/"+address).
Receive(&addressDetailResponse, &errorDetail)
return addressDetailResponse, relevantError(err, errorDetail)
}
type AddressDetailResponse struct {
Address string `json:"address"`
PublicKey string `json:"publicKey"`
Group int `json:"group"`
}
type TransferRequest struct {
Destinations []TransferDestination `json:"destinations"`
}
type TransferDestination struct {
Address string `json:"address"`
Amount ALPH `json:"amount"`
}
type TransferToken struct {
Id string `json:"id"`
Amount string `json:"amount"`
}
// Transfer transfers ALPH from one wallet to a given address
func (a *Client) Transfer(walletName string, address string, amount ALPH) (Transaction, error) {
// TODO: run sanity check on address and amount
body := TransferRequest{Destinations: []TransferDestination{{Address: address, Amount: amount}}}
var transaction Transaction
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/transfer").
BodyJSON(body).Receive(&transaction, &errorDetail)
return transaction, relevantError(err, errorDetail)
}
type SweepAllRequest struct {
Address string `json:"toAddress"`
}
// SweepAll transfers all (unlocked) ALPH from a wallet to another address
func (a *Client) SweepAll(walletName string, toAddress string) (Transaction, error) {
// TODO: run sanity check on address
body := SweepAllRequest{Address: toAddress}
var transaction Transaction
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/sweep-all").
BodyJSON(body).Receive(&transaction, &errorDetail)
return transaction, relevantError(err, errorDetail)
}
type RevealMnemonicRequest struct {
Password string `json:"password"`
}
type RevealMnemonicResponse struct {
Mnemonic string `json:"mnemonic"`
}
// RevealWalletMnemonic reveals your mnemonic. Please use with caution!!
func (a *Client) RevealWalletMnemonic(walletName string, password string) (string, error) {
body := RevealMnemonicRequest{Password: password}
var response RevealMnemonicResponse
var errorDetail ErrorDetail
_, err := a.slingClient.New().Get("wallets/"+walletName+"/reveal-mnemonic").
BodyJSON(body).Receive(&response, &errorDetail)
return response.Mnemonic, relevantError(err, errorDetail)
}
type SignRequest struct {
Data string `json:"data"`
}
type SignResponse struct {
Signature string `json:"signature"`
}
// Sign signs the given data and returns the signature
func (a *Client) Sign(walletName string, data string) (string, error) {
body := SignRequest{Data: data}
var response SignResponse
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/sign").
BodyJSON(body).Receive(&response, &errorDetail)
return response.Signature, relevantError(err, errorDetail)
}
// DeriveNextAddress derives the next address
func (a *Client) DeriveNextAddress(walletName string) (Address, error) {
var address Address
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/derive-next-address").
Receive(&address, &errorDetail)
return address, relevantError(err, errorDetail)
}
type AddressBodyRequest struct {
Address string `json:"address"`
}
// ChangeActiveAddress changes the active address of the wallet. Has no effect on non-miner wallet.
func (a *Client) ChangeActiveAddress(walletName string, activeAddress string) (bool, error) {
body := AddressBodyRequest{Address: activeAddress}
//var address string
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/change-active-address").
BodyJSON(body).Receive(nil, &errorDetail)
return true, relevantError(err, errorDetail)
}
// DeleteWallet deletes a wallet.
func (a *Client) DeleteWallet(walletName string, walletPassword string) (bool, error) {
body := WalletPasswordRequestBody{Password: walletPassword}
var errorDetail ErrorDetail
_, err := a.slingClient.New().Delete("wallets/"+walletName).
BodyJSON(body).Receive(nil, &errorDetail)
return true, relevantError(err, errorDetail)
}
// CheckWalletExist is a convenience function which checks if the wallet exists,
// since this information is based on the error string returned by the API call.
// TODO: should the "not found" exception being typed?
func (a *Client) CheckWalletExist(walletName string) (bool, error) {
_, err := a.GetWalletStatus(walletName)
if err != nil {
if strings.HasPrefix(walletName+" not found", err.Error()) {
return false, nil
}
return false, err
}
return true, nil
}
func GetAddressesAsString(walletAddresses []WalletAddress) []string {
addresses := make([]string, 0, len(walletAddresses))
for _, wa := range walletAddresses {
addresses = append(addresses, wa.Address)
}
return addresses
}
type MinerWalletAddresses struct {
Addresses []WalletAddress `json:"addresses"`
}
// GetMinerWalletAddresses lists all the addresses from a miner wallet
func (a *Client) GetMinerWalletAddresses(walletName string) ([]MinerWalletAddresses, error) {
var minerAddresses []MinerWalletAddresses
var errorDetail ErrorDetail
_, err := a.slingClient.New().Path("wallets/"+walletName+"/miner-addresses").
Receive(&minerAddresses, &errorDetail)
return minerAddresses, relevantError(err, errorDetail)
}
// DeriveNextMinerAddresses derives the next miner address
func (a *Client) DeriveNextMinerAddresses(walletName string) ([]WalletAddress, error) {
var addresses []WalletAddress
var errorDetail ErrorDetail
_, err := a.slingClient.New().Post("wallets/"+walletName+"/derive-next-miner-addresses").
Receive(&addresses, &errorDetail)
return addresses, relevantError(err, errorDetail)
}