-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
89 lines (70 loc) · 2.24 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
package blockchain
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"fmt"
"log"
)
//reward is the amnount of tokens given to someone that "mines" a new block
const reward = 100
type Transaction struct {
ID []byte
Inputs []TxInput
Outputs []TxOutput
}
//TxOutput represents a transaction in the blockchain
//For Example, I sent you 5 coins. Value would == 5, and it would have my unique PubKey
func (tx *Transaction) SetID() {
var encoded bytes.Buffer
var hash [32]byte
encoder := gob.NewEncoder(&encoded)
err := encoder.Encode(tx)
if err != nil {
log.Panic(err)
}
hash = sha256.Sum256(encoded.Bytes())
tx.ID = hash[:]
}
//CoinbaseTx is the function that will run when someone on a node succesfully "mines" a block. The reward inside as it were.
func CoinbaseTx(toAddress, data string) *Transaction {
if data == "" {
data = fmt.Sprintf("Coins to %s", toAddress)
}
//Since this is the "first" transaction of the block, it has no previous output to reference.
//This means that we initialize it with no ID, and it's OutputIndex is -1
txIn := TxInput{[]byte{}, -1, data}
//txOut will represent the amount of tokens(reward) given to the person(toAddress) that executed CoinbaseTx
txOut := TxOutput{reward, toAddress} // You can see it follows {value, PubKey}
tx := Transaction{nil, []TxInput{txIn}, []TxOutput{txOut}}
return &tx
}
func (tx *Transaction) IsCoinbase() bool {
//This checks a transaction and will only return true if it is a newly minted "coin"
// Aka a Coinbase transaction
return len(tx.Inputs) == 1 && len(tx.Inputs[0].ID) == 0 && tx.Inputs[0].Out == -1
}
func NewTransaction(from, to string, amount int, chain *BlockChain) *Transaction {
var inputs []TxInput
var outputs []TxOutput
acc, validOutputs := chain.FindSpendableOutputs(from, amount)
if acc < amount {
log.Panic("Error: Not enough funds!")
}
for txid, outs := range validOutputs {
txID, err := hex.DecodeString(txid)
Handle(err)
for _, out := range outs {
input := TxInput{txID, out, from}
inputs = append(inputs, input)
}
}
outputs = append(outputs, TxOutput{amount, to})
if acc > amount {
outputs = append(outputs, TxOutput{acc - amount, from})
}
tx := Transaction{nil, inputs, outputs}
tx.SetID()
return &tx
}