Skip to content

Instantly share code, notes, and snippets.

Avatar
🙇‍♂️
https://nft.storage

Alan Shaw alanshaw

🙇‍♂️
https://nft.storage
View GitHub Profile
View deadline.js
const deadline = async function * (source, deadineMs, onDeadline) {
class DeadlineError extends Error {}
while (true) {
let timeoutID
try {
const next = await Promise.race([
new Promise((resolve, reject) => {
timeoutID = setTimeout(() => reject(new DeadlineError()), deadineMs)
}),
source.next()
@alanshaw
alanshaw / address.ts
Created Jan 21, 2021
Utility functions for Addresses
View address.ts
import addr, { Address, newAddress } from '@glif/filecoin-address'
import { blake2b } from 'blakejs'
import varint from 'varint'
/**
* PayloadHashLength defines the hash length taken over addresses using the Actor and SECP256K1 protocols.
*/
export const payloadHashLength = 20
function addressHash (ingest: Uint8Array): Uint8Array {
View balances-to-json.js
// 1. Install deps with `npm i csv-parse @glif/filecoin-number`
// 2. Drop `balances.baseline.clean.out` and `balances.claus.clean.out` into the CWD
// 3. Run `node balances-to-json.js`
// 4. It'll create `balances.baseline.json` and `balances.claus.json` and `actors.json` in the CWD
const fs = require('fs')
const parse = require('csv-parse')
const { FilecoinNumber } = require('@glif/filecoin-number')
async function main () {
@alanshaw
alanshaw / format-percentage.js
Created Oct 12, 2020
Format a percentage according to some arbitrary rules that actually make it pleasing to look at.
View format-percentage.js
// Format a percentage according to some arbitrary rules that actually make it pleasing to look at.
// 100 -> 100 // whole numbers have no decimal
// 85.1543 -> 85.1 // it just truncates after first non-zero decimal
// 3.00345 -> 3.003 // leading zeros preserved before truncate
// 3.0000345 -> 3.00003 // ...up to a limit of 5dp
// 3.000001 -> 3 // ...so fractional numbers with 5+ leading zeros become an integer
function formatPercentage (num) {
const [int, frac] = num.toFixed(5).split('.')
const index = frac.split('').findIndex(n => n !== '0')
return index === -1 ? int : `${int}.${frac.slice(0, index + 1)}`
@alanshaw
alanshaw / switch-k8-context.sh
Last active Oct 9, 2020
Switching k8 contexts for DO
View switch-k8-context.sh
# what even contexts do I have?
kubectl config get-contexts
# switch to other one
kubectl config use-context CONTEXT_NAME
# ===
# for new or update existing cluster credentials:
# ===
@alanshaw
alanshaw / thanks.go
Created Jun 18, 2020
stringer detection
View thanks.go
name := "Unknown"
sc, ok := r.Client.(fmt.Stringer)
if ok {
name = sc.String()
}
oc.log.Info("optimizing_client", "thanks for the round", "client", name)
@alanshaw
alanshaw / drand.md
Last active Jul 21, 2020
drand dev setup
View drand.md

Setup

# Create keys
drand generate-keypair --tls-disable --folder ~/.drand0 127.0.0.1:3000
drand generate-keypair --tls-disable --folder ~/.drand1 127.0.0.1:3100
drand generate-keypair --tls-disable --folder ~/.drand2 127.0.0.1:3200

# Start daemons
drand start --tls-disable --folder ~/.drand0 --private-listen 127.0.0.1:3000 --control 3001 --public-listen 127.0.0.1:3002
@alanshaw
alanshaw / sysctl-tweaks.md
Created May 8, 2020
Notes on network debugging
View sysctl-tweaks.md
@alanshaw
alanshaw / cids.md
Last active May 1, 2020
Well known CIDs
View cids.md
  • Room Guardian
    • bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi
  • IPFS blog
    • bafybeifzcblbfi4nzmaq2ckaag63fufhkbfl7u3zqfkskmwjr3jsxtpyru
  • XKCD
    • QmdmQXB2mzChmMeKY47C43LxUdg1NDJ5MWcKMKxDu7RgQm
  • Project Apollo Archives
    • QmSnuWmxptJZdLJpKRarxBMS2Ju2oANVrgbr2xWbie9b2D
  • https://awesome.ipfs.io/datasets/
@alanshaw
alanshaw / example.js
Last active Mar 24, 2020
Code exercise
View example.js
import React, { Component } from 'react'
import IPFS from 'ipfs'
export default class App extends Component {
constructor (props) {
super(props)
this.state = {
ready: false,
files: [],
fileName: '',