Skip to content

Instantly share code, notes, and snippets.

View junderw's full-sized avatar

Jonathan Underwood junderw

  • Tokyo, Japan
  • 11:03 (UTC +09:00)
View GitHub Profile
@junderw
junderw / createNewRegtestServer.sh
Last active November 19, 2018 09:33
Simple setup for Ubuntu 18.04 server
### new ubuntu 18.04 server
### Be sure to run
# sudo apt-get update && sudo apt-get -y upgrade && sudo reboot
### For security updates
### Then run this script with passwordless sudo account ubuntu (default for ubuntu AWS EC2):
# CERTDOMAIN=something.yourdomain.com [email protected] ./thisScript.sh
# Install Bitcoin 0.17.0 etc.
sudo add-apt-repository -y ppa:bitcoin/bitcoin &>/dev/null
sudo apt-get update &>/dev/null
@junderw
junderw / getByteCount.js
Last active September 4, 2024 17:04
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 / nordvpnconnect.sh
Created December 29, 2018 16:11
NordVPN: JPN or USA to connect to the country you want.
#!/bin/bash
SELECTION=$1
if [ -z "$1" ]
then
SELECTION="JPN"
fi
# Default JPN
CODE=108
@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 / prunegit.sh
Created April 9, 2019 09:20
Some commands for cleaning up my git folders... too many branches over time.
# Get rid of remotes
git fetch --prune --all
# Check branches for stuff you might want to keep
# This will show branches that are NOT on the remote for origin or upstream
git branch | grep -v "$(git branch --remote | grep -e "origin/\|upstream/" | grep -v "\->" | cut -d/ -f2)"
# Here's another one to check. checks your local master and gives any branches that are merged to master
git branch --merged=master | grep -v master
@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 / 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 / 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 / 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 \
@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]];