-
Notifications
You must be signed in to change notification settings - Fork 128
/
swap.go
263 lines (233 loc) · 8.04 KB
/
swap.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2021, The Decred developers
// See LICENSE for details.
package swap
import (
"fmt"
"strings"
"time"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
// AtomicSwapContractPushes models the data pushes of an atomic swap contract.
type AtomicSwapContractPushes struct {
ContractAddress btcutil.Address `json:"contract_address"`
RecipientAddress btcutil.Address `json:"recipient_address"`
RefundAddress btcutil.Address `json:"refund_address"`
Locktime int64 `json:"locktime"`
SecretHash [32]byte `json:"secret_hash"`
FormattedLocktime string `json:"formatted_locktime"`
}
func ExtractSwapDataFromWitness(wit wire.TxWitness, params *chaincfg.Params) (*AtomicSwapContractPushes, []byte, []byte, bool, error) {
var contract, secret []byte
var refund bool
switch len(wit) {
case 5: // maybe redeem
if len(wit[3]) != 1 || wit[3][0] == txscript.OP_FALSE {
return nil, nil, nil, refund, nil
}
secret = wit[2]
contract = wit[4]
case 4: // maybe refund
refund = true
if len(wit[2]) != 0 {
return nil, nil, nil, refund, nil
} // allow a single zero byte?
contract = wit[3]
default:
return nil, nil, nil, false, nil
}
// validate the contract script by attempting to parse it for contract info.
contractData, err := ParseAtomicSwapContract(contract, params)
if err != nil {
return nil, nil, nil, false, err
}
if contractData == nil {
return nil, nil, nil, false, nil // not a contract script
}
return contractData, contract, secret, refund, nil
}
// ParseAtomicSwapContract checks if the provided script is an atomic swap
// contact and returns the data pushes of the contract.
func ParseAtomicSwapContract(script []byte, params *chaincfg.Params) (*AtomicSwapContractPushes, error) {
// validate the contract by calling txscript.ExtractAtomicSwapDataPushes
contractDataPushes, _ := txscript.ExtractAtomicSwapDataPushes(0, script)
if contractDataPushes == nil {
return nil, nil
}
contractP2SH, err := btcutil.NewAddressScriptHash(script, params)
if err != nil {
return nil, fmt.Errorf("contract script to p2sh address error: %v", err)
}
recipientAddr, err := btcutil.NewAddressPubKeyHash(contractDataPushes.RecipientHash160[:], params)
if err != nil {
return nil, fmt.Errorf("error parsing swap recipient address: %v", err)
}
refundAddr, err := btcutil.NewAddressPubKeyHash(contractDataPushes.RefundHash160[:], params)
if err != nil {
return nil, fmt.Errorf("error parsing swap refund address: %v", err)
}
var formattedLockTime string
if contractDataPushes.LockTime >= int64(txscript.LockTimeThreshold) {
formattedLockTime = time.Unix(contractDataPushes.LockTime, 0).UTC().Format("2006-01-02 15:04:05 (MST)")
} else {
formattedLockTime = fmt.Sprintf("block %v", contractDataPushes.LockTime)
}
return &AtomicSwapContractPushes{
ContractAddress: contractP2SH,
RecipientAddress: recipientAddr,
RefundAddress: refundAddr,
Locktime: contractDataPushes.LockTime,
SecretHash: contractDataPushes.SecretHash,
FormattedLocktime: formattedLockTime,
}, nil
}
// OutputSpender describes a transaction input that spends an output by
// specifying the spending transaction and the index of the spending input.
type OutputSpender struct {
Tx *btcjson.TxRawResult
InputIndex uint32
}
type OutputSpenderTxOut struct {
Tx *wire.MsgTx
Vin uint32
}
type AtomicSwapData struct {
ContractTx *chainhash.Hash
ContractVout uint32
SpendTx *chainhash.Hash
SpendVin uint32
Value int64
ContractAddress string
RecipientAddress string
RefundAddress string
Locktime int64
SecretHash [32]byte
Secret []byte
Contract []byte
IsRefund bool
}
type TxSwapResults struct {
TxID chainhash.Hash
Found string
Contracts map[uint32]*AtomicSwapData
Redemptions map[uint32]*AtomicSwapData
Refunds map[uint32]*AtomicSwapData
}
var zeroHash chainhash.Hash
func MsgTxAtomicSwapsInfo(msgTx *wire.MsgTx, outputSpenders map[uint32]*OutputSpenderTxOut,
params *chaincfg.Params) (*TxSwapResults, error) {
// Skip if the tx is generating coins (coinbase, treasurybase, stakebase).
for _, input := range msgTx.TxIn {
if input.PreviousOutPoint.Hash == zeroHash {
return nil, nil
}
}
hash := msgTx.TxHash()
txSwaps := &TxSwapResults{
TxID: hash,
}
appendFound := func(found string) {
if txSwaps.Found == "" {
txSwaps.Found = found
return
}
if strings.Contains(txSwaps.Found, found) {
return
}
txSwaps.Found = fmt.Sprintf("%s, %s", txSwaps.Found, found)
}
// Check if any of this tx's outputs are contracts. Requires the output to
// be spent AND the spending input to have the correct sigscript type.
for i, vout := range msgTx.TxOut {
spender, spent := outputSpenders[uint32(i)]
if !spent {
continue // output must be spent to determine if it is a contract
}
scriptClass := txscript.GetScriptClass(vout.PkScript)
if scriptClass != txscript.WitnessV0ScriptHashTy {
continue // non-p2wsh outputs cannot currently be contracts
}
spendHash := spender.Tx.TxHash()
// Sanity check that the provided `spender` actually spends this output.
if len(spender.Tx.TxIn) <= int(spender.Vin) {
fmt.Println("invalid:", spender.Vin)
}
spendingVin := spender.Tx.TxIn[spender.Vin]
if spendingVin.PreviousOutPoint.Hash != hash {
return nil, fmt.Errorf("invalid tx spending data, %s:%d not spent by %s",
hash, i, spendHash)
}
// Use the spending tx input script to retrieve swap details.
contractData, contractScript, secret, isRefund, err :=
ExtractSwapDataFromWitness(spendingVin.Witness, params)
if err != nil {
return nil, fmt.Errorf("error checking if tx output is a contract: %v", err)
}
if contractData != nil {
appendFound("Contract")
if txSwaps.Contracts == nil {
txSwaps.Contracts = make(map[uint32]*AtomicSwapData)
}
txSwaps.Contracts[uint32(i)] = &AtomicSwapData{
ContractTx: &hash,
ContractVout: uint32(i),
SpendTx: &spendHash,
SpendVin: spender.Vin,
Value: vout.Value,
ContractAddress: contractData.ContractAddress.String(),
RecipientAddress: contractData.RecipientAddress.String(),
RefundAddress: contractData.RefundAddress.String(),
Locktime: contractData.Locktime,
SecretHash: contractData.SecretHash,
Secret: secret,
Contract: contractScript,
IsRefund: isRefund,
}
}
}
// Check if any of this tx's inputs are redeems or refunds, i.e. inputs that
// spend the output of an atomic swap contract.
for i, vin := range msgTx.TxIn {
contractData, contractScript, secret, isRefund, err :=
ExtractSwapDataFromWitness(vin.Witness, params)
if err != nil {
return nil, fmt.Errorf("error checking if input redeems a contract: %v", err)
}
if contractData == nil {
continue
}
swapInfo := &AtomicSwapData{
ContractTx: &vin.PreviousOutPoint.Hash,
ContractVout: vin.PreviousOutPoint.Index,
SpendTx: &hash,
SpendVin: uint32(i),
// Value: vin.ValueIn, // lame, caller needs to retrieve the prev tx output
ContractAddress: contractData.ContractAddress.String(),
RecipientAddress: contractData.RecipientAddress.String(),
RefundAddress: contractData.RefundAddress.String(),
Locktime: contractData.Locktime,
SecretHash: contractData.SecretHash,
Secret: secret, // should be empty for refund
Contract: contractScript,
IsRefund: isRefund,
}
if isRefund {
if txSwaps.Refunds == nil {
txSwaps.Refunds = make(map[uint32]*AtomicSwapData)
}
txSwaps.Refunds[uint32(i)] = swapInfo
appendFound("Refund")
} else {
if txSwaps.Redemptions == nil {
txSwaps.Redemptions = make(map[uint32]*AtomicSwapData)
}
txSwaps.Redemptions[uint32(i)] = swapInfo
appendFound("Redemption")
}
}
return txSwaps, nil
}