How to sign bitcoin raw transactions with Python offline


I have a raw bitcoin transaction created using createrawtransaction

unsigned_tx = "02000000014bcb8bcbf347d2f91a11703afa644a3ba43cd25a1e8cf538732d65aeb54401070000000000cffgffff0137670000000000001970a91436c27d3f6dd809c25cfbf7d2670ab701a5717b0488ac00000000"

The raw transaction was created using multiple inputs and a single output.

Now I am trying to find a way to sign that transaction offline using Python3 and not connecting to a node.

I have been searching through the web with no luck.

Edit 1: After following the steps in, I get stuck with this exception:

In [41]: private_key = "3699556636270870316293620289962549511891625341283681895576015487131205320420"

In [42]: ecdsa.SigningKey.from_string(bytes.fromhex(privat_key), curve=ecdsa.SECP256k1)
AssertionError                            Traceback (most recent call last)
<ipython-input-42-48390f7d05da> in <module>
----> 1 ecdsa.SigningKey.from_string(bytes.fromhex(private_key), curve=ecdsa.SECP256k1)

~/workspace/virtualenvs/blah-folder/lib/python3.6/site-packages/ecdsa/ in from_string(klass, string, curve, hashfunc)
    147     @classmethod
    148     def from_string(klass, string, curve=NIST192p, hashfunc=sha1):
--> 149         assert len(string) == curve.baselen, (len(string), curve.baselen)
    150         secexp = string_to_number(string)
    151         return klass.from_secret_exponent(secexp, curve, hashfunc)

AssertionError: (38, 32)

Any ideas where I am going wrong?

Bongani Sibanda

Posted 2019-04-04T11:39:14.590

Reputation: 21

You could refer to

– Ugam Kamat – 2019-04-04T11:56:48.420

@UgamKamat I tried it out. Please see my edit 1 – Bongani Sibanda – 2019-04-05T11:58:13.057

1You have declared private key as a string and you are using it as hex. In that format, your private key is 38 bytes. The curve is asserting that it should be 32 bytes before it performs the operation and as a result you are getting the AssertionError. – Ugam Kamat – 2019-04-05T12:33:27.590

No answers