mirror of
https://github.com/lightninglabs/loop.git
synced 2026-08-13 12:33:03 +02:00
swap: add HTLC script version
This commit is contained in:
parent
e9f71e90c2
commit
9fdd531130
5 changed files with 48 additions and 20 deletions
18
client.go
18
client.go
|
|
@ -175,9 +175,9 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
|
|||
|
||||
for _, swp := range loopOutSwaps {
|
||||
htlc, err := swap.NewHtlc(
|
||||
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
||||
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
|
||||
s.lndServices.ChainParams,
|
||||
swap.HtlcV1, swp.Contract.CltvExpiry,
|
||||
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
|
||||
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -195,18 +195,18 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
|
|||
|
||||
for _, swp := range loopInSwaps {
|
||||
htlcNP2WSH, err := swap.NewHtlc(
|
||||
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
||||
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH,
|
||||
s.lndServices.ChainParams,
|
||||
swap.HtlcV1, swp.Contract.CltvExpiry,
|
||||
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
|
||||
swp.Hash, swap.HtlcNP2WSH, s.lndServices.ChainParams,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
htlcP2WSH, err := swap.NewHtlc(
|
||||
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
|
||||
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
|
||||
s.lndServices.ChainParams,
|
||||
swap.HtlcV1, swp.Contract.CltvExpiry,
|
||||
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
|
||||
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
|
|||
|
||||
for _, s := range swaps {
|
||||
htlc, err := swap.NewHtlc(
|
||||
swap.HtlcV1,
|
||||
s.Contract.CltvExpiry,
|
||||
s.Contract.SenderKey,
|
||||
s.Contract.ReceiverKey,
|
||||
|
|
@ -101,6 +102,7 @@ func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
|
|||
|
||||
for _, s := range swaps {
|
||||
htlc, err := swap.NewHtlc(
|
||||
swap.HtlcV1,
|
||||
s.Contract.CltvExpiry,
|
||||
s.Contract.SenderKey,
|
||||
s.Contract.ReceiverKey,
|
||||
|
|
|
|||
|
|
@ -331,8 +331,9 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
|
|||
}
|
||||
|
||||
htlc, err := swap.NewHtlc(
|
||||
contract.CltvExpiry, contract.SenderKey, contract.ReceiverKey,
|
||||
testPreimage.Hash(), swap.HtlcNP2WSH, cfg.lnd.ChainParams,
|
||||
swap.HtlcV1, contract.CltvExpiry, contract.SenderKey,
|
||||
contract.ReceiverKey, testPreimage.Hash(), swap.HtlcNP2WSH,
|
||||
cfg.lnd.ChainParams,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
2
swap.go
2
swap.go
|
|
@ -51,7 +51,7 @@ func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
|
|||
// getHtlc composes and returns the on-chain swap script.
|
||||
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
|
||||
return swap.NewHtlc(
|
||||
s.contract.CltvExpiry, s.contract.SenderKey,
|
||||
swap.HtlcV1, s.contract.CltvExpiry, s.contract.SenderKey,
|
||||
s.contract.ReceiverKey, s.hash, outputType,
|
||||
s.swapConfig.lnd.ChainParams,
|
||||
)
|
||||
|
|
|
|||
41
swap/htlc.go
41
swap/htlc.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"bytes"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/btcsuite/btcd/txscript"
|
||||
|
|
@ -25,8 +26,17 @@ const (
|
|||
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.
|
||||
type Htlc struct {
|
||||
Version ScriptVersion
|
||||
Script []byte
|
||||
PkScript []byte
|
||||
Hash lntypes.Hash
|
||||
|
|
@ -45,6 +55,7 @@ var (
|
|||
// the maximum value for cltv expiry to get the maximum (worst case)
|
||||
// script size.
|
||||
QuoteHtlc, _ = NewHtlc(
|
||||
HtlcV1,
|
||||
^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH,
|
||||
&chaincfg.MainNetParams,
|
||||
)
|
||||
|
|
@ -65,13 +76,26 @@ func (h HtlcOutputType) String() string {
|
|||
}
|
||||
|
||||
// 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,
|
||||
chainParams *chaincfg.Params) (*Htlc, error) {
|
||||
|
||||
script, err := swapHTLCScript(
|
||||
cltvExpiry, senderKey, receiverKey, hash,
|
||||
var (
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -135,6 +159,7 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
|
|||
|
||||
return &Htlc{
|
||||
Hash: hash,
|
||||
Version: version,
|
||||
Script: script,
|
||||
PkScript: pkScript,
|
||||
OutputType: outputType,
|
||||
|
|
@ -144,19 +169,19 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
|
|||
}, nil
|
||||
}
|
||||
|
||||
// SwapHTLCScript returns the on-chain HTLC witness script.
|
||||
// SwapHTLCScriptV1 returns the on-chain HTLC witness script.
|
||||
//
|
||||
// OP_SIZE 32 OP_EQUAL
|
||||
// OP_IF
|
||||
// OP_HASH160 <ripemd160(swap_hash)> OP_EQUALVERIFY
|
||||
// <recvr key>
|
||||
// OP_HASH160 <ripemd160(swapHash)> OP_EQUALVERIFY
|
||||
// <receiverHtlcKey>
|
||||
// OP_ELSE
|
||||
// OP_DROP
|
||||
// <cltv timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
|
||||
// <sender key>
|
||||
// <senderHtlcKey>
|
||||
// OP_ENDIF
|
||||
// OP_CHECKSIG
|
||||
func swapHTLCScript(cltvExpiry int32, senderHtlcKey,
|
||||
func swapHTLCScriptV1(cltvExpiry int32, senderHtlcKey,
|
||||
receiverHtlcKey [33]byte, swapHash lntypes.Hash) ([]byte, error) {
|
||||
|
||||
builder := txscript.NewScriptBuilder()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue