From b4e485031239d4ea7644505ebd80ddaf7e91af37 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 16 Jan 2024 20:41:18 +0700 Subject: [PATCH] feat: LND setup form --- alby.go | 4 ++ api.go | 21 +++++++++- breez.go | 18 ++++++++- config.go | 2 + frontend/src/screens/Setup.tsx | 73 +++++++++++++++++++++++++++++++++- lnd.go | 16 ++++++-- main.go | 18 ++++++--- models/api/api.go | 6 ++- 8 files changed, 144 insertions(+), 14 deletions(-) diff --git a/alby.go b/alby.go index df41e967..7481b5e2 100644 --- a/alby.go +++ b/alby.go @@ -51,6 +51,10 @@ func NewAlbyOauthService(svc *Service, e *echo.Echo) (result LNClient, err error return albySvc, err } +func (svc *AlbyOAuthService) Shutdown() error { + return nil +} + func (svc *AlbyOAuthService) FetchUserToken(ctx context.Context, app App) (token *oauth2.Token, err error) { user := app.User tok, err := svc.oauthConf.TokenSource(ctx, &oauth2.Token{ diff --git a/api.go b/api.go index 490c328c..3f323b09 100644 --- a/api.go +++ b/api.go @@ -43,6 +43,9 @@ func (svc *Service) Setup(setupRequest *api.SetupRequest) error { dbConfigEntries := []db.ConfigEntry{} dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LN_BACKEND_TYPE", Value: setupRequest.LNBackendType}) + + // TODO: reduce duplication + if setupRequest.BreezMnemonic != "" { dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "BREEZ_MNEMONIC", Value: setupRequest.BreezMnemonic}) } @@ -50,7 +53,23 @@ func (svc *Service) Setup(setupRequest *api.SetupRequest) error { dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "GREENLIGHT_INVITE_CODE", Value: setupRequest.GreenlightInviteCode}) } - // Update columns to default value on `id` conflict + if setupRequest.LNDAddress != "" { + dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LND_ADDRESS", Value: setupRequest.LNDAddress}) + } + if setupRequest.LNDCertFile != "" { + dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LND_CERT_FILE", Value: setupRequest.LNDCertFile}) + } + if setupRequest.LNDCertHex != "" { + dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LND_CERT_HEX", Value: setupRequest.LNDCertHex}) + } + if setupRequest.LNDMacaroonFile != "" { + dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LND_MACAROON_FILE", Value: setupRequest.LNDMacaroonFile}) + } + if setupRequest.LNDMacaroonHex != "" { + dbConfigEntries = append(dbConfigEntries, db.ConfigEntry{Key: "LND_MACAROON_HEX", Value: setupRequest.LNDMacaroonHex}) + } + + // replace existing keys with latest values res := svc.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "key"}}, DoUpdates: clause.AssignmentColumns([]string{"value"}), diff --git a/breez.go b/breez.go index e06c79ea..eac9dc30 100644 --- a/breez.go +++ b/breez.go @@ -28,7 +28,19 @@ func (BreezListener) OnEvent(e breez_sdk.BreezEvent) { log.Printf("received event %#v", e) } -func NewBreezService(mnemonic, apiKey, inviteCode, workDir string) (result LNClient, err error) { +func NewBreezService(nwcSvc *Service, mnemonic, apiKey, inviteCode, workDir string) (result LNClient, err error) { + // FIXME: split single and multi user app + //add default user to db + user := &User{} + err = nwcSvc.db.FirstOrInit(user, User{AlbyIdentifier: "breez"}).Error + if err != nil { + return nil, err + } + err = nwcSvc.db.Save(user).Error + if err != nil { + return nil, err + } + //create dir if not exists newpath := filepath.Join(".", workDir) err = os.MkdirAll(newpath, os.ModePerm) @@ -75,6 +87,10 @@ func NewBreezService(mnemonic, apiKey, inviteCode, workDir string) (result LNCli }, nil } +func (bs *BreezService) Shutdown() error { + return bs.svc.Disconnect() +} + func (bs *BreezService) SendPaymentSync(ctx context.Context, senderPubkey string, payReq string) (preimage string, err error) { sendPaymentRequest := breez_sdk.SendPaymentRequest{ Bolt11: payReq, diff --git a/config.go b/config.go index 44d16a9a..94ca8d5e 100644 --- a/config.go +++ b/config.go @@ -21,7 +21,9 @@ type Config struct { AppType string `envconfig:"APP_TYPE" default:"HTTP"` LNDAddress string `envconfig:"LND_ADDRESS"` LNDCertFile string `envconfig:"LND_CERT_FILE"` + LNDCertHex string `envconfig:"LND_CERT_HEX"` LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"` + LNDMacaroonHex string `envconfig:"LND_MACAROON_HEX"` BreezMnemonic string `envconfig:"BREEZ_MNEMONIC"` BreezAPIKey string `envconfig:"BREEZ_API_KEY"` GreenlightInviteCode string `envconfig:"GREENLIGHT_INVITE_CODE"` diff --git a/frontend/src/screens/Setup.tsx b/frontend/src/screens/Setup.tsx index f463a3af..0a6ca45e 100644 --- a/frontend/src/screens/Setup.tsx +++ b/frontend/src/screens/Setup.tsx @@ -56,7 +56,7 @@ export function Setup() { {backendType === "BREEZ" && } - {backendType === "LND" &&

Coming soon

} + {backendType === "LND" && } ); } @@ -111,7 +111,7 @@ function BreezForm({ handleSubmit }: SetupFormProps) { className="bg-gray-50 border border-gray-300 text-gray-900 focus:ring-purple-700 dark:focus:ring-purple-600 dark:ring-offset-gray-800 focus:ring-2 text-sm rounded-lg block w-full p-2.5 dark:bg-surface-00dp dark:border-gray-700 dark:placeholder-gray-400 dark:text-white" />