-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.go
160 lines (145 loc) · 3.43 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
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
package libbtc
import (
"bytes"
"context"
"encoding/hex"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
type tx struct {
receiveValues []int64
scriptPublicKey []byte
account *account
msgTx *wire.MsgTx
ctx context.Context
}
func (account *account) newTx(ctx context.Context, msgtx *wire.MsgTx) *tx {
return &tx{
msgTx: msgtx,
account: account,
ctx: ctx,
}
}
func (tx *tx) fund(addr btcutil.Address, fee int64) error {
if addr == nil {
var err error
addr, err = tx.account.Address()
if err != nil {
return err
}
}
var value int64
for _, j := range tx.msgTx.TxOut {
value = value + j.Value
}
value = value + fee
balance, err := tx.account.Balance(tx.ctx, addr.EncodeAddress(), 0)
if err != nil {
return err
}
if value > balance {
return NewErrInsufficientBalance(addr.EncodeAddress(), value, balance)
}
utxos, err := tx.account.GetUnspentOutputs(tx.ctx, addr.EncodeAddress(), 1000, 0)
if err != nil {
return err
}
for _, j := range utxos.Outputs {
ScriptPubKey, err := hex.DecodeString(j.ScriptPubKey)
if err != nil {
return err
}
if len(tx.scriptPublicKey) == 0 {
tx.scriptPublicKey = ScriptPubKey
} else {
if bytes.Compare(tx.scriptPublicKey, ScriptPubKey) != 0 {
continue
}
}
if value <= 0 {
break
}
tx.receiveValues = append(tx.receiveValues, j.Amount)
hashBytes, err := hex.DecodeString(j.TransactionHash)
if err != nil {
return err
}
hash, err := chainhash.NewHash(hashBytes)
if err != nil {
return err
}
tx.msgTx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(hash, j.TransactionOutputNumber), []byte{}, [][]byte{}))
value = value - j.Amount
}
if value > 0 {
return ErrMismatchedPubKeys
}
if value < 0 {
P2PKHScript, err := txscript.PayToAddrScript(addr)
if err != nil {
return err
}
tx.msgTx.AddTxOut(wire.NewTxOut(int64(-value), P2PKHScript))
}
return nil
}
func (tx *tx) sign(f func(*txscript.ScriptBuilder), updateTxIn func(*wire.TxIn), contract []byte) error {
var subScript []byte
if contract == nil {
subScript = tx.scriptPublicKey
} else {
subScript = contract
}
serializedPublicKey, err := tx.account.SerializedPublicKey()
if err != nil {
return err
}
for i, txin := range tx.msgTx.TxIn {
if updateTxIn != nil {
updateTxIn(txin)
}
sig, err := txscript.RawTxInSignature(tx.msgTx, i, subScript, txscript.SigHashAll, tx.account.PrivKey)
if err != nil {
return err
}
builder := txscript.NewScriptBuilder()
builder.AddData(sig)
builder.AddData(serializedPublicKey)
if f != nil {
f(builder)
}
if contract != nil {
builder.AddData(contract)
}
sigScript, err := builder.Script()
if err != nil {
return err
}
txin.SignatureScript = sigScript
}
return nil
}
func (tx *tx) verify() error {
for i, receiveValue := range tx.receiveValues {
engine, err := txscript.NewEngine(tx.scriptPublicKey, tx.msgTx, i,
txscript.StandardVerifyFlags, txscript.NewSigCache(10),
txscript.NewTxSigHashes(tx.msgTx), receiveValue)
if err != nil {
return err
}
if err := engine.Execute(); err != nil {
return err
}
}
return nil
}
func (tx *tx) submit() error {
var stxBuffer bytes.Buffer
stxBuffer.Grow(tx.msgTx.SerializeSize())
if err := tx.msgTx.Serialize(&stxBuffer); err != nil {
return err
}
return tx.account.PublishTransaction(tx.ctx, stxBuffer.Bytes())
}