Esempio n. 1
0
    def output_index_for_address(self, address_or_hash160):
        """ Returns the index of the output in this transaction
        that pays to the provided address.

        Args:
            address_or_hash160 (str or bytes): If a string, a
                Base58Check encoded address. If bytes, the hash160
                of the public key.

        Returns:
            int: The index of the corresponding output or None.
        """
        if isinstance(address_or_hash160, str):
            ver, h160 = address_to_key_hash(address_or_hash160)
        elif isinstance(address_or_hash160, bytes):
            h160 = address_or_hash160
        else:
            raise TypeError("address_or_hash160 can only be bytes or str")

        rv = None
        for i, o in enumerate(self.outputs):
            scr = o.script
            if scr.is_p2pkh() or scr.is_p2sh():
                if scr.get_hash160() == h160:
                    rv = i
                    break

        return rv
Esempio n. 2
0
    def output_index_for_address(self, address_or_hash160):
        """ Returns the index of the output in this transaction
        that pays to the provided address.

        Args:
            address_or_hash160 (str or bytes): If a string, a
                Base58Check encoded address. If bytes, the hash160
                of the public key.

        Returns:
            int: The index of the corresponding output or None.
        """
        if isinstance(address_or_hash160, str):
            ver, h160 = address_to_key_hash(address_or_hash160)
        elif isinstance(address_or_hash160, bytes):
            h160 = address_or_hash160
        else:
            raise TypeError("address_or_hash160 can only be bytes or str")

        rv = None
        for i, o in enumerate(self.outputs):
            scr = o.script
            if scr.is_p2pkh() or scr.is_p2sh():
                if scr.get_hash160() == h160:
                    rv = i
                    break

        return rv
Esempio n. 3
0
    def set_txn_side_effect_for_hd_discovery(self):
        # For each used account, there are at least 2 calls required:
        # 1 for the first DISCOVERY_INCREMENT payout addresses and 1
        # for the first DISCOVERY_INCREMENT change
        # addresses. Depending on the number of used addresses for the
        # account, this will change.

        effects = []

        n = self._num_used_accounts
        if n == 0:
            n = 1

        for acct_num in range(n):
            for change in [0, 1]:
                num_used = self._num_used_addresses[acct_num][change]
                r = math.ceil(
                    (num_used + HDAccount.GAP_LIMIT) / self.address_increment)
                k = 'change_addresses' if change else 'payout_addresses'
                addr_list = self._acct_keys[acct_num][k]

                if change:
                    metadata = dict(
                        block=234790 + r,
                        block_hash=Hash(
                            "000000000000000007d57f03ebe36dbe4f87ab2f340e93b45999ab249b6dc0df"
                        ),
                        confirmations=23890 - r)
                else:
                    metadata = dict(block=None,
                                    block_hash=None,
                                    confirmations=0)

                if r == 0:
                    r = 1
                for i in range(r):
                    start = i * self.address_increment
                    end = (i + 1) * self.address_increment
                    addr_range = range(start, end)

                    out = TransactionOutput(value=10000,
                                            script=Script.build_p2pkh(
                                                address_to_key_hash(
                                                    addr_list[i])[1]))
                    dummy_txn = Transaction(1, [], [out], 0)

                    m = MockTxnDict(num_used=num_used,
                                    addr_range=addr_range,
                                    addr_list=addr_list,
                                    used_value=[
                                        dict(metadata=metadata,
                                             transaction=dummy_txn)
                                    ],
                                    unused_value=[])
                    effects.append(m)

        self.get_transactions.side_effect = effects

        return len(effects)
Esempio n. 4
0
    def set_txn_side_effect_for_hd_discovery(self):
        # For each used account, there are at least 2 calls required:
        # 1 for the first DISCOVERY_INCREMENT payout addresses and 1
        # for the first DISCOVERY_INCREMENT change
        # addresses. Depending on the number of used addresses for the
        # account, this will change.

        effects = []

        n = self._num_used_accounts
        if n == 0:
            n = 1

        for acct_num in range(n):
            for change in [0, 1]:
                num_used = self._num_used_addresses[acct_num][change]
                r = math.ceil((num_used + HDAccount.GAP_LIMIT) /
                              self.address_increment)
                k = 'change_addresses' if change else 'payout_addresses'
                addr_list = self._acct_keys[acct_num][k]

                if change:
                    metadata = dict(block=234790 + r,
                                    block_hash=Hash("000000000000000007d57f03ebe36dbe4f87ab2f340e93b45999ab249b6dc0df"),
                                    confirmations=23890 - r)
                else:
                    metadata = dict(block=None,
                                    block_hash=None,
                                    confirmations=0)

                if r == 0:
                    r = 1
                for i in range(r):
                    start = i * self.address_increment
                    end = (i + 1) * self.address_increment
                    addr_range = range(start, end)

                    out = TransactionOutput(value=10000,
                                            script=Script.build_p2pkh(
                                                address_to_key_hash(
                                                    addr_list[i])[1]))
                    dummy_txn = Transaction(1,
                                            [],
                                            [out],
                                            0)

                    m = MockTxnDict(num_used=num_used,
                                    addr_range=addr_range,
                                    addr_list=addr_list,
                                    used_value=[dict(metadata=metadata,
                                                     transaction=dummy_txn)],
                                    unused_value=[])
                    effects.append(m)

        self.get_transactions.side_effect = effects

        return len(effects)
Esempio n. 5
0
from two1.lib.bitcoin.txn import TransactionOutput
from two1.lib.bitcoin.script import Script
from two1.lib.bitcoin.utils import address_to_key_hash
from two1.lib.bitcoin.utils import bytes_to_str

address = '137KzxStaf6vw5yGujViK3Tkigoix9N3v7'
_, hash160 = address_to_key_hash(address)
out_script = Script.build_p2pkh(hash160)
out1 = TransactionOutput(value=100000, script=out_script)

# Print the script
print("%s" % (out_script))

# Print the address
print("Addresses = %r" % (out1.get_addresses()))

# Print the value
print("Value: %d" % (out1.value))

# Serialize
out1_bytes = bytes(out1)
print(bytes_to_str(out1_bytes))