Last active
March 4, 2020 19:05
-
-
Save darklow/331184965c9d8e32073d9b0c297777fa to your computer and use it in GitHub Desktop.
pycoin_custom_sign.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pycoin.encoding.hexbytes import b2h | |
from pycoin.intbytes import int2byte | |
from pycoin.key import Key | |
from pycoin.networks.registry import network_for_netcode | |
from pycoin.satoshi.der import sigencode_der | |
network = network_for_netcode('XTN') | |
Tx = network.tx | |
TxIn = network.tx.TxIn | |
TxOut = network.tx.TxOut | |
def print_debug(key, s): | |
print(f'{key: <22} {s}') | |
key = network.keys.private(1) | |
hash160_c = key.hash160(is_compressed=True) | |
# address = network.address.for_p2pkh(hash160_c) | |
# p2sh_script = network.contract.for_p2pkh_wit(hash160_c) | |
p2sh_script = network.contract.for_p2pkh(hash160_c) | |
p2s_address = network.address.for_p2s(p2sh_script) | |
p2sh_script_hex = b2h(p2sh_script) | |
print_debug('p2s_address', p2s_address) | |
print_debug('p2sh_script_hex', b2h(p2sh_script)) | |
# prepare tx | |
tx_out_script = p2sh_script | |
tx_out = TxOut(100, tx_out_script) | |
tx = Tx(1, [TxIn(b'\1' * 32, 1)], [TxOut(100, tx_out_script)]) | |
tx.set_unspents([tx_out]) | |
print_debug('bad_solutions', tx.bad_solution_count()) | |
# prepare sig hash | |
flags = network.validator.flags | |
sig_type = flags.SIGHASH_ALL | |
solution_checker = network.tx.SolutionChecker(tx) | |
unsigned_txs_out_idx = 0 | |
sig_hash = solution_checker._signature_hash(tx_out_script, unsigned_txs_out_idx, sig_type) | |
print_debug('sig_hash', sig_hash) | |
print_debug('sig_hash hex', '{:x}'.format(sig_hash)) | |
# sign elsewhere (extracted by debug based on network.keys.private(1)) | |
r = 59003615986991787537031492581417205299428596081949568304619633893412949461700 | |
s = 11031196392734664346578037991328651715905060006389929906494918392260154422648 | |
sig_der = sigencode_der(r, s) + int2byte(sig_type) | |
sig_der_hex = b2h(sig_der) | |
print_debug('signature der', sig_der_hex) | |
p2sh_lookup = network.tx.solve.build_p2sh_lookup([tx_out_script]) | |
# Doesn't work | |
network.tx_utils.sign_tx(tx, p2sh_lookup=p2sh_lookup, signature_hints=[sig_der]) | |
print_debug('bad_solutions', tx.bad_solution_count()) | |
# Does work with WIF signing | |
# network.tx_utils.sign_tx(tx, [key.wif()]) | |
# print_debug('bad_solutions', tx.bad_solution_count()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment