-
Notifications
You must be signed in to change notification settings - Fork 23
/
accounts.go
301 lines (252 loc) · 8.35 KB
/
accounts.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
package ltc
import (
"encoding/json"
"fmt"
"math"
"strconv"
"time"
"decred.org/dcrwallet/v3/errors"
sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet"
"github.com/crypto-power/cryptopower/libwallet/utils"
"github.com/ltcsuite/ltcd/chaincfg"
"github.com/ltcsuite/ltcd/ltcutil"
"github.com/ltcsuite/ltcwallet/waddrmgr"
)
const (
// AddressGapLimit is the number of consecutive unused addresses that
// will be tracked before the wallet stops searching for new transactions.
AddressGapLimit uint32 = 20
// ImportedAccountNumber is the account number used for imported addresses.
ImportedAccountNumber = waddrmgr.ImportedAddrAccount
// DefaultAccountNum is the account number used for the default account.
DefaultAccountNum = waddrmgr.DefaultAccountNum
)
// GetAccounts returns a list of all accounts for the wallet.
func (asset *Asset) GetAccounts() (string, error) {
accountsResponse, err := asset.GetAccountsRaw()
if err != nil {
return "", err
}
result, err := json.Marshal(accountsResponse)
if err != nil {
return "", err
}
return string(result), nil
}
// GetAccountsRaw returns a list of all accounts for the wallet
// without marshalling the response.
func (asset *Asset) GetAccountsRaw() (*sharedW.Accounts, error) {
if !asset.WalletOpened() {
return nil, utils.ErrLTCNotInitialized
}
resp, err := asset.Internal().LTC.Accounts(GetScope())
if err != nil {
return nil, err
}
accounts := make([]*sharedW.Account, len(resp.Accounts))
for i, a := range resp.Accounts {
balance, err := asset.GetAccountBalance(int32(a.AccountNumber))
if err != nil {
return nil, err
}
accounts[i] = &sharedW.Account{
AccountProperties: sharedW.AccountProperties{
AccountNumber: a.AccountNumber,
AccountName: a.AccountName,
ExternalKeyCount: a.ExternalKeyCount + AddressGapLimit, // Add gap limit
InternalKeyCount: a.InternalKeyCount + AddressGapLimit,
ImportedKeyCount: a.ImportedKeyCount,
},
Number: int32(a.AccountNumber),
Name: a.AccountName,
WalletID: asset.ID,
Balance: balance,
}
}
return &sharedW.Accounts{
CurrentBlockHash: resp.CurrentBlockHash[:],
CurrentBlockHeight: resp.CurrentBlockHeight,
Accounts: accounts,
}, nil
}
// GetAccount returns the account for the provided account number.
// If the account does not exist, an error is returned.
func (asset *Asset) GetAccount(accountNumber int32) (*sharedW.Account, error) {
accounts, err := asset.GetAccountsRaw()
if err != nil {
return nil, err
}
for _, account := range accounts.Accounts {
if account.AccountNumber == uint32(accountNumber) {
return account, nil
}
}
return nil, errors.New(utils.ErrNotExist)
}
// GetAccountBalance returns the balance for the provided account number.
func (asset *Asset) GetAccountBalance(accountNumber int32) (*sharedW.Balance, error) {
if !asset.WalletOpened() {
return nil, utils.ErrLTCNotInitialized
}
balance, err := asset.Internal().LTC.CalculateAccountBalances(uint32(accountNumber), asset.RequiredConfirmations())
if err != nil {
return nil, err
}
// Account for locked amount.
lockedAmount, err := asset.lockedAmount()
if err != nil {
return nil, err
}
return &sharedW.Balance{
Total: Amount(balance.Total),
Spendable: Amount(balance.Spendable - lockedAmount),
ImmatureReward: Amount(balance.ImmatureReward),
Locked: Amount(lockedAmount),
}, nil
}
// lockedAmount is the total value of locked outputs, as locked with
// LockUnspent.
func (asset *Asset) lockedAmount() (ltcutil.Amount, error) {
lockedOutpoints := asset.Internal().LTC.LockedOutpoints()
var sum int64
for _, op := range lockedOutpoints {
tx, err := asset.GetTransactionRaw(op.Txid)
if err != nil {
return 0, err
}
sum += tx.Amount
}
return ltcutil.Amount(sum), nil
}
// SpendableForAccount returns the spendable balance for the provided account
func (asset *Asset) SpendableForAccount(account int32) (int64, error) {
if !asset.WalletOpened() {
return -1, utils.ErrLTCNotInitialized
}
bals, err := asset.Internal().LTC.CalculateAccountBalances(uint32(account), asset.RequiredConfirmations())
if err != nil {
return 0, utils.TranslateError(err)
}
// Account for locked amount.
lockedAmount, err := asset.lockedAmount()
if err != nil {
return 0, err
}
return int64(bals.Spendable - lockedAmount), nil
}
// UnspentOutputs returns all the unspent outputs available for the provided
// account index.
func (asset *Asset) UnspentOutputs(account int32) ([]*sharedW.UnspentOutput, error) {
if !asset.WalletOpened() {
return nil, utils.ErrLTCNotInitialized
}
accountName, err := asset.AccountName(account)
if err != nil {
return nil, err
}
// Only return UTXOs with the required number of confirmations.
unspents, err := asset.Internal().LTC.ListUnspent(asset.RequiredConfirmations(),
math.MaxInt32, accountName)
if err != nil {
return nil, err
}
resp := make([]*sharedW.UnspentOutput, 0, len(unspents))
for _, utxo := range unspents {
// error returned is ignored because the amount value is from upstream
// and doesn't require an extra layer of validation.
amount, _ := ltcutil.NewAmount(utxo.Amount)
txInfo, err := asset.GetTransactionRaw(utxo.TxID)
if err != nil {
return nil, fmt.Errorf("invalid TxID %v : error: %v", utxo.TxID, err)
}
resp = append(resp, &sharedW.UnspentOutput{
TxID: utxo.TxID,
Vout: utxo.Vout,
Address: utxo.Address,
ScriptPubKey: utxo.ScriptPubKey,
RedeemScript: utxo.RedeemScript,
Amount: Amount(amount),
Confirmations: int32(utxo.Confirmations),
Spendable: utxo.Spendable,
ReceiveTime: time.Unix(txInfo.Timestamp, 0),
})
}
return resp, nil
}
// CreateNewAccount creates a new account with the provided account name.
func (asset *Asset) CreateNewAccount(accountName, privPass string) (int32, error) {
err := asset.UnlockWallet(privPass)
if err != nil {
return -1, err
}
defer asset.LockWallet()
return asset.NextAccount(accountName)
}
// NextAccount returns the next account number for the provided account name.
func (asset *Asset) NextAccount(accountName string) (int32, error) {
if !asset.WalletOpened() {
return -1, utils.ErrLTCNotInitialized
}
if asset.IsLocked() {
return -1, errors.New(utils.ErrWalletLocked)
}
accountNumber, err := asset.Internal().LTC.NextAccount(GetScope(), accountName)
if err != nil {
return -1, err
}
return int32(accountNumber), nil
}
// RenameAccount renames the account with the provided account number.
func (asset *Asset) RenameAccount(accountNumber int32, newName string) error {
if !asset.WalletOpened() {
return utils.ErrLTCNotInitialized
}
err := asset.Internal().LTC.RenameAccount(GetScope(), uint32(accountNumber), newName)
if err != nil {
return utils.TranslateError(err)
}
return nil
}
// AccountName returns the account name for the provided account number.
func (asset *Asset) AccountName(accountNumber int32) (string, error) {
name, err := asset.AccountNameRaw(uint32(accountNumber))
if err != nil {
return "", utils.TranslateError(err)
}
return name, nil
}
// AccountNameRaw returns the account name for the provided account number
// from the internal wallet.
func (asset *Asset) AccountNameRaw(accountNumber uint32) (string, error) {
if !asset.WalletOpened() {
return "", utils.ErrLTCNotInitialized
}
return asset.Internal().LTC.AccountName(GetScope(), accountNumber)
}
// AccountNumber returns the account number for the provided account name.
func (asset *Asset) AccountNumber(accountName string) (int32, error) {
if !asset.WalletOpened() {
return -1, utils.ErrLTCNotInitialized
}
accountNumber, err := asset.Internal().LTC.AccountNumber(GetScope(), accountName)
return int32(accountNumber), utils.TranslateError(err)
}
// HasAccount returns true if there is an account with the provided account name.
func (asset *Asset) HasAccount(accountName string) bool {
if !asset.WalletOpened() {
log.Error(utils.ErrLTCNotInitialized)
return false
}
_, err := asset.Internal().LTC.AccountNumber(GetScope(), accountName)
return err == nil
}
// HDPathForAccount returns the HD path for the provided account number.
func (asset *Asset) HDPathForAccount(accountNumber int32) (string, error) {
var hdPath string
if asset.chainParams.Name == chaincfg.MainNetParams.Name {
hdPath = MainnetHDPath
} else {
hdPath = TestnetHDPath
}
return hdPath + strconv.Itoa(int(accountNumber)), nil
}