Exemple #1
0
    def test_fee_applied(self):
        unspents_original = [Unspent(1000, 0, '', '', 0),
                             Unspent(1000, 0, '', '', 0)]
        outputs_original = [('test', 2000, 'satoshi')]

        with pytest.raises(InsufficientFunds):
            sanitize_tx_data(
                unspents_original, outputs_original, fee=1, leftover=RETURN_ADDRESS,
                combine=True, message=None
            )
Exemple #2
0
    def test_no_combine_insufficient_funds(self):
        unspents_original = [Unspent(1000, 0, '', '', 0),
                             Unspent(1000, 0, '', '', 0)]
        outputs_original = [('test', 2500, 'satoshi')]

        with pytest.raises(InsufficientFunds):
            sanitize_tx_data(
                unspents_original, outputs_original, fee=50, leftover=RETURN_ADDRESS,
                combine=False, message=None
            )
Exemple #3
0
    def test_zero_remaining(self):
        unspents_original = [Unspent(1000, 0, '', '', 0),
                             Unspent(1000, 0, '', '', 0)]
        outputs_original = [('test', 2000, 'satoshi')]

        unspents, outputs = sanitize_tx_data(
            unspents_original, outputs_original, fee=0, leftover=RETURN_ADDRESS,
            combine=True, message=None
        )

        assert unspents == unspents_original
        assert outputs == [('test', 2000)]
Exemple #4
0
    def test_message(self):
        unspents_original = [Unspent(10000, 0, '', '', 0),
                             Unspent(10000, 0, '', '', 0)]
        outputs_original = [('test', 1000, 'satoshi')]

        unspents, outputs = sanitize_tx_data(
            unspents_original, outputs_original, fee=5, leftover=RETURN_ADDRESS,
            combine=True, message='hello'
        )

        assert len(outputs) == 3
        assert outputs[2][0] == b'hello'
        assert outputs[2][1] == 0
Exemple #5
0
    def test_no_combine_remaining(self):
        unspents_original = [Unspent(7000, 0, '', '', 0),
                             Unspent(3000, 0, '', '', 0)]
        outputs_original = [('test', 2000, 'satoshi')]

        unspents, outputs = sanitize_tx_data(
            unspents_original, outputs_original, fee=0, leftover=RETURN_ADDRESS,
            combine=False, message=None
        )

        assert unspents == [Unspent(3000, 0, '', '', 0)]
        assert len(outputs) == 2
        assert outputs[1][0] == RETURN_ADDRESS
        assert outputs[1][1] == 1000
Exemple #6
0
 def test_init(self):
     unspent = Unspent(10000, 7, 'script', 'txid', 0)
     assert unspent.amount == 10000
     assert unspent.confirmations == 7
     assert unspent.script == 'script'
     assert unspent.txid == 'txid'
     assert unspent.txindex == 0
Exemple #7
0
 def get_unspent(cls, address):
     r = requests.get(cls.MAIN_UNSPENT_API.format(address),
                      timeout=DEFAULT_TIMEOUT)
     if r.status_code != 200:  # pragma: no cover
         raise ConnectionError
     return [
         Unspent(currency_to_satoshi(tx['amount'],
                                     'btc'), tx['confirmations'],
                 tx['scriptPubKey'], tx['txid'], tx['vout'])
         for tx in r.json()
     ]
Exemple #8
0
 def get_unspent_testnet(cls, address):
     r = requests.get(cls.TEST_UNSPENT_API.format(address) + '?limit=1000',
                      timeout=DEFAULT_TIMEOUT)
     if r.status_code != 200:  # pragma: no cover
         raise ConnectionError
     return [
         Unspent(currency_to_satoshi(tx['value'],
                                     'btc'), tx['confirmations'],
                 tx['script_pub_key']['hex'], tx['txid'], tx['n'])
         for tx in r.json()['unspent']
     ]
Exemple #9
0
 def _get_unspent(cls, network, address):
     url = "{endpoint}/{address}".format(
         endpoint=cls.MAIN_TRANSACTIONS_UNSPENT, address=address)
     r = requests.get(url, timeout=DEFAULT_TIMEOUT)
     if r.status_code != 200:
         raise ConnectionError
     return [
         Unspent(currency_to_satoshi(tx['value'],
                                     'ltc'), tx['confirmations'],
                 tx['script_hex'], tx['txid'], tx['output_no'])
         for tx in r.json()['data']["txs"]
     ]
Exemple #10
0
    def get_unspent(cls, address):
        r = requests.get(cls.UNSPENT_API + address, timeout=DEFAULT_TIMEOUT)

        if r.status_code == 500:
            return []
        elif r.status_code != 200:  # pragma: no cover
            raise ConnectionError

        return [
            Unspent(tx['value'], tx['confirmations'], tx['script'],
                    tx['tx_hash_big_endian'], tx['tx_output_n'])
            for tx in r.json()['unspent_outputs']
        ][::-1]
