Beispiel #1
0
def reconstruct_composed_tx_spec(model, tx):
    if isinstance(tx, str):
        tx = deserialize(tx)
    if not isinstance(tx, Tx):
        raise Exception('Tx is neiether string nor pycoin.tx.Tx!')

    pycoin_tx = tx

    composed_tx_spec = txspec.ComposedTxSpec(None)

    for py_txin in pycoin_tx.txs_in:
        # lookup the previous hash and generate the utxo
        in_txhash, in_outindex = py_txin.previous_hash, py_txin.previous_index
        in_txhash = in_txhash[::-1].encode('hex')

        composed_tx_spec.add_txin(
            txspec.ComposedTxSpec.TxIn(in_txhash, in_outindex))
        # in_tx = ccc.blockchain_state.get_tx(in_txhash)
        # value = in_tx.outputs[in_outindex].value
        # raw_address = script_to_raw_address(py_txin.script)
        # address = ccc.raw_to_address(raw_address)

    for py_txout in pycoin_tx.txs_out:
        script = py_txout.script
        raw_address = script_to_raw_address(script)
        if raw_address:
            address = model.ccc.raw_to_address(raw_address)
        else:
            address = None
        composed_tx_spec.add_txout(
            txspec.ComposedTxSpec.TxOut(py_txout.coin_value, address))

    return composed_tx_spec
Beispiel #2
0
def fake_transaction(model=MockModel()):
    key = "5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF"

    address = LooseAddressRecord(address_data=key)
    script = tools.compile(
        "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format(
            address.rawPubkey().encode("hex"))).encode("hex")

    txin = utxodb.UTXO("D34DB33F", 0, 1, script)
    txin.address_rec = address
    txout = txspec.ComposedTxSpec.TxOut(1, address.get_address())
    composed = txspec.ComposedTxSpec([txin], [txout])
    return txcons.RawTxSpec.from_composed_tx_spec(model, composed), address
Beispiel #3
0
def compose_uncolored_tx_spec(tx_spec):
    targets = tx_spec.get_targets()
    ttotal = sum([target[2] for target in targets])
    fee = tx_spec.get_required_fee(500)
    sel_utxos, sum_sel_coins = tx_spec.select_coins(0, ttotal + fee)
    txins = [txspec.ComposedTxSpec.TxIn(utxo) for utxo in sel_utxos]
    change = sum_sel_coins - ttotal - fee
    txouts = [
        txspec.ComposedTxSpec.TxOut(target[2], target[0]) for target in targets
    ]
    if change > 0:
        outputs.append(
            txspec.ComposedTxSpec.TxOut(change, tx_spec.get_change_address(0)))
    return txspec.ComposedTxSpec(txins, txouts)
Beispiel #4
0
def compose_uncolored_tx(tx_spec):
    """ compose a simple bitcoin transaction """
    targets = tx_spec.get_targets()
    ttotal = sum([target[2] for target in targets])
    fee = tx_spec.get_required_fee(500)
    sel_utxos, sum_sel_coins = tx_spec.select_coins(colordef.UNCOLORED_MARKER,
                                                    ttotal + fee)
    change = sum_sel_coins - ttotal - fee
    txouts = [txspec.ComposedTxSpec.TxOut(target[2], target[0])
              for target in targets]
    # give ourselves the change
    if change > 0:
        txouts.append(
            txspec.ComposedTxSpec.TxOut(
                change, tx_spec.get_change_addr(colordef.UNCOLORED_MARKER)))
    return txspec.ComposedTxSpec(sel_utxos, txouts)
Beispiel #5
0
def fake_transaction(model=None):
    key = ecdsa.SigningKey.from_string(
        "\xe8\x00\xb8\xd4\xa1b\xb7o\x0f;\xf2\xcf\xca\xfd\x1a$\xb9\xa9"
        "\xeb\x0b\x08X\x9f}9C\xe4\x88\xfdD\x11b",
        curve=ecdsa.curves.SECP256k1)

    address = Address.fromPrivkey(key)
    script = tools.compile(
        "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format(
            address.rawPubkey()[1:-4].encode("hex"))).encode("hex")
    utxo = utxodb.UTXO("D34DB33F", 0, 1, script)
    utxo.address_rec = object()
    utxo.address_rec = AddressRec(address)
    txin = txspec.ComposedTxSpec.TxIn(utxo)
    txout = txspec.ComposedTxSpec.TxOut(1, address.pubkey)
    composed = txspec.ComposedTxSpec([txin], [txout])
    return txcons.SignedTxSpec(model, composed, False), address