From 0eb122de6923b9abd8a67334f90f5ebc9fb17f4a Mon Sep 17 00:00:00 2001 From: Felipe Forbeck Date: Tue, 7 Jan 2025 16:48:51 -0300 Subject: [PATCH] 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. --- .env.tpl | 4 ++++ .github/workflows/deploy-storacha.yml | 4 ++++ .github/workflows/deploy.yml | 4 ++++ src/components/SpaceCreator.tsx | 28 +++++++++++++++++++++++---- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.env.tpl b/.env.tpl index e659641..b00872b 100644 --- a/.env.tpl +++ b/.env.tpl @@ -4,6 +4,10 @@ NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.web3.storage/receipt/ NEXT_PUBLIC_W3UP_SERVICE_DID=did:web:staging.web3.storage NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage +# set these to your gateway service URL and DID +NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus +NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link + # set these to values from Stripe settings NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCeiEF6A5ufQX5vPFlWRkPm NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC diff --git a/.github/workflows/deploy-storacha.yml b/.github/workflows/deploy-storacha.yml index 3664654..b816a2d 100644 --- a/.github/workflows/deploy-storacha.yml +++ b/.github/workflows/deploy-storacha.yml @@ -40,6 +40,8 @@ jobs: echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://staging.up.storacha.network" >> .env echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.storacha.network/receipt/" >> .env echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QIDHGF6A5ufQX5vOK9Xl8Up" >> .env echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env @@ -135,6 +137,8 @@ jobs: echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://up.storacha.network" >> .env echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://up.storacha.network/receipt/" >> .env echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env echo "NEXT_PUBLIC_STRIPE_TRIAL_PRICING_TABLE_ID=prctbl_1QPYsuF6A5ufQX5vdIGAe54g" >> .env echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ec2cdb2..fab0334 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -40,6 +40,8 @@ jobs: echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://staging.up.web3.storage" >> .env echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://staging.up.web3.storage/receipt/" >> .env echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:staging.web3.storage" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://freeway-staging.dag.haus" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:staging.w3s.link" >> .env echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1NzhdvF6A5ufQX5vKNZuRhie" >> .env echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_51LO87hF6A5ufQX5viNsPTbuErzfavdrEFoBuaJJPfoIhzQXdOUdefwL70YewaXA32ZrSRbK4U4fqebC7SVtyeNcz00qmgNgueC" >> .env echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/test_6oE29Gff99KO6mk8ww" >> .env @@ -128,6 +130,8 @@ jobs: echo "NEXT_PUBLIC_W3UP_SERVICE_URL=https://up.web3.storage" >> .env echo "NEXT_PUBLIC_W3UP_RECEIPTS_URL=https://up.web3.storage/receipt/" >> .env echo "NEXT_PUBLIC_W3UP_PROVIDER=did:web:web3.storage" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_HOST=https://w3s.link" >> .env + echo "NEXT_PUBLIC_W3UP_GATEWAY_ID=did:web:w3s.link" >> .env echo "NEXT_PUBLIC_STRIPE_PRICING_TABLE_ID=prctbl_1OCJ1qF6A5ufQX5vM5DWg4rA" >> .env echo "NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_live_51LO87hF6A5ufQX5vQTO5BHyz8y9ybJp4kg1GsBjYuqwluuwtQTkbeZzkoQweFQDlv7JaGjuIdUWAyuwXp3tmCfsM005lJK9aS8" >> .env echo "NEXT_PUBLIC_STRIPE_CUSTOMER_PORTAL_LINK=https://billing.stripe.com/p/login/cN22aA62U6bO1sA9AA" >> .env diff --git a/src/components/SpaceCreator.tsx b/src/components/SpaceCreator.tsx index db44fa3..15ada8d 100644 --- a/src/components/SpaceCreator.tsx +++ b/src/components/SpaceCreator.tsx @@ -1,14 +1,17 @@ import type { ChangeEvent } from 'react' import React, { useState } from 'react' -import { Space, useW3 } from '@w3ui/react' +import { ContentServeService, Space, useW3 } from '@w3ui/react' import Loader from '../components/Loader' -import { DID, DIDKey } from '@ucanto/interface' +import { DIDKey } from '@ucanto/interface' import { DidIcon } from './DidIcon' import Link from 'next/link' import { FolderPlusIcon, InformationCircleIcon } from '@heroicons/react/24/outline' import Tooltip from './Tooltip' import { H3 } from './Text' +import * as UcantoClient from '@ucanto/client' +import { HTTP } from '@ucanto/transport' +import * as CAR from '@ucanto/transport/car' export function SpaceCreatorCreating(): JSX.Element { return ( @@ -50,11 +53,28 @@ export function SpaceCreatorForm({ throw new Error('a payment plan is required on account to provision a new space.') } + const toWebDID = (input?: string) => + UcantoClient.Schema.DID.match({ method: 'web' }).from(input) + setSubmitted(true) try { - const space = await client.createSpace(name) - const provider = (process.env.NEXT_PUBLIC_W3UP_PROVIDER || 'did:web:web3.storage') as DID<'web'> + const gatewayId = toWebDID(process.env.NEXT_PUBLIC_W3UP_GATEWAY_ID) || toWebDID('did:web:w3s.link') + const gatewayUrl = process.env.NEXT_PUBLIC_W3UP_GATEWAY_HOST || 'https://w3s.link' + + const storachaGateway = UcantoClient.connect({ + id: { + did: () => gatewayId + }, + codec: CAR.outbound, + channel: HTTP.open({ url: new URL(gatewayUrl) }), + }) + + const space = await client.createSpace(name, { + authorizeGatewayServices: [storachaGateway] + }) + + const provider = toWebDID(process.env.NEXT_PUBLIC_W3UP_PROVIDER) || toWebDID('did:web:web3.storage') const result = await account.provision(space.did(), { provider }) if (result.error) { setSubmitted(false)