Skip to content

Instantly share code, notes, and snippets.

Avatar
🇭🇰

Lio李歐 lionello

🇭🇰
View GitHub Profile
@lionello
lionello / 1password-to-pass.js
Created Feb 27, 2018
NodeJS script to import a 1password tab-delimited TXT file into 'pass'
View 1password-to-pass.js
#!/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
View .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 Jul 5, 2018
Assertion helper for async/promise methods that should fail
View rejects.js
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 Jul 31, 2018
Small JS template for writing Ethereum/Solidity tests
View ethereum-test-template.js
/* 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 Aug 20, 2018
Graceful shutdown of a Go HTTP server
View main.go
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
@lionello
lionello / .direnvrc
Last active Aug 6, 2019 — forked from adisbladis/.direnvrc
Direnv nix cache. Put this file in your HOME folder.
View .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 / qr.js
Last active Nov 10, 2018
Tiny QR code generator for showing text/urls in the terminal
View qr.js
#!/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 Mar 16, 2022
ECDSA verification and recovery for curve secp256r1
View ECMath.sol
/*
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 Jan 5, 2019
Add support for async middleware and handlers to Express
View express-async.js
/**
* 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 Nov 26, 2018
Little bash script to generate shell.nix default.nix
View mknix.sh
#!/usr/bin/env bash
usage () {
echo "Usage: $(basename $0) [--direnv] [--shell] [--default] -p|--packages packages... [--test]"
exit 0
}
error () {
echo "Error: $1"
exit 1