/
transaction.go
55 lines (43 loc) · 1.19 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
package account
// Should CreateTransaction be called from Mempool?
import (
"crypto/sha256"
"errors"
"fmt"
"hash"
)
type Transaction struct {
TransactionID hash.Hash
SetOfOperations []Operation
}
func (bch Blockchain) CreateTransaction(SetOfOperations []Operation) (Transaction, error) {
var output Transaction
var bytes []byte
for _, op := range SetOfOperations {
if bch.Verify(op) != true {
return output, errors.New("incorrect operation")
}
bytes = append(bytes, op.ToByte()...)
}
txHash := sha256.New()
txHash.Write(bytes)
output.TransactionID = txHash
output.SetOfOperations = SetOfOperations
return output, nil
}
func (tx Transaction) ToByte() ([]byte, error) {
var output []byte
output = append(output, tx.TransactionID.Sum(nil)...)
for _, el := range tx.SetOfOperations {
output = append(output, el.ToByte()...)
}
return output, nil
}
func (tx Transaction) String() string {
var str string
str += fmt.Sprintf("TransactionID: %x", tx.TransactionID.Sum(nil)) + "\n"
for _, el := range tx.SetOfOperations {
str += el.String() + "\n"
}
return str
}