-
Notifications
You must be signed in to change notification settings - Fork 246
/
transaction.go
199 lines (139 loc) · 4.27 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
package core
import (
"bytes"
"encoding/binary"
"errors"
"reflect"
"time"
"github.com/izqui/helpers"
)
type Transaction struct {
Header TransactionHeader
Signature []byte
Payload []byte
}
type TransactionHeader struct {
From []byte
To []byte
Timestamp uint32
PayloadHash []byte
PayloadLength uint32
Nonce uint32
}
// Returns bytes to be sent to the network
func NewTransaction(from, to, payload []byte) *Transaction {
t := Transaction{Header: TransactionHeader{From: from, To: to}, Payload: payload}
t.Header.Timestamp = uint32(time.Now().Unix())
t.Header.PayloadHash = helpers.SHA256(t.Payload)
t.Header.PayloadLength = uint32(len(t.Payload))
return &t
}
func (t *Transaction) Hash() []byte {
headerBytes, _ := t.Header.MarshalBinary()
return helpers.SHA256(headerBytes)
}
func (t *Transaction) Sign(keypair *Keypair) []byte {
s, _ := keypair.Sign(t.Hash())
return s
}
func (t *Transaction) VerifyTransaction(pow []byte) bool {
headerHash := t.Hash()
payloadHash := helpers.SHA256(t.Payload)
return reflect.DeepEqual(payloadHash, t.Header.PayloadHash) && CheckProofOfWork(pow, headerHash) && SignatureVerify(t.Header.From, t.Signature, headerHash)
}
func (t *Transaction) GenerateNonce(prefix []byte) uint32 {
newT := t
for {
if CheckProofOfWork(prefix, newT.Hash()) {
break
}
newT.Header.Nonce++
}
return newT.Header.Nonce
}
func (t *Transaction) MarshalBinary() ([]byte, error) {
headerBytes, _ := t.Header.MarshalBinary()
if len(headerBytes) != TRANSACTION_HEADER_SIZE {
return nil, errors.New("Header marshalling error")
}
return append(append(headerBytes, helpers.FitBytesInto(t.Signature, NETWORK_KEY_SIZE)...), t.Payload...), nil
}
func (t *Transaction) UnmarshalBinary(d []byte) ([]byte, error) {
buf := bytes.NewBuffer(d)
if len(d) < TRANSACTION_HEADER_SIZE+NETWORK_KEY_SIZE {
return nil, errors.New("Insuficient bytes for unmarshalling transaction")
}
header := &TransactionHeader{}
if err := header.UnmarshalBinary(buf.Next(TRANSACTION_HEADER_SIZE)); err != nil {
return nil, err
}
t.Header = *header
t.Signature = helpers.StripByte(buf.Next(NETWORK_KEY_SIZE), 0)
t.Payload = buf.Next(int(t.Header.PayloadLength))
return buf.Next(helpers.MaxInt), nil
}
func (th *TransactionHeader) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
buf.Write(helpers.FitBytesInto(th.From, NETWORK_KEY_SIZE))
buf.Write(helpers.FitBytesInto(th.To, NETWORK_KEY_SIZE))
binary.Write(buf, binary.LittleEndian, th.Timestamp)
buf.Write(helpers.FitBytesInto(th.PayloadHash, 32))
binary.Write(buf, binary.LittleEndian, th.PayloadLength)
binary.Write(buf, binary.LittleEndian, th.Nonce)
return buf.Bytes(), nil
}
func (th *TransactionHeader) UnmarshalBinary(d []byte) error {
buf := bytes.NewBuffer(d)
th.From = helpers.StripByte(buf.Next(NETWORK_KEY_SIZE), 0)
th.To = helpers.StripByte(buf.Next(NETWORK_KEY_SIZE), 0)
binary.Read(bytes.NewBuffer(buf.Next(4)), binary.LittleEndian, &th.Timestamp)
th.PayloadHash = buf.Next(32)
binary.Read(bytes.NewBuffer(buf.Next(4)), binary.LittleEndian, &th.PayloadLength)
binary.Read(bytes.NewBuffer(buf.Next(4)), binary.LittleEndian, &th.Nonce)
return nil
}
type TransactionSlice []Transaction
func (slice TransactionSlice) Len() int {
return len(slice)
}
func (slice TransactionSlice) Exists(tr Transaction) bool {
for _, t := range slice {
if reflect.DeepEqual(t.Signature, tr.Signature) {
return true
}
}
return false
}
func (slice TransactionSlice) AddTransaction(t Transaction) TransactionSlice {
// Inserted sorted by timestamp
for i, tr := range slice {
if tr.Header.Timestamp >= t.Header.Timestamp {
return append(append(slice[:i], t), slice[i:]...)
}
}
return append(slice, t)
}
func (slice *TransactionSlice) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
for _, t := range *slice {
bs, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(bs)
}
return buf.Bytes(), nil
}
func (slice *TransactionSlice) UnmarshalBinary(d []byte) error {
remaining := d
for len(remaining) > TRANSACTION_HEADER_SIZE+NETWORK_KEY_SIZE {
t := new(Transaction)
rem, err := t.UnmarshalBinary(remaining)
if err != nil {
return err
}
(*slice) = append((*slice), *t)
remaining = rem
}
return nil
}