-
Notifications
You must be signed in to change notification settings - Fork 9
/
transaction.go
307 lines (254 loc) · 7.33 KB
/
transaction.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
package transaction
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"math/big"
"github.com/cbergoon/merkletree"
"github.com/filefilego/filefilego/common/hexutil"
ffgcrypto "github.com/filefilego/filefilego/crypto"
"github.com/libp2p/go-libp2p/core/crypto"
"google.golang.org/protobuf/proto"
)
// ChainID represents the main-net chain id.
const ChainID = "0x01"
// the payload size inside a transaction.Data
const maxTransactionDataSizeBytes = 300000
// Transaction represents a transaction.
type Transaction struct {
// calculated after
Hash []byte
Signature []byte
// required
PublicKey []byte
Nounce []byte
Data []byte
From string
To string
Value string
TransactionFees string
Chain []byte
}
// Serialize the transaction to bytes.
func (tx Transaction) Serialize() ([]byte, error) {
mainChain, err := hexutil.Decode(ChainID)
if err != nil {
return nil, fmt.Errorf("failed to decode chainID: %w", err)
}
if len(tx.PublicKey) == 0 {
return nil, errors.New("publicKey is empty")
}
if len(tx.Nounce) == 0 {
return nil, errors.New("nounce is empty")
}
if tx.From == "" {
return nil, errors.New("from is empty")
}
if tx.To == "" {
return nil, errors.New("to is empty")
}
if tx.Value == "" {
return nil, errors.New("value is empty")
}
if tx.TransactionFees == "" {
return nil, errors.New("transactionFees is empty")
}
data := bytes.Join(
[][]byte{
tx.PublicKey,
tx.Nounce,
tx.Data,
[]byte(tx.From),
[]byte(tx.To),
[]byte(tx.Value),
[]byte(tx.TransactionFees),
mainChain,
},
[]byte{},
)
return data, nil
}
// CalculateHash gets a hash of a transaction.
func (tx Transaction) CalculateHash() ([]byte, error) {
data, err := tx.Serialize()
if err != nil {
return nil, err
}
h := sha256.New()
if _, err := h.Write(data); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
// Equals tests for equality of two Contents.
func (tx Transaction) Equals(other merkletree.Content) (bool, error) {
data, err := tx.Serialize()
if err != nil {
return false, err
}
dataOther, err := other.(Transaction).Serialize()
if err != nil {
return false, err
}
return bytes.Equal(data, dataOther), nil
}
// SignTransaction signs a transaction with a private key.
func (tx *Transaction) Sign(key crypto.PrivKey) error {
hash, err := tx.CalculateHash()
if err != nil {
return fmt.Errorf("failed to get transactionHash: %w", err)
}
tx.Hash = make([]byte, len(hash))
copy(tx.Hash, hash)
sig, err := key.Sign(tx.Hash)
if err != nil {
return fmt.Errorf("failed to sign transaction: %w", err)
}
tx.Signature = make([]byte, len(sig))
copy(tx.Signature, sig)
return nil
}
// VerifyWithPublicKey verifies a transaction with a public key.
func (tx Transaction) VerifyWithPublicKey(key crypto.PubKey) error {
ok, err := key.Verify(tx.Hash, tx.Signature)
if err != nil {
return fmt.Errorf("failed to verify transaction: %w", err)
}
if !ok {
return errors.New("failed verification of transaction")
}
return nil
}
// Validate a transaction.
func (tx Transaction) Validate() (bool, error) {
zero := big.NewInt(0)
if len(tx.Hash) == 0 || tx.Hash == nil {
return false, errors.New("hash is empty")
}
if len(tx.Data) > maxTransactionDataSizeBytes {
return false, fmt.Errorf("data with size %d is greater than %d bytes", len(tx.Data), maxTransactionDataSizeBytes)
}
mainChain, err := hexutil.Decode(ChainID)
if err != nil {
return false, fmt.Errorf("failed to decode chainID: %w", err)
}
if !bytes.Equal(tx.Chain, mainChain) {
return false, errors.New("wrong chain")
}
if tx.From == "" {
return false, errors.New("from is empty")
}
if tx.To == "" {
return false, errors.New("to is empty")
}
if len(tx.Nounce) == 0 {
return false, errors.New("nounce is empty")
}
if len(tx.PublicKey) == 0 {
return false, errors.New("publicKey is empty")
}
if tx.TransactionFees == "" {
return false, errors.New("transactionFees is empty")
}
if tx.Value == "" {
return false, errors.New("value is empty")
}
val, err := hexutil.DecodeBig(tx.Value)
if err != nil {
return false, fmt.Errorf("value is malformed: %w", err)
}
if val.Cmp(zero) == -1 {
return false, errors.New("value is negative")
}
valFees, err := hexutil.DecodeBig(tx.TransactionFees)
if err != nil {
return false, fmt.Errorf("failed to decode transactionFees: %w", err)
}
if valFees.Cmp(zero) == -1 {
return false, errors.New("transactionFees is negative")
}
hash, err := tx.CalculateHash()
if err != nil {
return false, errors.New("failed to get transaction hash")
}
if !bytes.Equal(tx.Hash, hash) {
return false, errors.New("transaction is altered and doesn't match the hash")
}
newPubKey, err := ffgcrypto.PublicKeyFromBytes(tx.PublicKey)
if err != nil {
return false, fmt.Errorf("failed to get publicKey: %w", err)
}
err = tx.VerifyWithPublicKey(newPubKey)
if err != nil {
return false, fmt.Errorf("failed to verify: %w", err)
}
fromAddr, err := ffgcrypto.RawPublicToAddress(tx.PublicKey)
if err != nil {
return false, fmt.Errorf("failed to get address from publicKey: %w", err)
}
if tx.From != fromAddr {
return false, errors.New("from address doesn't match the public key")
}
return true, nil
}
// ToProtoTransaction converts a transaction to protobuf message.
func ToProtoTransaction(tx Transaction) *ProtoTransaction {
ptx := &ProtoTransaction{
Hash: make([]byte, len(tx.Hash)),
Signature: make([]byte, len(tx.Signature)),
PublicKey: make([]byte, len(tx.PublicKey)),
Nounce: make([]byte, len(tx.Nounce)),
Data: make([]byte, len(tx.Data)),
From: tx.From,
To: tx.To,
Value: tx.Value,
TransactionFees: tx.TransactionFees,
Chain: make([]byte, len(tx.Chain)),
}
copy(ptx.Hash, tx.Hash)
copy(ptx.Signature, tx.Signature)
copy(ptx.PublicKey, tx.PublicKey)
copy(ptx.Nounce, tx.Nounce)
copy(ptx.Data, tx.Data)
copy(ptx.Chain, tx.Chain)
return ptx
}
// ProtoTransactionToTransaction returns a domain transaction from a protobuf message.
func ProtoTransactionToTransaction(ptx *ProtoTransaction) Transaction {
tx := Transaction{
Hash: make([]byte, len(ptx.Hash)),
Signature: make([]byte, len(ptx.Signature)),
PublicKey: make([]byte, len(ptx.PublicKey)),
Nounce: make([]byte, len(ptx.Nounce)),
Data: make([]byte, len(ptx.Data)),
From: ptx.From,
To: ptx.To,
Value: ptx.Value,
TransactionFees: ptx.TransactionFees,
Chain: make([]byte, len(ptx.Chain)),
}
copy(tx.Hash, ptx.Hash)
copy(tx.Signature, ptx.Signature)
copy(tx.PublicKey, ptx.PublicKey)
copy(tx.Nounce, ptx.Nounce)
copy(tx.Data, ptx.Data)
copy(tx.Chain, ptx.Chain)
return tx
}
// MarshalProtoTransaction serializes a block to a protobuf message.
func MarshalProtoTransaction(tx *ProtoTransaction) ([]byte, error) {
txData, err := proto.Marshal(tx)
if err != nil {
return nil, fmt.Errorf("failed to marshal transaction: %w", err)
}
return txData, nil
}
// UnmarshalProtoBlock unserializes a byte array to a protobuf transaction.
func UnmarshalProtoBlock(data []byte) (*ProtoTransaction, error) {
tx := ProtoTransaction{}
if err := proto.Unmarshal(data, &tx); err != nil {
return nil, fmt.Errorf("failed to unmarshal a transaction: %w", err)
}
return &tx, nil
}