-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtranasction.go
204 lines (139 loc) · 4.23 KB
/
tranasction.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package bitcoinwallet
import (
"bytes"
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/ranjbar-dev/bitcoin-wallet/models"
)
type Transaction struct {
hex string
inputs []models.TransactionInput
outputs []models.TransactionOutput
}
// NewTransaction creates a new transaction with the given inputs and outputs.
func NewTransaction(inputs []models.TransactionInput, outputs []models.TransactionOutput) *Transaction {
return &Transaction{
inputs: inputs,
outputs: outputs,
}
}
// Inputs returns copy of transaction inputs
func (t *Transaction) Inputs() []models.TransactionInput {
return t.inputs
}
// Outputs returns copy of transaction outputs
func (t *Transaction) Outputs() []models.TransactionOutput {
return t.outputs
}
// Hex
func (t *Transaction) Hex() string {
return t.hex
}
// Size returns the size of the transaction in bytes.
func (t *Transaction) Size() int {
transactionSize := 10
eachInputSizeInByte := 150
eachOutputSizeInByte := 32
for i := 0; i < len(t.inputs); i++ {
transactionSize += eachInputSizeInByte
}
for i := 0; i < len(t.outputs); i++ {
transactionSize += eachOutputSizeInByte
}
return transactionSize
}
// Fee returns the fee of the transaction in satoshi.
func (t *Transaction) Fee() int64 {
var inputsVal int64
var outputsVal int64
for _, input := range t.inputs {
inputsVal += input.Value
}
for _, output := range t.outputs {
outputsVal += output.Value
}
return inputsVal - outputsVal
}
// SignAndSerialize signs the transaction inputs and returns the transaction hex.
func (t *Transaction) SignAndSerialize() error {
var inputsVal int64
var outputsVal int64
for _, input := range t.inputs {
inputsVal += input.Value
}
for _, output := range t.outputs {
outputsVal += output.Value
}
tx := wire.NewMsgTx(2)
for _, utxo := range t.inputs {
hash, err := chainhash.NewHashFromStr(utxo.TxId)
if err != nil {
return err
}
txIn := wire.NewTxIn(wire.NewOutPoint(hash, uint32(utxo.Index)), nil, [][]byte{})
txIn.Sequence = txIn.Sequence - 2
tx.AddTxIn(txIn)
}
for _, output := range t.outputs {
toAddr, err := btcutil.DecodeAddress(output.Address, config.Chaincfg)
if err != nil {
return errors.New("DecodeAddress destAddrStr err " + err.Error())
}
toAddrByte, err := txscript.PayToAddrScript(toAddr)
if err != nil {
return errors.New("toAddr PayToAddrScript err " + err.Error())
}
tx.AddTxOut(wire.NewTxOut(output.Value, toAddrByte))
}
tx.LockTime = 0
signerMap := make(map[wire.OutPoint]*wire.TxOut)
for _, in := range tx.TxIn {
signerMap[in.PreviousOutPoint] = &wire.TxOut{}
}
sigHashes := txscript.NewTxSigHashes(tx, txscript.NewMultiPrevOutFetcher(signerMap))
// sign
for index, utxo := range t.inputs {
fromAddr, err := btcutil.DecodeAddress(utxo.Address, config.Chaincfg)
if err != nil {
return errors.New("DecodeAddress fromAddr err " + err.Error())
}
fromAddrScriptByte, err := txscript.PayToAddrScript(fromAddr)
if err != nil {
return errors.New("fromAddrScriptByte PayToAddrScript err " + err.Error())
}
secpPrivKey := secp256k1.PrivKeyFromBytes(utxo.PrivateKey.D.Bytes())
signature, err := txscript.WitnessSignature(tx, sigHashes, index, int64(utxo.Value), fromAddrScriptByte, txscript.SigHashAll, secpPrivKey, true)
if err != nil {
return errors.New("WitnessSignature err " + err.Error())
}
tx.TxIn[index].Witness = signature
}
var signedTx bytes.Buffer
err := tx.Serialize(&signedTx)
if err != nil {
return err
}
t.hex = hex.EncodeToString(signedTx.Bytes())
return nil
}
// Broadcast broadcasts the transaction hex to the blockchain and returns the transaction ID.
func (t *Transaction) Broadcast() (string, error) {
res, err := config.Explorer.BroadcastTransaction(t.hex)
if err != nil {
return "", err
}
return res, nil
}
// FetchTransactionByTxID fetches a transaction by its transaction ID.
func FetchTransactionByTxID(txID string) (models.Transaction, error) {
tx, err := config.Explorer.GetTransactionByTxID(txID)
if err != nil {
return models.Transaction{}, err
}
return tx, nil
}