This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
/
transaction.go
279 lines (203 loc) · 5.09 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
package blockchain
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"encoding/pem"
"math/big"
"time"
"github.com/vmihailenco/msgpack"
)
type TxIn struct {
PrevHash []byte
PrevIdx int
}
type TxOut struct {
Value int
Address []byte
}
type Stamp struct {
R []byte
S []byte
Pub []byte
Hash []byte
Timestamp int64
}
type Transaction struct {
Ins []TxIn
Outs []TxOut
Stamp Stamp
}
func (this *Transaction) Verify(bc *Blockchain) bool {
r := this.Stamp.R
s := this.Stamp.S
txHash := this.Stamp.Hash
this.Stamp.R = []byte{}
this.Stamp.S = []byte{}
this.Stamp.Hash = []byte{}
hash, err := msgpack.Marshal(this)
if err != nil {
bc.logger.Error("Tx verify: Cannot marshal the tx")
return false
}
newHash := NewHash(hash)
this.Stamp.Hash = txHash
if compare(newHash, this.Stamp.Hash) != 0 {
bc.logger.Error("Tx verify: Hash dont match", newHash)
return false
}
blockPub, _ := pem.Decode(this.Stamp.Pub)
if blockPub == nil {
bc.logger.Error("Tx verify: Cannot decode pub signature from tx",
string(this.Stamp.Pub))
return false
}
// verify timestamp
x509EncodedPub := blockPub.Bytes
genericPublicKey, _ := x509.ParsePKIXPublicKey(x509EncodedPub)
publicKey := genericPublicKey.(*ecdsa.PublicKey)
var r_ big.Int
// var r2 *big.Int
r_.SetBytes(r)
var s_ big.Int
s_.SetBytes(s)
if !ecdsa.Verify(publicKey, newHash, &r_, &s_) {
bc.logger.Error("Tx verify: Signatures does not match")
return false
}
this.Stamp.R = r
this.Stamp.S = s
// lets assume this will work any time
if len(this.Ins) == 0 && len(this.Outs) == 1 {
if this.Outs[0].Value != 100 {
bc.logger.Error("Tx verify: Bad coinbase amount")
return false
}
return true
}
insTotal := 0
for _, in := range this.Ins {
prevUnspentOut := bc.getCorrespondingOutTx(this.Stamp.Pub, &in)
if prevUnspentOut == nil {
bc.logger.Error("Tx verify: Cannot find corresponding OutTx for given In")
return false
}
insTotal += prevUnspentOut.Out.Value
}
outsTotal := 0
for _, out := range this.Outs {
outsTotal += out.Value
}
if outsTotal > insTotal {
bc.logger.Error("Tx verify: Outs total amount exceeds in amount")
return false
}
return true
}
func NewTransaction(value int, dest []byte, bc *Blockchain) *Transaction {
outs := bc.GetEnoughOwnUnspentOut(value)
insRes, outRes := bc.GetInOutFromUnspent(value, dest, outs)
if len(outs) == 0 {
bc.logger.Warning("Cannot create transaction: no outs")
return nil
}
transac := &Transaction{
Stamp: Stamp{
Pub: bc.wallets["main.key"].pub,
Timestamp: time.Now().Unix(),
Hash: []byte{},
R: []byte{},
S: []byte{},
},
Ins: insRes,
Outs: outRes,
}
hash, err := msgpack.Marshal(transac)
if err != nil {
bc.logger.Warning("Cannot marshal the transaction", err)
return nil
}
newHash := NewHash(hash)
transac.Stamp.Hash = newHash
r, s, err := ecdsa.Sign(rand.Reader, bc.wallets["main.key"].key, newHash)
if err != nil {
bc.logger.Warning("Cannot create transaction: Signature error", err)
return nil
}
transac.Stamp.R = r.Bytes()
transac.Stamp.S = s.Bytes()
return transac
}
func NewCoinBaseTransaction(bc *Blockchain) *Transaction {
transac := &Transaction{
Stamp: Stamp{
Pub: bc.wallets["main.key"].pub,
Timestamp: time.Now().Unix(),
Hash: []byte{},
R: []byte{},
S: []byte{},
},
Ins: []TxIn{},
Outs: []TxOut{TxOut{
Value: 100,
Address: []byte(SanitizePubKey(bc.wallets["main.key"].pub)),
}},
}
hash, err := msgpack.Marshal(transac)
if err != nil {
bc.logger.Warning("Cannot marshal the transaction", err)
return nil
}
newHash := NewHash(hash)
transac.Stamp.Hash = newHash
r, s, err := ecdsa.Sign(rand.Reader, bc.wallets["main.key"].key, newHash)
if err != nil {
return nil
}
transac.Stamp.R = r.Bytes()
transac.Stamp.S = s.Bytes()
return transac
}
func (this *Blockchain) RemovePendingTransaction(insTx []Transaction) {
for _, inTx := range insTx {
inTxHash := inTx.Stamp.Hash
idx := -1
for i, tx := range this.pendingTransactions {
if compare(inTxHash, tx.Stamp.Hash) == 0 {
idx = i
break
}
}
if idx > -1 {
this.pendingTransactions = append(this.pendingTransactions[:idx], this.pendingTransactions[idx+1:]...)
}
}
}
func (this *Blockchain) AddTransationToWaiting(tx *Transaction) bool {
if !tx.Verify(this) || this.hasPending(tx) {
this.logger.Warning("Cannot add transaction to waiting")
return false
}
if HasDoubleSpend(append(this.pendingTransactions, *tx)) {
this.logger.Warning("Cannot add transaction to waiting: Has double spend")
return false
}
outs := []*UnspentTxOut{}
for _, in := range tx.Ins {
out := this.getCorrespondingOutTx(tx.Stamp.Pub, &in)
outs = append(outs, out)
if out == nil {
this.logger.Warning("Cannot find corresponding out")
return false
}
if out.IsTargeted {
this.logger.Warning("Got transaction with double spending")
return false
}
}
for _, out := range outs {
out.IsTargeted = true
}
this.pendingTransactions = append(this.pendingTransactions, *tx)
return true
}