Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Last active October 5, 2023 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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 Signer from '@ucanto/principal/ed25519'
    import { importDAG } from '@ucanto/core/delegation'
    import { CarReader } from '@ipld/car'
    import * as Client from '@web3-storage/w3up-client'
    
    async function main () {
      // Load client with specific private key
      const principal = Signer.parse(process.env.KEY)
      const client = await Client.create({ principal })
      
      // Add proof that this agent has been delegated capabilities on the space
      const proof = await parseProof(process.env.PROOF)
      const space = await client.addSpace(proof)
      await client.setCurrentSpace(space.did())
      
      // READY to go!
    }
    
    /** @param {string} data Base64 encoded CAR file */
    async function parseProof (data) {
      const blocks = []
      const reader = await CarReader.fromBytes(Buffer.from(data, 'base64'))
      for await (const block of reader.blocks()) {
        blocks.push(block)
      }
      return importDAG(blocks)
    }
@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

web3-storage/ucanto#287 should make this even better when client deps are updated to ucanto@8

@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