backport some changes from openbazaar.