Exemple #11
0
    def sign_transaction(self, tx_data):  # pragma: no cover
        """Creates a signed P2PKH transaction using previously prepared
        transaction data.

        :param tx_data: Output of :func:`~bit.PrivateKey.prepare_transaction`.
        :type tx_data: ``str``
        :returns: The signed transaction as hex.
        :rtype: ``str``
        """
        data = json.loads(tx_data)

        unspents = [Unspent.from_dict(unspent) for unspent in data['unspents']]
        outputs = data['outputs']

        return create_p2pkh_transaction(self, unspents, outputs)
Exemple #12
0
 def _get_unspent(cls, network, address, token):
     loop = True
     data = []
     offset = 0
     while loop:
         url = "{endpoint}/{address}?pageSize=50&offset={offset}".format(
             endpoint=cls.MAIN_TRANSACTIONS, address=address, offset=offset)
         r = requests.get(url,
                          timeout=DEFAULT_TIMEOUT,
                          headers={'x-api-key': token})
         if r.status_code == 200:
             if len(r.json()) == 0:
                 loop = False
             else:
                 for i in r.json():
                     data.append(i)
         else:
             raise ConnectionError
         offset = 50
     final = []
     indexes = [0, 1]
     for i in data:
         for j in indexes:
             url = "{endpoint}/{hash}/{index}".format(
                 endpoint=cls.MAIN_TRANSACTIONS_UNSPENT,
                 hash=i['hash'],
                 index=j)
             r = requests.get(url,
                              timeout=DEFAULT_TIMEOUT,
                              headers={'x-api-key': token})
             if r.status_code == 200 and r.json()['address'] == address:
                 final.append(
                     Unspent(
                         currency_to_satoshi(r.json()['value'], 'satoshi'),
                         10,
                         r.json()['script'],
                         r.json()['hash'],
                         r.json()['index']))
     return final
Exemple #13
0
        b'\x8a',
        (b"\x88x9\x9d\x83\xec%\xc6'\xcf\xbfu?\xf9\xca6\x027>"
         b"\xacCz\xb2gaT\xa3\xc2\xda#\xad\xf3"),
        b'\x01\x00\x00\x00'
    )
]
INPUT_BLOCK = ('8878399d83ec25c627cfbf753ff9ca3602373eac437ab2676154a3c2da23adf30'
               '10000008a473044022045b743dbaaaa2cd1ef0b91346f5644e32dc70cde05091b'
               '3762d4cabb6ebd711a022074461056c26efeac0b448e7fa769773dd6e4436cde5'
               '05c8f6ca6303efe31f0950141043d5c2875c9bd116875a71a5db64cffcb13396b'
               '163d039b1d932782489180433476a4352a2add00ebb0d5c94c515b72eb10f1fd8'
               'f3f03b42f4a2b255bfc9aa9e3ffffffff')
UNSPENTS = [
    Unspent(83727960,
            15,
            '76a91492461bde6283b461ece7ddf4dbf1e0a48bd113d888ac',
            'f3ad23dac2a3546167b27a43ac3e370236caf93f75bfcf27c625ec839d397888',
            1)
]
OUTPUTS = [
    ('n2eMqTT929pb1RDNuqEnxdaLau1rxy3efi', 50000),
    ('mtrNwJxS1VyHYn3qBY1Qfsm3K3kh1mGRMS', 83658760)
]
MESSAGES = [
    (b'hello', 0),
    (b'there', 0)
]
OUTPUT_BLOCK = ('50c30000000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac'
                '0888fc04000000001976a91492461bde6283b461ece7ddf4dbf1e0a48bd113d888ac')
OUTPUT_BLOCK_MESSAGES = ('50c30000000000001976a914e7c1345fc8f87c68170b3aa798a956c2fe6a9eff88ac'
                         '0888fc04000000001976a91492461bde6283b461ece7ddf4dbf1e0a48bd113d888ac'
Exemple #14
0
    def test_repr(self):
        unspent = Unspent(10000, 7, 'script', 'txid', 0)

        assert repr(unspent) == ("Unspent(amount=10000, confirmations=7, "
                                 "script='script', txid='txid', txindex=0)")
Exemple #15
0
 def test_equality(self):
     unspent1 = Unspent(10000, 7, 'script', 'txid', 0)
     unspent2 = Unspent(10000, 7, 'script', 'txid', 0)
     unspent3 = Unspent(50000, 7, 'script', 'txid', 0)
     assert unspent1 == unspent2
     assert unspent1 != unspent3
Exemple #16
0
    def test_dict_conversion(self):
        unspent = Unspent(10000, 7, 'script', 'txid', 0)

        assert unspent == Unspent.from_dict(unspent.to_dict())