トランザクションへの署名

署名ハッシュzを取得する方法がわかっている
ScriptPubKey内の20バイトのハッシュ(hash160)の元となる公開鍵に対応する秘密鍵を知っている場合、zに署名してDER署名を生成できる

1
2
3
4
5
6
7
8
9
10
from ecc import PrivateKey
from helper import SIGHASH_ALL
z = tranzaction.sig_hash(0)
private_key = PrivateKey(secret=8675309)
der = private_key.sign(z).der()
sig = der + SIGHASH_ALL.to_bytes(1, 'big')
sec = private_key.point.sec()
script_sig = Script([sig, sec])
transaction.tx_ins[0].script_sig = script_sig
print(transaction.serialize().hex())

関数上では、input_indexは0固定ではなく、input_indexで指定する。
der署名のscript_sigはprivate_keyとzから作成する。

1
2
3
4
5
6
7
def sign_input(self, input_index, private_key):
    z = self.sig_hash(input_index)
    der = private_key.sign(z).der()
    sig = der = SIGHASH_ALL.to_bytes(1, 'big')
    sec = private_key.point.sec()
    self.tx_ins[input_index].script_sig = Script([sig, sec])
    return self.verify_input(input_index)