Пример #1
0
    def broadcast_tx(self, tx):
        """
        Send the tx to half our peers, wait for half of the remainder to announce the tx before
        calling back True.
        """
        def on_peer_anncounce(txid):
            self.pending_txs[txid][0] += 1
            if self.pending_txs[txid][0] >= self.pending_txs[txid][1] / 2:
                if self.pending_txs[txid][3].active():
                    self.pending_txs[txid][3].cancel()
                    self.pending_txs[txid][2].callback(True)

        d = defer.Deferred()
        self.inventory[bitcoin.txhash(tx)] = tx
        inv_packet = inv("TX", bitcoin.txhash(tx))
        self.bloom_filter.insert(bitcoin.bin_txhash(tx))
        self.pending_txs[bitcoin.txhash(tx)] = [
            0,
            len(self.peers) / 2, d,
            reactor.callLater(10, d.callback, False)
        ]
        for peer in self.peers[len(self.peers) / 2:]:
            peer.protocol.update_filter()
            peer.protocol.add_inv_callback(bitcoin.txhash(tx),
                                           on_peer_anncounce)
        for peer in self.peers[:len(self.peers) / 2]:
            peer.protocol.send_message(message(inv_packet, self.params))
        return d
Пример #2
0
def tx_make_input_signature(tx, idx, script, privkey_str, hashcode):
    """
    Sign a single input of a transaction, given the serialized tx,
    the input index, the output's scriptPubkey, and the hashcode.

    privkey_str must be a hex-encoded private key

    TODO: move to virtualchain

    Return the hex signature.
    """
    pk = virtualchain.BitcoinPrivateKey(str(privkey_str))
    pubk = pk.public_key()

    priv = pk.to_hex()
    pub = pubk.to_hex()
    addr = pubk.address()

    signing_tx = bitcoin.signature_form(tx, idx, script, hashcode)
    txhash = bitcoin.bin_txhash(signing_tx, hashcode)

    # sign using uncompressed private key
    pk_uncompressed_hex, pubk_uncompressed_hex = get_uncompressed_private_and_public_keys(
        priv)
    sigb64 = sign_digest(txhash.encode('hex'), priv)

    # sanity check
    assert verify_digest(txhash.encode('hex'), pubk_uncompressed_hex, sigb64)

    sig_r, sig_s = decode_signature(sigb64)
    sig_bin = ecdsa.util.sigencode_der(sig_r, sig_s, ecdsa.SECP256k1.order)
    sig = sig_bin.encode('hex') + bitcoin.encode(hashcode, 16, 2)
    return sig
Пример #3
0
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)
Пример #4
0
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)
Пример #5
0
 def __init__(self, txhex):
     P2PMessageHandler.__init__(self)
     self.txhex = txhex
     self.txid = btc.bin_txhash(self.txhex)[::-1]
     log.debug('broadcasting txid ' + str(self.txid[::-1].encode('hex')) +
         ' on ' + get_network())
     self.relay_txes = True
     self.rejected = False
     self.uploaded_tx = False
Пример #6
0
    def donation_address(tx, wallet):
        from bitcoin.main import multiply, G, deterministic_generate_k, add_pubkeys
        reusable_donation_pubkey = ('02be838257fbfddabaea03afbb9f16e852'
                                    '9dfe2de921260a5c46036d97b5eacf2a')

        privkey = wallet.get_key_from_addr(wallet.get_new_addr(0, 0))
        msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
        # generate unpredictable k
        global sign_k
        sign_k = deterministic_generate_k(msghash, privkey)
        c = btc.sha256(multiply(reusable_donation_pubkey, sign_k))
        sender_pubkey = add_pubkeys(reusable_donation_pubkey, multiply(G, c))
        sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
        log.debug('sending coins to ' + sender_address)
        return privkey, sender_address
