Skip to content

Commit

Permalink
added get transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
F483 committed May 24, 2016
1 parent fb7d18a commit cc2978f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
4 changes: 4 additions & 0 deletions btctxstore/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ def confirms(self, txid):
txid = deserialize.txid(txid)
return self.service.confirms(txid)

def get_transactions(self, address):
address = deserialize.address(self.testnet, address)
return self.service.transactions_for_address(address)

def split_utxos(self, wif, limit, fee=10000, max_outputs=100):
"""Split utxos of <wif> unitil <limit> or <max_outputs> reached."""
key = deserialize.key(self.testnet, wif)
Expand Down
13 changes: 7 additions & 6 deletions btctxstore/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from . import serialize
from . import exceptions
from . import common
from pycoin.tx.script import tools


SIZE_PREFIX_BYTES = 2
Expand Down Expand Up @@ -190,18 +191,18 @@ def add_hash160data_output(tx, hash160data_txout):

def get_hash160_data(tx, output_index):
out = tx.txs_out[output_index]
return pycoin.serialize.h2b(
pycoin.tx.script.tools.disassemble(out.script)[18:58]
)
opcode, data, pc = tools.get_opcode(out.script, 0)
opcode, data, pc = tools.get_opcode(out.script, pc)
opcode, data, pc = tools.get_opcode(out.script, pc)
return data


def get_nulldata(tx):
index, out = _get_nulldata_output(tx)
if not out:
raise exceptions.NoNulldataOutput(tx)
data = pycoin.serialize.h2b(
pycoin.tx.script.tools.disassemble(out.script)[10:]
)
opcode, data, pc = tools.get_opcode(out.script, 0)
opcode, data, pc = tools.get_opcode(out.script, pc)
return index, data


Expand Down
14 changes: 14 additions & 0 deletions btctxstore/services/automatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ def spendables_for_address(self, bitcoin_address):
_log.error(msg.format(name, repr(e)))
other_service = self._select_other_service(service)
return other_service.spendables_for_address(bitcoin_address)

def transactions_for_address(self, bitcoin_address):
service = self._select_service()
try:
return service.transactions_for_address(bitcoin_address)
except Exception as e:
# try only once with another service
# if two independant services fail something is wrong
# there are also only two working services right now ...
name = service.__class__.__name__
msg = "Service call to {0} failed: {1}"
_log.error(msg.format(name, repr(e)))
other_service = self._select_other_service(service)
return other_service.transactions_for_address(bitcoin_address)
8 changes: 8 additions & 0 deletions btctxstore/services/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def spendables_for_address(self, bitcoin_address):
spendables.append(spendable)
return spendables

def transactions_for_address(self, bitcoin_address):
url = "{0}/txs/?address={1}".format(self.base_url, bitcoin_address)
result = json.loads(urlopen(url).read().decode("utf8"))
transactions = []
for tx in result["txs"]:
transactions.append(tx["txid"])
return transactions

def send_tx(self, tx):
if self.dryrun:
return
Expand Down
4 changes: 4 additions & 0 deletions btctxstore/services/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def spendables_for_address(self, bitcoin_address):
"""TODO doc string"""
raise NotImplementedError()

def transactions_for_address(self, bitcoin_address):
"""TODO doc string"""
raise NotImplementedError()

def confirms(self, txid):
"""TODO doc string"""
raise NotImplementedError()
Expand Down
1 change: 1 addition & 0 deletions tests/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import confirms # NOQA
from . import create_wallet # NOQA
from . import get_address # NOQA
from . import get_transactions # NOQA
from . import get_key # NOQA
from . import validate_wallet # NOQA
from . import verify_signature # NOQA
Expand Down
33 changes: 33 additions & 0 deletions tests/api/get_transactions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) 2015 Fabian Barkhau <[email protected]>
# License: MIT (see LICENSE file)


from __future__ import print_function
from __future__ import unicode_literals
import unittest
from btctxstore import BtcTxStore


class TestGetTransactions(unittest.TestCase):

def setUp(self):
self.api = BtcTxStore(dryrun=True, testnet=True)

def test_used(self):
address = "2MwYLBKhvqezurTWsyzFRbA3BG2fYLmTTDK"
transactions = self.api.get_transactions(address)
self.assertEqual(transactions, [
"5599b00529a82dd5957be87681fb9da07ef54665ca8621f636a2a6ccd949a8c6",
"64754ea8ab2b42cedb95a1548d83e166b03c350fe21044be411b07a36ec66ebd"
])

def test_unused(self):
address = "mwacg9L7raork91AP6wiZpA291bATguX47"
transactions = self.api.get_transactions(address)
self.assertEqual(transactions, [])


if __name__ == '__main__':
unittest.main()

0 comments on commit cc2978f

Please sign in to comment.