From 8d216655cdebb6ecc88cb2353d8b0f6b83ad10fd Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Thu, 9 Nov 2023 19:04:59 +0100 Subject: [PATCH] sqlc: static address migrations, models, queries --- .../migrations/000007_static_address.down.sql | 1 + .../migrations/000007_static_address.up.sql | 38 ++++++ loopdb/sqlc/models.go | 11 ++ loopdb/sqlc/querier.go | 3 + loopdb/sqlc/queries/static_addresses.sql | 25 ++++ loopdb/sqlc/static_addresses.sql.go | 110 ++++++++++++++++++ 6 files changed, 188 insertions(+) create mode 100644 loopdb/sqlc/migrations/000007_static_address.down.sql create mode 100644 loopdb/sqlc/migrations/000007_static_address.up.sql create mode 100644 loopdb/sqlc/queries/static_addresses.sql create mode 100644 loopdb/sqlc/static_addresses.sql.go diff --git a/loopdb/sqlc/migrations/000007_static_address.down.sql b/loopdb/sqlc/migrations/000007_static_address.down.sql new file mode 100644 index 0000000..492ef97 --- /dev/null +++ b/loopdb/sqlc/migrations/000007_static_address.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS static_addresses; \ No newline at end of file diff --git a/loopdb/sqlc/migrations/000007_static_address.up.sql b/loopdb/sqlc/migrations/000007_static_address.up.sql new file mode 100644 index 0000000..f4b293e --- /dev/null +++ b/loopdb/sqlc/migrations/000007_static_address.up.sql @@ -0,0 +1,38 @@ +-- static_address stores the static loop-in addresses that clients +-- cooperatively created with the server. +CREATE TABLE IF NOT EXISTS static_addresses ( + -- id is the auto-incrementing primary key for a static address. + id INTEGER PRIMARY KEY, + + -- client_pubkey is the client side public taproot key that is used to + -- construct the 2-of-2 MuSig2 taproot output that represents the static + -- address. + client_pubkey BYTEA NOT NULL, + + -- server_pubkey is the server side public taproot key that is used to + -- construct the 2-of-2 MuSig2 taproot output that represents the static + -- address. + server_pubkey BYTEA NOT NULL, + + -- expiry denotes the CSV delay at which funds at a specific static address + -- can be swept back to the client. + expiry INT NOT NULL, + + -- client_key_family is the key family of the client public key from the + -- client's lnd wallet. + client_key_family INT NOT NULL, + + -- client_key_index is the key index of the client public key from the + -- client's lnd wallet. + client_key_index INT NOT NULL, + + -- pkscript is the witness program that represents the static address. It is + -- unique amongst all static addresses. + pkscript BYTEA NOT NULL UNIQUE, + + -- protocol_version is the protocol version that the swap was created with. + -- Note that this version is not upgraded if the client upgrades or + -- downgrades their protocol version for static address outputs already in + -- use. + protocol_version INTEGER NOT NULL +); \ No newline at end of file diff --git a/loopdb/sqlc/models.go b/loopdb/sqlc/models.go index 869bf62..d1cb5a7 100644 --- a/loopdb/sqlc/models.go +++ b/loopdb/sqlc/models.go @@ -88,6 +88,17 @@ type ReservationUpdate struct { UpdateTimestamp time.Time } +type StaticAddress struct { + ID int32 + ClientPubkey []byte + ServerPubkey []byte + Expiry int32 + ClientKeyFamily int32 + ClientKeyIndex int32 + Pkscript []byte + ProtocolVersion int32 +} + type Swap struct { ID int32 SwapHash []byte diff --git a/loopdb/sqlc/querier.go b/loopdb/sqlc/querier.go index bf9f71e..bfa6183 100644 --- a/loopdb/sqlc/querier.go +++ b/loopdb/sqlc/querier.go @@ -9,8 +9,10 @@ import ( ) type Querier interface { + AllStaticAddresses(ctx context.Context) ([]StaticAddress, error) ConfirmBatch(ctx context.Context, id int32) error CreateReservation(ctx context.Context, arg CreateReservationParams) error + CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error FetchLiquidityParams(ctx context.Context) ([]byte, error) GetBatchSweeps(ctx context.Context, batchID int32) ([]GetBatchSweepsRow, error) GetInstantOutSwap(ctx context.Context, swapHash []byte) (GetInstantOutSwapRow, error) @@ -23,6 +25,7 @@ type Querier interface { GetReservation(ctx context.Context, reservationID []byte) (Reservation, error) GetReservationUpdates(ctx context.Context, reservationID []byte) ([]ReservationUpdate, error) GetReservations(ctx context.Context) ([]Reservation, error) + GetStaticAddress(ctx context.Context, pkscript []byte) (StaticAddress, error) GetSwapUpdates(ctx context.Context, swapHash []byte) ([]SwapUpdate, error) GetSweepStatus(ctx context.Context, swapHash []byte) (bool, error) GetUnconfirmedBatches(ctx context.Context) ([]SweepBatch, error) diff --git a/loopdb/sqlc/queries/static_addresses.sql b/loopdb/sqlc/queries/static_addresses.sql new file mode 100644 index 0000000..a0a1fd0 --- /dev/null +++ b/loopdb/sqlc/queries/static_addresses.sql @@ -0,0 +1,25 @@ +-- name: AllStaticAddresses :many +SELECT * FROM static_addresses; + +-- name: GetStaticAddress :one +SELECT * FROM static_addresses +WHERE pkscript=$1; + +-- name: CreateStaticAddress :exec +INSERT INTO static_addresses ( + client_pubkey, + server_pubkey, + expiry, + client_key_family, + client_key_index, + pkscript, + protocol_version +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7 + ); \ No newline at end of file diff --git a/loopdb/sqlc/static_addresses.sql.go b/loopdb/sqlc/static_addresses.sql.go new file mode 100644 index 0000000..cfe1a46 --- /dev/null +++ b/loopdb/sqlc/static_addresses.sql.go @@ -0,0 +1,110 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 +// source: static_addresses.sql + +package sqlc + +import ( + "context" +) + +const allStaticAddresses = `-- name: AllStaticAddresses :many +SELECT id, client_pubkey, server_pubkey, expiry, client_key_family, client_key_index, pkscript, protocol_version FROM static_addresses +` + +func (q *Queries) AllStaticAddresses(ctx context.Context) ([]StaticAddress, error) { + rows, err := q.db.QueryContext(ctx, allStaticAddresses) + if err != nil { + return nil, err + } + defer rows.Close() + var items []StaticAddress + for rows.Next() { + var i StaticAddress + if err := rows.Scan( + &i.ID, + &i.ClientPubkey, + &i.ServerPubkey, + &i.Expiry, + &i.ClientKeyFamily, + &i.ClientKeyIndex, + &i.Pkscript, + &i.ProtocolVersion, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const createStaticAddress = `-- name: CreateStaticAddress :exec +INSERT INTO static_addresses ( + client_pubkey, + server_pubkey, + expiry, + client_key_family, + client_key_index, + pkscript, + protocol_version +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7 + ) +` + +type CreateStaticAddressParams struct { + ClientPubkey []byte + ServerPubkey []byte + Expiry int32 + ClientKeyFamily int32 + ClientKeyIndex int32 + Pkscript []byte + ProtocolVersion int32 +} + +func (q *Queries) CreateStaticAddress(ctx context.Context, arg CreateStaticAddressParams) error { + _, err := q.db.ExecContext(ctx, createStaticAddress, + arg.ClientPubkey, + arg.ServerPubkey, + arg.Expiry, + arg.ClientKeyFamily, + arg.ClientKeyIndex, + arg.Pkscript, + arg.ProtocolVersion, + ) + return err +} + +const getStaticAddress = `-- name: GetStaticAddress :one +SELECT id, client_pubkey, server_pubkey, expiry, client_key_family, client_key_index, pkscript, protocol_version FROM static_addresses +WHERE pkscript=$1 +` + +func (q *Queries) GetStaticAddress(ctx context.Context, pkscript []byte) (StaticAddress, error) { + row := q.db.QueryRowContext(ctx, getStaticAddress, pkscript) + var i StaticAddress + err := row.Scan( + &i.ID, + &i.ClientPubkey, + &i.ServerPubkey, + &i.Expiry, + &i.ClientKeyFamily, + &i.ClientKeyIndex, + &i.Pkscript, + &i.ProtocolVersion, + ) + return i, err +}