Skip to content

Instantly share code, notes, and snippets.

View JCloudYu's full-sized avatar

JCloudYu JCloudYu

View GitHub Profile
@JCloudYu
JCloudYu / keys
Created March 17, 2018 15:39 — forked from miguelmota/ethereum_keys.sh
Generate Ethereum Private key, Public key, and Address using bash and openssl
# Generate the private and public keys
openssl ecparam -name secp256k1 -genkey -noout | openssl ec -text -noout > key
# Extract the public key and remove the EC prefix 0x04
cat key | grep pub -A 5 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^04//' > pub
# Extract the private key and remove the leading zero byte
cat key | grep priv -A 3 | tail -n +2 | tr -d '\n[:space:]:' | sed 's/^00//' > priv
# Generate the hash and take the address part
String.prototype.shuffle = function(){
var a = this.split(""),
n = a.length;
for( var i = n - 1; i > 0; i-- ){
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
const crypto = require( 'crypto' );
module.exports = {
UUIDv4(){
let random_num = crypto.randomBytes(16);
random_num[6] = (random_num[6] & 0x0f) | 0x40;
random_num[8] = (random_num[8] & 0x3f) | 0x80;
return [
random_num.slice(0, 4).toString('hex'),
@JCloudYu
JCloudYu / native-node-fnv1a.js
Last active September 5, 2018 04:19
fnv1a hash without any external dependencies
/**
* Project: beson
* File: fnv1a.js
* Author: JCloudYu
* Create Date: Sep. 03, 2018
*/
(()=>{
"use strict";
// See http://www.isthe.com/chongo/tech/comp/fnv/#FNV-param for the definition of these parameters;
(()=>{
"use strict";
module.exports = ()=>{
let resolve, reject;
let promise = new Promise((rs, rj)=>{resolve=rs; reject=rj;});
promise.trigger = {resolve, reject};
return promise;
};
})();
@JCloudYu
JCloudYu / async-loop.js
Last active September 15, 2018 05:29
In case if I need a loop that requires heavy garbage collection...
(()=>{
"use strict";
module.exports = async_loop;
/**
* Provide asynchronous version of for / while loop controlling logic.
* This function mimic following logic using setTimeout mechanism.
@JCloudYu
JCloudYu / event-emitter.js
Last active September 15, 2018 15:50
A tinny reimplementation of node-js styled event emitter
/**
* Author: JCloudYu
* Create: 2018/09/15
**/
((exports)=>{
"use strict";
const _previous = exports.EventEmitter;
@JCloudYu
JCloudYu / base32.js
Created September 16, 2018 04:40
A base32 reimplementation using native nodejs
(()=>{
"use strict";
const __FORMAT_CHECK = /^[0123456789ABCDEFGHIJKLMNOPQRSTUV]+$/;
const __CHAR_MAP = '0123456789ABCDEFGHIJKLMNOPQRSTUV'.split('');
function __BASE32_ENCODE(inputData, map=null) {
if ( !Buffer.isBuffer(inputData) ) {
inputData = Buffer.from(inputData);
}
@JCloudYu
JCloudYu / unicode.fix.js
Last active October 19, 2018 05:10
Add unicode_length to count the correct number of characters in the string and fix incorrect index problem of codePointAt api
/**
ISC License
Copyright (c) 2018, JCloudYu
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
@JCloudYu
JCloudYu / esm-loader.mjs
Last active October 14, 2020 04:27
A loader module for node js that makes modules ended with .esm.js to be loaded as a standard es module
/**
* ISC License
*
* Copyright (c) 2020, J. Cloud Yu
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES