Skip to content

Instantly share code, notes, and snippets.

View alanshaw's full-sized avatar
🫀
https://web3.storage

Alan Shaw alanshaw

🫀
https://web3.storage
View GitHub Profile
const sumUsage = client => {
const period = getPeriod(new Date())
/** @type {Record<ProviderDID, Record<SpaceDID, number>>} */
let totalBySpace = {}
let total = 0
for (const account of Object.values(client.accounts())) {
const subscriptions = await client.capability.subscriptions.list(account.did())
for (const { consumers } of subscriptions.results) {
for (const space of consumers) {
const report = await client.capability.usage.report(space)
@alanshaw
alanshaw / fx.md
Created September 19, 2023 18:06
How fx invocations work

Want to do an async task of adding two number together? Not problem.

invoke do/async-addition 40, 2
  # create a delegation for the result and get the CID
  result-cid = cid-for(delegate(do/async-addition-result 40, 2))

  # create a receipt for this invocation, including the CID of the result delegation
  receipt = generate-receipt-for(do/async-addition 40, 2, fx.join: result-cid)
@alanshaw
alanshaw / lassie-fetch.sh
Created June 30, 2023 11:17
Lassie fetch from dag.w3s.link
lassie fetch --dag-scope=entity -providers=/dns4/dag.w3s.link/tcp/443/https/p2p/QmUA9D3H7HeCYsirB3KmPSvZh3dNXMZas6Lwgr4fv1HTTp bafybeicuc7vfbxzaperdc6ng2c4ufeulyjpfrmwuxhxrm6cfdamp5hpw5i
@alanshaw
alanshaw / curl.sh
Created May 19, 2023 14:19
curling some dfs carz from dag.w3s.link
$ curl -v -H "Accept: application/vnd.ipld.car;order=dfs" https://dag.w3s.link/ipfs/bafybeibrqc2se2p3k4kfdwg7deigdggamlumemkiggrnqw3edrjosqhvnm --output my.dfs.car
...
> GET /ipfs/bafybeibrqc2se2p3k4kfdwg7deigdggamlumemkiggrnqw3edrjosqhvnm HTTP/2
> Host: dag.w3s.link
> user-agent: curl/7.77.0
> accept: application/vnd.ipld.car;order=dfs
>
...
< HTTP/2 200
< date: Fri, 19 May 2023 14:16:47 GMT
@alanshaw
alanshaw / directory-builder.js
Created March 28, 2023 17:56
Building a directory after uploading individual files async
import { createWriter } from '@ipld/unixfs'
import * as CAR from '@web3-storage/upload-client/car'
import { create } from '@web3-storage/w3up-client'
/**
* @param {import('@web3-storage/w3up-client').Client} client
* @param {import('@web3-storage/w3up-client/types').FileLike} file
*/
async function uploadFile (client, file) {
/** @type {import('@web3-storage/upload-client/types').DirectoryEntryLink[]} */
@alanshaw
alanshaw / create-perma-map-fixture.js
Created March 7, 2023 15:26
Create a fixture for `@perma/map` from a CAR file containing a HAMT sharded directory
import { CarIndexedReader } from '@ipld/car'
import * as pb from '@ipld/dag-pb'
import { UnixFS } from 'ipfs-unixfs'
// usage: node create-perma-map-fixture sharded-dir.car
/** @typedef {{ bitfield: number[], links: Array<Link|Element>, prefix?: string }} Link */
/** @typedef {{ key: string, value: string, prefix: string }} Element */
async function main () {
@alanshaw
alanshaw / upload-large-cars.md
Last active January 11, 2023 18:29
Upload large CAR files to w3up

Install the new CLI:

npm install -g @web3-storage/w3cli

Create and register new space:

w3 space create SPACENAME
@alanshaw
alanshaw / w3-up-server.md
Last active October 5, 2023 09:03
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!

@alanshaw
alanshaw / agent.js
Last active December 1, 2022 11:26
w3access agent refactor
// Goals:
// 1. Decouple store from agent
// 2. Simplify agent creation
// 3. Agent governs data format not store
// 4. Initialization of agent, not store
// 5. DRY initialization in agent, not repeated in each store impl
/**
* @param {AgentData} [data] Agent data
* @param {object} [options]
@alanshaw
alanshaw / extract-gateway-peers.mjs
Created November 30, 2022 12:38
Extract the gateway peer IDs from the bifrost infra repo
import fs from 'fs'
import path from 'path'
const dir = path.join('ansible', 'inventories', 'bifrost', 'host_vars')
const regex = /ipfs_config_identity_peerid: "(.*)"/ig
async function main () {
const peers = []
for (const f of fs.readdirSync(dir)) {
if (!(f.startsWith('ipfs-bank') && f.endsWith('ipfs.gateway.dwebops.net.yml'))) continue