-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_manager.go
142 lines (114 loc) · 2.95 KB
/
transaction_manager.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
package kcoin
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"errors"
)
type TransactionManager struct {
tx *Transaction
}
func NewTransactionManager(transaction *Transaction) *TransactionManager {
return &TransactionManager{
tx: transaction,
}
}
func (tManager *TransactionManager) Sign(prKey ecdsa.PrivateKey) {
if tManager.tx.IsCoinbase() {
return
}
txCopy := tManager.trimmedCopy()
for i := 0; i < len(txCopy.Inputs); i++ {
r, s, err := ecdsa.Sign(rand.Reader, &prKey, txCopy.ID)
panicIfErrNotNil(err)
tManager.tx.Inputs[i].XYAppendedSignature = append(r.Bytes(), s.Bytes()...)
}
}
func (tManager *TransactionManager) Verify(blockchain *Blockchain) bool {
for _, input := range tManager.tx.Inputs {
if tManager.tx.IsCoinbase() || !isInputValid(input, blockchain) {
return false
}
txCopy := tManager.trimmedCopy()
if bytes.Compare(tManager.tx.ID, txCopy.ID) != 0 {
return false
}
r, s := input.GetSignature()
if !ecdsa.Verify(input.GetPublickKey(), tManager.tx.ID, r, s) {
return false
}
}
return true
}
func (tManager *TransactionManager) trimmedCopy() Transaction {
var inputs []TransactionInput
var outputs []TransactionOutput
for _, input := range tManager.tx.Inputs {
inputs = append(inputs, TransactionInput{
TxID: input.TxID,
OutputIndex: input.OutputIndex,
XYAppendedPubKey: input.XYAppendedPubKey,
XYAppendedSignature: nil,
})
}
for _, output := range tManager.tx.Outputs {
outputs = append(outputs, TransactionOutput{
Value: output.Value,
PubKeyHash: output.PubKeyHash,
})
}
txCopy := Transaction{
Inputs: inputs,
Outputs: outputs,
}
txCopy.SetID()
return txCopy
}
func isInputValid(input TransactionInput, blockchain *Blockchain) bool {
prevTx, err := findTransactionByID(input.TxID, blockchain)
if err != nil {
return false
}
outputPubKey := prevTx.Outputs[input.OutputIndex].PubKeyHash
inputPubKeyHash := hashPubKey(*input.GetPublickKey())
if bytes.Compare(outputPubKey, inputPubKeyHash) != 0 {
return false
}
if isOutputSpent(input.TxID, input.OutputIndex, blockchain) {
return false
}
return true
}
func findTransactionByID(txID []byte, blockchain *Blockchain) (Transaction, error) {
blockchainIterator := blockchain.Iterator()
for {
block := blockchainIterator.Next()
for _, tx := range block.Transactions {
if bytes.Compare(txID, tx.ID) == 0 {
return *tx, nil
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
return Transaction{}, errors.New("Can't find transaction")
}
func isOutputSpent(txID []byte, outputIndex int, blockchain *Blockchain) bool {
blockchainIterator := blockchain.Iterator()
for {
block := blockchainIterator.Next()
for _, tx := range block.Transactions {
for _, input := range tx.Inputs {
if bytes.Compare(input.TxID, txID) == 0 &&
input.OutputIndex == outputIndex {
return true
}
}
}
if len(block.PrevBlockHash) == 0 {
break
}
}
return false
}