Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Reverse Swaps #3

Merged
merged 1 commit into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# boltz-lnd
# Boltz LND

The LND node to which this daemon connects to, has to be version `v0.10.0-beta` or higher. Also, LND needs to be compiled with these build flags (binaries from the official releases already include them):

- `routerrpc` (multi path payments)
- `chainrpc` (block listener)
- `walletrpc` (fee estimations)
41 changes: 38 additions & 3 deletions boltz/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ type SwapStatusRequest struct {
}

type SwapStatusResponse struct {
Status string `json:"status"`
Status string `json:"status"`
Transaction struct {
Id string `json:"id"`
Hex string `json:"hex"`
} `json:"transaction"`

Error string `json:"error"`
}
Expand Down Expand Up @@ -93,12 +97,32 @@ type CreateSwapRequest struct {

type CreateSwapResponse struct {
Id string `json:"id"`
Bip21 string `json:"bip21"`
Address string `json:"address"`
RedeemScript string `json:"redeemScript"`
AcceptZeroConf bool `json:"acceptZeroConf"`
ExpectedAmount int `json:"expectedAmount"`
TimeoutBlockHeight int `json:"timeoutBlockHeight"`
Address string `json:"address"`
Bip21 string `json:"bip21"`

Error string `json:"error"`
}

type CreateReverseSwapRequest struct {
Type string `json:"type"`
PairId string `json:"pairId"`
OrderSide string `json:"orderSide"`
InvoiceAmount int `json:"invoiceAmount"`
PreimageHash string `json:"preimageHash"`
ClaimPublicKey string `json:"claimPublicKey"`
}

type CreateReverseSwapResponse struct {
Id string `json:"id"`
Invoice string `json:"invoice"`
OnchainAmount int `json:"onchainAmount"`
RedeemScript string `json:"redeemScript"`
LockupAddress string `json:"lockupAddress"`
TimeoutBlockHeight int `json:"TimeoutBlockHeight"`

Error string `json:"error"`
}
Expand Down Expand Up @@ -186,6 +210,17 @@ func (boltz *Boltz) CreateSwap(request CreateSwapRequest) (*CreateSwapResponse,
return &response, err
}

func (boltz *Boltz) CreateReverseSwap(request CreateReverseSwapRequest) (*CreateReverseSwapResponse, error) {
var response CreateReverseSwapResponse
err := boltz.sendPostRequest("/createswap", request, &response)

if response.Error != "" {
return nil, errors.New(response.Error)
}

return &response, err
}

func (boltz *Boltz) sendGetRequest(endpoint string, response interface{}) error {
res, err := http.Get(boltz.URL + endpoint)

Expand Down
1 change: 1 addition & 0 deletions boltz/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var swapUpdateEventStrings = map[string]SwapUpdateEvent{
var CompletedStatus = []string{
SwapRefunded.String(),
SwapAbandoned.String(),
InvoiceSettled.String(),
TransactionClaimed.String(),
}

Expand Down
37 changes: 36 additions & 1 deletion boltz/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
)

var invalidRedeemScript = errors.New("invalid redeem script")

func CheckSwapScript(redeemScript, preimageHash []byte, refundKey *btcec.PrivateKey, timeoutBlockHeight int) error {
disassembledScript, err := txscript.DisasmString(redeemScript)

Expand All @@ -33,7 +35,40 @@ func CheckSwapScript(redeemScript, preimageHash []byte, refundKey *btcec.Private
}

if disassembledScript != strings.Join(expectedScript, " ") {
return errors.New("invalid redeem script")
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
Expand Down
19 changes: 19 additions & 0 deletions boltz/scripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ func TestCheckSwapScript(t *testing.T) {
assert.Equal(t, err, CheckSwapScript(redeemScript, preimageHash, refundKey, 0))
}

func TestCheckReverseSwapScript(t *testing.T) {
redeemScript, _ := hex.DecodeString("8201208763a9147ba0ab22fcffda41fd324aba4b5ce192ba9ec5dd882102e82694032768e49526972307874d868b67c87c37e9256c05a2c5c0474e7395e3677502f800b175210247d7443123302272524c9754b44a6e7e6e1236719e9f468e15927aa4ea26301168ac")
preimageHash, _ := hex.DecodeString("fa9ef1d253d34e9e44da97b00c6ec6a95058f646de35ddb7649fc3313ac6fc61")

key, _ := hex.DecodeString("dddc90e33843662631fb8c3833c4743ffd8f00a94715735633bf178e62eb291c")
claimKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), key)

timeoutBlockHeight := 248

assert.Nil(t, CheckReverseSwapScript(redeemScript, preimageHash, claimKey, timeoutBlockHeight))

err := errors.New("invalid redeem script")
newKey, _ := btcec.NewPrivateKey(btcec.S256())

assert.Equal(t, err, CheckReverseSwapScript(redeemScript, []byte{}, claimKey, timeoutBlockHeight))
assert.Equal(t, err, CheckReverseSwapScript(redeemScript, preimageHash, newKey, timeoutBlockHeight))
assert.Equal(t, err, CheckReverseSwapScript(redeemScript, preimageHash, claimKey, 0))
}

func TestFormatHeight(t *testing.T) {
assert.Equal(t, "0400", formatHeight(4))
assert.Equal(t, "36a7", formatHeight(632630))
Expand Down
4 changes: 2 additions & 2 deletions boltz/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func ConstructTransaction(outputs []OutputDetails, outputAddress btcutil.Address
}

witnessSize := noFeeTransaction.SerializeSize() - noFeeTransaction.SerializeSizeStripped()
vByte := int64(noFeeTransaction.SerializeSizeStripped()) + int64(math.Ceil(float64(witnessSize) / 4))
vByte := int64(noFeeTransaction.SerializeSizeStripped()) + int64(math.Ceil(float64(witnessSize)/4))

return constructTransaction(outputs, outputAddress, vByte * satPerVbyte)
return constructTransaction(outputs, outputAddress, vByte*satPerVbyte)
}

func constructTransaction(outputs []OutputDetails, outputAddress btcutil.Address, fee int64) (*wire.MsgTx, error) {
Expand Down
Loading