Skip to content

Commit

Permalink
bfgd: remove pubkey access (#359)
Browse files Browse the repository at this point in the history
* Remove pubkey access

* Add v12 db
  • Loading branch information
marcopeereboom authored Jan 8, 2025
1 parent 49bf7f4 commit a93a0b2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 793 deletions.
26 changes: 1 addition & 25 deletions api/bfgapi/bfgapi.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -40,10 +40,6 @@ const (
CmdBitcoinInfoResponse = "bfgapi-bitcoin-info-response"
CmdBitcoinUTXOsRequest = "bfgapi-bitcoin-utxos-request"
CmdBitcoinUTXOsResponse = "bfgapi-bitcoin-utxos-response"
CmdAccessPublicKeyCreateRequest = "bfgapi-access-public-key-create-request"
CmdAccessPublicKeyCreateResponse = "bfgapi-access-public-key-create-response"
CmdAccessPublicKeyDeleteRequest = "bfgapi-access-public-key-delete-request"
CmdAccessPublicKeyDeleteResponse = "bfgapi-access-public-key-delete-response"
)

var (
Expand Down Expand Up @@ -164,22 +160,6 @@ type BTCNewBlockNotification struct{}

type L2KeystonesNotification struct{}

type AccessPublicKeyCreateRequest struct {
PublicKey string `json:"public_key"` // encoded compressed public key
}

type AccessPublicKeyCreateResponse struct {
Error *protocol.Error `json:"error,omitempty"`
}

type AccessPublicKeyDeleteRequest struct {
PublicKey string `json:"public_key"`
}

type AccessPublicKeyDeleteResponse struct {
Error *protocol.Error `json:"error,omitempty"`
}

type PopTx struct {
BtcTxId api.ByteSlice `json:"btc_tx_id"`
BtcRawTx api.ByteSlice `json:"btc_raw_tx"`
Expand Down Expand Up @@ -215,10 +195,6 @@ var commands = map[protocol.Command]reflect.Type{
CmdBitcoinInfoResponse: reflect.TypeOf(BitcoinInfoResponse{}),
CmdBitcoinUTXOsRequest: reflect.TypeOf(BitcoinUTXOsRequest{}),
CmdBitcoinUTXOsResponse: reflect.TypeOf(BitcoinUTXOsResponse{}),
CmdAccessPublicKeyCreateRequest: reflect.TypeOf(AccessPublicKeyCreateRequest{}),
CmdAccessPublicKeyCreateResponse: reflect.TypeOf(AccessPublicKeyCreateResponse{}),
CmdAccessPublicKeyDeleteRequest: reflect.TypeOf(AccessPublicKeyDeleteRequest{}),
CmdAccessPublicKeyDeleteResponse: reflect.TypeOf(AccessPublicKeyDeleteResponse{}),
}

type bfgAPI struct{}
Expand Down
8 changes: 1 addition & 7 deletions cmd/bfgd/bfgd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -49,12 +49,6 @@ var (
Help: "electrs max connections",
Print: config.PrintAll,
},
"BFG_PUBLIC_KEY_AUTH": config.Config{
Value: &cfg.PublicKeyAuth,
DefaultValue: false,
Help: "enable enforcing of public key auth handshake",
Print: config.PrintAll,
},
"BFG_BTC_START_HEIGHT": config.Config{
Value: &cfg.BTCStartHeight,
DefaultValue: uint64(0),
Expand Down
6 changes: 1 addition & 5 deletions database/bfgd/database.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand Down Expand Up @@ -38,10 +38,6 @@ type Database interface {

BtcBlockCanonicalHeight(ctx context.Context) (uint64, error)

AccessPublicKeyInsert(ctx context.Context, publicKey *AccessPublicKey) error
AccessPublicKeyExists(ctx context.Context, publicKey *AccessPublicKey) (bool, error)
AccessPublicKeyDelete(ctx context.Context, publicKey *AccessPublicKey) error

BtcTransactionBroadcastRequestInsert(ctx context.Context, serializedTx []byte, txId string) error
BtcTransactionBroadcastRequestGetNext(ctx context.Context, onlyNew bool) ([]byte, error)
BtcTransactionBroadcastRequestConfirmBroadcast(ctx context.Context, txId string) error
Expand Down
69 changes: 2 additions & 67 deletions database/bfgd/postgres/postgres.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 Hemi Labs, Inc.
// Copyright (c) 2024-2025 Hemi Labs, Inc.
// Use of this source code is governed by the MIT License,
// which can be found in the LICENSE file.

Expand All @@ -20,7 +20,7 @@ import (
)

const (
bfgdVersion = 11
bfgdVersion = 12

logLevel = "INFO"
verbose = false
Expand Down Expand Up @@ -908,71 +908,6 @@ func (p *pgdb) BtcBlockCanonicalHeight(ctx context.Context) (uint64, error) {
return result, nil
}

func (p *pgdb) AccessPublicKeyInsert(ctx context.Context, publicKey *bfgd.AccessPublicKey) error {
log.Tracef("AccessPublicKeyInsert")
defer log.Tracef("AccessPublicKeyInsert exit")

const sql = `
INSERT INTO access_public_keys (
public_key
) VALUES ($1)
`

_, err := p.db.ExecContext(ctx, sql, publicKey.PublicKey)
if err != nil {
var pqErr *pq.Error
if errors.As(err, &pqErr) && pqErr.Constraint == "access_public_keys_pkey" {
return database.DuplicateError("public key already exists")
}

return err
}

return nil
}

func (p *pgdb) AccessPublicKeyExists(ctx context.Context, publicKey *bfgd.AccessPublicKey) (bool, error) {
log.Tracef("AccessPublicKeyExists")
defer log.Tracef("AccessPublicKeyExists exit")

const q = `
SELECT EXISTS (
SELECT * FROM access_public_keys WHERE public_key = $1
)
`

var exists bool
if err := p.db.QueryRowContext(ctx, q, publicKey.PublicKey).Scan(&exists); err != nil {
return false, err
}

return exists, nil
}

func (p *pgdb) AccessPublicKeyDelete(ctx context.Context, publicKey *bfgd.AccessPublicKey) error {
log.Tracef("AccessPublicKeyDelete")
defer log.Tracef("AccessPublicKeyDelete exit")

const q = `
DELETE FROM access_public_keys WHERE public_key = $1
`

res, err := p.db.ExecContext(ctx, q, publicKey.PublicKey)
if err != nil {
return err
}

rows, err := res.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return database.NotFoundError("public key not found")
}

return nil
}

// BtcBlocksHeightsWithNoChildren returns the heights of blocks stored in the
// database that do not have any children, these represent possible forks that
// have not been handled yet.
Expand Down
11 changes: 11 additions & 0 deletions database/bfgd/scripts/0012.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- Copyright (c) 2025 Hemi Labs, Inc.
-- Use of this source code is governed by the MIT License,
-- which can be found in the LICENSE file.

BEGIN;

UPDATE version SET version = 12;

DROP TABLE access_public_keys;

COMMIT;
Loading

0 comments on commit a93a0b2

Please sign in to comment.