-
Notifications
You must be signed in to change notification settings - Fork 660
/
tx.go
161 lines (139 loc) · 4.37 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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"fmt"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)
// UnsignedTx ...
type UnsignedTx interface {
Initialize(unsignedBytes, bytes []byte)
ID() ids.ID
UnsignedBytes() []byte
Bytes() []byte
ConsumedAssetIDs() ids.Set
AssetIDs() ids.Set
NumCredentials() int
InputUTXOs() []*avax.UTXOID
UTXOs() []*avax.UTXO
SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
txFee uint64,
creationTxFee uint64,
numFxs int,
) error
SemanticVerify(vm *VM, tx UnsignedTx, creds []verify.Verifiable) error
ExecuteWithSideEffects(vm *VM, batch database.Batch) 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 {
UnsignedTx `serialize:"true" json:"unsignedTx"`
Creds []verify.Verifiable `serialize:"true" json:"credentials"` // The credentials of this transaction
}
// Credentials describes the authorization that allows the Inputs to consume the
// specified UTXOs. The returned array should not be modified.
func (t *Tx) Credentials() []verify.Verifiable { return t.Creds }
// SyntacticVerify verifies that this transaction is well-formed.
func (t *Tx) SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
txFee uint64,
creationTxFee uint64,
numFxs int,
) error {
if t == nil || t.UnsignedTx == nil {
return errNilTx
}
if err := t.UnsignedTx.SyntacticVerify(ctx, c, txFeeAssetID, txFee, creationTxFee, numFxs); err != nil {
return err
}
for _, cred := range t.Creds {
if err := cred.Verify(); err != nil {
return err
}
}
if numCreds := t.UnsignedTx.NumCredentials(); numCreds != len(t.Creds) {
return fmt.Errorf("tx has %d credentials but %d inputs. Should be same",
len(t.Creds),
numCreds,
)
}
return nil
}
// SemanticVerify verifies that this transaction is well-formed.
func (t *Tx) SemanticVerify(vm *VM, tx UnsignedTx) error {
if t == nil {
return errNilTx
}
return t.UnsignedTx.SemanticVerify(vm, tx, t.Creds)
}
// SignSECP256K1Fx ...
func (t *Tx) SignSECP256K1Fx(c codec.Manager, signers [][]*crypto.PrivateKeySECP256K1R) error {
unsignedBytes, err := c.Marshal(codecVersion, &t.UnsignedTx)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
hash := hashing.ComputeHash256(unsignedBytes)
for _, keys := range signers {
cred := &secp256k1fx.Credential{
Sigs: make([][crypto.SECP256K1RSigLen]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, cred)
}
signedBytes, err := c.Marshal(codecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.Initialize(unsignedBytes, signedBytes)
return nil
}
// SignNFTFx ...
func (t *Tx) SignNFTFx(c codec.Manager, signers [][]*crypto.PrivateKeySECP256K1R) error {
unsignedBytes, err := c.Marshal(codecVersion, &t.UnsignedTx)
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([][crypto.SECP256K1RSigLen]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, cred)
}
signedBytes, err := c.Marshal(codecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.Initialize(unsignedBytes, signedBytes)
return nil
}