Skip to content

Commit

Permalink
Merge branch 'feat/wails-v2' into feat/multi-entry
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 19, 2024
2 parents c958084 + 61b6dd4 commit e15b746
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 24 deletions.
9 changes: 3 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ func (svc *Service) Setup(setupRequest *api.SetupRequest) error {
if setupRequest.LNBackendType != "" {
dbConfig.LNBackendType = setupRequest.LNBackendType
}
if setupRequest.BreezAPIKey != "" {
dbConfig.BreezAPIKey = setupRequest.BreezAPIKey
}
if setupRequest.BreezMnemonic != "" {
dbConfig.BreezMnemonic = setupRequest.BreezMnemonic
}
Expand All @@ -242,15 +245,9 @@ func (svc *Service) Setup(setupRequest *api.SetupRequest) error {
if setupRequest.LNDAddress != "" {
dbConfig.LNDAddress = setupRequest.LNDAddress
}
if setupRequest.LNDCertFile != "" {
dbConfig.LNDCertFile = setupRequest.LNDCertFile
}
if setupRequest.LNDCertHex != "" {
dbConfig.LNDCertHex = setupRequest.LNDCertHex
}
if setupRequest.LNDMacaroonFile != "" {
dbConfig.LNDMacaroonFile = setupRequest.LNDMacaroonFile
}
if setupRequest.LNDMacaroonHex != "" {
dbConfig.LNDMacaroonHex = setupRequest.LNDMacaroonHex
}
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type Config struct {
ClientPubkey string `envconfig:"CLIENT_NOSTR_PUBKEY"`
Relay string `envconfig:"RELAY" default:"wss://relay.getalby.com/v1"`
PublicRelay string `envconfig:"PUBLIC_RELAY"`
BreezAPIKey string `envconfig:"BREEZ_API_KEY"`
LNDCertFile string `envconfig:"LND_CERT_FILE"`
LNDMacaroonFile string `envconfig:"LND_MACAROON_FILE"`
BreezWorkdir string `envconfig:"BREEZ_WORK_DIR" default:".breez"`
BasicAuthUser string `envconfig:"BASIC_AUTH_USER"`
BasicAuthPassword string `envconfig:"BASIC_AUTH_PASSWORD"`
Expand Down
57 changes: 48 additions & 9 deletions frontend/src/screens/Setup.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import Loading from "src/components/Loading";
import { useCSRF } from "src/hooks/useCSRF";
import { useInfo } from "src/hooks/useInfo";
import { BackendType } from "src/types";
import { request, handleRequestError } from "src/utils/request";

export function Setup() {
const [backendType, setBackendType] = React.useState<BackendType>("BREEZ");
const [isConnecting, setConnecting] = React.useState(false);
const navigate = useNavigate();

const { data: info } = useInfo();
const { data: csrf } = useCSRF();

async function handleSubmit(data: object) {
try {
setConnecting(true);
if (!csrf) {
throw new Error("info not loaded");
}
Expand All @@ -32,6 +35,8 @@ export function Setup() {
navigate("/apps");
} catch (error) {
handleRequestError("Failed to connect", error);
} finally {
setConnecting(false);
}
}

Expand Down Expand Up @@ -63,30 +68,49 @@ export function Setup() {
<option value={"LND"}>LND</option>
</select>

{backendType === "BREEZ" && <BreezForm handleSubmit={handleSubmit} />}
{backendType === "LND" && <LNDForm handleSubmit={handleSubmit} />}
{backendType === "BREEZ" && (
<BreezForm handleSubmit={handleSubmit} isConnecting={isConnecting} />
)}
{backendType === "LND" && (
<LNDForm handleSubmit={handleSubmit} isConnecting={isConnecting} />
)}
</>
);
}

function ConnectButton() {
type ConnectButtonProps = {
isConnecting: boolean;
};

function ConnectButton({ isConnecting }: ConnectButtonProps) {
return (
<button
type="submit"
className="mt-4 inline-flex w-full bg-purple-700 cursor-pointer dark:text-neutral-200 duration-150 focus-visible:ring-2 focus-visible:ring-offset-2 focus:outline-none font-medium hover:bg-purple-900 items-center justify-center px-5 py-3 rounded-md shadow text-white transition"
className={`mt-4 gap-2 inline-flex w-full ${
isConnecting ? "bg-gray-300 dark:bg-gray-700" : "bg-purple-700"
} cursor-pointer dark:text-neutral-200 duration-150 focus-visible:ring-2 focus-visible:ring-offset-2 focus:outline-none font-medium hover:bg-purple-900 items-center justify-center px-5 py-3 rounded-md shadow text-white transition`}
disabled={isConnecting}
>
Connect
{isConnecting ? (
<>
<Loading /> Connecting...
</>
) : (
<>Connect</>
)}
</button>
);
}

type SetupFormProps = {
isConnecting: boolean;
handleSubmit(data: unknown): void;
};

function BreezForm({ handleSubmit }: SetupFormProps) {
function BreezForm({ isConnecting, handleSubmit }: SetupFormProps) {
const [greenlightInviteCode, setGreenlightInviteCode] =
React.useState<string>("");
const [breezApiKey, setBreezApiKey] = React.useState<string>("");
const [breezMnemonic, setBreezMnemonic] = React.useState<string>("");

function onSubmit(e: React.FormEvent) {
Expand All @@ -97,6 +121,7 @@ function BreezForm({ handleSubmit }: SetupFormProps) {
}
handleSubmit({
greenlightInviteCode,
breezApiKey,
breezMnemonic,
});
}
Expand All @@ -118,6 +143,20 @@ function BreezForm({ handleSubmit }: SetupFormProps) {
id="greenlight-invite-code"
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"
/>
<label
htmlFor="breez-api-key"
className="mt-4 block font-medium text-gray-900 dark:text-white"
>
Breez API Key
</label>
<input
name="breez-api-key"
onChange={(e) => setBreezApiKey(e.target.value)}
value={breezApiKey}
type="password"
id="breez-api-key"
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"
/>
<label
htmlFor="mnemonic"
className="mt-4 block font-medium text-gray-900 dark:text-white"
Expand All @@ -133,12 +172,12 @@ 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"
/>
</>
<ConnectButton />
<ConnectButton isConnecting={isConnecting} />
</form>
);
}

function LNDForm({ handleSubmit }: SetupFormProps) {
function LNDForm({ isConnecting, handleSubmit }: SetupFormProps) {
const [lndAddress, setLndAddress] = React.useState<string>("");
const [lndCertHex, setLndCertHex] = React.useState<string>("");
const [lndMacaroonHex, setLndMacaroonHex] = React.useState<string>("");
Expand Down Expand Up @@ -202,7 +241,7 @@ function LNDForm({ 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"
/>
</>
<ConnectButton />
<ConnectButton isConnecting={isConnecting} />
</form>
);
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,10 @@ func (svc *Service) setupDbConfig() error {
ID: 1,
LNBackendType: svc.cfg.LNBackendType,
LNDAddress: svc.cfg.LNDAddress,
LNDCertFile: svc.cfg.LNDCertFile,
LNDCertHex: svc.cfg.LNDCertHex,
LNDMacaroonFile: svc.cfg.LNDMacaroonFile,
LNDMacaroonHex: svc.cfg.LNDMacaroonHex,
BreezMnemonic: svc.cfg.BreezMnemonic,
BreezAPIKey: svc.cfg.BreezAPIKey,
GreenlightInviteCode: svc.cfg.GreenlightInviteCode,
}
err := svc.db.Save(&newDbConfig).Error
Expand Down Expand Up @@ -267,9 +266,9 @@ func (svc *Service) launchLNBackend() error {
var lnClient LNClient
switch dbConfig.LNBackendType {
case LNDBackendType:
lnClient, err = NewLNDService(svc, dbConfig.LNDAddress, dbConfig.LNDCertFile, dbConfig.LNDCertHex, dbConfig.LNDMacaroonFile, dbConfig.LNDMacaroonHex)
lnClient, err = NewLNDService(svc, dbConfig.LNDAddress, svc.cfg.LNDCertFile, dbConfig.LNDCertHex, svc.cfg.LNDMacaroonFile, dbConfig.LNDMacaroonHex)
case BreezBackendType:
lnClient, err = NewBreezService(svc, dbConfig.BreezMnemonic, svc.cfg.BreezAPIKey, dbConfig.GreenlightInviteCode, svc.cfg.BreezWorkdir)
lnClient, err = NewBreezService(svc, dbConfig.BreezMnemonic, dbConfig.BreezAPIKey, dbConfig.GreenlightInviteCode, svc.cfg.BreezWorkdir)
default:
svc.Logger.Fatalf("Unsupported LNBackendType: %v", dbConfig.LNBackendType)
}
Expand Down
3 changes: 1 addition & 2 deletions migrations/202401161432_add_config_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ CREATE TABLE configs (
id int NOT NULL PRIMARY KEY,
ln_backend_type TEXT,
lnd_address TEXT,
lnd_cert_file TEXT,
lnd_cert_hex TEXT,
lnd_macaroon_file TEXT,
lnd_macaroon_hex TEXT,
breez_api_key TEXT,
breez_mnemonic TEXT,
greenlight_invite_code TEXT
);`).Error
Expand Down
1 change: 1 addition & 0 deletions models/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type SetupRequest struct {
LNBackendType string `json:"backendType"`
// Breez fields
BreezMnemonic string `json:"breezMnemonic"`
BreezAPIKey string `json:"breezApiKey"`
GreenlightInviteCode string `json:"greenlightInviteCode"`
// LND fields
LNDAddress string `json:"lndAddress"`
Expand Down
3 changes: 1 addition & 2 deletions models/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ type Config struct {
ID int // primary key, always 1
LNBackendType string `envconfig:"LN_BACKEND_TYPE"`
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"`
}

0 comments on commit e15b746

Please sign in to comment.