forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx.go
177 lines (151 loc) · 5.12 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package txs
import (
"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/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"
)
type UnsignedTx interface {
snow.ContextInitializable
SetBytes(unsignedBytes []byte)
Bytes() []byte
InputIDs() set.Set[ids.ID]
NumCredentials() int
// TODO: deprecate after x-chain linearization
InputUTXOs() []*avax.UTXOID
// 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
TxID ids.ID `json:"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.TxID = 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.TxID
}
// 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
}
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
}