ported over from Dogecoin and Litecoin (cherry picked from commit 8b09dad2d506fc5a6239863b1c0e33bf7e84da00)