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: get swap by payment hash #377

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ install-tools: $(TOOLS_PATH)

proto: $(TOOLS_PATH)
@$(call print, "Generating protosbufs")
eval cd boltzrpc && ./gen_protos.sh && cd ..
eval cd pkg/boltzrpc && ./gen_protos.sh

#
# Tests
Expand Down
7 changes: 4 additions & 3 deletions docs/grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This page was automatically generated.

Paths for the REST proxy of the gRPC interface can be found [here](https://github.com/BoltzExchange/boltz-client/blob/master/pkg/boltzrpc/rest-annotations.yaml).
Paths for the REST proxy of the gRPC interface can be found [here](https://github.com/BoltzExchange/boltz-client/blob/master/boltzrpc/rest-annotations.yaml).



Expand Down Expand Up @@ -880,6 +880,7 @@ Channel creations are an optional extension to a submarine swap in the data type
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `id` | [`string`](#string) | | |
| `payment_hash` | [`bytes`](#bytes) | optional | Only implemented for submarine swaps |



Expand Down Expand Up @@ -1746,9 +1747,9 @@ Reloads the configuration from disk.

| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the current balance of the configured `from_wallet` |
| `swap` | [`ChainSwap`](#chainswap) | optional | Populated when a swap is recommended based on the configured `wallet_balance` of the configured `from_wallet` exceeds the currently configured `max_balance` |
| `wallet_balance` | [`boltzrpc.Balance`](#boltzrpc.balance) | | |
| `max_balance` | [`uint64`](#uint64) | | Currently configured max_balance |
| `max_balance` | [`uint64`](#uint64) | | |



Expand Down
1 change: 1 addition & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ CREATE TABLE swaps
preimage VARCHAR,
redeemScript VARCHAR,
invoice VARCHAR,
paymentHash VARCHAR,
address VARCHAR,
expectedAmount INT,
timeoutBlockheight INTEGER,
Expand Down
34 changes: 33 additions & 1 deletion internal/database/migration.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package database

import (
"encoding/hex"
"errors"
"fmt"
"github.com/lightningnetwork/lnd/zpay32"
Expand All @@ -17,7 +18,7 @@ type swapStatus struct {
status string
}

const latestSchemaVersion = 13
const latestSchemaVersion = 14

func (database *Database) migrate() error {
version, err := database.queryVersion()
Expand Down Expand Up @@ -547,6 +548,37 @@ func (database *Database) performMigration(tx *Transaction, oldVersion int) erro
return err
}

case 13:
migration := `
ALTER TABLE swaps ADD COLUMN "paymentHash" VARCHAR NOT NULL DEFAULT '';
`
migration += createViews

if _, err := tx.Exec(migration); err != nil {
return err
}

reverseSwaps, err := tx.QuerySwaps(SwapQuery{})
if err != nil {
return err
}
for _, reverseSwap := range reverseSwaps {
decoded, err := zpay32.Decode(reverseSwap.Invoice, boltz.MainNet.Btc)
if err != nil {
decoded, err = zpay32.Decode(reverseSwap.Invoice, boltz.TestNet.Btc)
if err != nil {
decoded, err = zpay32.Decode(reverseSwap.Invoice, boltz.Regtest.Btc)
}

}
if err == nil && decoded.PaymentHash != nil {
encoded := hex.EncodeToString(decoded.PaymentHash[:])
if _, err := tx.Exec("UPDATE swaps SET paymentHash = ? WHERE id = ?", encoded, reverseSwap.Id); err != nil {
return err
}
}
}

case latestSchemaVersion:
logger.Info("database already at latest schema version: " + strconv.Itoa(latestSchemaVersion))
return nil
Expand Down
39 changes: 37 additions & 2 deletions internal/database/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Swap struct {
Preimage []byte
RedeemScript []byte
Invoice string
PaymentHash []byte
Address string
ExpectedAmount uint64
TimoutBlockHeight uint32
Expand Down Expand Up @@ -55,6 +56,7 @@ type SwapSerialized struct {
Preimage string
RedeemScript string
Invoice string
PaymentHash string
Address string
ExpectedAmount uint64
TimeoutBlockHeight uint32
Expand Down Expand Up @@ -96,6 +98,7 @@ func (swap *Swap) Serialize() SwapSerialized {
Preimage: preimage,
RedeemScript: hex.EncodeToString(swap.RedeemScript),
Invoice: swap.Invoice,
PaymentHash: hex.EncodeToString(swap.PaymentHash),
Address: swap.Address,
ExpectedAmount: swap.ExpectedAmount,
TimeoutBlockHeight: swap.TimoutBlockHeight,
Expand Down Expand Up @@ -127,6 +130,7 @@ func parseSwap(rows *sql.Rows) (*Swap, error) {
var status string
privateKey := PrivateKeyScanner{}
var preimage string
var paymentHash string
var redeemScript string
blindingKey := PrivateKeyScanner{Nullable: true}
var createdAt, serviceFee, onchainFee sql.NullInt64
Expand All @@ -150,6 +154,7 @@ func parseSwap(rows *sql.Rows) (*Swap, error) {
"preimage": &preimage,
"redeemScript": &redeemScript,
"invoice": &swap.Invoice,
"paymentHash": &paymentHash,
"address": &swap.Address,
"expectedAmount": &swap.ExpectedAmount,
"timeoutBlockheight": &swap.TimoutBlockHeight,
Expand Down Expand Up @@ -188,6 +193,14 @@ func parseSwap(rows *sql.Rows) (*Swap, error) {
}
}

if paymentHash != "" {
swap.PaymentHash, err = hex.DecodeString(paymentHash)

if err != nil {
return nil, err
}
}

swap.RedeemScript, err = hex.DecodeString(redeemScript)

if err != nil {
Expand Down Expand Up @@ -251,6 +264,27 @@ func (database *Database) QuerySwapByInvoice(invoice string) (swap *Swap, err er
return swap, err
}

func (database *Database) QuerySwapByPaymentHash(paymentHash []byte) (swap *Swap, err error) {
database.lock.RLock()
defer database.lock.RUnlock()
rows, err := database.Query("SELECT * FROM swaps WHERE paymentHash = ?", hex.EncodeToString(paymentHash))

if err != nil {
return swap, err
}

defer rows.Close()

if rows.Next() {
swap, err = parseSwap(rows)

if err != nil {
return swap, err
}
}
return swap, err
}

func (database *Database) querySwaps(query string, args ...any) (swaps []*Swap, err error) {
database.lock.RLock()
defer database.lock.RUnlock()
Expand Down Expand Up @@ -303,10 +337,10 @@ func (database *Database) QueryRefundableSwaps(tenantId *Id, currency boltz.Curr
}

const insertSwapStatement = `
INSERT INTO swaps (id, fromCurrency, toCurrency, chanIds, state, error, status, privateKey, preimage, redeemScript, invoice, address,
INSERT INTO swaps (id, fromCurrency, toCurrency, chanIds, state, error, status, privateKey, preimage, redeemScript, invoice, paymentHash, address,
expectedAmount, timeoutBlockheight, lockupTransactionId, refundTransactionId, refundAddress,
blindingKey, isAuto, createdAt, serviceFee, serviceFeePercent, onchainFee, walletId, claimPubKey, swapTree, tenantId)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`

func (database *Database) CreateSwap(swap Swap) error {
Expand All @@ -329,6 +363,7 @@ func (database *Database) CreateSwap(swap Swap) error {
preimage,
hex.EncodeToString(swap.RedeemScript),
swap.Invoice,
hex.EncodeToString(swap.PaymentHash[:]),
swap.Address,
swap.ExpectedAmount,
swap.TimoutBlockHeight,
Expand Down
21 changes: 17 additions & 4 deletions internal/rpcserver/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,24 @@ func (server *routedBoltzServer) ClaimSwaps(ctx context.Context, request *boltzr
}

func (server *routedBoltzServer) GetSwapInfo(ctx context.Context, request *boltzrpc.GetSwapInfoRequest) (*boltzrpc.GetSwapInfoResponse, error) {
swap, reverseSwap, chainSwap, err := server.database.QueryAnySwap(request.Id)
if err != nil {
return nil, errors.New("could not find Swap with ID " + request.Id)
if request.Id != "" {
swap, reverseSwap, chainSwap, err := server.database.QueryAnySwap(request.Id)
if err != nil {
return nil, errors.New("could not find Swap with ID " + request.Id)
}
return server.serializeAnySwap(ctx, swap, reverseSwap, chainSwap)
}
if request.PaymentHash != nil {
swap, err := server.database.QuerySwapByPaymentHash(request.PaymentHash)
if err != nil {
return nil, err
}
if swap == nil {
return nil, status.Errorf(codes.NotFound, "could not find Swap with preimage hash")
}
return server.serializeAnySwap(ctx, swap, nil, nil)
}
return server.serializeAnySwap(ctx, swap, reverseSwap, chainSwap)
return nil, status.Errorf(codes.InvalidArgument, "no ID or preimage hash provided")
}

func (server *routedBoltzServer) GetSwapInfoStream(request *boltzrpc.GetSwapInfoRequest, stream boltzrpc.Boltz_GetSwapInfoStreamServer) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/boltzrpc/autoswaprpc/autoswaprpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading