Skip to content

Instantly share code, notes, and snippets.

View akirattii's full-sized avatar

Akira TANAKA akirattii

View GitHub Profile
@akirattii
akirattii / async-iterator-example.js
Created November 13, 2018 04:39
NodeJS v10: An example of Async-Iterator with Stream API
const fs = require("fs");
const inputFilePath = process.argv[2];
console.log("file:", inputFilePath);
async function main(inputFilePath) {
const readStream = fs.createReadStream(inputFilePath, { encoding: 'utf8', highWaterMark: 1024 });
// NOTE: Async-Iterator starts with NodeJS v10, but it's experimental yet. (Current latest LTS version: v10.13.0)
for await (const chunk of readStream) {
@akirattii
akirattii / chart.js-simple-stock-chart-example.html
Created October 4, 2018 00:54
[Chart.js] Simple stock chart example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Char.js Simple Stock Chart Example</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js" integrity="sha256-XF29CBwU1MWLaGEnsELogU6Y6rcc5nCkhhx89nFMIDQ=" crossorigin="anonymous"></script>
@akirattii
akirattii / parsePaymentURIScheme.js
Created August 28, 2018 03:13
JS: How to parse Payments URI Scheme
/**
* Payments URI Scheme Parser
* @param {String} - eg. "bitcoin:1XXX?amount=123&comment=%66%77"
* @see also: https://github.com/bitcoin/bips/blob/master/bip-0021.mediawiki
*/
function parsePaymentURIScheme(s) {
const tmp = s.match(/^(\w+)\:(\w+)\??(.+)?/);
const protocol = (tmp && tmp.length >= 1) ? tmp[1] : null;
const address = (tmp && tmp.length >= 2) ? tmp[2] : null;
const qstr = (tmp && tmp.length >= 3) ? tmp[3] : null;
@akirattii
akirattii / generate-entropy-on-mouse-moving.js
Created August 26, 2018 23:07
JS: How to generate a random entropy by mouse-moving on browser
// const entropy = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
const entropy = [];
let captureStart = false;
/**
* Mouse Moving Entropy Generator on browser.
* Returns an entropy which is 16 bytes long array of unsigned char integer (0-255).
*/
$(document).on("mousemove", "html", function(e) {
const MAX_LEN = 16; // size of entropy's array
@akirattii
akirattii / aes-encrypt-decrypt.js
Last active August 21, 2018 03:56
NodeJS: aes-encrypt / aes-decrypt by using string password. (Browser compatible and I think as a secure way at this point ...)
const aesjs = require('aes-js');
const pbkdf2 = require('pbkdf2');
const password = "mySecretPassword";
const message = "HelloWorld!";
const encrypted = encrypt(message, password);
console.log("encrypted:", encrypted); // 3ed20b7c474c96af513d1c
const decrypted = decrypt(encrypted, password);
console.log("decrypted:", decrypted); // HelloWorld!
@akirattii
akirattii / ripple-tx-validation-check-memo.md
Last active August 17, 2018 02:19
Ripple: How to validate a tx's result after submitting it. (private memo)
// submit tx:
submitTx(signedTx, (err, res) => {
  if (err) throw err;
  // Check if `engine_result` is "tesSUCCESS": 
  if (res.engine_result !== "tesSUCCESS") {
    // the tx's failed immediately.
    return;
  }
  const txhash = res.tx_json.hash;
@akirattii
akirattii / ripple-understand-rippled-book_offers-request-response.md
Created August 17, 2018 01:16
Ripple: Understand rippled `book_offers` request & response

Ripple: Understand rippled book_offers request & response

Request example (on TestNet)

{
  "id": 4,
  "command": "book_offers",
  "taker": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59",
  "taker_gets": {
    "currency": "XRP"
@akirattii
akirattii / WsClient.js
Created August 10, 2018 07:24
NodeJS: WebSocket Client (Browser-compatible)
/*
websocket client module (supports either NodeJS and browser)
NOTE:
`browserify` makes this websocket client lib browser-compatible.
# Usage
```
const onmessage = function(e) {
console.log("onmessage:", e);
@akirattii
akirattii / RpcClient.js
Last active July 11, 2018 03:13
NodeJS: RPC Client module class (either callback and async/await available)
// Simple JSON RPC Client
const request = require("request");
const requestAsync = require("request-promise");
// const axios = require("axios");
module.exports = class RpcClient {
constructor({
url,
jsonrpc = "2.0",
@akirattii
akirattii / getCounterwalletAddress.js
Created July 3, 2018 00:12
[Counterparty] Get Counterwallet's addresses from passphrase by using bitcore-lib
/*
* Usage:
* $ node ./getCounterwalletAddress.js "<livenet_or_testnet>" <derived_index> "<passphrase of 12 words>"
*/
const network = process.argv[2]; // "testnet" or "livenet"
const index = process.argv[3]; // derived address's index
const passphrase = process.argv[4]; // passphrase
// Mnemonic function inspired by:
// Mnemonic.js v. 1.0.0