forked from Factom-Asset-Tokens/factom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
417 lines (349 loc) · 11.1 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// MIT License
//
// Copyright 2018 Canonical Ledgers, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
package factom
import (
"bytes"
"crypto/sha256"
"fmt"
"time"
"github.com/Factom-Asset-Tokens/factom/varintf"
)
// Transaction is a Factoid Transaction which is stored in the FBlock.
//
// Transactions can be between FAAddresses, or from an FAAddress to an
// ECAddress.
//
// Transaction amounts are all delimited in Factoshis.
type Transaction struct {
// ID the sha256 hash of the binary Transaction ledger, which includes
// the header, timestamp salt, and all inputs and outputs.
ID *Bytes32
// Timestamp is established by the FBlock. It is only populated if the
// Transaction was unmarshaled from within an FBlock
Timestamp time.Time
// TimestampSalt is accurate to the millisecond
TimestampSalt time.Time
// Totals all denoted in factoshis. Populated by UnmarshalBinary.
TotalIn, TotalFCTOut, TotalECOut, TotalBurn uint64
FCTInputs []AddressAmount
FCTOutputs []AddressAmount
ECOutputs []AddressAmount
Signatures []RCDSignature
// marshalBinaryCache is the binary data of the DBlock. It is cached by
// UnmarshalBinary so it can be re-used by MarshalBinary.
marshalBinaryCache []byte
}
// ClearMarshalBinaryCache discards the cached MarshalBinary data.
//
// Subsequent calls to MarshalBinary will re-construct the data from the fields
// of the DBlock.
func (tx *Transaction) ClearMarshalBinaryCache() {
tx.marshalBinaryCache = nil
}
// AddressAmount relates a 32 byte Address payload and an Amount.
//
// Bytes are used for the Address for improved unmarshaling efficiency of
// Transactions.
type AddressAmount struct {
Address Bytes
Amount uint64
}
// AddressBytes32 converts the Address to Bytes32.
func (adr AddressAmount) AddressBytes32() Bytes32 {
var b32 Bytes32
copy(b32[:], adr.Address)
return b32
}
// FAAddress converts the Address to an FAAddress.
func (adr AddressAmount) FAAddress() FAAddress {
return FAAddress(adr.AddressBytes32())
}
// ECAddress converts the Address to an ECAddress.
func (adr AddressAmount) ECAddress() ECAddress {
return ECAddress(adr.AddressBytes32())
}
// RCDSignature relates an RCD and a corresponding Signature of a signed
// Transaction.
type RCDSignature struct {
RCD RCD
Signature Bytes
}
// ValidateType01 validates the RCD and Signature against the msg, which is
// normally the Transaction Binary Ledger.
func (rs RCDSignature) ValidateType01(msg []byte) error {
return rs.RCD.ValidateType01(rs.Signature, msg)
}
// UnmarshalBinary unmarshals the variable length RCD and Signature block into
// rs.
//
// Use Len to determine how much data was read.
func (rs *RCDSignature) UnmarshalBinary(data []byte) error {
if err := rs.RCD.UnmarshalBinary(data); err != nil {
return err
}
data = data[len(rs.RCD):] // Skip past RCD
if len(data) < rs.RCD.SignatureBlockSize() {
return fmt.Errorf("%v: invalid signature block size", rs.RCD.Type())
}
rs.Signature = data[:rs.RCD.SignatureBlockSize()]
return nil
}
// MarshalBinary concatenates the RCD and the signature.
func (rs RCDSignature) MarshalBinary() ([]byte, error) {
return append(rs.RCD, rs.Signature...), nil
}
// Len is the total size of the RCD and Signature.
func (rs RCDSignature) Len() int {
return len(rs.RCD) + len(rs.Signature)
}
// IsPopulated returns true if s has already been successfully populated by a
// call to Get. IsPopulated returns false if s.SignatureBlock or
// s.ReedeemCondition are nil
func (rs RCDSignature) IsPopulated() bool {
return rs.RCD != nil && rs.Signature != nil
}
// IsPopulated returns true if tx has already been successfully populated by a
// call to Get. IsPopulated returns false if tx.FCTInputs is empty, or if
// tx.Signatures is not equal in length to the tx.FCTInputs, or if
// tx.TimestampSalt is zero.
func (tx Transaction) IsPopulated() bool {
return len(tx.FCTInputs) > 0 &&
len(tx.Signatures) == len(tx.FCTInputs) &&
!tx.TimestampSalt.IsZero()
}
const (
// TransactionVersion is the magic number Version byte in all
// Transactions.
TransactionVersion byte = 0x02
// TransactionHeaderSize is the size of a Binary Transaction Header.
TransactionHeaderSize = 1 + // Version byte
6 + // Timestamp salt
1 + // Input count
1 + // Output count
1 // EC output count
TransactionMinTotalSize = TransactionHeaderSize +
32 + 1 + // 1 Input
32 + 1 + // 1 Output
33 + 32 // RCD01 and Signature
)
// UnmarshalBinary unmarshals and validates the first Transaction from data,
// which may include subsequent Transactions. If no error is returned, the
// Transaction is valid, including all RCDs and signatures.
//
// Use MarshalBinaryLen to efficiently determine the number of bytes read from
// data.
func (tx *Transaction) UnmarshalBinary(data []byte) error {
// Parse header
if len(data) < TransactionHeaderSize {
return fmt.Errorf("insufficient length")
}
// Only Version 0x02 is supported.
if data[0] != TransactionVersion {
return fmt.Errorf("invalid version")
}
i := 1
msTsSalt := getInt48BE(data[i:])
tx.TimestampSalt = time.Unix(0, msTsSalt*1e6)
i += 6
fctInputCount := uint(data[i])
i++
fctOutputCount := uint(data[i])
i++
ecOutputCount := uint(data[i])
i++
adrs := make([]AddressAmount, fctInputCount+fctOutputCount+ecOutputCount)
var totalIn uint64
var totalOut uint64
var totalECOut uint64
for j := range adrs {
amount, size := varintf.Decode(data[i:])
if size == 0 {
return fmt.Errorf("insufficient length")
}
if size < 0 {
return fmt.Errorf("invalid amount")
}
i += size
if len(data[i:]) < 32 {
return fmt.Errorf("insufficient length")
}
adr := &adrs[j]
adr.Amount = amount
adr.Address = data[i : i+32]
i += 32
if uint(j) < fctInputCount {
totalIn += amount
} else {
totalOut += amount
if uint(j) >= fctInputCount+fctOutputCount {
totalECOut += amount
}
}
}
isCoinbaseTx := fctInputCount == 0 && ecOutputCount == 0
if !isCoinbaseTx && totalOut > totalIn {
return fmt.Errorf("outputs exceed inputs")
}
tx.TotalIn = totalIn
tx.TotalFCTOut = totalOut - totalECOut
tx.TotalECOut = totalECOut
tx.TotalBurn = totalIn - totalOut
ledger := data[:i]
tx.FCTInputs = adrs[:fctInputCount]
adrs = adrs[fctInputCount:]
tx.FCTOutputs = adrs[:fctOutputCount]
tx.ECOutputs = adrs[fctOutputCount:]
tx.Signatures = make([]RCDSignature, fctInputCount)
for j := range tx.Signatures {
rcdSig := &tx.Signatures[j]
if err := rcdSig.UnmarshalBinary(data[i:]); err != nil {
return err
}
i += rcdSig.Len()
// Validate RCD
rcdHash := rcdSig.RCD.Hash()
if bytes.Compare(tx.FCTInputs[j].Address, rcdHash[:]) != 0 {
return fmt.Errorf("invalid RCD hash")
}
if err := rcdSig.ValidateType01(ledger); err != nil {
return err
}
}
txID := Bytes32(sha256.Sum256(ledger))
if tx.ID == nil {
tx.ID = &txID
} else if *tx.ID != txID {
return fmt.Errorf("invalid TxID")
}
tx.marshalBinaryCache = data[:i]
return nil
}
// MarshalBinaryLedger marshals the header, inputs, outputs, and EC outputs of
// the Transaction. This is so that the data can be conveniently signed or
// hashed.
func (tx Transaction) MarshalBinaryLedger() ([]byte, error) {
if !tx.IsPopulated() {
return nil, fmt.Errorf("not populated")
}
if len(tx.FCTInputs) > 256 ||
len(tx.FCTOutputs) > 256 ||
len(tx.ECOutputs) > 256 {
return nil, fmt.Errorf("too many inputs or outputs")
}
if len(tx.FCTInputs) != len(tx.Signatures) {
return nil, fmt.Errorf("number of inputs and signatures differ")
}
data := make([]byte, tx.MarshalBinaryLen())
var i int
data[i] = TransactionVersion
i++
putInt48BE(data[i:], tx.TimestampSalt.Unix()*1e3)
i += 6
data[i] = byte(len(tx.FCTInputs))
i++
data[i] = byte(len(tx.FCTOutputs))
i++
data[i] = byte(len(tx.ECOutputs))
i++
for _, adrs := range [][]AddressAmount{
tx.FCTInputs,
tx.FCTOutputs, tx.ECOutputs,
} {
for _, adr := range adrs {
size := varintf.BufLen(adr.Amount)
varintf.Put(data[i:i+size], adr.Amount)
i += size
i += copy(data[i:], adr.Address)
}
}
return data[:i], nil
}
// MarshalBinary marshals the Transaction into its binary form. If the
// Transaction was orignally Unmarshaled, then the cached data is re-used, so
// this is efficient. See ClearMarshalBinaryCache.
//
// This assumes that the Transaction has all signatures in place already. See
// Transaction.Sign for signing transactions.
func (tx Transaction) MarshalBinary() ([]byte, error) {
if tx.marshalBinaryCache != nil {
return tx.marshalBinaryCache, nil
}
data, err := tx.MarshalBinaryLedger()
if err != nil {
return nil, err
}
for _, rcdSig := range tx.Signatures {
data = append(data, rcdSig.RCD...)
data = append(data, rcdSig.Signature...)
}
return data, nil
}
// Sign populates the Signatures of the tx with the given signingSet, which
// must correspond to the tx.FCTInputs. The complete binary marshaled
// Transaction is returned.
func (tx *Transaction) Sign(signingSet ...RCDSigner) ([]byte, error) {
if len(signingSet) != len(tx.FCTInputs) {
return nil, fmt.Errorf("signingSet size not equal to FCTInputs")
}
ledger, err := tx.MarshalBinaryLedger()
if err != nil {
return nil, err
}
tx.Signatures = make([]RCDSignature, len(tx.FCTInputs))
data := ledger
for i, rcdSigner := range signingSet {
rcdSig := &tx.Signatures[i]
rcdSig.RCD = rcdSigner.RCD()
rcdSig.Signature = rcdSigner.Sign(ledger)
hash := rcdSig.RCD.Hash()
if bytes.Compare(tx.FCTInputs[i].Address, hash[:]) != 0 {
return nil, fmt.Errorf("invalid RCD for FCTInput")
}
data = append(data, rcdSig.RCD...)
data = append(data, rcdSig.Signature...)
}
txID := Bytes32(sha256.Sum256(ledger))
tx.ID = &txID
tx.marshalBinaryCache = data
return data, nil
}
// MarshalBinaryLen efficiently calculates the full Transaction size. The
// cached binary marshal data is used if populated.
func (tx Transaction) MarshalBinaryLen() int {
if tx.marshalBinaryCache != nil {
return len(tx.marshalBinaryCache)
}
size := TransactionHeaderSize +
(len(tx.FCTInputs)+len(tx.FCTOutputs)+len(tx.ECOutputs))*32
for _, adrs := range [][]AddressAmount{
tx.FCTInputs,
tx.FCTOutputs, tx.ECOutputs,
} {
for _, adr := range adrs {
size += varintf.BufLen(adr.Amount)
}
}
for _, rcdSig := range tx.Signatures {
size += rcdSig.Len()
}
return size
}