feat(store): per-account inbound/outbound whitelist CRUD

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 23:41:10 +01:00
parent 2db459d701
commit a1e9f601ce
2 changed files with 126 additions and 0 deletions
+88
View File
@@ -0,0 +1,88 @@
package store
import (
"fmt"
"strings"
)
type Direction string
const (
DirIn Direction = "in"
DirOut Direction = "out"
)
func (d Direction) table() (string, error) {
switch d {
case DirIn:
return "whitelist_in", nil
case DirOut:
return "whitelist_out", nil
default:
return "", fmt.Errorf("invalid direction %q", d)
}
}
func (s *Store) accountID(name string) (int64, error) {
a, err := s.GetAccount(name)
if err != nil {
return 0, err
}
return a.ID, nil
}
func (s *Store) AddWhitelist(account string, dir Direction, address string) error {
tbl, err := dir.table()
if err != nil {
return err
}
id, err := s.accountID(account)
if err != nil {
return err
}
_, err = s.db.Exec(
fmt.Sprintf("INSERT OR IGNORE INTO %s(account_id,address) VALUES(?,?)", tbl),
id, strings.ToLower(address))
return err
}
func (s *Store) RemoveWhitelist(account string, dir Direction, address string) error {
tbl, err := dir.table()
if err != nil {
return err
}
id, err := s.accountID(account)
if err != nil {
return err
}
_, err = s.db.Exec(
fmt.Sprintf("DELETE FROM %s WHERE account_id=? AND address=?", tbl),
id, strings.ToLower(address))
return err
}
func (s *Store) ListWhitelist(account string, dir Direction) ([]string, error) {
tbl, err := dir.table()
if err != nil {
return nil, err
}
id, err := s.accountID(account)
if err != nil {
return nil, err
}
rows, err := s.db.Query(
fmt.Sprintf("SELECT address FROM %s WHERE account_id=? ORDER BY address", tbl), id)
if err != nil {
return nil, err
}
defer rows.Close()
out := []string{}
for rows.Next() {
var a string
if err := rows.Scan(&a); err != nil {
return nil, err
}
out = append(out, a)
}
return out, rows.Err()
}