Пример #7
0
def tx_make_input_signature(tx, idx, script, privkey_str, hashcode):
    """
    Sign a single input of a transaction, given the serialized tx,
    the input index, the output's scriptPubkey, and the hashcode.

    TODO: move to virtualchain

    Return the hex signature.
    """

    pk = virtualchain.BitcoinPrivateKey(str(privkey_str))
    pubk = pk.public_key()

    priv = pk.to_hex()
    pub = pubk.to_hex()
    addr = pubk.address()

    signing_tx = bitcoin.signature_form(tx, idx, script, hashcode)
    txhash = bitcoin.bin_txhash(signing_tx, hashcode)

    # sign using uncompressed private key
    pk_uncompressed_hex, pubk_uncompressed_hex = get_uncompressed_private_and_public_keys(
        priv)

    sk = ecdsa.SigningKey.from_string(pk_uncompressed_hex.decode('hex'),
                                      curve=ecdsa.SECP256k1)
    sig_bin = sk.sign_digest(txhash, sigencode=ecdsa.util.sigencode_der)

    # enforce low-s
    sig_r, sig_s = ecdsa.util.sigdecode_der(sig_bin, ecdsa.SECP256k1.order)
    if sig_s * 2 >= ecdsa.SECP256k1.order:
        log.debug("High-S to low-S")
        sig_s = ecdsa.SECP256k1.order - sig_s

    sig_bin = ecdsa.util.sigencode_der(sig_r, sig_s, ecdsa.SECP256k1.order)

    # sanity check
    vk = ecdsa.VerifyingKey.from_string(
        pubk_uncompressed_hex[2:].decode('hex'), curve=ecdsa.SECP256k1)
    assert vk.verify_digest(sig_bin,
                            txhash,
                            sigdecode=ecdsa.util.sigdecode_der
                            ), "Failed to verify signature ({}, {})".format(
                                sig_r, sig_s)

    sig = sig_bin.encode('hex') + bitcoin.encode(hashcode, 16, 2)
    return sig
Пример #8
0
    def donation_address(tx, wallet):
	from bitcoin.main import multiply, G, deterministic_generate_k, add_pubkeys
	reusable_donation_pubkey = ('02be838257fbfddabaea03afbb9f16e852'
	                            '9dfe2de921260a5c46036d97b5eacf2a')
    
	privkey = wallet.get_key_from_addr(wallet.get_new_addr(0,0))
	msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
	# generate unpredictable k
	global sign_k
	sign_k = deterministic_generate_k(msghash, privkey)
	c = btc.sha256(multiply(reusable_donation_pubkey, sign_k))
	sender_pubkey = add_pubkeys(
	        reusable_donation_pubkey, multiply(
	                G, c))
	sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
	log.debug('sending coins to ' + sender_address)
	return privkey, sender_address
Пример #9
0
def donation_address(cjtx):
	reusable_donation_pubkey = '02be838257fbfddabaea03afbb9f16e8529dfe2de921260a5c46036d97b5eacf2a'

	donation_utxo_data = cjtx.input_utxos.iteritems().next()
	global donation_utxo
	donation_utxo = donation_utxo_data[0]
	privkey = cjtx.wallet.get_key_from_addr(donation_utxo_data[1]['address'])

	tx = btc.mktx(cjtx.utxo_tx, cjtx.outputs) #tx without our inputs and outputs
	#address = privtoaddr(privkey)
	#signing_tx = signature_form(tx, 0, mk_pubkey_script(address), SIGHASH_ALL)
	msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
	#generate unpredictable k
	global sign_k
	sign_k = btc.deterministic_generate_k(msghash, privkey)
	c = btc.sha256(btc.multiply(reusable_donation_pubkey, sign_k))
	sender_pubkey = btc.add_pubkeys(reusable_donation_pubkey, btc.multiply(btc.G, c))
	sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
	debug('sending coins to ' + sender_address)
	return sender_address
