diff --git a/client/src/lib/system.ts b/client/src/lib/system.ts index 22cf4b5a..4972e133 100644 --- a/client/src/lib/system.ts +++ b/client/src/lib/system.ts @@ -1,10 +1,7 @@ import { TezosToolkit, Context } from '@taquito/taquito'; import { BeaconWallet } from '@taquito/beacon-wallet'; -import { - MetadataProvider, - DEFAULT_HANDLERS, - IpfsHttpHandler -} from '@taquito/tzip16'; +import { MetadataProvider, DEFAULT_HANDLERS } from '@taquito/tzip16'; +import CustomIpfsHttpHandler from './util/taquito-custom-ipfs-http-handler'; import { BetterCallDev } from './service/bcd'; import * as tzUtils from './util/tezosToolkit'; import { DAppClientOptions, NetworkType } from '@airgap/beacon-sdk'; @@ -79,10 +76,13 @@ function createMetadataResolver( contractAddress: string ): ResolveMetadata { const ipfsGateway = - system.config.network === 'sandbox' - ? 'localhost:5001' + system.config.network === 'sandboxnet' + ? 'localhost:8080' : 'cloudflare-ipfs.com'; - DEFAULT_HANDLERS.set('ipfs', new IpfsHttpHandler(ipfsGateway)); + const gatewayProtocol = + system.config.network === 'sandboxnet' ? 'http' : 'https'; + const ipfsHandler = new CustomIpfsHttpHandler(ipfsGateway, gatewayProtocol); + DEFAULT_HANDLERS.set('ipfs', ipfsHandler); const provider = new MetadataProvider(DEFAULT_HANDLERS); const context = new Context(toolkit.rpc); // This is a performance optimization: We're only resolving off-chain diff --git a/client/src/lib/util/ipfs.ts b/client/src/lib/util/ipfs.ts index 57dc536e..099bb0de 100644 --- a/client/src/lib/util/ipfs.ts +++ b/client/src/lib/util/ipfs.ts @@ -35,8 +35,8 @@ export function ipfsUriToCid(uri: string) { export function ipfsUriToGatewayUrl(network: string, uri: string) { const ipfsHost = - network === 'sandbox' - ? 'http://localhost:5001' + network === 'sandboxnet' + ? 'http://localhost:8080' : 'https://cloudflare-ipfs.com'; const cid = ipfsUriToCid(uri); return cid ? `${ipfsHost}/ipfs/${cid}` : uri; diff --git a/client/src/lib/util/taquito-custom-ipfs-http-handler.ts b/client/src/lib/util/taquito-custom-ipfs-http-handler.ts new file mode 100644 index 00000000..6627228c --- /dev/null +++ b/client/src/lib/util/taquito-custom-ipfs-http-handler.ts @@ -0,0 +1,35 @@ +import { Handler, Tzip16Uri } from '@taquito/tzip16'; +import { HttpBackend } from '@taquito/http-utils'; +import { + ContractAbstraction, + ContractProvider, + Wallet, + Context +} from '@taquito/taquito'; + +export default class CustomIpfsHttpHandler implements Handler { + private _ipfsGateway: string; + private _gatewayProtocol: string; + private _httpBackend = new HttpBackend(); + + constructor(ipfsGatheway?: string, gatewayProtocol?: string) { + this._ipfsGateway = ipfsGatheway ? ipfsGatheway : 'ipfs.io'; + this._gatewayProtocol = gatewayProtocol ? gatewayProtocol : 'https'; + } + + async getMetadata( + _contractAbstraction: ContractAbstraction, + { location }: Tzip16Uri, + _context: Context + ): Promise { + return this._httpBackend.createRequest({ + url: `${this._gatewayProtocol}://${ + this._ipfsGateway + }/ipfs/${location.substring(2)}/`, + method: 'GET', + headers: { 'Content-Type': 'text/plain' }, + mimeType: 'text; charset=utf-8', + json: false + }); + } +}