Skip to content

Instantly share code, notes, and snippets.

@matthewdowney
matthewdowney / gen.py
Created October 13, 2017 09:42
P2WPKH-P2SH (SegWit) Wallet Address Generation
"""
Implementation of "Pay to Witness Public Key Hash nested in BIP16 Pay to Script Hash" (P2WPKH-P2SH) address generation.
Described in BIP 141 (SegWit) https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#P2WPKH_nested_in_BIP16_P2SH
I.e.: Public key -> SegWit address
"""
import hashlib
from hashlib import sha256
@matthewdowney
matthewdowney / hmac.py
Created November 9, 2017 13:07
Python HMAC
import hmac
from functools import wraps
def safe_equal(a, b):
"""
Perform an equality check between strings that guards against a timing attack by checking every index in the each
string. Note that length is compared first, so the length may be leaked through a timing attack, which does not
compromise the security of an HMAC.
@matthewdowney
matthewdowney / benford.py
Created January 9, 2018 22:53
Pipe some data to this script to plot a data set vs benford's law.
# Pipe some data to stdin. Each line should contain a number.
import csv
from collections import defaultdict
from math import log10
import matplotlib
import matplotlib.pyplot as plt
import functools
import fileinput
@matthewdowney
matthewdowney / segwit-xpubs.md
Created May 15, 2018 01:55
Figuring out how to get segwit balances for an HD wallet via an xpub with blockchain.info's API.

Issues with the Blockchain Info API

The blockchain.info API for XPUBs only works with Legacy Bitcoin XPUBs, whereas their web interface confusingly works with Segwit as well.

I provide a couple solutions at the end, and we end up with three useful functions:

  • legacy_wallet_balance
  • segwit_wallet_balance
  • segwit_wallet_balance_hack
@matthewdowney
matthewdowney / sorting-transducer.clj
Created September 6, 2018 19:55
Clojure Sorting Transducer
(defn xf-sort
"A sorting transducer. Mostly a syntactic improvement to allow composition of
sorting with the standard transducers, but also provides a slight performance
increase over transducing, sorting, and then continuing to transduce."
([]
(xf-sort compare))
([cmp]
(fn [rf]
(let [temp-list (java.util.ArrayList.)]
(fn
@matthewdowney
matthewdowney / stats.clj
Created November 6, 2018 23:22
Running summary stats clojure
(defn stats
"Calculate :mean :min :max :stdev and :n given a data point, and possibly a prior `stats` result.
I.e. to calculate statistics from scratch:
```
(def statistics (reduce stats nil data-set))
```
Or to maintain statistics for a constantly updating data series that you don't want to keep in memory:
```
(def statistics (atom (stats 0))
(defn add-data-point! [dp]

Keybase proof

I hereby claim:

  • I am matthewdowney on github.
  • I am matthewdowney (https://keybase.io/matthewdowney) on keybase.
  • I have a public key ASA5-6Rf3lYjOJowTXj54xl2nhKdjc1jvET1YsVd4Q0YcQo

To claim this, I am signing this object:

@matthewdowney
matthewdowney / file-watch.clj
Created February 27, 2019 18:46
Lazy Clojure file-watch, re-parsing when file changes.
(defn parsed-file-delay
"A delay that returns `(parse-fn (slurp file-path))` on each dereference,
recalculating the return value only when `(.lastModified (io/file file-path))`
changes.
Useful if you don't need any sort of push notification when the file changes."
[file-path parse-fn]
(let [last-contents (atom {:modified -1 :contents nil})
^File f (io/file file-path)]
(reify IDeref
@matthewdowney
matthewdowney / blessed-cljs.cljs
Created April 29, 2019 05:06
Clojurescript client for blessed-contrib.
(ns blessed-cljs.core
"Compiles for :node-js. Attempts to connect to a TCP socket at the port
given as a command line argument, and receives pure data specifications
for a blessed-contrib dashboard which it updates in real time.
Expects JSON data following the format:
{:type (a string in gauge, table, sparkline, bar, donut, gauge, lcd, log, markdown, map)
:meta (a map to pass to grid.set along the the contrib.object)
:data (data to display)}
@matthewdowney
matthewdowney / dothreads.clj
Created August 13, 2019 19:19
A `do` macro in clojure for concurrent execution of worker code with ordered results.
(import java.util.concurrent.CountDownLatch)
(defmacro dothreads
"Like (dotimes [i n] ...) except the body is executed concurrently, and the result is
a map from `i` to the result (or an exception if one is thrown).
Spins up `n-threads` threads and execute `body` in each with the thread number bound to
the given `tid-binding`, but use a CountDownLatch to make sure they all start at as close
to the same exact time as possible."
[[tid-binding n-threads] & body]