Skip to content

Instantly share code, notes, and snippets.

@kepikoi
kepikoi / convert-public-key-to-ethereum-address.js
Last active March 17, 2021 12:06
Extract the Ethereum address from a secp256k1 public key as returned by `GET https://api.tangany.com/v1/wallet/:wallet`
const {keccak256} = require("js-sha3");
const assert = require("assert");
/**
* Extract the Ethereum address from a secp256k1 public key as returned by `GET https://api.tangany.com/v1/wallet/:wallet`
* @param {string | Buffer} pub - secp256k1 public key of the wallet
* @return {string} - Ethereum address of the wallet
* @example
* convertPublicKeyToEthereumAddress("04150341ab9f7b9a3d0eafc6180eac39b65b34da30bddbd13cee1bbdabf4b420b79962342664902c31c3da7376eaf65adeb7dfffccba1de1e11fb6779b428a3c31"));
*/
@kepikoi
kepikoi / convert-hex-to-pem.js
Last active January 22, 2021 12:44
Converts a secp256k1 public key as returned by https://docs.tangany.com/#f95ba7a2-5526-4eef-b0da-7d7a13be34d2 to OpenSSL PEM format
/**
* Converts a secp256k1 public key as returned by `GET https://api.tangany.com/v1/wallet/:wallet` to OpenSSL PEM format
* @param {string} publicKey - hexadecimal key to convert
* @return PEM-formatted representation of given public key
* @example
* const pem = convertPublicKeyToPEM("04f500418025ba3babca935e9f7617c438210ab72ae3ece0b25e5dff579c31ddd1549ab1ee4f0dcb53e51aacceb15147af6f4a1610040aa8e042a0f3e208496257");
*/
function convertPublicKeyToPEM (publicKey) {
let key = Buffer.from(publicKey, "hex");
@kepikoi
kepikoi / api-math.spec.js
Created September 20, 2018 13:12
intercept and test modules via tape and proxyquire
//test suite for math-api.js module, which mocks the api.mathjs.org api responses
const
{test} = require("tape")
, proxyquire = require("proxyquire")
, mathjs = require("mathjs")
, {URL} = require("url")
, requestPromiseStub = {
//request-promise stub that tests the argument and mocks the api response
"request-promise"(u) {
@kepikoi
kepikoi / curry-event-emitter.js
Created July 13, 2018 08:28
Curry node's event emitter listener to a standalone function
const Event = require("events");
const emitter = new Event();
const eventName = Symbol();
/**
* Echos given message to the callback function after a random amount of time
* @param {String} message - message that will be echoed back
* @param {Function} callback - function the echo will be passed to
*/
function belatedEcho(message, callback) {