44 "bytes"
55 "crypto/sha256"
66 "errors"
7+ "fmt"
78
89 "github.com/btcsuite/btcd/chaincfg"
910 "github.com/btcsuite/btcd/txscript"
@@ -25,8 +26,17 @@ const (
2526 HtlcNP2WSH
2627)
2728
29+ // ScriptVersion defines the HTLC script version.
30+ type ScriptVersion uint8
31+
32+ const (
33+ // HtlcV1 refers to the original version of the HTLC script.
34+ HtlcV1 ScriptVersion = iota
35+ )
36+
2837// Htlc contains relevant htlc information from the receiver perspective.
2938type Htlc struct {
39+ Version ScriptVersion
3040 Script []byte
3141 PkScript []byte
3242 Hash lntypes.Hash
4555 // the maximum value for cltv expiry to get the maximum (worst case)
4656 // script size.
4757 QuoteHtlc , _ = NewHtlc (
58+ HtlcV1 ,
4859 ^ int32 (0 ), quoteKey , quoteKey , quoteHash , HtlcP2WSH ,
4960 & chaincfg .MainNetParams ,
5061 )
@@ -65,13 +76,26 @@ func (h HtlcOutputType) String() string {
6576}
6677
6778// NewHtlc returns a new instance.
68- func NewHtlc (cltvExpiry int32 , senderKey , receiverKey [33 ]byte ,
79+ func NewHtlc (version ScriptVersion , cltvExpiry int32 ,
80+ senderKey , receiverKey [33 ]byte ,
6981 hash lntypes.Hash , outputType HtlcOutputType ,
7082 chainParams * chaincfg.Params ) (* Htlc , error ) {
7183
72- script , err := swapHTLCScript (
73- cltvExpiry , senderKey , receiverKey , hash ,
84+ var (
85+ err error
86+ script []byte
7487 )
88+
89+ switch version {
90+ case HtlcV1 :
91+ script , err = swapHTLCScriptV1 (
92+ cltvExpiry , senderKey , receiverKey , hash ,
93+ )
94+
95+ default :
96+ return nil , fmt .Errorf ("unknown script version: %v" , version )
97+ }
98+
7599 if err != nil {
76100 return nil , err
77101 }
@@ -135,6 +159,7 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
135159
136160 return & Htlc {
137161 Hash : hash ,
162+ Version : version ,
138163 Script : script ,
139164 PkScript : pkScript ,
140165 OutputType : outputType ,
@@ -144,19 +169,19 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
144169 }, nil
145170}
146171
147- // SwapHTLCScript returns the on-chain HTLC witness script.
172+ // SwapHTLCScriptV1 returns the on-chain HTLC witness script.
148173//
149174// OP_SIZE 32 OP_EQUAL
150175// OP_IF
151- // OP_HASH160 <ripemd160(swap_hash )> OP_EQUALVERIFY
152- // <recvr key >
176+ // OP_HASH160 <ripemd160(swapHash )> OP_EQUALVERIFY
177+ // <receiverHtlcKey >
153178// OP_ELSE
154179// OP_DROP
155180// <cltv timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
156- // <sender key >
181+ // <senderHtlcKey >
157182// OP_ENDIF
158183// OP_CHECKSIG
159- func swapHTLCScript (cltvExpiry int32 , senderHtlcKey ,
184+ func swapHTLCScriptV1 (cltvExpiry int32 , senderHtlcKey ,
160185 receiverHtlcKey [33 ]byte , swapHash lntypes.Hash ) ([]byte , error ) {
161186
162187 builder := txscript .NewScriptBuilder ()
0 commit comments