Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / reproducible-archiver.js
Created August 28, 2017 03:00
Reproducible zip builds for Archiver
var Zip = require('archiver/lib/plugins/zip')
, date = new Date(0)
, mode = 0755
// Set static timestamp/mode and append to queue instead of actually appending to zip
Zip.prototype.append = function(source, data, cb) {
(this._queue || (this._queue=[])).push([ source, Object.assign(data, { date, mode }) ]);
cb();
}
@shesek
shesek / bitcoinjs-lib-nodejs.sh
Last active July 2, 2017 16:53
Package bitcoinjs-lib as a nodejs module (VERY hacky, but good enough for now)
#!/bin/bash
# Package bitcoinjs-lib as a nodejs module
# Install bitcoinjs first (`npm install bitcoinjs-lib`),
# than run this in your app's directory.
TARGET=lib/bitcoinjs-lib.js
# Wrap all of bitcoinjs-lib and its dependencies in an IIFE
# and export the Bitcoin, Crypto and BigInteger objects.
@shesek
shesek / deterministic.coffee
Last active June 26, 2017 00:48
Bitcoin type-2 deterministic keys for nodejs/javascript, implemented in CoffeeScript
# Requires [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib/)
# (see https://gist.github.com/shesek/5847524 for getting it running under nodejs)
# Released under the MIT license
{ Address } = Bitcoin
{ SHA256 } = Crypto
sha256b = (bytes) -> SHA256 bytes, asBytes: true
lpad = (size, bytes) -> bytes.unshift 0x00 while bytes.length<size; bytes
@shesek
shesek / Y.coffee
Last active December 2, 2016 18:46
Y combinator
# The common one, converted to CoffeeScript
y = (f) -> ((y)-> y y) (y) -> f (n) -> (y y) n
# My version
y = ((y)->y y) (y) -> (f) -> (n) -> (f (y y) f) n
fact = y (f) -> (n) -> if n is 0 then 1 else n * f n-1
# And a simpler way, requires a different `fact` that passes itself to itself
y = (f) -> (n) -> (f f) n
@shesek
shesek / gist:3929926
Created October 22, 2012 06:01
Higher order functions for nodejs-style callbacks error handling

I've used promises for quite some time, but ended up going back to nodejs-style callbacks and creating a bunch of higher-order functions that makes handling that easier and reduce the boilerplate code. The two most useful ones, that really made it much easier, deal with error handling/bubbling: (CoffeeScript)

iferr = (errfn, succfn) -> (e, a...) -> if e? then errfn e else succfn a...
throwerr = iferr.bind null, (e) -> throw e


# To let an error bubble up, `iferr` decorates the success function and the error function, and decide which one should handle the response
# This replaces `if (err) return fn err` that you see all over the place
@shesek
shesek / exif.txt
Last active May 6, 2016 22:02
EXIF metadata for images posted on drcraigwright.net. More info and reproduction: https://www.reddit.com/r/Bitcoin/comments/4i76ut/exif_metadata_for_images_posted_on_craig_wrights/
======== 08fb5101c50f322723b6c5a64b1eba6c35f7d729.png
ExifTool Version Number : 10.10
File Name : 08fb5101c50f322723b6c5a64b1eba6c35f7d729.png
Directory : .
File Size : 12 kB
File Modification Date/Time : 2016:05:06 20:21:20+03:00
File Access Date/Time : 2016:05:06 23:36:21+03:00
File Inode Change Date/Time : 2016:05:06 23:34:19+03:00
File Permissions : rw-rw-r--
File Type : PNG
@shesek
shesek / package.json
Last active March 24, 2016 10:49
setTimeout and setInterval with nicer syntax for CoffeeScript, purely for syntactic sugar
{ "name": "timer-sugar", "version": "0.0.1", "main": "timers.coffee", "license": "BSD" }
@shesek
shesek / merge-connect.coffee
Last active December 26, 2015 10:19
Merge multiple connect middlewares into a single middleware that calls them in order
connect_middlewares = (fns...) -> (req, res, out) ->
curr = 0
do next = (err=null) ->
return out err if err? or not fn = fns[curr++]
fn req, res, next
sequence = (errcb) -> (fn, a..., cb) -> fn a..., (err, b...) -> if err? then errcb err else cb b...
compile = (files, callback) ->
seq = sequence callback
stored <- seq async.sortBy, files, @sort
mapped <- seq async.map, sorted, @map
reduced <- seq async.reduce, mapped, null, @reduce
<- seq @write, reduced
do @log
callback? @getClassName()
(function(){var e;e=function(e){var t,n;t=+(((n=e.match(/^\s*(?:#\d+)?\s*\(([\d\.]+)\)/))!=null?n[1]:void 0)||0);if(t===t){return t}else{return 0}};$(".list").each(function(){var t,n,r;t=$(this);n=t.find(".list-header h2");r=t.find(".list-card-title:visible").get().reduce(function(t,n){return t+e(n.innerText)},0);return n.find(".total").remove().end().append($("<span>").addClass("total").text(r))});return function(e){return $("<style>").attr({type:"text/css"}).text(e).appendTo("head")}(".list-header .total { float: right }")})()