@@ -6,10 +6,12 @@ import (
66 "errors"
77 "fmt"
88
9- btcec "github.com/btcsuite/btcd/btcec/v2"
9+ "github.com/btcsuite/btcd/btcec/v2"
1010 "github.com/btcsuite/btcd/btcec/v2/schnorr"
11+ "github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
1112 "github.com/btcsuite/btcd/btcutil"
1213 "github.com/btcsuite/btcd/chaincfg"
14+ "github.com/btcsuite/btcd/chaincfg/chainhash"
1315 "github.com/btcsuite/btcd/txscript"
1416 "github.com/btcsuite/btcd/wire"
1517 secp "github.com/decred/dcrd/dcrec/secp256k1/v4"
@@ -106,7 +108,7 @@ var (
106108 // script size.
107109 QuoteHtlc , _ = NewHtlc (
108110 HtlcV2 ,
109- ^ int32 (0 ), quoteKey , quoteKey , nil , quoteHash , HtlcP2WSH ,
111+ ^ int32 (0 ), quoteKey , quoteKey , quoteHash , HtlcP2WSH ,
110112 & chaincfg .MainNetParams ,
111113 )
112114
@@ -119,17 +121,6 @@ var (
119121 // selected for a v1 or v2 script.
120122 ErrInvalidOutputSelected = fmt .Errorf ("taproot output selected for " +
121123 "non taproot htlc" )
122-
123- // ErrSharedKeyNotNeeded is returned when a shared key is provided for
124- // either the v1 or v2 script. Shared key is only necessary for the v3
125- // script.
126- ErrSharedKeyNotNeeded = fmt .Errorf ("shared key not supported for " +
127- "script version" )
128-
129- // ErrSharedKeyRequired is returned when a script version requires a
130- // shared key.
131- ErrSharedKeyRequired = fmt .Errorf ("shared key required for script " +
132- "version" )
133124)
134125
135126// String returns the string value of HtlcOutputType.
@@ -152,9 +143,8 @@ func (h HtlcOutputType) String() string {
152143// NewHtlc returns a new instance. For v3 scripts, an internal pubkey generated
153144// by both participants must be provided.
154145func NewHtlc (version ScriptVersion , cltvExpiry int32 ,
155- senderKey , receiverKey [33 ]byte , sharedKey * btcec.PublicKey ,
156- hash lntypes.Hash , outputType HtlcOutputType ,
157- chainParams * chaincfg.Params ) (* Htlc , error ) {
146+ senderKey , receiverKey [33 ]byte , hash lntypes.Hash ,
147+ outputType HtlcOutputType , chainParams * chaincfg.Params ) (* Htlc , error ) {
158148
159149 var (
160150 err error
@@ -163,28 +153,18 @@ func NewHtlc(version ScriptVersion, cltvExpiry int32,
163153
164154 switch version {
165155 case HtlcV1 :
166- if sharedKey != nil {
167- return nil , ErrSharedKeyNotNeeded
168- }
169156 htlc , err = newHTLCScriptV1 (
170157 cltvExpiry , senderKey , receiverKey , hash ,
171158 )
172159
173160 case HtlcV2 :
174- if sharedKey != nil {
175- return nil , ErrSharedKeyNotNeeded
176- }
177161 htlc , err = newHTLCScriptV2 (
178162 cltvExpiry , senderKey , receiverKey , hash ,
179163 )
180164
181165 case HtlcV3 :
182- if sharedKey == nil {
183- return nil , ErrSharedKeyRequired
184- }
185166 htlc , err = newHTLCScriptV3 (
186- cltvExpiry , senderKey , receiverKey ,
187- sharedKey , hash ,
167+ cltvExpiry , senderKey , receiverKey , hash ,
188168 )
189169
190170 default :
@@ -650,44 +630,50 @@ func (h *HtlcScriptV2) lockingConditions(htlcOutputType HtlcOutputType,
650630
651631// HtlcScriptV3 encapsulates the htlc v3 script.
652632type HtlcScriptV3 struct {
653- // The final locking script for the timeout path which is available to
654- // the sender after the set blockheight.
633+ // TimeoutScript is the final locking script for the timeout path which
634+ // is available to the sender after the set blockheight.
655635 TimeoutScript []byte
656636
657- // The final locking script for the success path in which the receiver
658- // reveals the preimage.
637+ // SuccessScript is the final locking script for the success path in
638+ // which the receiver reveals the preimage.
659639 SuccessScript []byte
660640
661- // The public key for the keyspend path which bypasses the above two
662- // locking scripts.
641+ // InternalPubKey is the public key for the keyspend path which bypasses
642+ // the above two locking scripts.
663643 InternalPubKey * btcec.PublicKey
664644
665- // The taproot public key which is created with the above 3 inputs.
645+ // TaprootKey is the taproot public key which is created with the above
646+ // 3 inputs.
666647 TaprootKey * btcec.PublicKey
648+
649+ // RootHash is the root hash of the taptree.
650+ RootHash chainhash.Hash
667651}
668652
669653// newHTLCScriptV3 constructs a HtlcScipt with the HTLC V3 taproot script.
670- func newHTLCScriptV3 (cltvExpiry int32 , senderHtlcKey ,
671- receiverHtlcKey [33 ]byte , sharedKey * btcec.PublicKey ,
654+ func newHTLCScriptV3 (cltvExpiry int32 , senderHtlcKey , receiverHtlcKey [33 ]byte ,
672655 swapHash lntypes.Hash ) (* HtlcScriptV3 , error ) {
673656
674- receiverPubKey , err := btcec .ParsePubKey (
675- receiverHtlcKey [:],
676- )
657+ senderPubKey , err := schnorr .ParsePubKey (senderHtlcKey [1 :])
658+ if err != nil {
659+ return nil , err
660+ }
661+
662+ receiverPubKey , err := schnorr .ParsePubKey (receiverHtlcKey [1 :])
677663 if err != nil {
678664 return nil , err
679665 }
680666
681- senderPubKey , err := btcec . ParsePubKey (
682- senderHtlcKey [:] ,
667+ aggregateKey , _ , _ , err := musig2 . AggregateKeys (
668+ [] * btcec. PublicKey { senderPubKey , receiverPubKey }, true ,
683669 )
684670 if err != nil {
685671 return nil , err
686672 }
687673
688674 var schnorrSenderKey , schnorrReceiverKey [32 ]byte
689- copy (schnorrSenderKey [:], schnorr . SerializePubKey ( senderPubKey ) )
690- copy (schnorrReceiverKey [:], schnorr . SerializePubKey ( receiverPubKey ) )
675+ copy (schnorrSenderKey [:], senderHtlcKey [ 1 :] )
676+ copy (schnorrReceiverKey [:], receiverHtlcKey [ 1 :] )
691677
692678 // Create our success path script, we'll use this separately
693679 // to generate the success path leaf.
@@ -717,14 +703,15 @@ func newHTLCScriptV3(cltvExpiry int32, senderHtlcKey,
717703
718704 // Calculate top level taproot key.
719705 taprootKey := txscript .ComputeTaprootOutputKey (
720- sharedKey , rootHash [:],
706+ aggregateKey . PreTweakedKey , rootHash [:],
721707 )
722708
723709 return & HtlcScriptV3 {
724710 TimeoutScript : timeoutPathScript ,
725711 SuccessScript : successPathScript ,
726- InternalPubKey : sharedKey ,
712+ InternalPubKey : aggregateKey . PreTweakedKey ,
727713 TaprootKey : taprootKey ,
714+ RootHash : rootHash ,
728715 }, nil
729716}
730717
0 commit comments