You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm following the example about using delegation to upload directly to the network. I have a next.js route located at /api/w3up/delegation:
// app/api/w3up/delegation/route.tsimport*asDIDfrom'@ipld/dag-ucan/did';import{getWeb3StorageClient}from'./getWeb3StorageClient';import{NextResponse}from'next/server';exportasyncfunctionGET(){constweb3StorageClient=awaitgetWeb3StorageClient();constdid=web3StorageClient.did();// Create a delegation for a specific DIDconstaudience=DID.parse(did);constabilities=['store/add','upload/add'];constexpiration=Math.floor(Date.now()/1000)+60*60*24;// 24 hours from nowconstdelegation=awaitweb3StorageClient.createDelegation(audience,abilities,{
expiration,});// Serialize the delegation and send it to the clientconstarchive=awaitdelegation.archive();constu4=archive.ok;returnNextResponse.json({delegation: u4?.toString(),});}
on the frontend, I have a hook to create a client:
// api/hooks/useWeb3StorageClient.tsimport*asDelegationfrom'@ucanto/core/delegation';import*asClientfrom'@web3-storage/w3up-client';import{useEffect,useState}from'react';exportasyncfunctiongetWeb3StorageClient(): Promise<{error: Error|null;client: Client.Client|null;}>{try{// Fetch the delegation from the backendconstresponse=awaitfetch(`/api/w3up/delegation`);constdata=awaitresponse.arrayBuffer();console.log('getDelegationResponse',data);constdelegationUint8Array=newUint8Array(data);console.log('delegationUint8Array',delegationUint8Array);constclient=awaitClient.create();// Deserialize the delegationconstdelegation=awaitDelegation.extract(delegationUint8Array);if(!delegation.ok){throwdelegation.errorasError;}// Add proof that this agent has been delegated capabilities on the spaceconstspace=awaitclient.addSpace(delegation.ok);client.setCurrentSpace(space.did());return{error: null,
client,};}catch(error){return{error: errorasError,client: null,};}}exportfunctionuseWeb3StorageClient(){const[ready,setReady]=useState(false);const[client,setClient]=useState<Client.Client|null>(null);const[error,setError]=useState<Error|null>(null);useEffect(()=>{if(ready){return;}getWeb3StorageClient().then(({ error, client })=>{if(error){console.error(error);setReady(false);setClient(null);setError(error);return;}setClient(client);setReady(true);setError(null);});},[]);return{ ready, client, error };}
I get the following error:
The text was updated successfully, but these errors were encountered:
hm, unless I'm reading wrong, I think that in your server code you are returning the delegation inside a json blob but then in the client you are trying to read the json as CBOR - this is, of course, failing.
Hi, I'm following the example about using delegation to upload directly to the network. I have a next.js route located at
/api/w3up/delegation
:on the frontend, I have a hook to create a client:
I get the following error:
The text was updated successfully, but these errors were encountered: