-
Notifications
You must be signed in to change notification settings - Fork 9
/
transaction.go
432 lines (333 loc) · 10 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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package structures
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
"errors"
"math"
"strings"
"time"
"encoding/gob"
"fmt"
"github.com/gelembjuk/democoin/lib"
"github.com/gelembjuk/democoin/lib/utils"
)
// Transaction represents a Bitcoin transaction
type Transaction struct {
ID []byte
Vin []TXInput
Vout []TXOutput
Time int64
}
// IsCoinbase checks whether the transaction is coinbase
func (tx Transaction) IsCoinbase() bool {
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
}
// Hash returns the hash of the Transaction
func (tx *Transaction) TimeNow() {
tx.Time = time.Now().UTC().UnixNano()
}
// Hash returns the hash of the Transaction
func (tx *Transaction) Hash() ([]byte, error) {
var hash [32]byte
txCopy := *tx
txCopy.ID = []byte{}
txser, err := txCopy.Serialize()
if err != nil {
return nil, err
}
hash = sha256.Sum256(txser)
tx.ID = hash[:]
return tx.ID, nil
}
// String returns a human-readable representation of a transaction
func (tx Transaction) String() string {
var lines []string
from, _ := utils.PubKeyToAddres(tx.Vin[0].PubKey)
fromhash, _ := utils.HashPubKey(tx.Vin[0].PubKey)
to := ""
amount := 0.0
for _, output := range tx.Vout {
if bytes.Compare(fromhash, output.PubKeyHash) != 0 {
to, _ = utils.PubKeyHashToAddres(output.PubKeyHash)
amount = output.Value
break
}
}
lines = append(lines, fmt.Sprintf("--- Transaction %x:", tx.ID))
lines = append(lines, fmt.Sprintf(" FROM %s TO %s VALUE %f", from, to, amount))
lines = append(lines, fmt.Sprintf(" Time %d (%s)", tx.Time, time.Unix(0, tx.Time)))
for i, input := range tx.Vin {
address, _ := utils.PubKeyToAddres(input.PubKey)
lines = append(lines, fmt.Sprintf(" Input %d:", i))
lines = append(lines, fmt.Sprintf(" TXID: %x", input.Txid))
lines = append(lines, fmt.Sprintf(" Out: %d", input.Vout))
lines = append(lines, fmt.Sprintf(" Signature: %x", input.Signature))
lines = append(lines, fmt.Sprintf(" PubKey: %x", input.PubKey))
lines = append(lines, fmt.Sprintf(" Address: %s", address))
}
for i, output := range tx.Vout {
address, _ := utils.PubKeyHashToAddres(output.PubKeyHash)
lines = append(lines, fmt.Sprintf(" Output %d:", i))
lines = append(lines, fmt.Sprintf(" Value: %f", output.Value))
lines = append(lines, fmt.Sprintf(" Script: %x", output.PubKeyHash))
lines = append(lines, fmt.Sprintf(" Address: %s", address))
}
return strings.Join(lines, "\n")
}
// TrimmedCopy creates a trimmed copy of Transaction to be used in signing
func (tx *Transaction) TrimmedCopy() Transaction {
var inputs []TXInput
var outputs []TXOutput
for _, vin := range tx.Vin {
inputs = append(inputs, TXInput{vin.Txid, vin.Vout, nil, nil})
}
for _, vout := range tx.Vout {
pkh := utils.CopyBytes(vout.PubKeyHash)
outputs = append(outputs, TXOutput{vout.Value, pkh})
}
txID := utils.CopyBytes(tx.ID)
txCopy := Transaction{txID, inputs, outputs, tx.Time}
return txCopy
}
// TrimmedCopy creates a trimmed copy of Transaction to be used in signing
func (tx *Transaction) Copy() (Transaction, error) {
//if tx.IsCoinbase() {
// return *tx, nil
//}
var inputs []TXInput
var outputs []TXOutput
for _, vin := range tx.Vin {
sig := utils.CopyBytes(vin.Signature)
pk := utils.CopyBytes(vin.PubKey)
inputs = append(inputs, TXInput{vin.Txid, vin.Vout, sig, pk})
}
for _, vout := range tx.Vout {
pkh := utils.CopyBytes(vout.PubKeyHash)
outputs = append(outputs, TXOutput{vout.Value, pkh})
}
txID := utils.CopyBytes(tx.ID)
txCopy := Transaction{txID, inputs, outputs, tx.Time}
return txCopy, nil
}
// prepare data to sign as part of transaction
// this return slice of slices. Every of them must be signed for each TX Input
func (tx *Transaction) PrepareSignData(prevTXs map[int]*Transaction) ([][]byte, error) {
if tx.IsCoinbase() {
return nil, nil
}
for vinInd, vin := range tx.Vin {
if _, ok := prevTXs[vinInd]; !ok {
return nil, errors.New("Previous transaction is not correct")
}
if bytes.Compare(prevTXs[vinInd].ID, vin.Txid) != 0 {
return nil, errors.New("Previous transaction is not correct")
}
}
signdata := make([][]byte, len(tx.Vin))
txCopy := tx.TrimmedCopy()
txCopy.ID = []byte{}
for inID, _ := range txCopy.Vin {
txCopy.Vin[inID].Signature = nil
}
for inID, vin := range txCopy.Vin {
prevTx := prevTXs[inID]
txCopy.Vin[inID].PubKey = prevTx.Vout[vin.Vout].PubKeyHash
dataToSign := fmt.Sprintf("%x\n", txCopy)
signdata[inID] = []byte(dataToSign)
txCopy.Vin[inID].PubKey = nil
}
return signdata, nil
}
// Sign Inouts for transaction
// DataToSign is output of the function PrepareSignData
func (tx *Transaction) SignData(privKey ecdsa.PrivateKey, PubKey []byte, DataToSign [][]byte) error {
if tx.IsCoinbase() {
return nil
}
for inID, _ := range tx.Vin {
dataToSign := DataToSign[inID]
attempt := 1
var signature []byte
var err error
for {
// we can do more 1 attempt to sign. we found some cases where verification of signature fails
// we don't know the reason
signature, err = utils.SignData(privKey, dataToSign)
if err != nil {
return err
}
attempt = attempt + 1
v, err := utils.VerifySignature(signature, dataToSign, PubKey)
if err != nil {
return err
}
if v {
break
}
if attempt > 10 {
break
}
}
tx.Vin[inID].Signature = signature
}
// when transaction i complete, we can add ID to it
tx.Hash()
return nil
}
// Sets signatures for inputs. Signatures were created separately for data set prepared before
// in the function PrepareSignData
func (tx *Transaction) SetSignatures(signatures [][]byte) error {
if tx.IsCoinbase() {
return nil
}
for inID, _ := range tx.Vin {
tx.Vin[inID].Signature = signatures[inID]
}
// when transaction is complete, we can add ID to it
tx.Hash()
return nil
}
// Verify verifies signatures of Transaction inputs
// And total amount of inputs and outputs
func (tx *Transaction) Verify(prevTXs map[int]*Transaction) error {
if tx.IsCoinbase() {
// coinbase has only 1 output and it must have value equal to constant
if tx.Vout[0].Value != lib.PaymentForBlockMade {
return errors.New("Value of coinbase transaction is wrong")
}
if len(tx.Vout) > 1 {
return errors.New("Coinbase transaction can have only 1 output")
}
return nil
}
// calculate total input
totalinput := float64(0)
for vind, vin := range tx.Vin {
if prevTXs[vind].ID == nil {
return errors.New("Previous transaction is not correct")
}
amount := prevTXs[vind].Vout[vin.Vout].Value
totalinput += amount
}
txCopy := tx.TrimmedCopy()
txCopy.ID = []byte{}
for inID, _ := range tx.Vin {
txCopy.Vin[inID].Signature = nil
txCopy.Vin[inID].PubKey = nil
}
for inID, vin := range tx.Vin {
// full input transaction
prevTx := prevTXs[inID]
//hash of key who signed this input
signPubKeyHash, _ := utils.HashPubKey(vin.PubKey)
if bytes.Compare(prevTx.Vout[vin.Vout].PubKeyHash, signPubKeyHash) != 0 {
return errors.New(fmt.Sprintf("Sign Key Hash for input %x is different from output hash", vin.Txid))
}
// replace pub key with its hash. same was done when signing
txCopy.Vin[inID].PubKey = prevTx.Vout[vin.Vout].PubKeyHash
dataToVerify := fmt.Sprintf("%x\n", txCopy)
v, err := utils.VerifySignature(vin.Signature, []byte(dataToVerify), vin.PubKey)
if err != nil {
return err
}
if !v {
return errors.New(fmt.Sprintf("Signatire doe not match for input TX %x.", vin.Txid))
}
txCopy.Vin[inID].PubKey = nil
}
// calculate total output of transaction
totaloutput := float64(0)
for _, vout := range tx.Vout {
if vout.Value < lib.SmallestUnit {
return errors.New(fmt.Sprintf("Too small output value %f", vout.Value))
}
totaloutput += vout.Value
}
if math.Abs(totalinput-totaloutput) >= lib.SmallestUnit {
return errors.New(fmt.Sprintf("Input and output values of a transaction are not same: %.10f vs %.10f . Diff %.10f", totalinput, totaloutput, totalinput-totaloutput))
}
return nil
}
/*
* Make a transaction to be coinbase.
*/
func (tx *Transaction) MakeCoinbaseTX(to, data string) error {
if data == "" {
randData := make([]byte, 20)
_, err := rand.Read(randData)
if err != nil {
return err
}
data = fmt.Sprintf("%x", randData)
}
txin := TXInput{[]byte{}, -1, nil, []byte(data)}
txout := NewTXOutput(lib.PaymentForBlockMade, to)
tx.Vin = []TXInput{txin}
tx.Vout = []TXOutput{*txout}
tx.Hash()
return nil
}
// Serialize returns a serialized Transaction
func (tx Transaction) Serialize() ([]byte, error) {
// to remove any references to other ponters
// do full copy of the TX
txCopy, _ := tx.Copy()
var encoded bytes.Buffer
enc := gob.NewEncoder(&encoded)
err := enc.Encode(&txCopy)
if err != nil {
return nil, err
}
return encoded.Bytes(), nil
}
// DeserializeTransaction deserializes a transaction
func (tx *Transaction) DeserializeTransaction(data []byte) error {
decoder := gob.NewDecoder(bytes.NewReader(data))
err := decoder.Decode(tx)
if err != nil {
return err
}
return nil
}
// converts transaction to slice of bytes
// this will be used to do a hash of transactions
func (tx Transaction) ToBytes() ([]byte, error) {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, tx.ID)
if err != nil {
return nil, err
}
for _, vin := range tx.Vin {
b, err := vin.ToBytes()
if err != nil {
return nil, err
}
err = binary.Write(buff, binary.BigEndian, b)
if err != nil {
return nil, err
}
}
for _, vout := range tx.Vout {
b, err := vout.ToBytes()
if err != nil {
return nil, err
}
err = binary.Write(buff, binary.BigEndian, b)
if err != nil {
return nil, err
}
}
err = binary.Write(buff, binary.BigEndian, tx.Time)
if err != nil {
return nil, err
}
return buff.Bytes(), nil
}
// Sorting of transactions slice
type Transactions []*Transaction
func (c Transactions) Len() int { return len(c) }
func (c Transactions) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c Transactions) Less(i, j int) bool { return c[i].Time < c[j].Time }