2017-09-02 21:20:47 -05:00
|
|
|
package main
|
|
|
|
|
2017-09-02 21:45:49 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-09-08 09:29:34 -05:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
2017-09-02 21:45:49 -05:00
|
|
|
"crypto/sha256"
|
2017-09-08 09:29:34 -05:00
|
|
|
"math/big"
|
2017-09-07 23:31:34 -05:00
|
|
|
|
2017-09-02 21:45:49 -05:00
|
|
|
"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-10 00:02:46 -05:00
|
|
|
// Serialize returns a serialized Transaction
|
|
|
|
func (tx Transaction) Serialize() []byte {
|
|
|
|
var encoded bytes.Buffer
|
|
|
|
|
|
|
|
enc := gob.NewEncoder(&encoded)
|
|
|
|
err := enc.Encode(tx)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoded.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the hash of the Transaction
|
|
|
|
func (tx *Transaction) Hash() []byte {
|
|
|
|
var hash [32]byte
|
|
|
|
|
|
|
|
txCopy := *tx
|
|
|
|
txCopy.ID = []byte{}
|
|
|
|
|
|
|
|
hash = sha256.Sum256(txCopy.Serialize())
|
|
|
|
|
|
|
|
return hash[:]
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:29:34 -05:00
|
|
|
// Sign signs each input of a Transaction
|
2017-09-09 22:34:39 -05:00
|
|
|
func (tx *Transaction) Sign(privKey ecdsa.PrivateKey, prevTXs map[string]Transaction) {
|
2017-09-08 09:29:34 -05:00
|
|
|
if tx.IsCoinbase() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vin := range tx.Vin {
|
2017-09-09 22:34:39 -05:00
|
|
|
if prevTXs[hex.EncodeToString(vin.Txid)].ID == nil {
|
2017-09-08 09:29:34 -05:00
|
|
|
log.Panic("ERROR: Previous transaction is not correct")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txCopy := tx.TrimmedCopy()
|
|
|
|
|
|
|
|
for inID, vin := range txCopy.Vin {
|
2017-09-09 22:34:39 -05:00
|
|
|
prevTx := prevTXs[hex.EncodeToString(vin.Txid)]
|
2017-09-10 02:11:21 -05:00
|
|
|
txCopy.Vin[inID].Signature = nil
|
2017-09-10 02:05:23 -05:00
|
|
|
txCopy.Vin[inID].PubKey = prevTx.Vout[vin.Vout].PubKeyHash
|
2017-09-10 01:34:47 -05:00
|
|
|
txCopy.ID = txCopy.Hash()
|
2017-09-10 02:11:21 -05:00
|
|
|
txCopy.Vin[inID].PubKey = nil
|
2017-09-08 09:29:34 -05:00
|
|
|
|
|
|
|
r, s, err := ecdsa.Sign(rand.Reader, &privKey, txCopy.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
signature := append(r.Bytes(), s.Bytes()...)
|
|
|
|
|
2017-09-10 02:05:23 -05:00
|
|
|
tx.Vin[inID].Signature = signature
|
2017-09-08 09:29:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 22:54:58 -05:00
|
|
|
// String returns a human-readable representation of a transaction
|
|
|
|
func (tx Transaction) String() string {
|
|
|
|
var lines []string
|
|
|
|
|
2017-09-10 02:31:01 -05:00
|
|
|
lines = append(lines, fmt.Sprintf("--- Transaction %x:", tx.ID))
|
2017-09-09 22:54:58 -05:00
|
|
|
|
|
|
|
for i, input := range tx.Vin {
|
|
|
|
|
2017-09-10 02:31:01 -05:00
|
|
|
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(" Signature: %x", input.Signature))
|
|
|
|
lines = append(lines, fmt.Sprintf(" PubKey: %x", input.PubKey))
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, output := range tx.Vout {
|
2017-09-10 02:31:01 -05:00
|
|
|
lines = append(lines, fmt.Sprintf(" Output %d:", i))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Value: %d", output.Value))
|
|
|
|
lines = append(lines, fmt.Sprintf(" Script: %x", output.PubKeyHash))
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrimmedCopy creates a trimmed copy of Transaction to be used in signing
|
|
|
|
func (tx *Transaction) TrimmedCopy() Transaction {
|
|
|
|
var inputs []TXInput
|
|
|
|
var outputs []TXOutput
|
|
|
|
|
|
|
|
for _, vin := range tx.Vin {
|
2017-09-10 02:11:21 -05:00
|
|
|
inputs = append(inputs, TXInput{vin.Txid, vin.Vout, nil, nil})
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, vout := range tx.Vout {
|
2017-09-10 02:05:23 -05:00
|
|
|
outputs = append(outputs, TXOutput{vout.Value, vout.PubKeyHash})
|
2017-09-09 22:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
txCopy := Transaction{tx.ID, inputs, outputs}
|
|
|
|
|
|
|
|
return txCopy
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:29:34 -05:00
|
|
|
// Verify verifies signatures of Transaction inputs
|
2017-09-09 22:34:39 -05:00
|
|
|
func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool {
|
2017-09-08 09:29:34 -05:00
|
|
|
if tx.IsCoinbase() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, vin := range tx.Vin {
|
2017-09-09 22:34:39 -05:00
|
|
|
if prevTXs[hex.EncodeToString(vin.Txid)].ID == nil {
|
2017-09-08 09:29:34 -05:00
|
|
|
log.Panic("ERROR: Previous transaction is not correct")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txCopy := tx.TrimmedCopy()
|
|
|
|
curve := elliptic.P256()
|
|
|
|
|
|
|
|
for inID, vin := range tx.Vin {
|
2017-09-09 22:34:39 -05:00
|
|
|
prevTx := prevTXs[hex.EncodeToString(vin.Txid)]
|
2017-09-12 09:09:13 -05:00
|
|
|
txCopy.Vin[inID].Signature = nil
|
2017-09-10 02:05:23 -05:00
|
|
|
txCopy.Vin[inID].PubKey = prevTx.Vout[vin.Vout].PubKeyHash
|
2017-09-10 01:34:47 -05:00
|
|
|
txCopy.ID = txCopy.Hash()
|
2017-09-12 09:10:18 -05:00
|
|
|
txCopy.Vin[inID].PubKey = nil
|
2017-09-08 09:29:34 -05:00
|
|
|
|
|
|
|
r := big.Int{}
|
|
|
|
s := big.Int{}
|
2017-09-10 02:05:23 -05:00
|
|
|
sigLen := len(vin.Signature)
|
|
|
|
r.SetBytes(vin.Signature[:(sigLen / 2)])
|
|
|
|
s.SetBytes(vin.Signature[(sigLen / 2):])
|
2017-09-08 09:29:34 -05:00
|
|
|
|
|
|
|
x := big.Int{}
|
|
|
|
y := big.Int{}
|
2017-09-10 02:05:23 -05:00
|
|
|
keyLen := len(vin.PubKey)
|
|
|
|
x.SetBytes(vin.PubKey[:(keyLen / 2)])
|
|
|
|
y.SetBytes(vin.PubKey[(keyLen / 2):])
|
2017-09-08 09:29:34 -05:00
|
|
|
|
|
|
|
rawPubKey := ecdsa.PublicKey{curve, &x, &y}
|
|
|
|
if ecdsa.Verify(&rawPubKey, txCopy.ID, &r, &s) == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-02 21:20:47 -05:00
|
|
|
// NewCoinbaseTX creates a new coinbase transaction
|
2017-09-02 21:45:49 -05:00
|
|
|
func NewCoinbaseTX(to, data string) *Transaction {
|
|
|
|
if data == "" {
|
2017-09-19 03:47:03 -05:00
|
|
|
randData := make([]byte, 20)
|
|
|
|
_, err := rand.Read(randData)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data = fmt.Sprintf("%x", randData)
|
2017-09-02 21:45:49 -05:00
|
|
|
}
|
|
|
|
|
2017-09-10 02:11:21 -05:00
|
|
|
txin := TXInput{[]byte{}, -1, nil, []byte(data)}
|
2017-09-07 23:31:34 -05:00
|
|
|
txout := NewTXOutput(subsidy, to)
|
|
|
|
tx := Transaction{nil, []TXInput{txin}, []TXOutput{*txout}}
|
2017-09-10 01:34:47 -05:00
|
|
|
tx.ID = tx.Hash()
|
2017-09-02 21:20:47 -05:00
|
|
|
|
|
|
|
return &tx
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUTXOTransaction creates a new transaction
|
2017-09-30 22:48:51 -05:00
|
|
|
func NewUTXOTransaction(wallet *Wallet, to string, amount int, UTXOSet *UTXOSet) *Transaction {
|
2017-09-02 21:20:47 -05:00
|
|
|
var inputs []TXInput
|
|
|
|
var outputs []TXOutput
|
|
|
|
|
2017-09-07 23:31:34 -05:00
|
|
|
pubKeyHash := HashPubKey(wallet.PublicKey)
|
2017-09-16 23:49:59 -05:00
|
|
|
acc, validOutputs := UTXOSet.FindSpendableOutputs(pubKeyHash, 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-10 02:11:21 -05:00
|
|
|
input := TXInput{txID, out, nil, wallet.PublicKey}
|
2017-09-02 21:20:47 -05:00
|
|
|
inputs = append(inputs, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of outputs
|
2017-09-30 22:48:51 -05:00
|
|
|
from := fmt.Sprintf("%s", wallet.GetAddress())
|
2017-09-07 23:31:34 -05:00
|
|
|
outputs = append(outputs, *NewTXOutput(amount, to))
|
2017-09-05 09:35:45 -05:00
|
|
|
if acc > amount {
|
2017-09-07 23:31:34 -05:00
|
|
|
outputs = append(outputs, *NewTXOutput(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}
|
2017-09-10 01:34:47 -05:00
|
|
|
tx.ID = tx.Hash()
|
2017-09-16 23:49:59 -05:00
|
|
|
UTXOSet.Blockchain.SignTransaction(&tx, wallet.PrivateKey)
|
2017-09-02 21:20:47 -05:00
|
|
|
|
|
|
|
return &tx
|
|
|
|
}
|