/
Transaction.go
101 lines (96 loc) · 2.64 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
package BLC
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"fmt"
"log"
)
//交易管理文件
//定义一个交易基本结构
type Transaction struct{
//交易哈希(标识)
TxHash []byte
//输入列表
Vins []*TxInput
//输出列表
Vouts []*TxOutput
}
//实现coinbase交易
func NewCoinbaseTransaction(address string) *Transaction {
//输入
//coinbase
//txHash:nil
//vout:-1(为了对是否是coinbase交易进行判断)
//ScriptSig:系统奖励
txInput:=&TxInput{[]byte{},-1,nil,nil}
//输出:
//value:
//address:
// txOuput:=&TxOutput{10,StringToHash160(address)}
txOutput:=NewTxOutput(10,address)
//输入输出组装交易
txCoinbase:=&Transaction{nil,[]*TxInput{txInput},[]*TxOutput{txOutput}}
//交易哈希生成
txCoinbase.HashTransaction()
return txCoinbase
}
//生成交易哈希(交易序列化)
func (tx *Transaction) HashTransaction() {
var result bytes.Buffer
//设置编码对象
encoder:=gob.NewEncoder(&result)
if err:=encoder.Encode(tx); err!=nil{
log.Panicf("tx Hash encoded failed %v\n",err)
}
//生成哈希值
hash:=sha256.Sum256(result.Bytes())
tx.TxHash=hash[:]
}
//生成普通转账交易
func NewSimpleTransaction(from string,to string,amount int,bc*BlockChain,txs []*Transaction) *Transaction {
var txInputs []*TxInput //输入列表
var txOutputs []*TxOutput //输出列表
//调用可话费UTXO函数
money,spendableUTXODic:=bc.FindSpendableUTXO(from,amount,txs)
fmt.Printf("money : %v\n",money)
//获取钱包集合对象
wallets:=NewWallets()
//查找对应的钱包结构
wallet:=wallets.Wallets[from]
//输入
for txHash,indexArray:=range spendableUTXODic{
txHashBytes,err:=hex.DecodeString(txHash)
if nil!=err{
log.Panicf("decode string to []byte failed! %v\n",err)
}
//遍历索引列表
for _,index:=range indexArray{
txInput:=&TxInput{txHashBytes,index,nil,wallet.PublicKey}
txInputs=append(txInputs,txInput)
}
}
/*txInput:=&TxInput{[]byte("6f92e00377b91ddb1660fe222eb71" +
"d24caced56a737a986bf807e6cd885e22f7"),0,from,}
txInputs=append(txInputs,txInput)*/
//输出(转账源)
//txOutput:=&TxOutput{amount,to}
txOutput:=NewTxOutput(amount,to)
txOutputs=append(txOutputs,txOutput)
//输出(找零)
if money > amount{
//txOutput=&TxOutput{money-amount,from}
txOutput=NewTxOutput(money-amount,from)
txOutputs=append(txOutputs,txOutput)
}else{
log.Panicf("余额不足...\n")
}
tx:=Transaction{nil,txInputs,txOutputs}
tx.HashTransaction()
return &tx
}
//判断指定的交易是否是一个coinbase交易
func (tx *Transaction)IsCoinbaseTransaction() bool {
return tx.Vins[0].Vout==-1&&len(tx.Vins[0].TxHash)==0
}