2017-09-02 21:20:47 -05:00
|
|
|
package main
|
|
|
|
|
2017-09-02 21:45:49 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/gob"
|
2017-09-02 22:41:45 -05:00
|
|
|
"encoding/hex"
|
2017-09-03 23:02:24 -05:00
|
|
|
"fmt"
|
2017-09-02 21:45:49 -05:00
|
|
|
"log"
|
2017-09-07 22:06:19 -05:00
|
|
|
"strings"
|
2017-09-02 21:45:49 -05:00
|
|
|
)
|
2017-09-02 21:20:47 -05:00
|
|
|
|
|
|
|
const subsidy = 10
|
|
|
|
|
|
|
|
// Transaction represents a Bitcoin transaction
|
|
|
|
type Transaction struct {
|
2017-09-03 23:32:24 -05:00
|
|
|
ID []byte
|
2017-09-02 21:20:47 -05:00
|
|
|
Vin []TXInput
|
|
|
|
Vout []TXOutput
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsCoinbase checks whether the transaction is coinbase
|
|
|
|
func (tx Transaction) IsCoinbase() bool {
|
2017-09-02 22:41:45 -05:00
|
|
|
return len(tx.Vin) == 1 && len(tx.Vin[0].Txid) == 0 && tx.Vin[0].Vout == -1
|
2017-09-02 21:20:47 -05:00
|
|
|
}
|
|
|
|
|
2017-09-07 22:06:19 -05:00
|
|
|
// String returns a human-readable representation of a transaction
|
|
|
|
func (tx Transaction) String() string {
|
|
|
|
var lines []string
|
|
|
|
|
|
|
|
lines = append(lines, fmt.Sprintf("Transaction %x:", tx.ID))
|
|
|
|
|
|
|
|
for i, input := range tx.Vin {
|
|
|
|
lines = append(lines, fmt.Sprintf(" Input %d:", i))
|
|
|
|
lines = append(lines, fmt.Sprintf(" TXID: %x", input.Txid))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Out: %d", input.Vout))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Script: %s", input.ScriptSig))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, output := range tx.Vout {
|
|
|
|
lines = append(lines, fmt.Sprintf(" Output %d:", i))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Value: %d", output.Value))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Script: %s", output.ScriptPubKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:32:24 -05:00
|
|
|
// SetID sets ID of a transaction
|
|
|
|
func (tx Transaction) SetID() {
|
2017-09-02 21:45:49 -05:00
|
|
|
var encoded bytes.Buffer
|
|
|
|
var hash [32]byte
|
|
|
|
|
|
|
|
enc := gob.NewEncoder(&encoded)
|
|
|
|
err := enc.Encode(tx)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
hash = sha256.Sum256(encoded.Bytes())
|
2017-09-03 23:32:24 -05:00
|
|
|
tx.ID = hash[:]
|
2017-09-02 21:45:49 -05:00
|
|
|
}
|
|
|
|
|
2017-09-02 21:20:47 -05:00
|
|
|
// TXInput represents a transaction input
|
|
|
|
type TXInput struct {
|
2017-09-02 22:41:45 -05:00
|
|
|
Txid []byte
|
2017-09-02 21:20:47 -05:00
|
|
|
Vout int
|
|
|
|
ScriptSig string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TXOutput represents a transaction output
|
|
|
|
type TXOutput struct {
|
|
|
|
Value int
|
|
|
|
ScriptPubKey string
|
|
|
|
}
|
|
|
|
|
2017-09-03 23:26:30 -05:00
|
|
|
// CanUnlockOutputWith checks whether the address initiated the transaction
|
|
|
|
func (in *TXInput) CanUnlockOutputWith(unlockingData string) bool {
|
|
|
|
return in.ScriptSig == unlockingData
|
2017-09-02 22:41:45 -05:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:26:30 -05:00
|
|
|
// CanBeUnlockedWith checks if the output can be unlocked with the provided data
|
|
|
|
func (out *TXOutput) CanBeUnlockedWith(unlockingData string) bool {
|
2017-09-02 21:20:47 -05:00
|
|
|
return out.ScriptPubKey == unlockingData
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCoinbaseTX creates a new coinbase transaction
|
2017-09-02 21:45:49 -05:00
|
|
|
func NewCoinbaseTX(to, data string) *Transaction {
|
|
|
|
if data == "" {
|
2017-09-03 23:02:24 -05:00
|
|
|
data = fmt.Sprintf("Reward to '%s'", to)
|
2017-09-02 21:45:49 -05:00
|
|
|
}
|
|
|
|
|
2017-09-02 22:41:45 -05:00
|
|
|
txin := TXInput{[]byte{}, -1, data}
|
2017-09-02 21:20:47 -05:00
|
|
|
txout := TXOutput{subsidy, to}
|
2017-09-03 23:32:24 -05:00
|
|
|
tx := Transaction{nil, []TXInput{txin}, []TXOutput{txout}}
|
|
|
|
tx.SetID()
|
2017-09-02 21:20:47 -05:00
|
|
|
|
|
|
|
return &tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUTXOTransaction creates a new transaction
|
2017-09-05 09:35:45 -05:00
|
|
|
func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction {
|
2017-09-02 21:20:47 -05:00
|
|
|
var inputs []TXInput
|
|
|
|
var outputs []TXOutput
|
|
|
|
|
2017-09-05 09:35:45 -05:00
|
|
|
acc, validOutputs := bc.FindSpendableOutputs(from, amount)
|
2017-09-02 21:20:47 -05:00
|
|
|
|
2017-09-05 09:35:45 -05:00
|
|
|
if acc < amount {
|
2017-09-02 21:20:47 -05:00
|
|
|
log.Panic("ERROR: Not enough funds")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of inputs
|
|
|
|
for txid, outs := range validOutputs {
|
2017-09-05 09:35:45 -05:00
|
|
|
txID, err := hex.DecodeString(txid)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-02 22:41:45 -05:00
|
|
|
|
2017-09-05 09:35:45 -05:00
|
|
|
for _, out := range outs {
|
2017-09-03 23:02:24 -05:00
|
|
|
input := TXInput{txID, out, from}
|
2017-09-02 21:20:47 -05:00
|
|
|
inputs = append(inputs, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of outputs
|
2017-09-05 09:35:45 -05:00
|
|
|
outputs = append(outputs, TXOutput{amount, to})
|
|
|
|
if acc > amount {
|
|
|
|
outputs = append(outputs, TXOutput{acc - amount, from}) // a change
|
2017-09-02 21:20:47 -05:00
|
|
|
}
|
|
|
|
|
2017-09-03 23:32:24 -05:00
|
|
|
tx := Transaction{nil, inputs, outputs}
|
|
|
|
tx.SetID()
|
2017-09-02 21:20:47 -05:00
|
|
|
|
|
|
|
return &tx
|
|
|
|
}
|