-
Notifications
You must be signed in to change notification settings - Fork 13
/
scripts.go
79 lines (66 loc) · 1.78 KB
/
scripts.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
package boltz
import (
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/txscript"
"github.com/lightningnetwork/lnd/input"
"strings"
)
var invalidRedeemScript = errors.New("invalid redeem script")
func CheckSwapScript(redeemScript, preimageHash []byte, refundKey *btcec.PrivateKey, timeoutBlockHeight int) error {
disassembledScript, err := txscript.DisasmString(redeemScript)
if err != nil {
return err
}
expectedScript := []string{
"OP_HASH160",
hex.EncodeToString(input.Ripemd160H(preimageHash)),
"OP_EQUAL",
"OP_IF",
strings.Split(disassembledScript, " ")[4],
"OP_ELSE",
formatHeight(timeoutBlockHeight),
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
hex.EncodeToString(refundKey.PubKey().SerializeCompressed()),
"OP_ENDIF",
"OP_CHECKSIG",
}
if disassembledScript != strings.Join(expectedScript, " ") {
return invalidRedeemScript
}
return nil
}
func CheckReverseSwapScript(redeemScript, preimageHash []byte, claimKey *btcec.PrivateKey, timeoutBlockHeight int) error {
disassembledScript, err := txscript.DisasmString(redeemScript)
if err != nil {
return err
}
expectedScript := []string{
"OP_SIZE",
"20",
"OP_EQUAL",
"OP_IF",
"OP_HASH160",
hex.EncodeToString(input.Ripemd160H(preimageHash)),
"OP_EQUALVERIFY",
hex.EncodeToString(claimKey.PubKey().SerializeCompressed()),
"OP_ELSE",
"OP_DROP",
formatHeight(timeoutBlockHeight),
"OP_CHECKLOCKTIMEVERIFY",
"OP_DROP",
strings.Split(disassembledScript, " ")[13],
"OP_ENDIF",
"OP_CHECKSIG",
}
if disassembledScript != strings.Join(expectedScript, " ") {
return invalidRedeemScript
}
return nil
}
func formatHeight(height int) string {
test, _ := txscript.NewScriptBuilder().AddInt64(int64(height)).Script()
return hex.EncodeToString(test[1:])
}