Пример #10
0
def donation_address(cjtx):
    from bitcoin.main import multiply, G, deterministic_generate_k, add_pubkeys
    reusable_donation_pubkey = ('02be838257fbfddabaea03afbb9f16e852'
                                '9dfe2de921260a5c46036d97b5eacf2a')

    donation_utxo_data = cjtx.input_utxos.iteritems().next()
    global donation_utxo
    donation_utxo = donation_utxo_data[0]
    privkey = cjtx.wallet.get_key_from_addr(donation_utxo_data[1]['address'])
    # tx without our inputs and outputs
    tx = btc.mktx(cjtx.utxo_tx, cjtx.outputs)
    msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
    # generate unpredictable k
    global sign_k
    sign_k = deterministic_generate_k(msghash, privkey)
    c = btc.sha256(multiply(reusable_donation_pubkey, sign_k))
    sender_pubkey = add_pubkeys(reusable_donation_pubkey, multiply(G, c))
    sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
    log.debug('sending coins to ' + sender_address)
    return sender_address
Пример #11
0
def donation_address(cjtx):
    reusable_donation_pubkey = '02be838257fbfddabaea03afbb9f16e8529dfe2de921260a5c46036d97b5eacf2a'

    donation_utxo_data = cjtx.input_utxos.iteritems().next()
    global donation_utxo
    donation_utxo = donation_utxo_data[0]
    privkey = cjtx.wallet.get_key_from_addr(donation_utxo_data[1]['address'])

    tx = btc.mktx(cjtx.utxo_tx,
                  cjtx.outputs)  #tx without our inputs and outputs
    #address = privtoaddr(privkey)
    #signing_tx = signature_form(tx, 0, mk_pubkey_script(address), SIGHASH_ALL)
    msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
    #generate unpredictable k
    global sign_k
    sign_k = btc.deterministic_generate_k(msghash, privkey)
    c = btc.sha256(btc.multiply(reusable_donation_pubkey, sign_k))
    sender_pubkey = btc.add_pubkeys(reusable_donation_pubkey,
                                    btc.multiply(btc.G, c))
    sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
    debug('sending coins to ' + sender_address)
    return sender_address
Пример #12
0
def donation_address(cjtx):
    from bitcoin.main import multiply, G, deterministic_generate_k, add_pubkeys
    reusable_donation_pubkey = ('02be838257fbfddabaea03afbb9f16e852'
                                '9dfe2de921260a5c46036d97b5eacf2a')

    donation_utxo_data = cjtx.input_utxos.iteritems().next()
    global donation_utxo
    donation_utxo = donation_utxo_data[0]
    privkey = cjtx.wallet.get_key_from_addr(donation_utxo_data[1]['address'])
    # tx without our inputs and outputs
    tx = btc.mktx(cjtx.utxo_tx, cjtx.outputs)
    msghash = btc.bin_txhash(tx, btc.SIGHASH_ALL)
    # generate unpredictable k
    global sign_k
    sign_k = deterministic_generate_k(msghash, privkey)
    c = btc.sha256(multiply(reusable_donation_pubkey, sign_k))
    sender_pubkey = add_pubkeys(
            reusable_donation_pubkey, multiply(
                    G, c))
    sender_address = btc.pubtoaddr(sender_pubkey, get_p2pk_vbyte())
    log.debug('sending coins to ' + sender_address)
    return sender_address
Пример #13
0
    def broadcast_tx(self, tx):
        """
        Send the tx to half our peers, wait for half of the remainder to announce the tx before
        calling back True.
        """
        def on_peer_anncounce(txid):
            self.pending_txs[txid][0] += 1
            if self.pending_txs[txid][0] >= self.pending_txs[txid][1] / 2:
                if self.pending_txs[txid][3].active():
                    self.pending_txs[txid][3].cancel()
                    self.pending_txs[txid][2].callback(True)

        d = defer.Deferred()
        self.inventory[bitcoin.txhash(tx)] = tx
        inv_packet = inv("TX", bitcoin.txhash(tx))
        self.bloom_filter.insert(bitcoin.bin_txhash(tx))
        self.pending_txs[bitcoin.txhash(tx)] = [0, len(self.peers)/2, d, reactor.callLater(10, d.callback, False)]
        for peer in self.peers[len(self.peers)/2:]:
            peer.protocol.update_filter()
            peer.protocol.add_inv_callback(bitcoin.txhash(tx), on_peer_anncounce)
        for peer in self.peers[:len(self.peers)/2]:
            peer.protocol.send_message(message(inv_packet, self.params))
        return d