Skip to content

Instantly share code, notes, and snippets.

View rumkin's full-sized avatar
🧠
Getting deeper

Paul Rumkin rumkin

🧠
Getting deeper
View GitHub Profile
@rumkin
rumkin / engine.md
Last active January 29, 2020 06:36
ES and SQL based smart contract engine

ES and SQL as a smart contract enginge

This is a simple example of a smart contract written in ECMAScript which uses SQL database for storing the state.

class Engine {
  constructor(db, contracts) {
    this.db = db
    this.contracts = contracts
  }
@rumkin
rumkin / readme.md
Last active January 9, 2020 02:40
Step-by-step article template

Steps

  1. Do the first step.
  2. Do the second step.
  3. Do the third step.

✅ 1. Do the first step

Do the first step actions.

@rumkin
rumkin / plant.js
Created November 16, 2019 03:54
Plant Request example
plant.use(async ({req, res}) => {
const body = await req.json()
res.json(body)
})
@rumkin
rumkin / protocol.js
Last active November 16, 2019 03:53
Serve HTTPS protocol in Electron with Plant
import {app} from 'electron'
import {createServer} from '@plant/electron'
import Plant from '@plant/plant'
app.on('ready', () => {
// Define simple Plant server
const plant = new Plant()
plant.use(({res}) => {
res.text('OK')
@rumkin
rumkin / json64.js
Created October 23, 2019 03:53
JSON wrapped into base64 encoding
export function decodeJson64(value) {
return JSON.parse(
Buffer.from(value, 'base64')
)
}
export function encodeJson64(value) {
return Buffer.from(
JSON.stringify(value), 'utf8'
)
@rumkin
rumkin / split.js
Last active July 22, 2020 22:07
Split string into chunks by text delimiter
/**
* Split string by delimiter limited times. By default 1.
*
* @example
* split(':', 'user:id:1') -> ['user', 'id:1']
* split(':', 'user:id:1:login:admin', 2) -> ['user', 'id' , '1:login:admin']
* split(':', 'hello') -> ['hello']
*/
export function split(delim, str, count = 1) {
if (count < 1) {
@rumkin
rumkin / index.js
Created August 12, 2019 10:13
rlp `npm run build` output result
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP
* This function takes in a data, convert it to buffer if not, and a length for recursion
* @param input - will be converted to buffer
* @returns returns buffer of encoded data
**/
function encode(input) {
if (Array.isArray(input)) {
@rumkin
rumkin / prism-chrome-alike.css
Last active July 9, 2019 12:04
Chrome alike source highlighting theme for Prism.
/*
Source: https://mariusschulz.com
*/
.token.cdata,.token.comment,.token.doctype,.token.prolog {
color: #a0a1a7;
font-style: italic
}
.token.boolean {
@rumkin
rumkin / pseudo-random.js
Created March 15, 2019 06:22
Generate deterministic randomness
// Got from https://github.com/NodeGuy/Deterministic.js
function createRandomizer() {
let s0, s1, s2, c;
s0 = 0.8725217853207141;
s1 = 0.520505596883595;
s2 = 0.22893249243497849;
c = 1;
@rumkin
rumkin / chacha20.js
Last active May 29, 2020 10:50
Chacha20-Poly1305.js
/* chacha20 - 256 bits */
// Written in 2014 by Devi Mandiri. Public domain.
//
// Implementation derived from chacha-ref.c version 20080118
// See for details: http://cr.yp.to/chacha/chacha-20080128.pdf
function U8TO32_LE(x, i) {
return x[i] | (x[i+1]<<8) | (x[i+2]<<16) | (x[i+3]<<24);
}