2024-03-07 17:27:13 +01:00
|
|
|
package address
|
2023-11-09 19:07:48 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
|
"github.com/lightninglabs/loop/loopdb"
|
|
|
|
|
"github.com/lightninglabs/loop/loopdb/sqlc"
|
2026-05-01 15:54:37 +05:30
|
|
|
"github.com/lightninglabs/loop/staticaddr/script"
|
2024-03-07 17:27:13 +01:00
|
|
|
"github.com/lightninglabs/loop/staticaddr/version"
|
2023-11-09 19:07:48 +01:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SqlStore is the backing store for static addresses.
|
|
|
|
|
type SqlStore struct {
|
|
|
|
|
baseDB *loopdb.BaseDB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewSqlStore constructs a new SQLStore from a BaseDB. The BaseDB is agnostic
|
|
|
|
|
// to the underlying driver which can be postgres or sqlite.
|
|
|
|
|
func NewSqlStore(db *loopdb.BaseDB) *SqlStore {
|
|
|
|
|
return &SqlStore{
|
|
|
|
|
baseDB: db,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateStaticAddress creates a static address record in the database.
|
|
|
|
|
func (s *SqlStore) CreateStaticAddress(ctx context.Context,
|
2026-05-01 15:54:37 +05:30
|
|
|
addrParams *script.Parameters) error {
|
2023-11-09 19:07:48 +01:00
|
|
|
|
|
|
|
|
createArgs := sqlc.CreateStaticAddressParams{
|
2024-12-09 13:55:29 +01:00
|
|
|
ClientPubkey: addrParams.ClientPubkey.SerializeCompressed(),
|
|
|
|
|
ServerPubkey: addrParams.ServerPubkey.SerializeCompressed(),
|
|
|
|
|
Expiry: int32(addrParams.Expiry),
|
|
|
|
|
ClientKeyFamily: int32(addrParams.KeyLocator.Family),
|
|
|
|
|
ClientKeyIndex: int32(addrParams.KeyLocator.Index),
|
|
|
|
|
Pkscript: addrParams.PkScript,
|
|
|
|
|
ProtocolVersion: int32(addrParams.ProtocolVersion),
|
|
|
|
|
InitiationHeight: addrParams.InitiationHeight,
|
2023-11-09 19:07:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s.baseDB.Queries.CreateStaticAddress(ctx, createArgs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAllStaticAddresses returns all address known to the server.
|
2026-05-01 15:54:37 +05:30
|
|
|
func (s *SqlStore) GetAllStaticAddresses(ctx context.Context) (
|
|
|
|
|
[]*script.Parameters, error) {
|
2023-11-09 19:07:48 +01:00
|
|
|
|
|
|
|
|
staticAddresses, err := s.baseDB.Queries.AllStaticAddresses(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 15:54:37 +05:30
|
|
|
var result []*script.Parameters
|
2023-11-09 19:07:48 +01:00
|
|
|
for _, address := range staticAddresses {
|
|
|
|
|
res, err := s.toAddressParameters(address)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = append(result, res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// toAddressParameters transforms a database representation of a static address
|
|
|
|
|
// to an AddressParameters struct.
|
|
|
|
|
func (s *SqlStore) toAddressParameters(row sqlc.StaticAddress) (
|
2026-05-01 15:54:37 +05:30
|
|
|
*script.Parameters, error) {
|
2023-11-09 19:07:48 +01:00
|
|
|
|
|
|
|
|
clientPubkey, err := btcec.ParsePubKey(row.ClientPubkey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverPubkey, err := btcec.ParsePubKey(row.ServerPubkey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-01 15:54:37 +05:30
|
|
|
return &script.Parameters{
|
2023-11-09 19:07:48 +01:00
|
|
|
ClientPubkey: clientPubkey,
|
|
|
|
|
ServerPubkey: serverPubkey,
|
|
|
|
|
PkScript: row.Pkscript,
|
|
|
|
|
Expiry: uint32(row.Expiry),
|
|
|
|
|
KeyLocator: keychain.KeyLocator{
|
|
|
|
|
Family: keychain.KeyFamily(row.ClientKeyFamily),
|
|
|
|
|
Index: uint32(row.ClientKeyIndex),
|
|
|
|
|
},
|
2024-03-07 17:27:13 +01:00
|
|
|
ProtocolVersion: version.AddressProtocolVersion(
|
|
|
|
|
row.ProtocolVersion,
|
|
|
|
|
),
|
2024-12-09 13:55:29 +01:00
|
|
|
InitiationHeight: row.InitiationHeight,
|
2023-11-09 19:07:48 +01:00
|
|
|
}, nil
|
|
|
|
|
}
|