From 7cb82b8e85c280101cc6232717cb533e6b60444a Mon Sep 17 00:00:00 2001 From: AugustoL Date: Thu, 24 Nov 2022 09:01:14 -0300 Subject: [PATCH] fix(ipfshashes): now save decoded ipfsHash on cache and decode when loading, supports v1 format --- src/pages/Proposal.tsx | 30 ++++++++++++++---------------- src/services/CacheService.ts | 9 +++------ src/services/IPFSService.ts | 21 ++++++++++++--------- src/utils/ipfs.ts | 22 ++++------------------ 4 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/pages/Proposal.tsx b/src/pages/Proposal.tsx index 52f81a0ce6..62526b6454 100644 --- a/src/pages/Proposal.tsx +++ b/src/pages/Proposal.tsx @@ -6,7 +6,6 @@ import { useContext } from '../contexts'; import MDEditor from '@uiw/react-md-editor'; import { useHistory } from 'react-router-dom'; -import contentHash from 'content-hash'; import { Box, Title } from '../components/common'; import { @@ -17,6 +16,7 @@ import { Stakes, Details, } from 'components/Proposal'; +import { descriptionHashToIPFSHash } from 'utils'; const WarningMessage = styled.span` margin: 2px 0px; @@ -89,19 +89,19 @@ const ProposalPage = observer(() => { const proposalEvents = daoStore.getProposalEvents(proposalId); + const ipfsHash = descriptionHashToIPFSHash(proposal.descriptionHash); + // @ts-ignore try { if (proposalDescription === '## Getting proposal description from IPFS...') - ipfsService - .getContentFromIPFS(contentHash.decode(proposal.descriptionHash)) - .then(data => { - try { - setProposalTitle(data.title); - setProposalDescription(data.description); - } catch (error) { - setProposalDescription(data); - } - }); + ipfsService.getContentFromIPFS(ipfsHash).then(data => { + try { + setProposalTitle(data.title); + setProposalDescription(data.description); + } catch (error) { + setProposalDescription(data); + } + }); } catch (error) { console.error('[IPFS ERROR]', error); setProposalTitle('Error getting proposal title from ipfs'); @@ -123,18 +123,16 @@ const ProposalPage = observer(() => { skipHtml escapeHtml /> - {proposal.descriptionHash.length > 0 && ( + {ipfsHash.length > 0 && (

IPFS Document:{' '} - ipfs://{contentHash.decode(proposal.descriptionHash)} + ipfs://{ipfsHash}

diff --git a/src/services/CacheService.ts b/src/services/CacheService.ts index 5a045bd69d..6adca639e6 100644 --- a/src/services/CacheService.ts +++ b/src/services/CacheService.ts @@ -23,7 +23,6 @@ import { executeMulticall, sortNetworkCache, descriptionHashToIPFSHash, - ipfsHashToDescriptionHash, getSchemeConfig, } from '../utils'; @@ -896,7 +895,6 @@ export default class UtilsService { Object.keys(networkCache.proposals)[proposalIndex] ]; const ipfsHash = descriptionHashToIPFSHash(proposal.descriptionHash); - // TODO: Move this somewhere else later. const invalidTitleProposals = [ '0xbd5a578170b28eedb9ed05adcd7a904180a18178a7fee5627640bce217601f60', @@ -912,6 +910,7 @@ export default class UtilsService { '0xdef15e241a2dcc52c6ec1970b8e2f6cd13dd9f85f63d9702c78881dacafb6f34', '0xfda75410e3f54bca6828995cce7864fdf5f2961510c0515835b4d06c87f5754e', '0xfb15b6f9e3bf61099d20bb3b39375d4e2a6f7ac3c72179537ce147ed991d61b4', + '0xe2f86b3545a1266c98d57bf828296a5b00b7dc39ec2d33e4b7008edc24c7f00e', ]; // If the script is running on the client side and it already tried once, or has the title, continue. @@ -931,7 +930,7 @@ export default class UtilsService { proposal.descriptionHash.length > 0 && // Try to get title if cache is running in node script or if proposal was submitted in last 100000 blocks proposal.title?.length === 0 && - proposal.creationEvent.blockNumber > networkCache.blockNumber - 100000 + proposal.creationEvent.blockNumber > networkCache.blockNumber - 1000000 ) try { console.debug( @@ -1543,9 +1542,7 @@ export default class UtilsService { creationLogDecoded._descriptionHash !== ZERO_HASH ) { schemeProposalInfo.descriptionHash = - ipfsHashToDescriptionHash( - creationLogDecoded._descriptionHash - ); + creationLogDecoded._descriptionHash; } } }); diff --git a/src/services/IPFSService.ts b/src/services/IPFSService.ts index 30da3d8855..47dfdfbbec 100644 --- a/src/services/IPFSService.ts +++ b/src/services/IPFSService.ts @@ -49,15 +49,18 @@ export default class IPFSService { } async getContentFromIPFS(hash: string, timeout = 60000) { - const gatewayURLBaseList = [ - 'https://dxgov.mypinata.cloud/ipfs/', - 'https://davi.mypinata.cloud/ipfs/', - 'https://ipfs.io/ipfs/', - 'https://gateway.ipfs.io/ipfs/', - 'https://cloudflare-ipfs.com/ipfs/', - 'https://dweb.link/ipfs/', - 'https://infura-ipfs.io/ipfs/', - ]; + const gatewayURLBaseList = + hash.substring(0, 2) == 'Qm' + ? [ + 'https://dxgov.mypinata.cloud/ipfs/', + 'https://davi.mypinata.cloud/ipfs/', + 'https://ipfs.io/ipfs/', + 'https://gateway.ipfs.io/ipfs/', + 'https://cloudflare-ipfs.com/ipfs/', + 'https://dweb.link/ipfs/', + 'https://infura-ipfs.io/ipfs/', + ] + : ['https://w3s.link/ipfs/']; const response = await Promise.any( gatewayURLBaseList.map(gatewayURLBase => diff --git a/src/utils/ipfs.ts b/src/utils/ipfs.ts index f85c178ff5..b1766ce070 100644 --- a/src/utils/ipfs.ts +++ b/src/utils/ipfs.ts @@ -2,26 +2,12 @@ import contentHash from 'content-hash'; export const descriptionHashToIPFSHash = function (descriptionHash) { try { - if (contentHash.getCodec(descriptionHash) === 'ipfs-ns') + if (contentHash.getCodec(descriptionHash) === 'ipfs-ns') { return contentHash.decode(descriptionHash); - else if ( - descriptionHash.length > 1 && - descriptionHash.substring(0, 2) !== 'Qm' - ) + } else { return descriptionHash; - else return ''; + } } catch (error) { - return ''; - } -}; - -export const ipfsHashToDescriptionHash = function (ipfsHash) { - try { - if (ipfsHash.length > 1 && ipfsHash.substring(0, 2) === 'Qm') - return contentHash.fromIpfs(ipfsHash); - else if (contentHash.getCodec(ipfsHash) === 'ipfs-ns') return ipfsHash; - else return ''; - } catch (error) { - return ''; + return descriptionHash; } };