moved code out for bitpay-core