This repository has been archived by the owner on Apr 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
transaction.go
191 lines (161 loc) · 4.94 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
package core
import (
"bytes"
"encoding/binary"
"errors"
"math"
"reflect"
"time"
"github.com/borisding1994/hathcoin/utils"
"github.com/borisding1994/hathcoin/utils/crypto"
)
// Transaction is a transfer of HathCoin value that is broadcast to the network and collected into blocks.
type Transaction struct {
Header TransactionHeader
// Hash == Crypto.SM3Hash(current transaction header)
Hash []byte
// Payload is raw transaction data.
Payload []byte
}
// TransactionHeader defines information about a transaction
type TransactionHeader struct {
// From is origin public key
From []byte
// To is destination public key
To []byte
// Timestamp the transaction was create.
Timestamp uint32
// PayloadHash == Crypto.SM3Hash(current transaction payload)
PayloadHash []byte
// PayloadLength == len(current transaction payload)
PayloadLength uint32
// Nonce Proof of work.
Nonce uint32
}
// TransactionSlice
type TransactionSlice []Transaction
// 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 = []byte(crypto.SM3HashByte(t.Payload))
t.Header.PayloadLength = uint32(len(t.Payload))
return &t
}
// CalcHash can calculate Transaction Hash
func (t *Transaction) CalcHash() []byte {
h, _ := t.Header.MarshalBinary()
return crypto.SM3HashByte(h)
}
// Sign Transaction
func (t *Transaction) Sign(keypair *crypto.Keypair) []byte {
s, err := keypair.Sign(t.CalcHash())
if err != nil {
utils.Logger.Error("Sign Block Error", err)
}
return s
}
// VerifyTransaction by Hash
func (t *Transaction) VerifyTransaction(pow []byte) bool {
headerHash := t.CalcHash()
payloadHash := crypto.SM3HashByte(t.Payload)
return reflect.DeepEqual(payloadHash, t.Header.PayloadHash) &&
CheckProofOfWork(pow, headerHash) &&
crypto.VerifySign(t.Header.From, t.Hash, headerHash)
}
// GenerateNonce to TransactionHeader
func (t *Transaction) GenerateNonce(prefix []byte) uint32 {
newT := t
for {
if CheckProofOfWork(prefix, newT.CalcHash()) {
break
}
newT.Header.Nonce++
}
return newT.Header.Nonce
}
func (t *Transaction) MarshalBinary() ([]byte, error) {
headerBytes, _ := t.Header.MarshalBinary()
if len(headerBytes) != TransactionHeaderSize {
return nil, errors.New("Header marshalling error")
}
return append(append(headerBytes, utils.FitBytesInto(t.Hash, Sm2PublicKeySize)...), t.Payload...), nil
}
func (t *Transaction) UnmarshalBinary(d []byte) ([]byte, error) {
buf := bytes.NewBuffer(d)
if len(d) < TransactionHeaderSize+Sm2PublicKeySize {
return nil, errors.New("Insuficient bytes for unmarshalling transaction")
}
h := &TransactionHeader{}
if err := h.UnmarshalBinary(buf.Next(TransactionHeaderSize)); err != nil {
return nil, err
}
t.Header = *h
t.Hash = utils.StripByte(buf.Next(Sm2PublicKeySize), 0)
t.Payload = buf.Next(int(t.Header.PayloadLength))
return buf.Next(math.MaxInt32), nil
}
func (th *TransactionHeader) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
buf.Write(utils.FitBytesInto(th.From, Sm2PublicKeySize))
buf.Write(utils.FitBytesInto(th.To, Sm2PublicKeySize))
binary.Write(buf, binary.LittleEndian, th.Timestamp)
buf.Write(utils.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 = utils.StripByte(buf.Next(Sm2PublicKeySize), 0)
th.To = utils.StripByte(buf.Next(Sm2PublicKeySize), 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
}
func (slice TransactionSlice) Len() int {
return len(slice)
}
func (slice TransactionSlice) Exists(tr Transaction) bool {
for _, t := range slice {
if reflect.DeepEqual(t.Hash, tr.Hash) {
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) > TransactionHeaderSize+Sm2PublicKeySize {
t := new(Transaction)
rem, err := t.UnmarshalBinary(remaining)
if err != nil {
return err
}
*slice = append(*slice, *t)
remaining = rem
}
return nil
}