Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Last active June 25, 2024 10:46
Show Gist options
  • Save alanshaw/e949abfcf6728f590ac9fa083dba5648 to your computer and use it in GitHub Desktop.
Save alanshaw/e949abfcf6728f590ac9fa083dba5648 to your computer and use it in GitHub Desktop.
How do you use w3up on the server/in CI where there's no persistent filesystem?

On your local computer:

  1. Create a keypair for the server

    npx ucan-key ed --json

    Note down did and key values!

  2. Install the w3cli:

    npm i -g @web3-storage/w3cli
  3. Authorize your agent to use spaces owned by your email address:

    w3 authorize you@email.com
  4. Create a space for where the uploads will be registered:

    w3 space create myspacename
    w3 space register
  5. Delegate from your local machine to the server:

    w3 delegation create <did_from_ucan-key_command_above> --can 'store/add' --can 'upload/add' | base64

On the server:

  1. Set up environment variables KEY with the output of key from step 1 and PROOF with the output of step 4.

  2. In the script, load the agent with the server private key and add the proof of delegation:

    import * as Client from '@web3-storage/w3up-client'
    import { StoreMemory } from '@web3-storage/w3up-client/stores/memory'
    import * as Proof from '@web3-storage/w3up-client/proof'
    import { Signer } from '@web3-storage/w3up-client/principal/ed25519'
    
    async function main () {
      // Load client with specific private key
      const principal = Signer.parse(process.env.KEY)
      const store = new StoreMemory()
      const client = await Client.create({ principal, store })
      // Add proof that this agent has been delegated capabilities on the space
      const proof = await Proof.parse(process.env.PROOF)
      const space = await client.addSpace(proof)
      await client.setCurrentSpace(space.did())
      // READY to go!
    }
@alanshaw
Copy link
Author

alanshaw commented Jan 11, 2023

web3-storage/w3up-client#74 should make this a little better. ✅ done

@alanshaw
Copy link
Author

alanshaw commented May 22, 2023

storacha-network/ucanto#287 should make this even better when client deps are updated to ucanto@8 ✅ done

@anistark
Copy link

anistark commented Sep 1, 2023

Probably should add these:

w3 authorize you@email.com before space register.

And add before code snippet:

yarn add @ucanto/core @ucanto/principal @ipld/car @web3-storage/w3up-client

Can also add an example with an image file to show complete script in action?

@kespinola
Copy link

When doing the delegation is it needed to use the space just created to be able to create the delegation?

Or does creating a space automatically use it?

w3 space use did:key:{}

@alanshaw
Copy link
Author

Yes the CLI automatically “uses” the created space for you so it’s not necessary to w3 space use did…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment