<p>OK, I figured out how to sign the raw Tx using Python ecdsa. I'll step through it:</p>

<p>Recall:</p>

<p><code>createrawtransaction '[{"txid" : "72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a", "vout" : 1}]' '{"n2kx7k6JuA5Wy27fawaeiPX7dq8mbRDPAv" : 0.99}'</code> =
<code>01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914e900510876cb689f1db6fa982376c301362b740c88ac00000000</code></p>

<p>And then adding in the scriptPubKey for scriptSig's <code>00</code>:</p>

<p><code>unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'</code></p>

<p>Note <code>unsigned</code> has the <code>01000000</code> appended because the SIGNHASH is <code>01</code> (which will also be appended to the DER signature in a minute)</p>

<p>So, we double SHA256 <em>bytes</em> and return <code>txhash</code> as <em>bytes</em>:
<code>txhash = hashlib.sha256(hashlib.sha256(unsigned.decode('hex')).digest()).digest()</code></p>

<p><strong>We also need the <em>privkey</em></strong> for <code>mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB</code> (in secret exponent form, ie hex): 
<code>privkey = 'dc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee'.zfill(64)</code>
privkey = 'dc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee'</p>

<p>Now, we sign using this <strong>corrected code</strong>:</p>

<pre><code>signingkey = ecdsa.SigningKey.from_string(privkey.decode('hex'), curve=ecdsa.SECP256k1)
SIG = signingkey.sign_digest(txhash, sigencode=ecdsa.util.sigencode_der) +'01'.decode('hex')
</code></pre>

<p>This is working code. From here on in, we're simply making the signature:</p>

<p><code>ScriptSig</code> = <code>&lt;varint of total sig length&gt;</code> <code>&lt;SIG from code, including appended 01 SIGNHASH&gt;</code> <code>&lt;length of pubkey (0x21 or 0x41)&gt;</code> <code>&lt;pubkey&gt;</code></p>

<p>where <code>pubkey = '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1</code></p>

<p>Giving us a valid signed Tx <em>without using bitcoincore</em>:</p>

<p><code>01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008a47304402204d78d2e6c0f801573e4960fb8e51ad939380d119d25f97d15efdedf815b05f02022066bd2ab0b401e32e7ce67ea45f8224097eeafbef2335d563776e5efe6632732d01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000</code></p>

CC BY-SA 3.0
2015-03-23T18:23:16.477Z
Thank you very much!!! Took a bit of figuring out but your tips helped a lot.
ethought
CC BY-SA 3.0
2015-12-12T08:34:01.910Z
That ecdsa signing code often returns 'Non-canonical signature: High S Value' that are now non-standard. Any ideas of how can this be solved?
Nathan Parker
CC BY-SA 3.0
2015-12-13T08:56:54.850Z
@NathanParker `SIG` is `<r><s>` concatenated together. We need to check `s < N/2` where `N` is the curve order, 115792089237316195423570985008687907852837564279074904382605163141518161494337. If `s>N/2`, then `s = N-s`
Wizard Of Ozzie
CC BY-SA 3.0
2015-12-13T09:43:49.770Z
@NathanParker to use Python-ECDSA, replace `sigencode=ecdsa.util.sigencode_der` with `sigencode=ecdsa.util.sigencode_der_canonize`
Wizard Of Ozzie
CC BY-SA 3.0
2015-12-14T10:36:27.447Z
I would have never guessed it was so incredibly easy. Thanks!
Nathan Parker
CC BY-SA 3.0
2015-03-16T03:40:19.920Z
2015-03-16T03:40:19.920Z
Wizard Of Ozzie
<p>I'm looking for specifics of Step15-17 from <a href="https://bitcoin.stackexchange.com/a/32695/9382" title="Redeeming a raw tx step by step">Redeeming a raw Tx Step By Step</a>, which is essentially the step where the concatenated raw Tx structure is double sha256 hashed, and then signed with an ECDSA library. I've also referred to <a href="https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx">other raw transactions guides</a>; <strong>I'm looking specifically how to sign a raw hex Tx structure without a GUI/website/etc</strong> </p>

<p>I've constructed a <em>testnet</em> Tx using Bitcoincore v0.10 (a full node) and attempted the same with Python and cannot correctly sign the raw Tx structure using <a href="https://github.com/warner/python-ecdsa" rel="nofollow noreferrer">Python <em>ecdsa</em></a>.</p>

<ul>
<li><strong>TxID:</strong> <a href="http://test.webbtc.com/tx/72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a.json" rel="nofollow noreferrer">72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a</a></li>
<li><strong>vout:</strong> 1 </li>
<li><strong>To:</strong> n1hjyVvYQPQtejJcANd5ZJM5rmxHCCgWL7</li>
<li><strong>Amount:</strong> 0.990 BTC</li>
</ul>

<p>I'll show the <strong>Bitcoincore commands</strong> first:</p>

<pre><code>importprivkey 93FxXUeMJp93YQAtGeW5cE23gFN4sJbBr1RBmerLFVUDuqQqKL5
mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB
createrawtransaction '[{"txid" : "72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a", "vout" : 1}]' '{"n1hjyVvYQPQtejJcANd5ZJM5rmxHCCgWL7" : 0.990}'
signrawtransaction 01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720000000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000
</code></pre>

<p>Final bitcoincore hex Tx (using <code>signrawtransaction</code>): 
 <code>01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008a47304402204d78d2e6c0f801573e4960fb8e51ad939380d119d25f97d15efdedf815b05f02022066bd2ab0b401e32e7ce67ea45f8224097eeafbef2335d563776e5efe6632732d01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000</code></p>

<p>Now the <strong>Python 2.7.9</strong> code follows (FYI, using <em>iPython 2.0</em> so there's boilerplate missing in the following code) :</p>

<pre><code># please excuse the hexlify/unhexlify, I work better in strings than bytes
import __future__
from pybitcointools import sha256
import hashlib, ecdsa
from ecdsa import SigningKey, SECP256k1
addr, pubkey = 'mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB', '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1'
rawtx = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000' # TX structure with 00 as scriptSig, from bitcoincore createrawtransaction
# replacing the '00' scriptSig value
unsigned.append(rawtx[:82])  
unsigned.append('19'+'76a914953de657be4b305f606d9a9fbd35b070a682475788ac') # scriptSig = scriptPubKey input
unsigned.append(rawtx[84:]) 
unsigned.append('01000000')  # appending sighash_all
unsigned = ''.join(unsigned)

unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'

# See https://github.com/warner/python-ecdsa/blob/master/README.md
sig1 = sk.sign(sha256(sha256(unsigned)), hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der) # note this references 2 different sha256 functions
sig2 = [hex(len(sig1 + '\x01'))[2:], hexlify(sig1), hexlify('\x01'), hex(len(unhexlify(pubkey)))[2:], pubkey]
sig2
&gt;&gt;&gt; ['48',  '304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b', '01', '41', '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1']
sig2 = len(''.join(map(unhexlify, sig2)))
hex(len(''.join(map(unhexlify, sig2))))[2:] 
&gt;&gt;&gt; '8b'  # push 139 bytes
sig3 
&gt;&gt;&gt; 8b48304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1 
signed = unsigned[:82] + sig3 + unsigned[84:]  # inserting the signature into the 00 the core software uses in place of ScriptSig
</code></pre>

<p>Final (<strong>invalid</strong>) Python Tx:</p>

<p><code>01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008b48304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000</code></p>

<p>Sending the Python generated Tx does not work. If I <code>sendrawtransaction &lt;PYTHON HEX&gt;</code> in Bitcoincore it returns an invalid signature error (Specifically, <em>16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) (code -26)</em>).</p>

<p><strong>(TL;DR) QUESTION</strong>: Where am I going wrong in signing the raw transaction structure manually?</p>

<p>NB: I am aware pybitcointools and related libraries can sign Txs, but I am looking for <strong>specifics</strong>, as even these two very thorough Bitcoin SE resources (<a href="https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required">LINK1</a>, <a href="https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx">LINK2</a>) brush past the details. I'd prefer an answer for <a href="https://github.com/warner/python-ecdsa" rel="nofollow noreferrer">Python ECDSA</a> or <a href="https://pypi.python.org/pypi/pycrypto" rel="nofollow noreferrer">PyCrypto</a>, however OpenSSL I suppose is the next best answer.</p>

CC BY-SA 3.0
2015-03-15T19:06:17.523Z
I've debugged this for the last two hours or so but haven't been able to find any errors. Sorry, I can't find anything wrong, and every time I try to submit a transaction it has error code -25. I've even verified that the hash being signed is the same one that the daemon signs through print lines in my compiled daemon. You can see my debugging file [here](http://pastebin.com/N7QCpdKg), in case it helps you.
morsecoder
CC BY-SA 3.0
2015-03-15T20:37:45.743Z
I see now that [that transaction output has already been spent](http://tbtc.blockr.io/tx/info/72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a). Does this mean you successfully found your error? If so, you should post it!
morsecoder
CC BY-SA 3.0
2015-03-16T01:32:22.173Z
@StephenM347 Yes! I'll add the code. The problem was the code for ECDSA
Wizard Of Ozzie
CC BY-SA 3.0
2015-03-16T07:46:07.900Z
@StephenM347 See the answer below but the issue seemed to arise from using the public key in signing and a disparity between string/bytes/DER input
Wizard Of Ozzie
<raw-transaction><ecdsa><python>
Signing a raw transaction with Python ECDSA (or OpenSSL)
Wizard Of Ozzie
# Signing a raw transaction with Python ECDSA (or OpenSSL)

(Asked by: username_0 on 2015-03-13)

I'm looking for specifics of Step15-17 from [Redeeming a raw Tx Step By Step](https://bitcoin.stackexchange.com/a/32695/9382 "Redeeming a raw tx step by step"), which is essentially the step where the concatenated raw Tx structure is double sha256 hashed, and then signed with an ECDSA library. I've also referred to [other raw transactions guides](https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx); **I'm looking specifically how to sign a raw hex Tx structure without a GUI/website/etc**

I've constructed a *testnet* Tx using Bitcoincore v0.10 (a full node) and attempted the same with Python and cannot correctly sign the raw Tx structure using [Python *ecdsa*](https://github.com/warner/python-ecdsa).

* **TxID:** [72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a](http://test.webbtc.com/tx/72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a.json)
* **vout:** 1
* **To:** n1hjyVvYQPQtejJcANd5ZJM5rmxHCCgWL7
* **Amount:** 0.990 BTC

I'll show the **Bitcoincore commands** first:

```
importprivkey 93FxXUeMJp93YQAtGeW5cE23gFN4sJbBr1RBmerLFVUDuqQqKL5
mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB
createrawtransaction '[{"txid" : "72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a", "vout" : 1}]' '{"n1hjyVvYQPQtejJcANd5ZJM5rmxHCCgWL7" : 0.990}'
signrawtransaction 01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720000000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000

```

Final bitcoincore hex Tx (using `signrawtransaction`):
`01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008a47304402204d78d2e6c0f801573e4960fb8e51ad939380d119d25f97d15efdedf815b05f02022066bd2ab0b401e32e7ce67ea45f8224097eeafbef2335d563776e5efe6632732d01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000`

Now the **Python 2.7.9** code follows (FYI, using *iPython 2.0* so there's boilerplate missing in the following code) :

```
# please excuse the hexlify/unhexlify, I work better in strings than bytes
import __future__
from pybitcointools import sha256
import hashlib, ecdsa
from ecdsa import SigningKey, SECP256k1
addr, pubkey = 'mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB', '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1'
rawtx = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000' # TX structure with 00 as scriptSig, from bitcoincore createrawtransaction
# replacing the '00' scriptSig value
unsigned.append(rawtx[:82])  
unsigned.append('19'+'76a914953de657be4b305f606d9a9fbd35b070a682475788ac') # scriptSig = scriptPubKey input
unsigned.append(rawtx[84:]) 
unsigned.append('01000000')  # appending sighash_all
unsigned = ''.join(unsigned)

unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'

# See https://github.com/warner/python-ecdsa/blob/master/README.md
sig1 = sk.sign(sha256(sha256(unsigned)), hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der) # note this references 2 different sha256 functions
sig2 = [hex(len(sig1 + '\x01'))[2:], hexlify(sig1), hexlify('\x01'), hex(len(unhexlify(pubkey)))[2:], pubkey]
sig2
>>> ['48',  '304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b', '01', '41', '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1']
sig2 = len(''.join(map(unhexlify, sig2)))
hex(len(''.join(map(unhexlify, sig2))))[2:] 
>>> '8b'  # push 139 bytes
sig3 
>>> 8b48304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1 
signed = unsigned[:82] + sig3 + unsigned[84:]  # inserting the signature into the 00 the core software uses in place of ScriptSig

```

Final (**invalid**) Python Tx:

`01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008b48304502200e98d54ad642488121fa1fd4d055ff8f5b40773a21ecb42b5ef44ed9fd3b103c022100a2a56ffc3f27021a4be5d1d7a3016eff08fe7f550a4a894cca845ceab54dc53b01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000`

Sending the Python generated Tx does not work. If I `sendrawtransaction <PYTHON HEX>` in Bitcoincore it returns an invalid signature error (Specifically, *16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) (code -26)*).

**(TL;DR) QUESTION**: Where am I going wrong in signing the raw transaction structure manually?

NB: I am aware pybitcointools and related libraries can sign Txs, but I am looking for **specifics**, as even these two very thorough Bitcoin SE resources ([LINK1](https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required), [LINK2](https://bitcoin.stackexchange.com/questions/3374/how-to-redeem-a-basic-tx)) brush past the details. I'd prefer an answer for [Python ECDSA](https://github.com/warner/python-ecdsa) or [PyCrypto](https://pypi.python.org/pypi/pycrypto), however OpenSSL I suppose is the next best answer.

**Tags:** raw-transaction, ecdsa, python

> **Comments:**
> **username_1**: I've debugged this for the last two hours or so but haven't been able to find any errors. Sorry, I can't find anything wrong, and every time I try to submit a transaction it has error code -25. I've even verified that the hash being signed is the same one that the daemon signs through print lines in my compiled daemon. You can see my debugging file [here](http://pastebin.com/N7QCpdKg), in case it helps you.  
> **username_1**: I see now that [that transaction output has already been spent](http://tbtc.blockr.io/tx/info/72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a). Does this mean you successfully found your error? If so, you should post it!  
> **username_0**: @StephenM347 Yes! I'll add the code. The problem was the code for ECDSA  
> **username_0**: @StephenM347 See the answer below but the issue seemed to arise from using the public key in signing and a disparity between string/bytes/DER input  

---

## 1 answers

### Answer by username_0 on 2015-03-16. Score: 6 | Accepted answer

OK, I figured out how to sign the raw Tx using Python ecdsa. I'll step through it:

Recall:

`createrawtransaction '[{"txid" : "72b764383b99fb3d112ac8b474a5d7c4242b75dbfee2d4e9cf9a6703d90f805a", "vout" : 1}]' '{"n2kx7k6JuA5Wy27fawaeiPX7dq8mbRDPAv" : 0.99}'` =
`01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b7720100000000ffffffff01c09ee605000000001976a914e900510876cb689f1db6fa982376c301362b740c88ac00000000`

And then adding in the scriptPubKey for scriptSig's `00`:

`unsigned = '01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000001976a914953de657be4b305f606d9a9fbd35b070a682475788acffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac0000000001000000'`

Note `unsigned` has the `01000000` appended because the SIGNHASH is `01` (which will also be appended to the DER signature in a minute)

So, we double SHA256 *bytes* and return `txhash` as *bytes*:
`txhash = hashlib.sha256(hashlib.sha256(unsigned.decode('hex')).digest()).digest()`

**We also need the *privkey*** for `mu858WTEPiWWpAJRTMxC4ka6DJqiaCZiSB` (in secret exponent form, ie hex):
`privkey = 'dc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee'.zfill(64)`
privkey = 'dc57c6d067376c36bbed632c9d00f03767867f337d5a86b5b0308a60004f08ee'

Now, we sign using this **corrected code**:

```
signingkey = ecdsa.SigningKey.from_string(privkey.decode('hex'), curve=ecdsa.SECP256k1)
SIG = signingkey.sign_digest(txhash, sigencode=ecdsa.util.sigencode_der) +'01'.decode('hex')

```

This is working code. From here on in, we're simply making the signature:

`ScriptSig` = `<varint of total sig length>` `<SIG from code, including appended 01 SIGNHASH>` `<length of pubkey (0x21 or 0x41)>` `<pubkey>`

where `pubkey = '0479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1`

Giving us a valid signed Tx *without using bitcoincore*:

`01000000015a800fd903679acfe9d4e2fedb752b24c4d7a574b4c82a113dfb993b3864b772010000008a47304402204d78d2e6c0f801573e4960fb8e51ad939380d119d25f97d15efdedf815b05f02022066bd2ab0b401e32e7ce67ea45f8224097eeafbef2335d563776e5efe6632732d01410479b22a5127d176a49d506c86f791031f94a389227ef46a8ddb725a88c944c37e3f753de6ee0b441a0237801f140810e111a1fd8276a2a5d0ee07224a1b551cc1ffffffff01c09ee605000000001976a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac00000000`

> **Comments:**
> **username_2**: Thank you very much!!! Took a bit of figuring out but your tips helped a lot.  
> **username_3**: That ecdsa signing code often returns 'Non-canonical signature: High S Value' that are now non-standard. Any ideas of how can this be solved?  
> **username_0**: @username_3 `SIG` is `<r><s>` concatenated together. We need to check `s < N/2` where `N` is the curve order, 115792089237316195423570985008687907852837564279074904382605163141518161494337. If `s>N/2`, then `s = N-s`  
> **username_0**: @username_3 to use Python-ECDSA, replace `sigencode=ecdsa.util.sigencode_der` with `sigencode=ecdsa.util.sigencode_der_canonize`  
> **username_3**: I would have never guessed it was so incredibly easy. Thanks!  

---

