This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
forked from getAlby/nostr-wallet-connect
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move address storage to backend and return last unused address
- Loading branch information
Showing
11 changed files
with
204 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func (api *api) RequestEsploraApi(endpoint string) (interface{}, error) { | ||
url := api.svc.GetConfig().GetEnv().LDKEsploraServer + endpoint | ||
|
||
client := http.Client{ | ||
Timeout: time.Second * 10, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
api.logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to create http request") | ||
return nil, err | ||
} | ||
|
||
res, err := client.Do(req) | ||
if err != nil { | ||
api.logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to send request") | ||
return nil, err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
body, readErr := io.ReadAll(res.Body) | ||
if readErr != nil { | ||
api.logger.WithError(err).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to read response body") | ||
return nil, errors.New("failed to read response body") | ||
} | ||
|
||
var jsonContent interface{} | ||
jsonErr := json.Unmarshal(body, &jsonContent) | ||
if jsonErr != nil { | ||
api.logger.WithError(jsonErr).WithFields(logrus.Fields{ | ||
"url": url, | ||
}).Error("Failed to deserialize json") | ||
return nil, fmt.Errorf("failed to deserialize json %s %s", url, string(body)) | ||
} | ||
return jsonContent, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import useSWR from "swr"; | ||
|
||
import React from "react"; | ||
import { useCSRF } from "src/hooks/useCSRF"; | ||
import { GetOnchainAddressResponse } from "src/types"; | ||
import { request } from "src/utils/request"; | ||
import { swrFetcher } from "src/utils/swr"; | ||
|
||
export function useOnchainAddress() { | ||
const { data: csrf } = useCSRF(); | ||
const swr = useSWR<string>("/api/wallet/address", swrFetcher); | ||
const [isLoading, setLoading] = React.useState(false); | ||
|
||
const getNewAddress = React.useCallback(async () => { | ||
if (!csrf) { | ||
return; | ||
} | ||
setLoading(true); | ||
try { | ||
const response = await request<GetOnchainAddressResponse>( | ||
"/api/wallet/new-address", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"X-CSRF-Token": csrf, | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
); | ||
if (!response?.address) { | ||
throw new Error("No address in response"); | ||
} | ||
swr.mutate(response.address, false); | ||
} catch (error) { | ||
alert("Failed to request a new address: " + error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [csrf, swr]); | ||
|
||
return React.useMemo( | ||
() => ({ | ||
...swr, | ||
getNewAddress, | ||
loadingAddress: isLoading || !swr.data, | ||
}), | ||
[swr, getNewAddress, isLoading] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.