def sign(tx, i, priv, t="default", script="", hashcode=SIGHASH_ALL): i = int(i) #if (not is_python2 and isinstance(re, bytes)) or not re.match('^[0-9a-fA-F]*$', tx): if not re.match('^[0-9a-fA-F]*$', tx): return binascii.unhexlify( custom_sign(safe_hexlify(tx), i, priv, hashcode)) if len(priv) <= 33: priv = b.safe_hexlify(priv) pub = b.privkey_to_pubkey(priv) address = b.pubkey_to_address(pub) if t not in ["atomic_1", "atomic_2"]: script = b.mk_pubkey_script(address) if script == "": error() signing_tx = b.signature_form( tx, i, script, hashcode) #mk_pubkey_scrip needs to be our custom scriptn sig = b.ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = b.deserialize(tx) if t == "atomic_1": txobj["ins"][i]["script"] = b.serialize_script([sig]) if t == "atomic_2": old_sig = txobj["ins"][i]["script"] txobj["ins"][i]["script"] = b.serialize_script([old_sig, sig, 1]) else: txobj["ins"][i]["script"] = b.serialize_script([sig, pub]) return b.serialize(txobj)
def sign_donation_tx(tx, i, priv): from bitcoin.main import fast_multiply, decode_privkey, G, inv, N from bitcoin.transaction import der_encode_sig k = sign_k hashcode = btc.SIGHASH_ALL i = int(i) if len(priv) <= 33: priv = btc.safe_hexlify(priv) pub = btc.privkey_to_pubkey(priv) address = btc.pubkey_to_address(pub) signing_tx = btc.signature_form( tx, i, btc.mk_pubkey_script(address), hashcode) msghash = btc.bin_txhash(signing_tx, hashcode) z = btc.hash_to_int(msghash) # k = deterministic_generate_k(msghash, priv) r, y = fast_multiply(G, k) s = inv(k, N) * (z + r * decode_privkey(priv)) % N rawsig = 27 + (y % 2), r, s sig = der_encode_sig(*rawsig) + btc.encode(hashcode, 16, 2) # sig = ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = btc.deserialize(tx) txobj["ins"][i]["script"] = btc.serialize_script([sig, pub]) return btc.serialize(txobj)
def sign_donation_tx(tx, i, priv): from bitcoin.main import fast_multiply, decode_privkey, G, inv, N from bitcoin.transaction import der_encode_sig k = sign_k hashcode = btc.SIGHASH_ALL i = int(i) if len(priv) <= 33: priv = btc.safe_hexlify(priv) pub = btc.privkey_to_pubkey(priv) address = btc.pubkey_to_address(pub) signing_tx = btc.signature_form(tx, i, btc.mk_pubkey_script(address), hashcode) msghash = btc.bin_txhash(signing_tx, hashcode) z = btc.hash_to_int(msghash) # k = deterministic_generate_k(msghash, priv) r, y = fast_multiply(G, k) s = inv(k, N) * (z + r * decode_privkey(priv)) % N rawsig = 27 + (y % 2), r, s sig = der_encode_sig(*rawsig) + btc.encode(hashcode, 16, 2) # sig = ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = btc.deserialize(tx) txobj["ins"][i]["script"] = btc.serialize_script([sig, pub]) return btc.serialize(txobj)
def bitcoin_address(digest): bitcoin_keys = [ '\x02\xc0T!\xaa\x00\x13\xee\xd38T#\xb6<\xd2\x89' + '\xc3BR\x118\xaa\xffj\x91U\xb3\xc7\xc8t\xc3\x1e\xa9', '\x03\xb3\xb1|\xe2\x13\xe4\xed\xb9\xf1\x7f\x0e\x11' + '\xf5h\x80\xa8\x96r\xd2 4\x83\xbb\x7fu\xb1\x1a%_\x08\xdc\x96' ] script = bitcoin.serialize_script([digest, 117, 2] + bitcoin_keys + [2, 174]) return bitcoin.p2sh_scriptaddr(script)
def apply_segwit_multisignatures(tx, i, witness_program, signatures, dummy=True, nested=False): o = [""] + signatures + [witness_program] if dummy else signatures + [witness_program] txobj = deserialize(tx) txobj['ins'][i]['txinwitness'] = o if nested: redeem_script = hashlib.sha256(binascii.unhexlify( witness_program )).hexdigest() length = len(redeem_script) // 2 redeem_script = serialize_script( [length + 2, None, redeem_script]) txobj["ins"][i]["script"] = redeem_script return serialize(txobj)
def sign(tx, i, priv, t="default", script="", hashcode=SIGHASH_ALL): i = int(i) #if (not is_python2 and isinstance(re, bytes)) or not re.match('^[0-9a-fA-F]*$', tx): if not re.match('^[0-9a-fA-F]*$', tx): return binascii.unhexlify(custom_sign(safe_hexlify(tx), i, priv, hashcode)) if len(priv) <= 33: priv = b.safe_hexlify(priv) pub = b.privkey_to_pubkey(priv) address = b.pubkey_to_address(pub) if t not in ["atomic_1", "atomic_2"]: script=b.mk_pubkey_script(address) if script=="": error() signing_tx = b.signature_form(tx, i, script, hashcode)#mk_pubkey_scrip needs to be our custom scriptn sig = b.ecdsa_tx_sign(signing_tx, priv, hashcode) txobj = b.deserialize(tx) if t=="atomic_1": txobj["ins"][i]["script"] = b.serialize_script([sig]) if t=="atomic_2": old_sig = txobj["ins"][i]["script"] txobj["ins"][i]["script"] = b.serialize_script([old_sig, sig, 1]) else: txobj["ins"][i]["script"] = b.serialize_script([sig, pub]) return b.serialize(txobj)
def segwit_strip_script_separator(script, index=0): if index == None: return script OP_CODESEPARATOR = 171 def get_pos(script, index): i = 0 for x in range(0, index): try: i = script.index(OP_CODESEPARATOR, i) except ValueError: return i return i + 1 deserialized_script = deserialize_script(str(script)) pos = get_pos(deserialized_script, index) return serialize_script(deserialized_script[pos:])
def apply_segwit_multisignatures_deserialized_data(deserialized_tx, i, witness_program, signatures, dummy=True, nested=True): o = [""] + signatures + [witness_program ] if dummy else signatures + [witness_program] deserialized_tx['ins'][i]['txinwitness'] = o if nested: redeem_script = hashlib.sha256( binascii.unhexlify(witness_program)).hexdigest() length = len(redeem_script) // 2 redeem_script = serialize_script([length + 2, None, redeem_script]) deserialized_tx["ins"][i]["script"] = redeem_script return deserialized_tx
def apply_multisignatures(*args): # tx,i,script,sigs OR tx,i,script,sig1,sig2...,sig[n] tx, i, script = args[0], int(args[1]), args[2] sigs = args[3] if isinstance(args[3], list) else list(args[3:]) if isinstance(script, str) and re.match('^[0-9a-fA-F]*$', script): script = binascii.unhexlify(script) sigs = [binascii.unhexlify(x) if x[:2] == '30' else x for x in sigs] if isinstance(tx, str) and re.match('^[0-9a-fA-F]*$', tx): signed_tx = apply_multisignatures(binascii.unhexlify(tx), i, script, sigs) return binascii.hexlify(signed_tx) # Not pushing empty elements on the top of the stack if passing no # script (in case of bare multisig inputs there is no script) script_blob = [] if script.__len__() == 0 else [script] txobj = deserialize(tx) txobj["ins"][i]["script"] = serialize_script([None]+sigs+script_blob) return serialize(txobj)
def apply_multisignatures(*args): # tx,i,script,sigs OR tx,i,script,sig1,sig2...,sig[n] tx, i, script = args[0], int(args[1]), args[2] sigs = args[3] if isinstance(args[3], list) else list(args[3:]) if isinstance(script, str) and re.match('^[0-9a-fA-F]*$', script): script = binascii.unhexlify(script) sigs = [binascii.unhexlify(x) if x[:2] == '30' else x for x in sigs] if isinstance(tx, str) and re.match('^[0-9a-fA-F]*$', tx): signed_tx = apply_multisignatures(binascii.unhexlify(tx), i, script, sigs) return binascii.hexlify(signed_tx) # Not pushing empty elements on the top of the stack if passing no # script (in case of bare multisig inputs there is no script) script_blob = [] if script.__len__() == 0 else [script] txobj = deserialize(tx) txobj["ins"][i]["script"] = serialize_script([None] + sigs + script_blob) return serialize(txobj)
def tx_sign_singlesig(tx, idx, private_key_info, hashcode=bitcoin.SIGHASH_ALL): """ Sign a p2pkh input Return the signed transaction TODO: move to virtualchain NOTE: implemented here instead of bitcoin, since bitcoin.sign() can cause a stack overflow while converting the private key to a public key. """ pk = virtualchain.BitcoinPrivateKey(str(private_key_info)) pubk = pk.public_key() pub = pubk.to_hex() addr = pubk.address() script = virtualchain.make_payment_script(addr) sig = tx_make_input_signature(tx, idx, script, private_key_info, hashcode) txobj = bitcoin.deserialize(str(tx)) txobj['ins'][idx]['script'] = bitcoin.serialize_script([sig, pub]) return bitcoin.serialize(txobj)
import binascii import bitcoin address = '3BSBPJyP7r6W7WBz74ghL6W3A1aB7F6SoF' pk = 'b3a6b8c655d471f003be2d0ccf5766c48cc36cdf02110cfcf3920971571c02b5' msg = 'Grazie mille per il bellissimo ed originale regalo di matrimonio!' OP_RETURN = 106 OP_PUSHDATA1 = 76 script = [OP_RETURN, OP_PUSHDATA1, len(msg)] + [int(x) for x in msg.encode()] print(script) print([ ':02x'.format(x) for x in [OP_RETURN, OP_PUSHDATA1, len(msg)] + [int(x) for x in msg.encode()] ]) print(['{:02x}'.format(x) for x in [OP_RETURN, OP_PUSHDATA1, len(msg)]] + [binascii.hexlify(msg.encode()).decode()]) print( bitcoin.serialize_script( ['{:02x}'.format(x) for x in [OP_RETURN, OP_PUSHDATA1, len(msg)]] + [binascii.hexlify(msg.encode())]))