Skip to content

Commit

Permalink
fix: link to alby account using correct app wallet service pubkey (#858)
Browse files Browse the repository at this point in the history
* fix: link to alby account using correct app wallet service pubkey

* chore: remove unnecessary parameter in create node request
  • Loading branch information
rolznz authored Dec 8, 2024
1 parent 1c2caa9 commit a0c5bf3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
52 changes: 36 additions & 16 deletions alby/alby_oauth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ func (svc *albyOAuthService) LinkAccount(ctx context.Context, lnClient lnclient.
"app": app,
}).Info("Created alby app connection")

err = svc.activateAlbyAccountNWCNode(ctx)
err = svc.activateAlbyAccountNWCNode(ctx, *app.WalletPubkey)
if err != nil {
logger.Logger.WithError(err).Error("Failed to activate alby account nwc node")
return err
Expand Down Expand Up @@ -883,14 +883,9 @@ func (svc *albyOAuthService) createAlbyAccountNWCNode(ctx context.Context) (stri
client := svc.oauthConf.Client(ctx, token)

type createNWCNodeRequest struct {
WalletPubkey string `json:"wallet_pubkey"`
RelayUrl string `json:"relay_url"`
}

createNodeRequest := createNWCNodeRequest{
WalletPubkey: svc.keys.GetNostrPublicKey(),
RelayUrl: svc.cfg.GetRelayUrl(),
}
createNodeRequest := createNWCNodeRequest{}

body := bytes.NewBuffer([]byte{})
err = json.NewEncoder(body).Encode(&createNodeRequest)
Expand All @@ -910,16 +905,13 @@ func (svc *albyOAuthService) createAlbyAccountNWCNode(ctx context.Context) (stri

resp, err := client.Do(req)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"createNodeRequest": createNodeRequest,
}).WithError(err).Error("Failed to send request to /internal/nwcs")
logger.Logger.WithError(err).Error("Failed to send request to /internal/nwcs")
return "", err
}

if resp.StatusCode >= 300 {
logger.Logger.WithFields(logrus.Fields{
"createNodeRequest": createNodeRequest,
"status": resp.StatusCode,
"status": resp.StatusCode,
}).Error("Request to /internal/nwcs returned non-success status")
return "", errors.New("request to /internal/nwcs returned non-success status")
}
Expand Down Expand Up @@ -976,15 +968,28 @@ func (svc *albyOAuthService) destroyAlbyAccountNWCNode(ctx context.Context) erro
return nil
}

func (svc *albyOAuthService) activateAlbyAccountNWCNode(ctx context.Context) error {
func (svc *albyOAuthService) activateAlbyAccountNWCNode(ctx context.Context, walletServicePubkey string) error {
token, err := svc.fetchUserToken(ctx)
if err != nil {
logger.Logger.WithError(err).Error("Failed to fetch user token")
}

client := svc.oauthConf.Client(ctx, token)

req, err := http.NewRequest("PUT", fmt.Sprintf("%s/internal/nwcs/activate", albyOAuthAPIURL), nil)
type activateNWCNodeRequest struct {
WalletPubkey string `json:"wallet_pubkey"`
RelayUrl string `json:"relay_url"`
}

activateNodeRequest := activateNWCNodeRequest{
WalletPubkey: walletServicePubkey,
RelayUrl: svc.cfg.GetRelayUrl(),
}

body := bytes.NewBuffer([]byte{})
err = json.NewEncoder(body).Encode(&activateNodeRequest)

req, err := http.NewRequest("PUT", fmt.Sprintf("%s/internal/nwcs/activate", albyOAuthAPIURL), body)
if err != nil {
logger.Logger.WithError(err).Error("Error creating request /internal/nwcs/activate")
return err
Expand All @@ -994,13 +999,28 @@ func (svc *albyOAuthService) activateAlbyAccountNWCNode(ctx context.Context) err

resp, err := client.Do(req)
if err != nil {
logger.Logger.WithError(err).Error("Failed to send request to /internal/nwcs/activate")
logger.Logger.WithFields(logrus.Fields{
"activate_node_request": activateNodeRequest,
}).WithError(err).Error("Failed to send request to /internal/nwcs/activate")
return err
}

if resp.StatusCode >= 300 {
bodyString := ""
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"activate_node_request": activateNodeRequest,
"status": resp.StatusCode,
}).Error("Failed to read response body from to /internal/nwcs/activate")
}
if bodyBytes != nil {
bodyString = string(bodyBytes)
}

logger.Logger.WithFields(logrus.Fields{
"status": resp.StatusCode,
"status": resp.StatusCode,
"message": bodyString,
}).Error("Request to /internal/nwcs/activate returned non-success status")
return errors.New("request to /internal/nwcs/activate returned non-success status")
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/connections/AlbyConnectionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ function AlbyConnectionCard({ connection }: { connection?: App }) {
</div>
<div className="flex flex-col sm:flex-row gap-3 sm:items-center">
{loadingLinkStatus && <Loading />}
{!connection || linkStatus === LinkStatus.SharedNode ? (
{!connection ||
linkStatus === LinkStatus.SharedNode ||
linkStatus === LinkStatus.Unlinked ? (
<Dialog>
<DialogTrigger asChild>
<LoadingButton loading={loading}>
Expand Down
7 changes: 5 additions & 2 deletions frontend/src/hooks/useLinkAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export enum LinkStatus {
SharedNode,
ThisNode,
OtherNode,
Unlinked,
}

export function useLinkAccount() {
Expand All @@ -22,12 +23,14 @@ export function useLinkAccount() {

let linkStatus: LinkStatus | undefined;
if (me && nodeConnectionInfo) {
if (me?.keysend_pubkey === nodeConnectionInfo.pubkey) {
if (me.keysend_pubkey === nodeConnectionInfo.pubkey) {
linkStatus = LinkStatus.ThisNode;
} else if (me.shared_node) {
linkStatus = LinkStatus.SharedNode;
} else {
} else if (me.keysend_pubkey) {
linkStatus = LinkStatus.OtherNode;
} else {
linkStatus = LinkStatus.Unlinked;
}
}

Expand Down

0 comments on commit a0c5bf3

Please sign in to comment.