forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
wallet_service.go
255 lines (218 loc) · 6.5 KB
/
wallet_service.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"fmt"
"net/http"
"go.uber.org/zap"
"golang.org/x/exp/maps"
"github.com/MetalBlockchain/metalgo/api"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils/formatting"
"github.com/MetalBlockchain/metalgo/utils/linkedhashmap"
"github.com/MetalBlockchain/metalgo/utils/logging"
"github.com/MetalBlockchain/metalgo/utils/math"
"github.com/MetalBlockchain/metalgo/vms/avm/txs"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
)
type WalletService struct {
vm *VM
pendingTxs linkedhashmap.LinkedHashmap[ids.ID, *txs.Tx]
}
func (w *WalletService) decided(txID ids.ID) {
w.pendingTxs.Delete(txID)
}
func (w *WalletService) issue(txBytes []byte) (ids.ID, error) {
tx, err := w.vm.parser.ParseTx(txBytes)
if err != nil {
return ids.ID{}, err
}
txID, err := w.vm.IssueTx(txBytes)
if err != nil {
return ids.ID{}, err
}
if _, ok := w.pendingTxs.Get(txID); !ok {
w.pendingTxs.Put(txID, tx)
}
return txID, nil
}
func (w *WalletService) update(utxos []*avax.UTXO) ([]*avax.UTXO, error) {
utxoMap := make(map[ids.ID]*avax.UTXO, len(utxos))
for _, utxo := range utxos {
utxoMap[utxo.InputID()] = utxo
}
iter := w.pendingTxs.NewIterator()
for iter.Next() {
tx := iter.Value()
for _, inputUTXO := range tx.Unsigned.InputUTXOs() {
if inputUTXO.Symbolic() {
continue
}
utxoID := inputUTXO.InputID()
if _, exists := utxoMap[utxoID]; !exists {
return nil, errMissingUTXO
}
delete(utxoMap, utxoID)
}
for _, utxo := range tx.UTXOs() {
utxoMap[utxo.InputID()] = utxo
}
}
return maps.Values(utxoMap), nil
}
// IssueTx attempts to issue a transaction into consensus
func (w *WalletService) IssueTx(_ *http.Request, args *api.FormattedTx, reply *api.JSONTxID) error {
w.vm.ctx.Log.Warn("deprecated API called",
zap.String("service", "wallet"),
zap.String("method", "issueTx"),
logging.UserString("tx", args.Tx),
)
txBytes, err := formatting.Decode(args.Encoding, args.Tx)
if err != nil {
return fmt.Errorf("problem decoding transaction: %w", err)
}
txID, err := w.issue(txBytes)
reply.TxID = txID
return err
}
// Send returns the ID of the newly created transaction
func (w *WalletService) Send(r *http.Request, args *SendArgs, reply *api.JSONTxIDChangeAddr) error {
return w.SendMultiple(r, &SendMultipleArgs{
JSONSpendHeader: args.JSONSpendHeader,
Outputs: []SendOutput{args.SendOutput},
Memo: args.Memo,
}, reply)
}
// SendMultiple sends a transaction with multiple outputs.
func (w *WalletService) SendMultiple(_ *http.Request, args *SendMultipleArgs, reply *api.JSONTxIDChangeAddr) error {
w.vm.ctx.Log.Warn("deprecated API called",
zap.String("service", "wallet"),
zap.String("method", "sendMultiple"),
logging.UserString("username", args.Username),
)
// Validate the memo field
memoBytes := []byte(args.Memo)
if l := len(memoBytes); l > avax.MaxMemoSize {
return fmt.Errorf("max memo length is %d but provided memo field is length %d",
avax.MaxMemoSize,
l)
} else if len(args.Outputs) == 0 {
return errNoOutputs
}
// Parse the from addresses
fromAddrs, err := avax.ParseServiceAddresses(w.vm, args.From)
if err != nil {
return fmt.Errorf("couldn't parse 'From' addresses: %w", err)
}
// Load user's UTXOs/keys
utxos, kc, err := w.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
return err
}
utxos, err = w.update(utxos)
if err != nil {
return err
}
// Parse the change address.
if len(kc.Keys) == 0 {
return errNoKeys
}
changeAddr, err := w.vm.selectChangeAddr(kc.Keys[0].PublicKey().Address(), args.ChangeAddr)
if err != nil {
return err
}
// Calculate required input amounts and create the desired outputs
// String repr. of asset ID --> asset ID
assetIDs := make(map[string]ids.ID)
// Asset ID --> amount of that asset being sent
amounts := make(map[ids.ID]uint64)
// Outputs of our tx
outs := []*avax.TransferableOutput{}
for _, output := range args.Outputs {
if output.Amount == 0 {
return errZeroAmount
}
assetID, ok := assetIDs[output.AssetID] // Asset ID of next output
if !ok {
assetID, err = w.vm.lookupAssetID(output.AssetID)
if err != nil {
return fmt.Errorf("couldn't find asset %s", output.AssetID)
}
assetIDs[output.AssetID] = assetID
}
currentAmount := amounts[assetID]
newAmount, err := math.Add64(currentAmount, uint64(output.Amount))
if err != nil {
return fmt.Errorf("problem calculating required spend amount: %w", err)
}
amounts[assetID] = newAmount
// Parse the to address
to, err := avax.ParseServiceAddress(w.vm, output.To)
if err != nil {
return fmt.Errorf("problem parsing to address %q: %w", output.To, err)
}
// Create the Output
outs = append(outs, &avax.TransferableOutput{
Asset: avax.Asset{ID: assetID},
Out: &secp256k1fx.TransferOutput{
Amt: uint64(output.Amount),
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
})
}
amountsWithFee := maps.Clone(amounts)
amountWithFee, err := math.Add64(amounts[w.vm.feeAssetID], w.vm.TxFee)
if err != nil {
return fmt.Errorf("problem calculating required spend amount: %w", err)
}
amountsWithFee[w.vm.feeAssetID] = amountWithFee
amountsSpent, ins, keys, err := w.vm.Spend(
utxos,
kc,
amountsWithFee,
)
if err != nil {
return err
}
// Add the required change outputs
for assetID, amountWithFee := range amountsWithFee {
amountSpent := amountsSpent[assetID]
if amountSpent > amountWithFee {
outs = append(outs, &avax.TransferableOutput{
Asset: avax.Asset{ID: assetID},
Out: &secp256k1fx.TransferOutput{
Amt: amountSpent - amountWithFee,
OutputOwners: secp256k1fx.OutputOwners{
Locktime: 0,
Threshold: 1,
Addrs: []ids.ShortID{changeAddr},
},
},
})
}
}
codec := w.vm.parser.Codec()
avax.SortTransferableOutputs(outs, codec)
tx := txs.Tx{Unsigned: &txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: w.vm.ctx.NetworkID,
BlockchainID: w.vm.ctx.ChainID,
Outs: outs,
Ins: ins,
Memo: memoBytes,
}}}
if err := tx.SignSECP256K1Fx(codec, keys); err != nil {
return err
}
txID, err := w.issue(tx.Bytes())
if err != nil {
return fmt.Errorf("problem issuing transaction: %w", err)
}
reply.TxID = txID
reply.ChangeAddr, err = w.vm.FormatLocalAddress(changeAddr)
return err
}