-
Notifications
You must be signed in to change notification settings - Fork 124
/
utxo.go
219 lines (192 loc) · 6.62 KB
/
utxo.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package bitcoin
import (
"bytes"
"fmt"
"math/big"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/renproject/multichain/api/utxo"
"github.com/renproject/pack"
)
// Version of Bitcoin transactions supported by the multichain.
const Version int32 = 2
// The TxBuilder is an implementation of a UTXO-compatible transaction builder
// for Bitcoin.
type TxBuilder struct {
params *chaincfg.Params
}
// NewTxBuilder returns a transaction builder that builds UTXO-compatible
// Bitcoin transactions for the given chain configuration (this means that it
// can be used for regnet, testnet, and mainnet, but also for networks that are
// minimally modified forks of the Bitcoin network).
func NewTxBuilder(params *chaincfg.Params) TxBuilder {
return TxBuilder{params: params}
}
// BuildTx returns a Bitcoin transaction that consumes funds from the given
// inputs, and sends them to the given recipients. The difference in the sum
// value of the inputs and the sum value of the recipients is paid as a fee to
// the Bitcoin network. This fee must be calculated independently of this
// function. Outputs produced for recipients will use P2PKH, P2SH, P2WPKH, or
// P2WSH scripts as the pubkey script, based on the format of the recipient
// address.
func (txBuilder TxBuilder) BuildTx(inputs []utxo.Input, recipients []utxo.Recipient) (utxo.Tx, error) {
msgTx := wire.NewMsgTx(Version)
// Inputs
for _, input := range inputs {
hash := chainhash.Hash{}
copy(hash[:], input.Hash)
index := input.Index.Uint32()
msgTx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&hash, index), nil, nil))
}
// Outputs
for _, recipient := range recipients {
addr, err := btcutil.DecodeAddress(string(recipient.To), txBuilder.params)
if err != nil {
return nil, err
}
if !addr.IsForNet(txBuilder.params) {
return nil, fmt.Errorf("addr of a different network")
}
script, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
}
value := recipient.Value.Int().Int64()
if value < 0 {
return nil, fmt.Errorf("expected value >= 0, got value %v", value)
}
msgTx.AddTxOut(wire.NewTxOut(value, script))
}
return &Tx{inputs: inputs, recipients: recipients, msgTx: msgTx, signed: false}, nil
}
// Tx represents a simple Bitcoin transaction that implements the Bitcoin Compat
// API.
type Tx struct {
inputs []utxo.Input
recipients []utxo.Recipient
msgTx *wire.MsgTx
signed bool
}
// Hash returns the transaction hash of the given underlying transaction.
func (tx *Tx) Hash() (pack.Bytes, error) {
txhash := tx.msgTx.TxHash()
return pack.NewBytes(txhash[:]), nil
}
// Inputs returns the UTXO inputs in the underlying transaction.
func (tx *Tx) Inputs() ([]utxo.Input, error) {
return tx.inputs, nil
}
// Outputs returns the UTXO outputs in the underlying transaction.
func (tx *Tx) Outputs() ([]utxo.Output, error) {
hash, err := tx.Hash()
if err != nil {
return nil, fmt.Errorf("bad hash: %v", err)
}
outputs := make([]utxo.Output, len(tx.msgTx.TxOut))
for i := range outputs {
outputs[i].Outpoint = utxo.Outpoint{
Hash: hash,
Index: pack.NewU32(uint32(i)),
}
outputs[i].PubKeyScript = pack.Bytes(tx.msgTx.TxOut[i].PkScript)
if tx.msgTx.TxOut[i].Value < 0 {
return nil, fmt.Errorf("bad output %v: value is less than zero", i)
}
outputs[i].Value = pack.NewU256FromU64(pack.NewU64(uint64(tx.msgTx.TxOut[i].Value)))
}
return outputs, nil
}
// Sighashes returns the digests that must be signed before the transaction
// can be submitted by the client.
func (tx *Tx) Sighashes() ([]pack.Bytes32, error) {
sighashes := make([]pack.Bytes32, len(tx.inputs))
for i, txin := range tx.inputs {
pubKeyScript := txin.PubKeyScript
sigScript := txin.SigScript
value := txin.Value.Int().Int64()
if value < 0 {
return []pack.Bytes32{}, fmt.Errorf("expected value >= 0, got value %v", value)
}
var hash []byte
var err error
if sigScript == nil {
if txscript.IsPayToWitnessPubKeyHash(pubKeyScript) {
hash, err = txscript.CalcWitnessSigHash(pubKeyScript, txscript.NewTxSigHashes(tx.msgTx), txscript.SigHashAll, tx.msgTx, i, value)
} else {
hash, err = txscript.CalcSignatureHash(pubKeyScript, txscript.SigHashAll, tx.msgTx, i)
}
} else {
if txscript.IsPayToWitnessScriptHash(pubKeyScript) {
hash, err = txscript.CalcWitnessSigHash(sigScript, txscript.NewTxSigHashes(tx.msgTx), txscript.SigHashAll, tx.msgTx, i, value)
} else {
hash, err = txscript.CalcSignatureHash(sigScript, txscript.SigHashAll, tx.msgTx, i)
}
}
if err != nil {
return []pack.Bytes32{}, err
}
sighash := [32]byte{}
copy(sighash[:], hash)
sighashes[i] = pack.NewBytes32(sighash)
}
return sighashes, nil
}
// Sign consumes a list of signatures, and adds them to the list of UTXOs in
// the underlying transactions.
func (tx *Tx) Sign(signatures []pack.Bytes65, pubKey pack.Bytes) error {
if tx.signed {
return fmt.Errorf("already signed")
}
if len(signatures) != len(tx.msgTx.TxIn) {
return fmt.Errorf("expected %v signatures, got %v signatures", len(tx.msgTx.TxIn), len(signatures))
}
for i, rsv := range signatures {
var err error
// Decode the signature and the pubkey script.
r := new(big.Int).SetBytes(rsv[:32])
s := new(big.Int).SetBytes(rsv[32:64])
signature := btcec.Signature{
R: r,
S: s,
}
pubKeyScript := tx.inputs[i].Output.PubKeyScript
sigScript := tx.inputs[i].SigScript
// Support segwit.
if sigScript == nil {
if txscript.IsPayToWitnessPubKeyHash(pubKeyScript) || txscript.IsPayToWitnessScriptHash(pubKeyScript) {
tx.msgTx.TxIn[i].Witness = wire.TxWitness([][]byte{append(signature.Serialize(), byte(txscript.SigHashAll)), pubKey})
continue
}
} else {
if txscript.IsPayToWitnessPubKeyHash(sigScript) || txscript.IsPayToWitnessScriptHash(sigScript) {
tx.msgTx.TxIn[i].Witness = wire.TxWitness([][]byte{append(signature.Serialize(), byte(txscript.SigHashAll)), pubKey, sigScript})
continue
}
}
// Support non-segwit
builder := txscript.NewScriptBuilder()
builder.AddData(append(signature.Serialize(), byte(txscript.SigHashAll)))
builder.AddData(pubKey)
if sigScript != nil {
builder.AddData(sigScript)
}
tx.msgTx.TxIn[i].SignatureScript, err = builder.Script()
if err != nil {
return err
}
}
tx.signed = true
return nil
}
// Serialize serializes the UTXO transaction to bytes
func (tx *Tx) Serialize() (pack.Bytes, error) {
buf := new(bytes.Buffer)
if err := tx.msgTx.Serialize(buf); err != nil {
return pack.Bytes{}, err
}
return pack.NewBytes(buf.Bytes()), nil
}