Exemple #1
0
 def test_sign_p2sh(self):
     tx_out_script = h2b(
         "76a91491b24bf9f5288532960ac687abb035127b1d28a588ac")
     script = script_for_address("1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm")
     self.assertEqual(tx_out_script, 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])
     hl = build_hash160_lookup([1], [secp256k1_generator])
     self.assertEqual(tx.bad_signature_count(), 1)
     tx.sign(hash160_lookup=hl)
     self.assertEqual(tx.bad_signature_count(), 0)
Exemple #2
0
 def test_solve_pay_to_public_pair(self):
     for se in range(1, 10):
         key = Key(secret_exponent=se, generator=secp256k1_generator)
         for b in [True, False]:
             addr = key.address(use_uncompressed=b)
             script = script_for_p2pk(key.sec(use_uncompressed=b))
             afs_address = address_for_script(script)
             self.assertEqual(afs_address, addr)
             hl = build_hash160_lookup([se], [secp256k1_generator])
             tx = Tx(1, [], [Tx.TxOut(100, script)])
             tx.sign(hash160_lookup=hl)
             afs_address = address_for_script(tx.txs_out[0].puzzle_script())
             self.assertEqual(afs_address, addr)
Exemple #3
0
    def test_p2sh_multisig_sequential_signing(self):
        raw_scripts = [
            h2b("52210234abcffd2e80ad01c2ec0276ad02682808169c6fafdd25ebfb60703df272b461"
                "2102e5baaafff8094e4d77ce8b009d5ebc3de9110085ebd3d96e50cc7ce70faf175221"
                "0316ee25e80eb6e6fc734d9c86fa580cbb9c4bfd94a19f0373a22353ececd4db6853ae"
                )
        ]
        spendable = {
            'script_hex':
            'a914c4ed4de526461e3efbb79c8b688a6f9282c0464687',
            'does_seem_spent':
            0,
            'block_index_spent':
            0,
            'coin_value':
            10000,
            'block_index_available':
            0,
            'tx_out_index':
            0,
            'tx_hash_hex':
            '0ca152ba6b88db87a7ef1afd24554102aca1ab86cf2c10ccbc374472145dc943'
        }

        key_1 = 'Kz6pytJCigYHeMsGLmfHQPJhN5og2wpeSVrU43xWwgHLCAvpsprh'
        key_2 = 'Kz7NHgX7MBySA3RSKj9GexUSN6NepEDoPNugSPr5absRDoKgn2dT'
        for ordered_keys in [(key_1, key_2), (key_2, key_1)]:
            txs_in = [
                TxIn(previous_hash=h2b(
                    '43c95d14724437bccc102ccf86aba1ac02415524fd1aefa787db886bba52a10c'
                ),
                     previous_index=0)
            ]
            txs_out = [
                TxOut(10000,
                      script_for_address('3KeGeLFmsbmbVdeMLrWp7WYKcA3tdsB4AR'))
            ]
            unspents = [Spendable.from_dict(spendable)]
            tx = Tx(version=DEFAULT_VERSION,
                    txs_in=txs_in,
                    txs_out=txs_out,
                    unspents=unspents)
            for key in ordered_keys:
                self.assertEqual(tx.bad_signature_count(), 1)
                p2sh_lookup = build_p2sh_lookup(raw_scripts)
                tx.sign(LazySecretExponentDB([key], {}, [secp256k1_generator]),
                        p2sh_lookup=p2sh_lookup)
            self.assertEqual(tx.bad_signature_count(), 0)
Exemple #4
0
def spend_pkh_fund(tx_ins, in_keys, tx_outs):
    """
    p2pkh address send to p2pkh p2sh transaction
    :param tx_ins: list with tuple(tx_id, idx, balance, address)
    :param in_keys: list of private keys in hex format corresponding to each input
    :param tx_outs: balance, receiver_address
    :return: raw hex and tx id
    """
    _txs_in = []
    _un_spent = []
    for tx_id, idx, balance, address in tx_ins:
        # must h2b_rev NOT h2b
        tx_id_b = h2b_rev(tx_id)
        _txs_in.append(TxIn(tx_id_b, idx))

        _un_spent.append(
            Spendable(
                balance,
                script_obj_from_address(address, netcodes=[NET_CODE]).script(),
                tx_id_b, idx))

    _txs_out = []
    for balance, receiver_address in tx_outs:
        _txs_out.append(
            TxOut(
                balance,
                script_obj_from_address(receiver_address,
                                        netcodes=[NET_CODE]).script()))

    version, lock_time = 1, 0
    tx = Tx(version, _txs_in, _txs_out, lock_time)
    tx.set_unspents(_un_spent)

    solver = build_hash160_lookup([int(pri_hex, 16) for pri_hex in in_keys])
    tx.sign(solver, hash_type=SIGHASH_ALL)

    return tx.as_hex(), tx.id()
def tether_tx(tx_ins, private_key, send_amount, receiver):
    """
    simple usdt transaction
    here assume utxo comes from the sender and is used for mine fee
    bitcoin change will be sent back to sender address

    of course different address's utxo can be used for mine fee,
    but should be aware sender is determined by the first input in the tx
    bitcoin change can also be sent back to different address,
    but should be aware receiver is indicated by the last output address that is not the sender

    for full customization, use btc sample p2pkh_tx
    :param tx_ins: utxo from the sender
    :param private_key: private key of the same sender
    :param send_amount: (display amount) * (10 ** 8)
    :param receiver: address to receive usdt
    """
    _txs_in = []
    _un_spent = []
    total_bal = 0

    for tx_id, idx, balance, address in tx_ins:
        total_bal += balance

        # must h2b_rev NOT h2b
        tx_id_b = h2b_rev(tx_id)
        _txs_in.append(TxIn(tx_id_b, idx))

        _un_spent.append(Spendable(balance, standard_tx_out_script(address), tx_id_b, idx))

    txn_fee = estimate_p2pkh_tx_bytes(len(tx_ins), 3) * recommend_satoshi_per_byte()

    _txs_out = [TxOut(total_bal - txn_fee - 546, standard_tx_out_script(tx_ins[0][3])),
                TxOut(0, binascii.unhexlify(omni_tether_script(send_amount))),
                TxOut(546, standard_tx_out_script(receiver))]

    version, lock_time = 1, 0
    tx = Tx(version, _txs_in, _txs_out, lock_time)
    tx.set_unspents(_un_spent)

    solver = build_hash160_lookup([int(private_key, 16)] * len(tx_ins))
    signed_tx = tx.sign(solver, hash_type=SIGHASH_ALL)

    return signed_tx.as_hex(), signed_tx.id()