`sign_transaction` and `sign_message` use very similar code, with just the decoding and encoding functions changing. Wrap this code in a new `sign` method called by the other two methods. This will make it simpler to switch the signing code to not rely on the `ecdsa` library.
Partial backport of https://github.com/spesmilo/electrum/commit/ac94d6d055807ecb3b2ba044b0bcd9a12a9b3b7c
Depends on D16685