Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
fix: Implement CustomIpfsHttpHandler for metadata retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Diaz committed Feb 17, 2021
1 parent d06cf5c commit f097583
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
16 changes: 8 additions & 8 deletions client/src/lib/system.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib/util/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 35 additions & 0 deletions client/src/lib/util/taquito-custom-ipfs-http-handler.ts
Original file line number Diff line number Diff line change
@@ -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<ContractProvider | Wallet>,
{ location }: Tzip16Uri,
_context: Context
): Promise<string> {
return this._httpBackend.createRequest<string>({
url: `${this._gatewayProtocol}://${
this._ipfsGateway
}/ipfs/${location.substring(2)}/`,
method: 'GET',
headers: { 'Content-Type': 'text/plain' },
mimeType: 'text; charset=utf-8',
json: false
});
}
}

0 comments on commit f097583

Please sign in to comment.