Skip to content

Commit

Permalink
Merge pull request #942 from AugustoL/v1.0.18
Browse files Browse the repository at this point in the history
Small refactor on ipfs proposal description hash
  • Loading branch information
AugustoL authored Nov 24, 2022
2 parents d5fa1be + 7cb82b8 commit 3923b37
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 49 deletions.
30 changes: 14 additions & 16 deletions src/pages/Proposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -17,6 +16,7 @@ import {
Stakes,
Details,
} from 'components/Proposal';
import { descriptionHashToIPFSHash } from 'utils';

const WarningMessage = styled.span`
margin: 2px 0px;
Expand Down Expand Up @@ -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');
Expand All @@ -123,18 +123,16 @@ const ProposalPage = observer(() => {
skipHtml
escapeHtml
/>
{proposal.descriptionHash.length > 0 && (
{ipfsHash.length > 0 && (
<h3 style={{ margin: '0px' }}>
<small>
IPFS Document:{' '}
<a
target="_blank"
href={`https://ipfs.io/ipfs/${contentHash.decode(
proposal.descriptionHash
)}`}
href={`https://ipfs.io/ipfs/${ipfsHash}`}
rel="noreferrer"
>
ipfs://{contentHash.decode(proposal.descriptionHash)}
ipfs://{ipfsHash}
</a>
</small>
</h3>
Expand Down
9 changes: 3 additions & 6 deletions src/services/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
executeMulticall,
sortNetworkCache,
descriptionHashToIPFSHash,
ipfsHashToDescriptionHash,
getSchemeConfig,
} from '../utils';

Expand Down Expand Up @@ -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',
Expand All @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -1543,9 +1542,7 @@ export default class UtilsService {
creationLogDecoded._descriptionHash !== ZERO_HASH
) {
schemeProposalInfo.descriptionHash =
ipfsHashToDescriptionHash(
creationLogDecoded._descriptionHash
);
creationLogDecoded._descriptionHash;
}
}
});
Expand Down
21 changes: 12 additions & 9 deletions src/services/IPFSService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
22 changes: 4 additions & 18 deletions src/utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

0 comments on commit 3923b37

Please sign in to comment.