-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from lidofinance/develop
Merge develop to main
- Loading branch information
Showing
24 changed files
with
274 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ WALLETCONNECT_PROJECT_ID= | |
|
||
# ETH Stake Widget API for IPFS mode | ||
WIDGET_API_BASE_PATH_FOR_IPFS= | ||
|
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,4 @@ | ||
{ | ||
"__warning__": "For testing purposes only", | ||
"cid": "bafybeightexodcodz3srlseidd6olah7lezz4tmyeiy7qod6oz2ol45r3i" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 @@ | ||
export { OutdatedHashBanner } from './outdated-hash-banner'; |
43 changes: 43 additions & 0 deletions
43
features/ipfs/outdated-hash-banner/outdated-hash-banner.tsx
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,43 @@ | ||
import { Button, Modal } from '@lidofinance/lido-ui'; | ||
import { dynamics } from 'config'; | ||
import { WarningIcon, Wrapper, WarningText } from './styles'; | ||
import { useIpfsHashCheck } from './use-ipfs-hash-check'; | ||
import NoSsrWrapper from 'shared/components/no-ssr-wrapper'; | ||
|
||
export const OutdatedHashBanner = dynamics.ipfsMode | ||
? () => { | ||
const { isUpdateAvailable, data, setConditionsAccepted } = | ||
useIpfsHashCheck(); | ||
|
||
return ( | ||
<NoSsrWrapper> | ||
<Modal open={isUpdateAvailable}> | ||
<Wrapper> | ||
<WarningIcon /> | ||
<WarningText> | ||
This is not the most recent version of IPFS Widget | ||
</WarningText> | ||
<a | ||
href={data.remoteCidLink} | ||
target="_self" | ||
rel="noopener noreferrer" | ||
> | ||
<Button size="sm" fullwidth variant="filled"> | ||
Get to the actual version | ||
</Button> | ||
</a> | ||
<Button | ||
size="sm" | ||
fullwidth | ||
color="error" | ||
variant="outlined" | ||
onClick={() => setConditionsAccepted(true)} | ||
> | ||
Accept risks and use the current version | ||
</Button> | ||
</Wrapper> | ||
</Modal> | ||
</NoSsrWrapper> | ||
); | ||
} | ||
: () => null; |
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,38 @@ | ||
import styled from 'styled-components'; | ||
import WarningIconSrc from 'assets/icons/attention-triangle-ipfs.svg'; | ||
import { Text } from '@lidofinance/lido-ui'; | ||
|
||
export const WarningIcon = styled.img.attrs({ | ||
src: WarningIconSrc, | ||
alt: 'warning', | ||
})` | ||
display: block; | ||
width: 58px; | ||
height: 51px; | ||
`; | ||
|
||
export const Wrapper = styled.div` | ||
display: flex; | ||
align-items: center; | ||
flex-direction: column; | ||
gap: 12px; | ||
padding: 0px; | ||
button { | ||
white-space: unset; | ||
} | ||
a { | ||
align-self: stretch; | ||
width: 100%; | ||
} | ||
`; | ||
|
||
export const WarningText = styled(Text).attrs({ | ||
weight: 700, | ||
size: 'lg', | ||
})` | ||
text-align: center; | ||
margin: 12px 0 28px; | ||
text-wrap: balance; | ||
`; |
103 changes: 103 additions & 0 deletions
103
features/ipfs/outdated-hash-banner/use-ipfs-hash-check.ts
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,103 @@ | ||
import { useLidoSWR } from '@lido-sdk/react'; | ||
import { BASE_PATH_ASSET, dynamics } from 'config'; | ||
import { useState } from 'react'; | ||
import { useMainnetStaticRpcProvider } from 'shared/hooks/use-mainnet-static-rpc-provider'; | ||
import { standardFetcher } from 'utils/standardFetcher'; | ||
import { STRATEGY_IMMUTABLE, STRATEGY_LAZY } from 'utils/swrStrategies'; | ||
|
||
type EnsHashCheckReturn = { | ||
cid: string; | ||
ens?: string; | ||
link: string; | ||
} | null; | ||
|
||
type ReleaseInfo = { | ||
cid?: string; | ||
ens?: string; | ||
}; | ||
|
||
// works with any type of IPFS hash | ||
const URL_CID_REGEX = | ||
/[/.](?<cid>Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})([./#?]|$)/; | ||
|
||
// for dev and local testing you can set to '/runtime/IPFS.json' and have file at /public/runtime/ | ||
const IPFS_RELEASE_URL = | ||
'https://raw.githubusercontent.com/lidofinance/ethereum-staking-widget/main/IPFS.json'; | ||
|
||
export const useIpfsHashCheck = () => { | ||
const [areConditionsAccepted, setConditionsAccepted] = useState(false); | ||
const provider = useMainnetStaticRpcProvider(); | ||
|
||
// local cid extraction | ||
const currentCidSWR = useLidoSWR( | ||
['swr:ipfs-cid-extraction'], | ||
async () => { | ||
const urlCid = URL_CID_REGEX.exec(window.location.href)?.groups?.cid; | ||
if (urlCid) return urlCid; | ||
const headers = await fetch(`${BASE_PATH_ASSET}/runtime/window-env.js`, { | ||
method: 'HEAD', | ||
}); | ||
return headers.headers.get('X-Ipfs-Roots'); | ||
}, | ||
{ ...STRATEGY_IMMUTABLE, isPaused: () => !dynamics.ipfsMode }, | ||
); | ||
|
||
// ens cid extraction | ||
const remoteCidSWR = useLidoSWR<EnsHashCheckReturn>( | ||
['swr:ipfs-hash-check'], | ||
async (): Promise<EnsHashCheckReturn> => { | ||
const releaseInfo = await standardFetcher<ReleaseInfo>(IPFS_RELEASE_URL, { | ||
headers: { Accept: 'application/json' }, | ||
}); | ||
if (releaseInfo.ens) { | ||
const resolver = await provider.getResolver(releaseInfo.ens); | ||
if (resolver) { | ||
const contentHash = await resolver.getContentHash(); | ||
if (contentHash) { | ||
return { | ||
cid: contentHash, | ||
ens: releaseInfo.ens, | ||
link: `https://${releaseInfo.ens}.link`, | ||
}; | ||
} | ||
} | ||
} | ||
if (releaseInfo.cid) { | ||
return { | ||
cid: releaseInfo.cid, | ||
link: `https://${releaseInfo.cid}.ipfs.cf-ipfs.com`, | ||
}; | ||
} | ||
return null; | ||
}, | ||
{ ...STRATEGY_LAZY, isPaused: () => !dynamics.ipfsMode }, | ||
); | ||
|
||
const isUpdateAvailable = Boolean( | ||
!areConditionsAccepted && | ||
remoteCidSWR.data && | ||
currentCidSWR.data && | ||
remoteCidSWR.data.cid !== currentCidSWR.data, | ||
); | ||
|
||
return { | ||
isUpdateAvailable, | ||
setConditionsAccepted, | ||
get data() { | ||
return { | ||
remoteCid: remoteCidSWR.data?.cid, | ||
currentCid: currentCidSWR.data, | ||
remoteCidLink: remoteCidSWR.data?.link, | ||
}; | ||
}, | ||
get initialLoading() { | ||
return remoteCidSWR.initialLoading || currentCidSWR.initialLoading; | ||
}, | ||
get loading() { | ||
return remoteCidSWR.loading || currentCidSWR.loading; | ||
}, | ||
get error() { | ||
return remoteCidSWR.error || currentCidSWR.error; | ||
}, | ||
}; | ||
}; |
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
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
Oops, something went wrong.