Skip to content

Instantly share code, notes, and snippets.

View justmoon's full-sized avatar

Stefan Thomas justmoon

View GitHub Profile
@justmoon
justmoon / custom-error.js
Last active April 22, 2024 17:19 — forked from subfuzion/error.md
Creating custom Error classes in Node.js
'use strict';
module.exports = function CustomError(message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
require('util').inherits(module.exports, Error);
@justmoon
justmoon / signals.ts
Created July 5, 2023 18:11
Signals with effects based on async generators
/**
* This is an experimental API design for a signal/reactive library.
*
* Compared to @preact/signals, the main difference is the introduction of a
* different way of creating effects. Effects are async functions which contain
* a loop centered around a signal reader which is an async generator.
*
* This makes it so there is a persistent scope (outside of the loop) and a
* dynamic scope (inside the loop). This results in very readable reactive
* code that is mostly plain JavaScript.
@justmoon
justmoon / di.ts
Created June 26, 2023 05:32
Another idea for dependency injection with runtime access to the dependency tree
const makeComponent = <TDeps extends Dependencies, TProduct>(dependencies: TDeps, factory: (deps: ResolvedDependencies<TDeps>) => TProduct): SmartFactory<TProduct, TDeps> => {
const smartFactory = (predefinedDependencies = new Map()) => {
const resolvedDependencies: Partial<ResolvedDependencies<TDeps>> = {}
for (const key of Object.keys(dependencies)) {
if (predefinedDependencies.has(dependencies[key])) {
resolvedDependencies[key as keyof ResolvedDependencies<TDeps>] = predefinedDependencies.get(dependencies[key]) as ResolvedDependencies<TDeps>[string]
} else {
const resolvedDependency = dependencies[key](predefinedDependencies) as ResolvedDependencies<TDeps>[string]
resolvedDependencies[key as keyof ResolvedDependencies<TDeps>] = resolvedDependency
@justmoon
justmoon / seeded-random.ts
Last active June 8, 2023 13:52
Simple pseudo-random generator using SFC32
/**
* This is a simple PRNG I built for fun using the SFC32 algorithm by Chris Doty-Humphrey.
*
* This code is provided without any warranty, express or implied, and is hereby placed in the public domain.
*
* If you're looking for a good quality pseudo-random generator as an NPM package, I recommend `pure-rand`.
*/
/* eslint-disable unicorn/prefer-code-point */
/* eslint-disable unicorn/prefer-math-trunc */
@justmoon
justmoon / test.html
Created August 19, 2012 19:50
SJCL secp256k1
<script src="https://raw.github.com/bitwiseshiftleft/sjcl/master/sjcl.js"></script>
<script src="https://raw.github.com/bitwiseshiftleft/sjcl/master/core/bn.js"></script>
<script src="https://raw.github.com/bitwiseshiftleft/sjcl/master/core/ecc.js"></script>
<script>
// Overwrite NIST-P256 with secp256k1
sjcl.ecc.curves.c256 = new sjcl.ecc.curve(
sjcl.bn.pseudoMersennePrime(256, [[0,-1],[4,-1],[6,-1],[7,-1],[8,-1],[9,-1],[32,-1]]),
"0x14551231950b75fc4402da1722fc9baee",
0,
7,
@justmoon
justmoon / README.md
Last active November 24, 2018 13:02
ILP Kit Peering Flow

User Experience

Ben would like to peer with Stefan. So he logs into his server running ILP kit and runs:

$ ilp-admin help

Usage: ilp-admin [command] ...

peer list      List my peers
@justmoon
justmoon / README.md
Last active November 24, 2018 13:01
ILP Traceroute

Use Case

We'd like to know the path that a given ILP payment took or would take.

Solution

We add a header which lets you define a IPv6 UDP address and port and a unique ID.

{
@justmoon
justmoon / Bitcoin_ILP.md
Last active November 24, 2018 12:25
ILP Compatibility for Bitcoin

Introduction

Bitcoin is a popular digital asset hosted by a public proof-of-work blockchain. As such, it would be nice to allow Bitcoin users to (securely) participate in Interledger Protocol (ILP) transactions.

There are three modes of operation described in this document, each with different security and performance trade-offs:

  1. Escrow Hashed Timelocked Contract (HTLC)

    This is the most secure, but also slowest way to integrate with Bitcoin. Each ILP transaction requires two consecutive Bitcoin transactions, so a delay over more than an hour is to be expected. This mode would be suitable for larger payments.

  2. Payment Channel
@justmoon
justmoon / benchmark.js
Last active October 23, 2018 17:49
BigNumber.js vs Long for UInt64 serialization
const Benchmark = require('benchmark')
const BigNumber = require('bignumber.js').BigNumber
const Long = require('long')
const ITERATIONS_PER_CALL = 1000
const TEST_DATA_STRINGS = new Array(ITERATIONS_PER_CALL).fill(null).map((v, i) => String(i))
const TEST_DATA_BUFFERS = TEST_DATA_STRINGS.map(str => Buffer.from(new BigNumber(str).toString(16).padStart(16, '0'), 'hex'))
const HIGH_WORD_MULTIPLIER = 0x100000000
@justmoon
justmoon / codius.geojson
Created June 19, 2018 12:16
Codius hosts as of 2018-06-18
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.