Skip to content

Commit 0eb122d

Browse files
authored
fix: gateway authorization based on env (#174)
In order to create new spaces in `Staging` or `localhost` environments we need to configure the w3up-client to authorize the correct gateway service, otherwise, it will default to the Production Storacha Gateway. This PR implements the logic to create the gateway connection based on the environment variables and passes that connection to the `w3up-client.createSpace` call, so the correct service is authorized.
1 parent 597240f commit 0eb122d

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

.env.tpl

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ 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
66

7+
# set these to your gateway service URL and DID
8+
NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus
9+
NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link
10+
711
# set these to values from Stripe settings
812
NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCeiEF6A5ufQX5vPFlWRkPm
913
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC

.github/workflows/deploy-storacha.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ jobs:
4040
echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://staging.up.storacha.network" >> .env
4141
echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.storacha.network/receipt/" >> .env
4242
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env
43+
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env
44+
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env
4345
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env
4446
echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QIDHGF6A5ufQX5vOK9Xl8Up" >> .env
4547
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env
@@ -135,6 +137,8 @@ jobs:
135137
echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://up.storacha.network" >> .env
136138
echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://up.storacha.network/receipt/" >> .env
137139
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env
140+
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env
141+
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env
138142
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env
139143
echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QPYsuF6A5ufQX5vdIGAe54g" >> .env
140144
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env

.github/workflows/deploy.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ jobs:
4040
echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://staging.up.web3.storage" >> .env
4141
echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.web3.storage/receipt/" >> .env
4242
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env
43+
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env
44+
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env
4345
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env
4446
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env
4547
echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/test_6oE29Gff99KO6mk8ww" >> .env
@@ -128,6 +130,8 @@ jobs:
128130
echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://up.web3.storage" >> .env
129131
echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://up.web3.storage/receipt/" >> .env
130132
echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env
133+
echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env
134+
echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env
131135
echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env
132136
echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env
133137
echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/cN22aA62U6bO1sA9AA" >> .env

src/components/SpaceCreator.tsx

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import type { ChangeEvent } from 'react'
22

33
import React, { useState } from 'react'
4-
import { Space, useW3 } from '@w3ui/react'
4+
import { ContentServeService, Space, useW3 } from '@w3ui/react'
55
import Loader from '../components/Loader'
6-
import { DID, DIDKey } from '@ucanto/interface'
6+
import { DIDKey } from '@ucanto/interface'
77
import { DidIcon } from './DidIcon'
88
import Link from 'next/link'
99
import { FolderPlusIcon, InformationCircleIcon } from '@heroicons/react/24/outline'
1010
import Tooltip from './Tooltip'
1111
import { H3 } from './Text'
12+
import * as UcantoClient from '@ucanto/client'
13+
import { HTTP } from '@ucanto/transport'
14+
import * as CAR from '@ucanto/transport/car'
1215

1316
export function SpaceCreatorCreating(): JSX.Element {
1417
return (
@@ -50,11 +53,28 @@ export function SpaceCreatorForm({
5053
throw new Error('a payment plan is required on account to provision a new space.')
5154
}
5255

56+
const toWebDID = (input?: string) =>
57+
UcantoClient.Schema.DID.match({ method: 'web' }).from(input)
58+
5359
setSubmitted(true)
5460
try {
55-
const space = await client.createSpace(name)
5661

57-
const provider = (process.env.NEXT_PUBLIC_W3UP_PROVIDER || 'did:web:web3.storage') as DID<'web'>
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'
64+
65+
const storachaGateway = UcantoClient.connect({
66+
id: {
67+
did: () => gatewayId
68+
},
69+
codec: CAR.outbound,
70+
channel: HTTP.open<ContentServeService>({ url: new URL(gatewayUrl) }),
71+
})
72+
73+
const space = await client.createSpace(name, {
74+
authorizeGatewayServices: [storachaGateway]
75+
})
76+
77+
const provider = toWebDID(process.env.NEXT_PUBLIC_W3UP_PROVIDER) || toWebDID('did:web:web3.storage')
5878
const result = await account.provision(space.did(), { provider })
5979
if (result.error) {
6080
setSubmitted(false)

0 commit comments

Comments
 (0)