forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
199 lines (171 loc) · 5.44 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"errors"
"fmt"
"github.com/dim4egster/qmallgo/codec"
"github.com/dim4egster/qmallgo/ids"
"github.com/dim4egster/qmallgo/snow"
"github.com/dim4egster/qmallgo/utils/crypto"
"github.com/dim4egster/qmallgo/utils/hashing"
"github.com/dim4egster/qmallgo/vms/avm/fxs"
"github.com/dim4egster/qmallgo/vms/components/avax"
"github.com/dim4egster/qmallgo/vms/nftfx"
"github.com/dim4egster/qmallgo/vms/propertyfx"
"github.com/dim4egster/qmallgo/vms/secp256k1fx"
)
var errNilTx = errors.New("nil tx is not valid")
type UnsignedTx interface {
snow.ContextInitializable
Initialize(unsignedBytes []byte)
Bytes() []byte
ConsumedAssetIDs() ids.Set
AssetIDs() ids.Set
NumCredentials() int
InputUTXOs() []*avax.UTXOID
SyntacticVerify(
ctx *snow.Context,
c codec.Manager,
txFeeAssetID ids.ID,
txFee uint64,
creationTxFee uint64,
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(unsignedBytes, signedBytes []byte) {
t.id = hashing.ComputeHash256Array(signedBytes)
t.bytes = signedBytes
t.Unsigned.Initialize(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,
txFee uint64,
creationTxFee uint64,
numFxs int,
) error {
if t == nil || t.Unsigned == nil {
return errNilTx
}
if err := t.Unsigned.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.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 [][]*crypto.PrivateKeySECP256K1R) 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([][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, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.Initialize(unsignedBytes, signedBytes)
return nil
}
func (t *Tx) SignPropertyFx(c codec.Manager, signers [][]*crypto.PrivateKeySECP256K1R) 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([][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, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.Initialize(unsignedBytes, signedBytes)
return nil
}
func (t *Tx) SignNFTFx(c codec.Manager, signers [][]*crypto.PrivateKeySECP256K1R) 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([][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, &fxs.FxCredential{Verifiable: cred})
}
signedBytes, err := c.Marshal(CodecVersion, t)
if err != nil {
return fmt.Errorf("problem creating transaction: %w", err)
}
t.Initialize(unsignedBytes, signedBytes)
return nil
}