Skip to content

Instantly share code, notes, and snippets.

View lionello's full-sized avatar
🇭🇰

Lio李歐 lionello

🇭🇰
View GitHub Profile
@lionello
lionello / 1password-to-pass.js
Created February 27, 2018 16:22
NodeJS script to import a 1password tab-delimited TXT file into 'pass'
#!/usr/bin/env node
const FS = require('fs')
const URL = require('url')
const Child_process = require('child_process')
const all = FS.readFileSync('pww.txt', {encoding:'utf8'})
const regex = /\n(?:"([^"]*)")?\t"([^"]*)"\t"([^"]*)"\t"([^"]*)"\t"([^"]*)"\t(?:"([^"]*)")?/mg
while (true) {
const match = regex.exec(all)
if (!match) break
@lionello
lionello / .direnvrc
Last active August 6, 2019 08:31 — forked from sveitser/.direnvrc
#!/bin/bash
#
# Cache nix-shell environment
#
# - watches shell.nix (or default.nix), ~/.direnvrc and .envrc
# - based on https://github.com/direnv/direnv/wiki/Nix
#
use_nix() {
local shell_file=$(test -f shell.nix && echo shell.nix || echo default.nix)
if [[ ! -f "$shell_file" ]]; then return; fi
@lionello
lionello / rejects.js
Created July 5, 2018 23:29
Assertion helper for async/promise methods that should fail
Assert.rejects = async function rejects (blockOrPromise, error, message) {
return new Promise((resolve,reject) => {
if (typeof blockOrPromise === 'function') {
blockOrPromise = blockOrPromise()
}
blockOrPromise.then(x => {
reject(Error('Should fail, not return ' + x))
}).catch(async err => {
if (typeof error === 'function') {
err = (await error(err)) || err
@lionello
lionello / ethereum-test-template.js
Last active July 31, 2018 08:07
Small JS template for writing Ethereum/Solidity tests
/* Use this with an overmind Procfile like this:
geth: geth --dev --rpc console
jest: pnpx jest --watchAll
*/
const ChildProcess = require('child_process')
const Web3 = require('web3')
const Assert = require('assert')
function addressOf(contract) {
return contract.options.address
@lionello
lionello / main.go
Created August 20, 2018 06:20
Graceful shutdown of a Go HTTP server
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
@lionello
lionello / .direnvrc
Last active August 6, 2019 08:37 — forked from adisbladis/.direnvrc
Direnv nix cache. Put this file in your HOME folder.
#!/bin/bash
#
# Cache nix-shell environment
#
# - watches shell.nix (or default.nix), ~/.direnvrc and .envrc
# - based on https://github.com/direnv/direnv/wiki/Nix
#
use_nix() {
local shell_file=$(test -f shell.nix && echo shell.nix || echo default.nix)
if [[ ! -f "$shell_file" ]]; then return; fi
@lionello
lionello / qr.js
Last active November 10, 2018 01:57
Tiny QR code generator for showing text/urls in the terminal
#!/usr/bin/env node
const QR = require("qr-image");
const arrayOf = (len, filler) => new Array(len).fill(filler);
const text = process.argv[2];
const matrix = QR.matrix(text, "L");
const topBottomPad = arrayOf(4, arrayOf(matrix[0].length, 0));
const rowPad = arrayOf(4, 0);
@lionello
lionello / ECMath.sol
Last active June 11, 2023 05:44
ECDSA verification and recovery for curve secp256r1
/*
MIT License
Copyright (c) 2018 Lionello Lunesu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@lionello
lionello / express-async.js
Last active January 5, 2019 03:56
Add support for async middleware and handlers to Express
/**
* Wrap an Express middleware or handler for async support.
* @param {function} fn The middleware or handler to wrap
* @returns {function} Wrapped function
*/
exports.wrap = function(fn) {
if (fn.length === 4) {
// Wrap error handler
return (err, req, res, next) => {
const r = fn(err, req, res, next)
@lionello
lionello / mknix.sh
Created November 26, 2018 06:56
Little bash script to generate shell.nix default.nix
#!/usr/bin/env bash
usage () {
echo "Usage: $(basename $0) [--direnv] [--shell] [--default] -p|--packages packages... [--test]"
exit 0
}
error () {
echo "Error: $1"
exit 1