Skip to content

Instantly share code, notes, and snippets.

@qpwo
qpwo / multisig-spl-token.ts
Created July 19, 2022 17:33
how to sign transactions separately for solana spl-token (web3.js) multisig account/wallet
/** Normally you provide all signers (private keys) at once with an spl-token multisig.
* This is of course useless.
* Here is how you can get the signatures one-by-one without having all the private keys.
* (For example, you'd send the tx over the network and get a signature back from the other party.)
*/
import { createMint, createMultisig, getAccount, getOrCreateAssociatedTokenAccount, mintTo, TokenInstruction, TOKEN_PROGRAM_ID, transferInstructionData } from '@solana/spl-token';
import { Connection, Keypair, PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
import nacl from 'tweetnacl';
const log = console.log;
@qpwo
qpwo / index.html
Created July 14, 2022 22:31
htm preact minimal page without jsx
<script type="module">
import { h, Component, render } from 'https://unpkg.com/preact?module';
import htm from 'https://unpkg.com/htm?module';
// Initialize htm with Preact
const html = htm.bind(h);
function App (props) {
return html`<h1>Hello ${props.name}!</h1>`;
}
@qpwo
qpwo / compress-shots-webm-webp.zsh
Last active June 22, 2022 19:15
zsh script to compress screen shots and recordings on macos. Use as folder action using automator
for f in "$@"; do
now=$(date +%Y-%m-%d--%H-%M-%S)
bn=$(basename $f)
if [ "$f:e" = "mov" ]; then
say "compressing video"
ffmpeg -i "$f" -c:v libvpx-vp9 -crf 30 -b:v 0 -b:a 128k -c:a libopus "$HOME/docs/compressed-shots/$now.webm"
say "compressed"
fi
if [ "$f:e" = "png" ]; then
@qpwo
qpwo / loadSingleAsset.ts
Created March 21, 2022 18:19
pixi js v6 load single asset typescript
import { Loader } from 'pixi.js'
export function loadSingleAsset(url: string) {
return new Promise(resolve =>
new Loader().add(url).load(() => resolve(null))
)
}
@qpwo
qpwo / tasks.json
Created March 18, 2022 19:32
esbuild problem matcher
"problemMatcher": {
"pattern": [
{
"regexp": "\\[(ERROR)\\] (.*)",
"severity": 1,
"message": 2
},
{ "regexp": "" },
{
"regexp": "(.*?):(\\d+):(\\d+)",
@qpwo
qpwo / fetchJson.mjs
Last active March 9, 2022 20:23
fetch json with promise in node in typescript and javascript without libraries (one function)
// public domain: https://gist.github.com/qpwo/0d91803074290167e40f79908038f3cb
import https from 'https';
export async function fetchJson(url) {
return await new Promise(resolve => https
.get(url, res => {
let body = '';
res.on('data', chunk => (body += chunk));
res.on('end', () => {
try {
resolve(JSON.parse(body));
@qpwo
qpwo / 1-import-ts-repl.sh
Last active February 26, 2022 03:34
use esbuild to get a node repl for a typescript file
function import-ts-repl() { (
set -e
file=$1
rm -f ${file}.js ${file}.js.map
esbuild ${file} --target=node14 --format=cjs --sourcemap --outfile=${file}.js --platform=node --bundle
node -r ${file}.js
rm -f ${file}.js ${file}.js.map
); }
@qpwo
qpwo / untitled-canvas.js
Last active February 23, 2022 19:46
untitled gui thingy
// problem: canvas burns your CPU because you redraw everything every frame
// solution: just re-draw stuff that's changed or shares a layer & overlap with something changed
// inspired by mithril
function Counter() {
let x = 0
let y = 0
const width = 100
const height = 100
return {
state: () => {x, y}, // checked for equality to run render
@qpwo
qpwo / launch.json
Created February 23, 2022 02:10
debug typescript node in vscode with esbuild (has sourcemaps too!) (Alternative to ts-node approach: https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695)
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "build & run ts",
"program": "${file}.js",
"envFile": "${fileDirname}/.env",
"preLaunchTask": "esbuild-current-file",
@qpwo
qpwo / deepEquals.min.js
Last active January 28, 2022 20:17
fast recursive deepstrictequals (deep object comparison) for node and the browser in typescript and javascript. Some assumption on json-like data. Minifies to 431 bytes
function deepEquals(e,r){if(e===r||Number.isNaN(e)&&Number.isNaN(r))return!0;if(typeof e!=typeof r||"object"!=typeof e||null===e||null===r)return!1;const t=Array.isArray(e),n=Array.isArray(r);if(t!==n)return!1;if(t&&n)return e.length===r.length&&e.every((e,t)=>deepEquals(e,r[t]));const s=Object.keys(e),u=Object.keys(r);return s.length===u.length&&(s.sort(),u.sort(),!!s.every((e,r)=>e===u[r])&&s.every(t=>deepEquals(e[t],r[t])))}