-
Notifications
You must be signed in to change notification settings - Fork 671
/
spender.go
440 lines (389 loc) · 9.62 KB
/
spender.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utxo
import (
"errors"
"fmt"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
var (
errSpendOverflow = errors.New("spent amount overflows uint64")
errInsufficientFunds = errors.New("insufficient funds")
errAddressesCantMintAsset = errors.New("provided addresses don't have the authority to mint the provided asset")
)
type Spender interface {
// Spend the provided amount while deducting the provided fee.
// Arguments:
// - [utxos] contains assets ID and amount to be spend for each assestID
// - [kc] are the owners of the funds
// - [amounts] is the amount of funds that are available to be spent for each assetID
// Returns:
// - [amountsSpent] the amount of funds that are spent
// - [inputs] the inputs that should be consumed to fund the outputs
// - [signers] the proof of ownership of the funds being moved
Spend(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
amounts map[ids.ID]uint64,
) (
map[ids.ID]uint64, // amountsSpent
[]*avax.TransferableInput, // inputs
[][]*secp256k1.PrivateKey, // signers
error,
)
SpendNFT(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
assetID ids.ID,
groupID uint32,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
)
SpendAll(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
) (
map[ids.ID]uint64,
[]*avax.TransferableInput,
[][]*secp256k1.PrivateKey,
error,
)
Mint(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
amounts map[ids.ID]uint64,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
)
MintNFT(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
assetID ids.ID,
payload []byte,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
)
}
func NewSpender(
clk *mockable.Clock,
codec codec.Manager,
) Spender {
return &spender{
clock: clk,
codec: codec,
}
}
type spender struct {
clock *mockable.Clock
codec codec.Manager
}
func (s *spender) Spend(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
amounts map[ids.ID]uint64,
) (
map[ids.ID]uint64, // amountsSpent
[]*avax.TransferableInput, // inputs
[][]*secp256k1.PrivateKey, // signers
error,
) {
amountsSpent := make(map[ids.ID]uint64, len(amounts))
time := s.clock.Unix()
ins := []*avax.TransferableInput{}
keys := [][]*secp256k1.PrivateKey{}
for _, utxo := range utxos {
assetID := utxo.AssetID()
amount := amounts[assetID]
amountSpent := amountsSpent[assetID]
if amountSpent >= amount {
// we already have enough inputs allocated to this asset
continue
}
inputIntf, signers, err := kc.Spend(utxo.Out, time)
if err != nil {
// this utxo can't be spent with the current keys right now
continue
}
input, ok := inputIntf.(avax.TransferableIn)
if !ok {
// this input doesn't have an amount, so I don't care about it here
continue
}
newAmountSpent, err := math.Add64(amountSpent, input.Amount())
if err != nil {
// there was an error calculating the consumed amount, just error
return nil, nil, nil, errSpendOverflow
}
amountsSpent[assetID] = newAmountSpent
// add the new input to the array
ins = append(ins, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: avax.Asset{ID: assetID},
In: input,
})
// add the required keys to the array
keys = append(keys, signers)
}
for asset, amount := range amounts {
if amountsSpent[asset] < amount {
return nil, nil, nil, fmt.Errorf("want to spend %d of asset %s but only have %d",
amount,
asset,
amountsSpent[asset],
)
}
}
avax.SortTransferableInputsWithSigners(ins, keys)
return amountsSpent, ins, keys, nil
}
func (s *spender) SpendNFT(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
assetID ids.ID,
groupID uint32,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
) {
time := s.clock.Unix()
ops := []*txs.Operation{}
keys := [][]*secp256k1.PrivateKey{}
for _, utxo := range utxos {
// makes sure that the variable isn't overwritten with the next iteration
utxo := utxo
if len(ops) > 0 {
// we have already been able to create the operation needed
break
}
if utxo.AssetID() != assetID {
// wrong asset ID
continue
}
out, ok := utxo.Out.(*nftfx.TransferOutput)
if !ok {
// wrong output type
continue
}
if out.GroupID != groupID {
// wrong group id
continue
}
indices, signers, ok := kc.Match(&out.OutputOwners, time)
if !ok {
// unable to spend the output
continue
}
// add the new operation to the array
ops = append(ops, &txs.Operation{
Asset: utxo.Asset,
UTXOIDs: []*avax.UTXOID{&utxo.UTXOID},
Op: &nftfx.TransferOperation{
Input: secp256k1fx.Input{
SigIndices: indices,
},
Output: nftfx.TransferOutput{
GroupID: out.GroupID,
Payload: out.Payload,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
},
})
// add the required keys to the array
keys = append(keys, signers)
}
if len(ops) == 0 {
return nil, nil, errInsufficientFunds
}
txs.SortOperationsWithSigners(ops, keys, s.codec)
return ops, keys, nil
}
func (s *spender) SpendAll(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
) (
map[ids.ID]uint64,
[]*avax.TransferableInput,
[][]*secp256k1.PrivateKey,
error,
) {
amountsSpent := make(map[ids.ID]uint64)
time := s.clock.Unix()
ins := []*avax.TransferableInput{}
keys := [][]*secp256k1.PrivateKey{}
for _, utxo := range utxos {
assetID := utxo.AssetID()
amountSpent := amountsSpent[assetID]
inputIntf, signers, err := kc.Spend(utxo.Out, time)
if err != nil {
// this utxo can't be spent with the current keys right now
continue
}
input, ok := inputIntf.(avax.TransferableIn)
if !ok {
// this input doesn't have an amount, so I don't care about it here
continue
}
newAmountSpent, err := math.Add64(amountSpent, input.Amount())
if err != nil {
// there was an error calculating the consumed amount, just error
return nil, nil, nil, errSpendOverflow
}
amountsSpent[assetID] = newAmountSpent
// add the new input to the array
ins = append(ins, &avax.TransferableInput{
UTXOID: utxo.UTXOID,
Asset: avax.Asset{ID: assetID},
In: input,
})
// add the required keys to the array
keys = append(keys, signers)
}
avax.SortTransferableInputsWithSigners(ins, keys)
return amountsSpent, ins, keys, nil
}
func (s *spender) Mint(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
amounts map[ids.ID]uint64,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
) {
time := s.clock.Unix()
ops := []*txs.Operation{}
keys := [][]*secp256k1.PrivateKey{}
for _, utxo := range utxos {
// makes sure that the variable isn't overwritten with the next iteration
utxo := utxo
assetID := utxo.AssetID()
amount := amounts[assetID]
if amount == 0 {
continue
}
out, ok := utxo.Out.(*secp256k1fx.MintOutput)
if !ok {
continue
}
inIntf, signers, err := kc.Spend(out, time)
if err != nil {
continue
}
in, ok := inIntf.(*secp256k1fx.Input)
if !ok {
continue
}
// add the operation to the array
ops = append(ops, &txs.Operation{
Asset: utxo.Asset,
UTXOIDs: []*avax.UTXOID{&utxo.UTXOID},
Op: &secp256k1fx.MintOperation{
MintInput: *in,
MintOutput: *out,
TransferOutput: secp256k1fx.TransferOutput{
Amt: amount,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{to},
},
},
},
})
// add the required keys to the array
keys = append(keys, signers)
// remove the asset from the required amounts to mint
delete(amounts, assetID)
}
for _, amount := range amounts {
if amount > 0 {
return nil, nil, errAddressesCantMintAsset
}
}
txs.SortOperationsWithSigners(ops, keys, s.codec)
return ops, keys, nil
}
func (s *spender) MintNFT(
utxos []*avax.UTXO,
kc *secp256k1fx.Keychain,
assetID ids.ID,
payload []byte,
to ids.ShortID,
) (
[]*txs.Operation,
[][]*secp256k1.PrivateKey,
error,
) {
time := s.clock.Unix()
ops := []*txs.Operation{}
keys := [][]*secp256k1.PrivateKey{}
for _, utxo := range utxos {
// makes sure that the variable isn't overwritten with the next iteration
utxo := utxo
if len(ops) > 0 {
// we have already been able to create the operation needed
break
}
if utxo.AssetID() != assetID {
// wrong asset id
continue
}
out, ok := utxo.Out.(*nftfx.MintOutput)
if !ok {
// wrong output type
continue
}
indices, signers, ok := kc.Match(&out.OutputOwners, time)
if !ok {
// unable to spend the output
continue
}
// add the operation to the array
ops = append(ops, &txs.Operation{
Asset: avax.Asset{ID: assetID},
UTXOIDs: []*avax.UTXOID{
&utxo.UTXOID,
},
Op: &nftfx.MintOperation{
MintInput: secp256k1fx.Input{
SigIndices: indices,
},
GroupID: out.GroupID,
Payload: payload,
Outputs: []*secp256k1fx.OutputOwners{{
Threshold: 1,
Addrs: []ids.ShortID{to},
}},
},
})
// add the required keys to the array
keys = append(keys, signers)
}
if len(ops) == 0 {
return nil, nil, errAddressesCantMintAsset
}
txs.SortOperationsWithSigners(ops, keys, s.codec)
return ops, keys, nil
}