Skip to content

Commit

Permalink
enhancement: check ipfs gateway health (#2342)
Browse files Browse the repository at this point in the history
* add default ipfs gateway to profiles + migrations

* improve ipfsgateway table

* remove and set primary gateway

* add input popup

* add validation function

* rewrite ipfs gateway regarding

* fix trailing slash

* increase alpha version

* update ipfsgateways

* improve rewrite ipfs

* download nfts after changing nodes

* add IPFS health check

* add https at start if missing

* improve errormessage

---------

Co-authored-by: Nicole O'Brien <[email protected]>
  • Loading branch information
MarkNerdi and nicole-obrien authored Apr 22, 2024
1 parent 2c242a8 commit e9a2e6c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/desktop/components/popup/popups/InputPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
type InputProp = {
placeholder: string
startValue: string
validate: (text: string) => void
validate: (text: string) => Promise<void>
}
export let variant: 'primary' | 'success' | 'warning' | 'danger' | 'info' = 'primary'
export let title: string
export let description: string = ''
export let input: InputProp = { placeholder: '', startValue: '', validate: () => {} }
export let input: InputProp = { placeholder: '', startValue: '', validate: async () => {} }
export let backText: string = localize('actions.cancel')
export let confirmText: string = localize('actions.confirm')
export let onConfirm: (inputText: string) => Promise<void>
Expand All @@ -29,7 +29,7 @@
if (onConfirm) {
errorText = ''
try {
input.validate(inputText)
await input.validate(inputText)
await onConfirm(inputText)
} catch (error) {
errorText = error as string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import { Button, IconName, Pill, Text } from '@bloomwalletio/ui'
import { IpfsGatewayMenu } from './components'
import { PopupId, closePopup, openPopup } from '@desktop/auxiliary/popup'
import { isValidUrl, stripTrailingSlash } from '@core/utils'
import { addNftsToDownloadQueue } from '@core/nfts/actions'
import { selectedAccountNfts } from '@core/nfts/stores'
import { isValidUrl } from '@core/utils'
import { IPFS_HEALTH_CHECKER_PATH } from '@core/profile/constants'
function addIpfsGateway(url: string): void {
function addIpfsGateway(inputtedUrl: string): void {
const url = new URL(getOriginFromInputtedUrl(inputtedUrl)).origin
const nftSettings = $activeProfile?.settings.nfts
const ipfsGateways =
nftSettings.ipfsGateways?.map((ipfsGateway) => ({
Expand All @@ -25,10 +27,27 @@
void addNftsToDownloadQueue($selectedAccountNfts)
}
function validateIpfsGateway(url: string): void {
async function validateIpfsGateway(_url: string): Promise<void> {
const url = getOriginFromInputtedUrl(_url)
if (!url || !isValidUrl(url)) {
throw localize('error.global.invalidUrl')
return Promise.reject(localize('error.global.invalidUrl'))
}
try {
const ipfsHealthCheckUrl = new URL(IPFS_HEALTH_CHECKER_PATH, url)
const response = await fetch(ipfsHealthCheckUrl, { method: 'HEAD' })
if (!response.ok) {
return Promise.reject(localize('views.settings.ipfsGateways.addGateway.error.invalidGateway'))
}
} catch (error) {
return Promise.reject(localize('views.settings.ipfsGateways.addGateway.error.notReachable'))
}
}
function getOriginFromInputtedUrl(inputtedUrl: string): string {
return inputtedUrl.startsWith('http://') || inputtedUrl.startsWith('https://')
? inputtedUrl.trim()
: `https://${inputtedUrl.trim()}`
}
function onIpfsGatewayAdd(): void {
Expand All @@ -42,7 +61,7 @@
validate: validateIpfsGateway,
},
onConfirm: (inputText: string) => {
addIpfsGateway(stripTrailingSlash(inputText))
addIpfsGateway(inputText)
closePopup()
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/core/profile/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './default-ipfs-gateways.constant'
export * from './default-persisted-profile-object.constant'
export * from './dev-storage-directory.constant'
export * from './initial-active-profile.constant'
export * from './ipfs-health-checker-path.constant'
export * from './login-steps.constant'
export * from './max-profile-name-length.constant'
export * from './max-stronghold-password-length.constant'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const IPFS_HEALTH_CHECKER_PATH =
'/ipfs/bafybeifx7yeb55armcsxwwitkymga5xf53dxiarykms3ygqic223w5sk3m#x-ipfs-companion-no-redirect'
6 changes: 5 additions & 1 deletion packages/shared/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@
"setAsPrimary": "Set as primary",
"addGateway": {
"title": "Add new IPFS gateway",
"placeholder": "IPFS gateway"
"placeholder": "IPFS gateway",
"error": {
"notReachable": "Gateway isn't reachable",
"invalidGateway": "Invalid IPFS gateway"
}
},
"removeConfirmation": {
"title": "Remove IPFS gateway",
Expand Down

0 comments on commit e9a2e6c

Please sign in to comment.