Skip to content

Commit 1defed6

Browse files
authored
fix: set ipfs gateway url based on env var (#177)
### Context - When creating a space, the system will use an environment variable to determine which Gateway is authorized to serve its content. If the environment variable is not defined, the Storacha Production Gateway will be authorized by default. - If the Staging Gateway is specified, IPFS links must point to `ipfs-staging.w3s.link` instead of `ipfs.w3s.link`. Otherwise, requests will bypass the correct service, preventing proper validation of the content authorization flow. ### Changes - Refactored the code to dynamically read the IPFS Gateway URL from the environment variable and construct the appropriate URL for content retrieval. - Updated deployment configuration files to include the new environment variable for Gateway selection. - Resolved a minor compilation issue in `nft-storage.ts` and `web3-storage.ts`.
1 parent 5194dd2 commit 1defed6

File tree

9 files changed

+20
-10
lines changed

9 files changed

+20
-10
lines changed

.env.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ NEXT_PUBLIC_W3UP_SERVICE_URL=https://staging.up.web3.storage
33
NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.web3.storage/receipt/
44
NEXT_PUBLIC_W3UP_SERVICE_DID=did:web:staging.web3.storage
55
NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage
6+
NEXT_PUBLIC_IPFS_GATEWAY_URL=https://%ROOT_CID%.ipfs-staging.w3s.link
67

78
# set these to your gateway service URL and DID
89
NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus

.github/workflows/deploy-storacha.yml

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env
4343
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env
4444
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env
45+
echo "NEXT_PUBLIC_IPFS_GATEWAY_URL=https://%ROOT_CID%.ipfs-staging.w3s.link" >> .env
4546
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env
4647
echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QIDHGF6A5ufQX5vOK9Xl8Up" >> .env
4748
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env
@@ -139,6 +140,7 @@ jobs:
139140
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env
140141
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env
141142
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env
143+
echo "NEXT_PUBLIC_IPFS_GATEWAY_URL=https://%ROOT_CID%.ipfs.w3s.link" >> .env
142144
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env
143145
echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QPYsuF6A5ufQX5vdIGAe54g" >> .env
144146
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env

.github/workflows/deploy.yml

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env
4343
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env
4444
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env
45+
echo "NEXT_PUBLIC_IPFS_GATEWAY_URL=https://%ROOT_CID%.ipfs-staging.w3s.link" >> .env
4546
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env
4647
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env
4748
echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/test_6oE29Gff99KO6mk8ww" >> .env
@@ -132,6 +133,7 @@ jobs:
132133
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env
133134
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env
134135
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env
136+
echo "NEXT_PUBLIC_IPFS_GATEWAY_URL=https://%ROOT_CID%.ipfs.w3s.link" >> .env
135137
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env
136138
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env
137139
echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/cN22aA62U6bO1sA9AA" >> .env

src/app/space/[did]/root/[cid]/page.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import CopyIcon from '@/components/CopyIcon'
1313
import { Breadcrumbs } from '@/components/Breadcrumbs'
1414
import { useRouter } from 'next/navigation'
1515
import { createUploadsListKey } from '@/cache'
16+
import { ipfsGatewayURL } from '@/components/services'
1617

1718
interface PageProps {
1819
params: {
@@ -58,7 +59,7 @@ export default function ItemPage ({ params }: PageProps): JSX.Element {
5859
router.replace(`/space/${spaceDID}`)
5960
}
6061

61-
const url = `https://${root}.ipfs.w3s.link`
62+
const url = ipfsGatewayURL(root)
6263
return (
6364
<div>
6465
<Breadcrumbs space={space.did()} root={root} />

src/components/SpaceCreator.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { H3 } from './Text'
1212
import * as UcantoClient from '@ucanto/client'
1313
import { HTTP } from '@ucanto/transport'
1414
import * as CAR from '@ucanto/transport/car'
15+
import { gatewayHost } from './services'
1516

1617
export function SpaceCreatorCreating(): JSX.Element {
1718
return (
@@ -59,15 +60,14 @@ export function SpaceCreatorForm({
5960
setSubmitted(true)
6061
try {
6162

62-
const gatewayId = toWebDID(process.env.NEXT_PUBLIC_W3UP_GATEWAY_ID) || toWebDID('did:web:w3s.link')
63-
const gatewayUrl = process.env.NEXT_PUBLIC_W3UP_GATEWAY_HOST || 'https://w3s.link'
63+
const gatewayId = toWebDID(process.env.NEXT_PUBLIC_W3UP_GATEWAY_ID) ?? toWebDID('did:web:w3s.link')
6464

6565
const storachaGateway = UcantoClient.connect({
6666
id: {
6767
did: () => gatewayId
6868
},
6969
codec: CAR.outbound,
70-
channel: HTTP.open<ContentServeService>({ url: new URL(gatewayUrl) }),
70+
channel: HTTP.open<ContentServeService>({ url: new URL(gatewayHost) }),
7171
})
7272

7373
const space = await client.createSpace(name, {

src/components/Uploader.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
WrapInDirectoryCheckbox,
1313
useUploader
1414
} from '@w3ui/react'
15-
import { gatewayHost } from '../components/services'
15+
import { ipfsGatewayURL } from '../components/services'
1616
import { useEffect, useState } from 'react'
1717
import { RadioGroup } from '@headlessui/react'
1818
import { H2 } from './Text'
@@ -94,7 +94,7 @@ export const Done = ({ dataCID }: DoneProps): JSX.Element => {
9494
<H2>Uploaded</H2>
9595
<a
9696
className='font-mono text-xs max-w-full overflow-hidden no-wrap text-ellipsis'
97-
href={`https://${cid}.ipfs.${gatewayHost}/`}
97+
href={ipfsGatewayURL(cid)}
9898
>
9999
{cid}
100100
</a>

src/components/services.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Service } from '@w3ui/react'
1+
import type { Service, UnknownLink } from '@w3ui/react'
22
import { connect } from '@ucanto/client'
33
import { CAR, HTTP } from '@ucanto/transport'
44
import * as DID from '@ipld/dag-ucan/did'
@@ -19,6 +19,11 @@ export const servicePrincipal = DID.parse(
1919
process.env.NEXT_PUBLIC_W3UP_SERVICE_DID ?? 'did:web:web3.storage'
2020
)
2121

22+
export const ipfsGatewayURL = (rootCID: UnknownLink | string) => new URL(
23+
// 'https://%ROOT_CID%.ipfs.w3s.link' or 'https://%ROOT_CID%.ipfs-staging.w3s.link'
24+
process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL?.replace('%ROOT_CID%', rootCID.toString()) ?? `https://${rootCID}.ipfs.w3s.link`
25+
).toString()
26+
2227
export const serviceConnection = connect<Service>({
2328
id: servicePrincipal,
2429
codec: CAR.outbound,
@@ -28,4 +33,4 @@ export const serviceConnection = connect<Service>({
2833
}),
2934
})
3035

31-
export const gatewayHost = process.env.NEXT_PUBLIC_W3UP_GATEWAY_HOST ?? 'w3s.link'
36+
export const gatewayHost = process.env.NEXT_PUBLIC_W3UP_GATEWAY_HOST ?? 'https://w3s.link'

src/lib/migrations/nft-storage.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Reader {
101101
//
102102
// If so we should be able to get a location claim, and key in the
103103
// claim should be the CAR CID.
104-
const claims = await Claims.read(root)
104+
const claims = await Claims.read(root.multihash)
105105
const locationClaims = []
106106
for (const c of claims) {
107107
if (c.type === 'assert/location') {

src/lib/migrations/web3-storage.ts

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class Reader {
5050
}
5151

5252
const root = Link.parse(raw.cid)
53-
// @ts-expect-error not in client types
5453
const parts: string[] = raw.parts
5554

5655
const shards: Shard[] = []

0 commit comments

Comments
 (0)