Skip to content

Instantly share code, notes, and snippets.

View junderw's full-sized avatar

Jonathan Underwood junderw

  • Tokyo, Japan
  • 07:20 (UTC +09:00)
View GitHub Profile
@junderw
junderw / getByteCount.js
Last active April 19, 2024 07:02
Estimate bytes for bitcoin transactions
// Usage:
// getByteCount({'MULTISIG-P2SH:2-4':45},{'P2PKH':1}) Means "45 inputs of P2SH Multisig and 1 output of P2PKH"
// getByteCount({'P2PKH':1,'MULTISIG-P2SH:2-3':2},{'P2PKH':2}) means "1 P2PKH input and 2 Multisig P2SH (2 of 3) inputs along with 2 P2PKH outputs"
function getByteCount(inputs, outputs) {
var totalWeight = 0
var hasWitness = false
var inputCount = 0
var outputCount = 0
// assumes compressed pubkeys in all cases.
var types = {
@junderw
junderw / aesWebCrypto.js
Created March 26, 2019 02:10
Using web crypto API for AES-GCM encryption and decryption.
async function aesEncrypt(data, password, difficulty = 10) {
const hashKey = await grindKey(password, difficulty)
const iv = await getIv(password, data)
const key = await window.crypto.subtle.importKey(
'raw',
hashKey, {
name: 'AES-GCM',
},
false,
@junderw
junderw / lnd_openssl.sh
Created March 25, 2021 15:45
openssl command to generate a tls cert just like the ones generated by LND Lightning Network Daemon
# This is one big command. Fill it out in a text editor and copy-paste
# the whole thing to get tls.key and tls.cert files for use with lnd
openssl \
req \
-newkey ec:<(openssl ecparam -name prime256v1) \
-nodes `# No password` \
-keyout \
tls.key `# private key filename` \
-x509 \

Keybase proof

I hereby claim:

  • I am junderw on github.
  • I am junderwood (https://keybase.io/junderwood) on keybase.
  • I have a public key ASDoYhX66stiR0f07xk-EYyEh9z_eQJVYp5ozEJRHLbqpQo

To claim this, I am signing this object:

@junderw
junderw / AuthyToOtherAuthenticator.md
Created March 9, 2022 22:22 — forked from gboudreau/AuthyToOtherAuthenticator.md
Export TOTP tokens from Authy

Generating Authy passwords on other authenticators


There is an increasing count of applications which use Authy for two-factor authentication. However many users who aren't using Authy, have their own authenticator setup up already and do not wish to use two applications for generating passwords.

Since I use 1Password for all of my password storing/generating needs, I was looking for a solution to use Authy passwords on that. I couldn't find any completely working solutions, however I stumbled upon a gist by Brian Hartvigsen. His post had a neat code with it to generate QR codes for you to use on your favorite authenticator.

His method is to extract the secret keys using Authy's Google Chrome app via Developer Tools. If this was not possible, I guess people would be reverse engineering the Android app or something like that. But when I tried that code, nothing appeared on the screen. My guess is that Brian used the

@junderw
junderw / Uint8array.buffermethods.js
Last active October 16, 2021 11:37
This is very bad. Don't do this. but hey... if no one's looking... ;-)
// Mimics Buffer.from(x, 'hex') logic
// Stops on first non-hex string and returns
// https://github.com/nodejs/node/blob/v14.18.1/src/string_bytes.cc#L246-L261
Uint8Array.fromHex = function (hexString) {
const MAP_HEX = {0:0,1:1,2:2,3:3,4:4,5:5,6:6,7:7,8:8,9:9,a:10,b:11,c:12,d:13,e:14,f:15,A:10,B:11,C:12,D:13,E:14,F:15};
const bytes = new this(Math.floor((hexString || "").length / 2));
let finalLen = bytes.length;
for (let i = 0; i < finalLen; i++) {
const a = MAP_HEX[hexString[i * 2]];
const b = MAP_HEX[hexString[i * 2 + 1]];
@junderw
junderw / descriptorChecksum.js
Last active February 17, 2021 15:20
Bitcoin Output Descriptor Checksum algorithm in JavaScript
/*
* input: "wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)"
* output: "wpkh([d34db33f/84h/0h/0h]0279be667ef9dcbbac55a06295Ce870b07029Bfcdb2dce28d959f2815b16f81798)#qwlqgth7"
* (This has been checked to match bitcoin-core)
*/
function descriptorChecksum(desc) {
if (!(typeof desc === 'string' || desc instanceof String)) throw new Error('desc must be string')
const descParts = desc.match(/^(.*?)(?:#([qpzry9x8gf2tvdw0s3jn54khce6mua7l]{8}))?$/);
if (descParts[1] === '') throw new Error('desc string must not be empty')
@junderw
junderw / cbor-encoder.ts
Created July 19, 2020 00:57
A CBOR encoder in TypeScript
class CBOREncoder {
private entryCount: number = 0;
private data: Uint8Array = Uint8Array.from([]);
pushBool(key: string, value: boolean): void {
this.entryCount++;
this.pushTextInternal(key);
this.pushBoolInternal(value);
}
@junderw
junderw / bitwise53bit.js
Created July 8, 2020 00:42
Functions to perform bitwise operations on unsigned ints up to 53 bit (highest JS precision) without bigint libraries.
var POW31 = 0x80000000
var MSK31 = 0x7FFFFFFF
function checkBitSize(bs) {
if (bs > 53 || bs < 1 || Math.floor(bs) !== bs) {
throw new Error('Invalid Bit Size')
}
}
function splitBits(val, size) {
@junderw
junderw / Dockerfile
Created February 24, 2018 00:51
Dockerfile for running keybase in a container... requires KEYBASEUSER.ss config.json secretkeys.KEYBASEUSER.mpack session.json in the same directory as the Dockerfile... also, if you ever logout, the session.json will change.
FROM ubuntu:16.04
MAINTAINER Jonathan Underwood
# set env vars for linux user and keybase user
ENV LINUX_USER="kbuser" \
KEYBASE_USER="youruser"
# use curl to grab the latest build from keybase.io
RUN apt update && apt install -y \
curl