/
wallet.go
329 lines (290 loc) · 8.5 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
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package x
import (
"errors"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/vms/avm"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/chain/x/builder"
"github.com/ava-labs/avalanchego/wallet/chain/x/signer"
"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
)
var (
ErrNotAccepted = errors.New("not accepted")
_ Wallet = (*wallet)(nil)
)
type Wallet interface {
// Builder returns the builder that will be used to create the transactions.
Builder() builder.Builder
// Signer returns the signer that will be used to sign the transactions.
Signer() signer.Signer
// IssueBaseTx creates, signs, and issues a new simple value transfer.
//
// - [outputs] specifies all the recipients and amounts that should be sent
// from this transaction.
IssueBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.Tx, error)
// IssueCreateAssetTx creates, signs, and issues a new asset.
//
// - [name] specifies a human readable name for this asset.
// - [symbol] specifies a human readable abbreviation for this asset.
// - [denomination] specifies how many times the asset can be split. For
// example, a denomination of [4] would mean that the smallest unit of the
// asset would be 0.001 units.
// - [initialState] specifies the supported feature extensions for this
// asset as well as the initial outputs for the asset.
IssueCreateAssetTx(
name string,
symbol string,
denomination byte,
initialState map[uint32][]verify.State,
options ...common.Option,
) (*txs.Tx, error)
// IssueOperationTx creates, signs, and issues state changes on the UTXO
// set. These state changes may be more complex than simple value transfers.
//
// - [operations] specifies the state changes to perform.
IssueOperationTx(
operations []*txs.Operation,
options ...common.Option,
) (*txs.Tx, error)
// IssueOperationTxMintFT creates, signs, and issues a set of state changes
// that mint new tokens for the requested assets.
//
// - [outputs] maps the assetID to the output that should be created for the
// asset.
IssueOperationTxMintFT(
outputs map[ids.ID]*secp256k1fx.TransferOutput,
options ...common.Option,
) (*txs.Tx, error)
// IssueOperationTxMintNFT creates, signs, and issues a state change that
// mints new NFTs for the requested asset.
//
// - [assetID] specifies the asset to mint the NFTs under.
// - [payload] specifies the payload to provide each new NFT.
// - [owners] specifies the new owners of each NFT.
IssueOperationTxMintNFT(
assetID ids.ID,
payload []byte,
owners []*secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error)
// IssueOperationTxMintProperty creates, signs, and issues a state change
// that mints a new property for the requested asset.
//
// - [assetID] specifies the asset to mint the property under.
// - [owner] specifies the new owner of the property.
IssueOperationTxMintProperty(
assetID ids.ID,
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error)
// IssueOperationTxBurnProperty creates, signs, and issues state changes
// that burns all the properties of the requested asset.
//
// - [assetID] specifies the asset to burn the property of.
IssueOperationTxBurnProperty(
assetID ids.ID,
options ...common.Option,
) (*txs.Tx, error)
// IssueImportTx creates, signs, and issues an import transaction that
// attempts to consume all the available UTXOs and import the funds to [to].
//
// - [chainID] specifies the chain to be importing funds from.
// - [to] specifies where to send the imported funds to.
IssueImportTx(
chainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error)
// IssueExportTx creates, signs, and issues an export transaction that
// attempts to send all the provided [outputs] to the requested [chainID].
//
// - [chainID] specifies the chain to be exporting the funds to.
// - [outputs] specifies the outputs to send to the [chainID].
IssueExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.Tx, error)
// IssueUnsignedTx signs and issues the unsigned tx.
IssueUnsignedTx(
utx txs.UnsignedTx,
options ...common.Option,
) (*txs.Tx, error)
// IssueTx issues the signed tx.
IssueTx(
tx *txs.Tx,
options ...common.Option,
) error
}
func NewWallet(
builder builder.Builder,
signer signer.Signer,
client avm.Client,
backend Backend,
) Wallet {
return &wallet{
backend: backend,
builder: builder,
signer: signer,
client: client,
}
}
type wallet struct {
backend Backend
builder builder.Builder
signer signer.Signer
client avm.Client
}
func (w *wallet) Builder() builder.Builder {
return w.builder
}
func (w *wallet) Signer() signer.Signer {
return w.signer
}
func (w *wallet) IssueBaseTx(
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewBaseTx(outputs, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueCreateAssetTx(
name string,
symbol string,
denomination byte,
initialState map[uint32][]verify.State,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewCreateAssetTx(name, symbol, denomination, initialState, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueOperationTx(
operations []*txs.Operation,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewOperationTx(operations, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueOperationTxMintFT(
outputs map[ids.ID]*secp256k1fx.TransferOutput,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewOperationTxMintFT(outputs, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueOperationTxMintNFT(
assetID ids.ID,
payload []byte,
owners []*secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewOperationTxMintNFT(assetID, payload, owners, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueOperationTxMintProperty(
assetID ids.ID,
owner *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewOperationTxMintProperty(assetID, owner, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueOperationTxBurnProperty(
assetID ids.ID,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewOperationTxBurnProperty(assetID, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueImportTx(
chainID ids.ID,
to *secp256k1fx.OutputOwners,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewImportTx(chainID, to, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueExportTx(
chainID ids.ID,
outputs []*avax.TransferableOutput,
options ...common.Option,
) (*txs.Tx, error) {
utx, err := w.builder.NewExportTx(chainID, outputs, options...)
if err != nil {
return nil, err
}
return w.IssueUnsignedTx(utx, options...)
}
func (w *wallet) IssueUnsignedTx(
utx txs.UnsignedTx,
options ...common.Option,
) (*txs.Tx, error) {
ops := common.NewOptions(options)
ctx := ops.Context()
tx, err := signer.SignUnsigned(ctx, w.signer, utx)
if err != nil {
return nil, err
}
return tx, w.IssueTx(tx, options...)
}
func (w *wallet) IssueTx(
tx *txs.Tx,
options ...common.Option,
) error {
ops := common.NewOptions(options)
ctx := ops.Context()
txID, err := w.client.IssueTx(ctx, tx.Bytes())
if err != nil {
return err
}
if f := ops.PostIssuanceFunc(); f != nil {
f(txID)
}
if ops.AssumeDecided() {
return w.backend.AcceptTx(ctx, tx)
}
txStatus, err := w.client.ConfirmTx(ctx, txID, ops.PollFrequency())
if err != nil {
return err
}
if err := w.backend.AcceptTx(ctx, tx); err != nil {
return err
}
if txStatus != choices.Accepted {
return ErrNotAccepted
}
return nil
}