swap: add HTLC script version

pull/255/head
Andras Banki-Horvath 4 years ago
parent e9f71e90c2
commit 9fdd531130
No known key found for this signature in database
GPG Key ID: 80E5375C094198D8

@ -175,9 +175,9 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
for _, swp := range loopOutSwaps { for _, swp := range loopOutSwaps {
htlc, err := swap.NewHtlc( htlc, err := swap.NewHtlc(
swp.Contract.CltvExpiry, swp.Contract.SenderKey, swap.HtlcV1, swp.Contract.CltvExpiry,
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH, swp.Contract.SenderKey, swp.Contract.ReceiverKey,
s.lndServices.ChainParams, swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -195,18 +195,18 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
for _, swp := range loopInSwaps { for _, swp := range loopInSwaps {
htlcNP2WSH, err := swap.NewHtlc( htlcNP2WSH, err := swap.NewHtlc(
swp.Contract.CltvExpiry, swp.Contract.SenderKey, swap.HtlcV1, swp.Contract.CltvExpiry,
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH, swp.Contract.SenderKey, swp.Contract.ReceiverKey,
s.lndServices.ChainParams, swp.Hash, swap.HtlcNP2WSH, s.lndServices.ChainParams,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
htlcP2WSH, err := swap.NewHtlc( htlcP2WSH, err := swap.NewHtlc(
swp.Contract.CltvExpiry, swp.Contract.SenderKey, swap.HtlcV1, swp.Contract.CltvExpiry,
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH, swp.Contract.SenderKey, swp.Contract.ReceiverKey,
s.lndServices.ChainParams, swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
) )
if err != nil { if err != nil {
return nil, err return nil, err

@ -50,6 +50,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
for _, s := range swaps { for _, s := range swaps {
htlc, err := swap.NewHtlc( htlc, err := swap.NewHtlc(
swap.HtlcV1,
s.Contract.CltvExpiry, s.Contract.CltvExpiry,
s.Contract.SenderKey, s.Contract.SenderKey,
s.Contract.ReceiverKey, s.Contract.ReceiverKey,
@ -101,6 +102,7 @@ func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
for _, s := range swaps { for _, s := range swaps {
htlc, err := swap.NewHtlc( htlc, err := swap.NewHtlc(
swap.HtlcV1,
s.Contract.CltvExpiry, s.Contract.CltvExpiry,
s.Contract.SenderKey, s.Contract.SenderKey,
s.Contract.ReceiverKey, s.Contract.ReceiverKey,

@ -331,8 +331,9 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
} }
htlc, err := swap.NewHtlc( htlc, err := swap.NewHtlc(
contract.CltvExpiry, contract.SenderKey, contract.ReceiverKey, swap.HtlcV1, contract.CltvExpiry, contract.SenderKey,
testPreimage.Hash(), swap.HtlcNP2WSH, cfg.lnd.ChainParams, contract.ReceiverKey, testPreimage.Hash(), swap.HtlcNP2WSH,
cfg.lnd.ChainParams,
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

@ -51,7 +51,7 @@ func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
// getHtlc composes and returns the on-chain swap script. // getHtlc composes and returns the on-chain swap script.
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) { func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
return swap.NewHtlc( return swap.NewHtlc(
s.contract.CltvExpiry, s.contract.SenderKey, swap.HtlcV1, s.contract.CltvExpiry, s.contract.SenderKey,
s.contract.ReceiverKey, s.hash, outputType, s.contract.ReceiverKey, s.hash, outputType,
s.swapConfig.lnd.ChainParams, s.swapConfig.lnd.ChainParams,
) )

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/txscript"
@ -25,8 +26,17 @@ const (
HtlcNP2WSH HtlcNP2WSH
) )
// ScriptVersion defines the HTLC script version.
type ScriptVersion uint8
const (
// HtlcV1 refers to the original version of the HTLC script.
HtlcV1 ScriptVersion = iota
)
// Htlc contains relevant htlc information from the receiver perspective. // Htlc contains relevant htlc information from the receiver perspective.
type Htlc struct { type Htlc struct {
Version ScriptVersion
Script []byte Script []byte
PkScript []byte PkScript []byte
Hash lntypes.Hash Hash lntypes.Hash
@ -45,6 +55,7 @@ var (
// the maximum value for cltv expiry to get the maximum (worst case) // the maximum value for cltv expiry to get the maximum (worst case)
// script size. // script size.
QuoteHtlc, _ = NewHtlc( QuoteHtlc, _ = NewHtlc(
HtlcV1,
^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH, ^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH,
&chaincfg.MainNetParams, &chaincfg.MainNetParams,
) )
@ -65,13 +76,26 @@ func (h HtlcOutputType) String() string {
} }
// NewHtlc returns a new instance. // NewHtlc returns a new instance.
func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte, func NewHtlc(version ScriptVersion, cltvExpiry int32,
senderKey, receiverKey [33]byte,
hash lntypes.Hash, outputType HtlcOutputType, hash lntypes.Hash, outputType HtlcOutputType,
chainParams *chaincfg.Params) (*Htlc, error) { chainParams *chaincfg.Params) (*Htlc, error) {
script, err := swapHTLCScript( var (
cltvExpiry, senderKey, receiverKey, hash, err error
script []byte
) )
switch version {
case HtlcV1:
script, err = swapHTLCScriptV1(
cltvExpiry, senderKey, receiverKey, hash,
)
default:
return nil, fmt.Errorf("unknown script version: %v", version)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -135,6 +159,7 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
return &Htlc{ return &Htlc{
Hash: hash, Hash: hash,
Version: version,
Script: script, Script: script,
PkScript: pkScript, PkScript: pkScript,
OutputType: outputType, OutputType: outputType,
@ -144,19 +169,19 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
}, nil }, nil
} }
// SwapHTLCScript returns the on-chain HTLC witness script. // SwapHTLCScriptV1 returns the on-chain HTLC witness script.
// //
// OP_SIZE 32 OP_EQUAL // OP_SIZE 32 OP_EQUAL
// OP_IF // OP_IF
// OP_HASH160 <ripemd160(swap_hash)> OP_EQUALVERIFY // OP_HASH160 <ripemd160(swapHash)> OP_EQUALVERIFY
// <recvr key> // <receiverHtlcKey>
// OP_ELSE // OP_ELSE
// OP_DROP // OP_DROP
// <cltv timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP // <cltv timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
// <sender key> // <senderHtlcKey>
// OP_ENDIF // OP_ENDIF
// OP_CHECKSIG // OP_CHECKSIG
func swapHTLCScript(cltvExpiry int32, senderHtlcKey, func swapHTLCScriptV1(cltvExpiry int32, senderHtlcKey,
receiverHtlcKey [33]byte, swapHash lntypes.Hash) ([]byte, error) { receiverHtlcKey [33]byte, swapHash lntypes.Hash) ([]byte, error) {
builder := txscript.NewScriptBuilder() builder := txscript.NewScriptBuilder()

Loading…
Cancel
Save