forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx.go
222 lines (190 loc) · 6.04 KB
/
tx.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/codec"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/snow"
"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
"github.com/MetalBlockchain/metalgo/utils/hashing"
"github.com/MetalBlockchain/metalgo/utils/set"
"github.com/MetalBlockchain/metalgo/vms/avm/config"
"github.com/MetalBlockchain/metalgo/vms/avm/fxs"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/nftfx"
"github.com/MetalBlockchain/metalgo/vms/propertyfx"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
)
var errNilTx = errors.New("nil tx is not valid")
type UnsignedTx interface {
snow.ContextInitializable
SetBytes(unsignedBytes []byte)
Bytes() []byte
InputIDs() set.Set[ids.ID]
ConsumedAssetIDs() set.Set[ids.ID]
AssetIDs() set.Set[ids.ID]
NumCredentials() int
// TODO: deprecate after x-chain linearization
InputUTXOs() []*avax.UTXOID
SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
config *config.Config,
numFxs int,
) error
// Visit calls [visitor] with this transaction's concrete type
Visit(visitor Visitor) error
}
// Tx is the core operation that can be performed. The tx uses the UTXO model.
// Specifically, a txs inputs will consume previous txs outputs. A tx will be
// valid if the inputs have the authority to consume the outputs they are
// attempting to consume and the inputs consume sufficient state to produce the
// outputs.
type Tx struct {
Unsigned UnsignedTx `serialize:"true" json:"unsignedTx"`
Creds []*fxs.FxCredential `serialize:"true" json:"credentials"` // The credentials of this transaction
id ids.ID
bytes []byte
}
func (t *Tx) Initialize(c codec.Manager) error {
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
unsignedBytesLen, err := c.Size(CodecVersion, &t.Unsigned)
if err != nil {
return fmt.Errorf("couldn't calculate UnsignedTx marshal length: %w", err)
}
unsignedBytes := signedBytes[:unsignedBytesLen]
t.SetBytes(unsignedBytes, signedBytes)
return nil
}
func (t *Tx) SetBytes(unsignedBytes, signedBytes []byte) {
t.id = hashing.ComputeHash256Array(signedBytes)
t.bytes = signedBytes
t.Unsigned.SetBytes(unsignedBytes)
}
// ID returns the unique ID of this tx
func (t *Tx) ID() ids.ID {
return t.id
}
// Bytes returns the binary representation of this tx
func (t *Tx) Bytes() []byte {
return t.bytes
}
// UTXOs returns the UTXOs transaction is producing.
func (t *Tx) UTXOs() []*avax.UTXO {
u := utxoGetter{tx: t}
// The visit error is explicitly dropped here because no error is ever
// returned from the utxoGetter.
_ = t.Unsigned.Visit(&u)
return u.utxos
}
// SyntacticVerify verifies that this transaction is well-formed.
func (t *Tx) SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
config *config.Config,
numFxs int,
) error {
if t == nil || t.Unsigned == nil {
return errNilTx
}
if err := t.Unsigned.SyntacticVerify(ctx, c, txFeeAssetID, config, numFxs); err != nil {
return err
}
for _, cred := range t.Creds {
if err := cred.Verify(); err != nil {
return err
}
}
if numCreds := t.Unsigned.NumCredentials(); numCreds != len(t.Creds) {
return fmt.Errorf("tx has %d credentials but %d inputs. Should be same",
len(t.Creds),
numCreds,
)
}
return nil
}
func (t *Tx) SignSECP256K1Fx(c codec.Manager, signers [][]*secp256k1.PrivateKey) error {
unsignedBytes, err := c.Marshal(CodecVersion, &t.Unsigned)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
hash := hashing.ComputeHash256(unsignedBytes)
for _, keys := range signers {
cred := &secp256k1fx.Credential{
Sigs: make([][secp256k1.SignatureLen]byte, len(keys)),
}
for i, key := range keys {
sig, err := key.SignHash(hash)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
copy(cred.Sigs[i][:], sig)
}
t.Creds = append(t.Creds, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.SetBytes(unsignedBytes, signedBytes)
return nil
}
func (t *Tx) SignPropertyFx(c codec.Manager, signers [][]*secp256k1.PrivateKey) error {
unsignedBytes, err := c.Marshal(CodecVersion, &t.Unsigned)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
hash := hashing.ComputeHash256(unsignedBytes)
for _, keys := range signers {
cred := &propertyfx.Credential{Credential: secp256k1fx.Credential{
Sigs: make([][secp256k1.SignatureLen]byte, len(keys)),
}}
for i, key := range keys {
sig, err := key.SignHash(hash)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
copy(cred.Sigs[i][:], sig)
}
t.Creds = append(t.Creds, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.SetBytes(unsignedBytes, signedBytes)
return nil
}
func (t *Tx) SignNFTFx(c codec.Manager, signers [][]*secp256k1.PrivateKey) error {
unsignedBytes, err := c.Marshal(CodecVersion, &t.Unsigned)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
hash := hashing.ComputeHash256(unsignedBytes)
for _, keys := range signers {
cred := &nftfx.Credential{Credential: secp256k1fx.Credential{
Sigs: make([][secp256k1.SignatureLen]byte, len(keys)),
}}
for i, key := range keys {
sig, err := key.SignHash(hash)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
copy(cred.Sigs[i][:], sig)
}
t.Creds = append(t.Creds, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.SetBytes(unsignedBytes, signedBytes)
return nil
}