def setUp(self): self.testnet_api = BtcTxStore(dryrun=True, testnet=True) self.mainnet_api = BtcTxStore(dryrun=True, testnet=False) self.testnet_wallet = self.testnet_api.create_wallet() self.mainnet_wallet = self.mainnet_api.create_wallet() self.testnet_key = self.testnet_api.get_key(self.testnet_wallet) self.mainnet_key = self.mainnet_api.get_key(self.mainnet_wallet)
def _setup_data_transfer_client(self, store_config, passive_port, passive_bind, node_type, nat_type, wan_ip): # Setup handlers for callbacks registered via the API. handlers = { "complete": self._transfer_complete_handlers, "accept": self._transfer_request_handlers } wallet = BtcTxStore(testnet=False, dryrun=True) wif = self.get_key() node_id = address_to_node_id(wallet.get_address(wif)) #dht_node = SimDHT(node_id=node_id) dht_node = self self._data_transfer = FileTransfer( net=Net( net_type="direct", node_type=node_type, nat_type=nat_type, dht_node=dht_node, debug=1, passive_port=passive_port, passive_bind=passive_bind, wan_ip=wan_ip ), wif=wif, store_config=store_config, handlers=handlers ) # Setup success callback values. self._data_transfer.success_value = (self.sync_get_wan_ip(), self.port) self.process_data_transfers()
def test_together_sender_key_and_btctx_api(self): """ Test of possibility to provide the ``sender_key`` and ``btctx_api`` only together. """ btctx_api = BtcTxStore(testnet=True, dryrun=True) sender_key = btctx_api.create_key() self.mock_get.return_value = Response() # test only "sender_key" given self.assertRaises( TypeError, core.download, *(self.test_url_address, self.file_hash), **{'sender_key': sender_key} ) # test only "btctx_api" given self.assertRaises( TypeError, core.download, *(self.test_url_address, self.file_hash), **{'btctx_api': btctx_api} ) # test of now exception when both args are given download_call_result = core.download( self.test_url_address, self.file_hash, sender_key=sender_key, btctx_api=btctx_api ) self.assertIsInstance(download_call_result, Response, 'Must return a response object')
def test_core_audit(self): """ Test of providing correct arguments to the ``requests.post()`` and returning gotten response object. """ test_url_address = 'http://test.url.com' file_hash = sha256(b'some test data').hexdigest() seed = sha256(b'some test challenge seed').hexdigest() btctx_api = BtcTxStore(testnet=True, dryrun=True) sender_key = btctx_api.create_key() audit_call_result = core.audit(test_url_address, sender_key, btctx_api, file_hash, seed) expected_calls = [call( urljoin(test_url_address, '/api/audit/'), data={ 'data_hash': file_hash, 'challenge_seed': seed, }, headers={ 'sender-address': btctx_api.get_address(sender_key), 'signature': btctx_api.sign_unicode(sender_key, file_hash), } )] self.assertListEqual( self.mock_post.call_args_list, expected_calls, 'In the audit() function requests.post() calls are unexpected' ) self.assertIs( self.mock_post.return_value, audit_call_result, 'Returned value must be the object returned by the ' '``requests.post()``' )
def setUp(self): app.config["SKIP_AUTHENTICATION"] = False # monkey patch self.app = app.test_client() self.btctxstore = BtcTxStore() db.create_all()
def test_authenticate_headers_provide(self): """ Test of preparing and providing credential headers when ``sender_key`` and ``btctx_api`` are provided. """ btctx_api = BtcTxStore(testnet=True, dryrun=True) sender_key = btctx_api.create_key() signature = btctx_api.sign_unicode(sender_key, self.file_hash) sender_address = btctx_api.get_address(sender_key) self.mock_get.return_value = Response() self.test_data_for_requests['headers'] = { 'sender-address': sender_address, 'signature': signature, } download_call_result = core.download( self.test_url_address, self.file_hash, sender_key=sender_key, btctx_api=btctx_api ) expected_mock_calls = [call( urljoin(self.test_url_address, '/api/files/' + self.file_hash), **self.test_data_for_requests )] self.assertListEqual( self.mock_get.call_args_list, expected_mock_calls, 'In the download() function requests.get() calls are unexpected' ) self.assertIsInstance(download_call_result, Response, 'Must return a response object')
class TestAddInputs(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) # TODO test signing def test_add_inputs_withchange(self): txouts = fixtures["add_inputs"]["withchange"]["txouts"] wifs = fixtures["add_inputs"]["withchange"]["wifs"] change_address = fixtures["add_inputs"]["withchange"]["change_address"] expected = fixtures["add_inputs"]["withchange"]["expected"] rawtx = self.api.create_tx(txouts=txouts) result = self.api.add_inputs(rawtx, wifs, change_address, dont_sign=True) self.assertEqual(expected, result) def test_add_inputs_nochange(self): txouts = fixtures["add_inputs"]["nochange"]["txouts"] wifs = fixtures["add_inputs"]["nochange"]["wifs"] expected = fixtures["add_inputs"]["nochange"]["expected"] rawtx = self.api.create_tx(txouts=txouts) result = self.api.add_inputs(rawtx, wifs, dont_sign=True) self.assertEqual(expected, result)
def setUp(self): # Create a temporary that will be used as uploaded file. self.testing_dir = os.path.dirname(os.path.abspath(__file__)) self.test_source_file = tempfile.NamedTemporaryFile( prefix='tmp_', suffix='.spam', mode="w+", dir=self.testing_dir, ) self.test_source_file.write('some file content') self.test_source_file.flush() # Mock the ``requests`` package. self.post_patch = patch('requests.post') self.mock_post = self.post_patch.start() self.mock_post.return_value = Response() # Prepare common arguments for the API's ``upload()`` function call. btctx_api = BtcTxStore(testnet=True, dryrun=True) self.upload_param = dict( url_base='http://test.url.com', btctx_api=btctx_api, sender_key=btctx_api.create_key(), file_role='101', )
def test_fail(self): # register without auth headres fails rv = self.app.get('/api/register/{0}'.format(addresses["eta"])) self.assertEqual(rv.status_code, 401) # register first because ping is lazy blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) message = app.config["ADDRESS"] + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": header_date, "Authorization": header_authorization} url = '/api/register/{0}'.format(address) rv = self.app.get(url, headers=headers) self.assertEqual(rv.status_code, 200) # ping without auth headres fails time.sleep(app.config["MAX_PING"]) rv = self.app.get('/api/ping/{0}'.format(address)) self.assertEqual(rv.status_code, 401) # set height without auth headres fails rv = self.app.get('/api/height/{0}/10'.format(addresses["eta"])) self.assertEqual(rv.status_code, 401)
def __init__(self, url=common.DEFAULT_URL, debug=False, max_size=common.DEFAULT_MAX_SIZE, store_path=common.DEFAULT_STORE_PATH, config_path=common.DEFAULT_CONFIG_PATH, set_master_secret=None, set_payout_address=None, connection_retry_limit=common.DEFAULT_CONNECTION_RETRY_LIMIT, connection_retry_delay=common.DEFAULT_CONNECTION_RETRY_DELAY): # FIXME validate master_secret self.url = url self.config = None # lazy self.messanger = None # lazy self.debug = debug # TODO validate self.retry_limit = deserialize.positive_integer(connection_retry_limit) self.retry_delay = deserialize.positive_integer(connection_retry_delay) self.max_size = deserialize.byte_count(max_size) # paths self.config_path = os.path.realpath(config_path) # TODO validate self._ensure_path_exists(os.path.dirname(self.config_path)) self.store_path = os.path.realpath(store_path) self._ensure_path_exists(self.store_path) # validate payout address self.btctxstore = BtcTxStore() if set_payout_address and (not self.btctxstore.validate_address(set_payout_address)): raise exceptions.InvalidAddress(set_payout_address) self._initialize_config(set_master_secret, set_payout_address)
def __init__(self, url=common.DEFAULT_URL, debug=False, quiet=False, use_folder_tree=False, max_size=common.DEFAULT_MAX_SIZE, store_path=common.DEFAULT_STORE_PATH, config_path=common.DEFAULT_CONFIG_PATH, connection_retry_limit=common.DEFAULT_CONNECTION_RETRY_LIMIT, connection_retry_delay=common.DEFAULT_CONNECTION_RETRY_DELAY): debug = deserialize.flag(debug) quiet = deserialize.flag(quiet) self.url = deserialize.url(url) self.use_folder_tree = deserialize.flag(use_folder_tree) self.max_size = deserialize.byte_count(max_size) self.messenger = None # lazy self.btctxstore = BtcTxStore() self.retry_limit = deserialize.positive_integer(connection_retry_limit) self.retry_delay = deserialize.positive_integer(connection_retry_delay) # paths self.cfg_path = os.path.realpath(config_path) control.util.ensure_path_exists(os.path.dirname(self.cfg_path)) self.store_path = os.path.realpath(store_path) control.util.ensure_path_exists(self.store_path) # check for vfat partions fstype = control.util.get_fs_type(self.store_path) if fstype == "vfat": logger.info("Detected vfat partition, using folder tree.") self.use_folder_tree = True if fstype is None: msg = "Couldn't detected partition type for '{0}'" logger.warning(msg.format(self.store_path)) self.cfg = control.config.get(self.btctxstore, self.cfg_path)
def generate_block(self): """Close the current block, and generate a new one.""" try: last_block = latest_block(self.conn) last_hash = latest_hash(self.conn) # Get Merkle Root self.find_leaves() self.close() merkle_root = self.merkle_root() # Get TXID blockchain = BtcTxStore() hexdata = merkle_root privatekeys = app.config["PRIVATE_KEYS"] changeaddress = app.config["CHANGE_ADDRESS"] tx_id = blockchain.store(hexdata, privatekeys, changeaddress) # Close current block c = self.conn.cursor() query1 = "UPDATE block_table SET end_hash=?, closed=?, merkle_root=?, tx_id=? WHERE id=?" c.execute(query1, (last_hash, True, merkle_root, tx_id, last_block)) # Start new block query2 = "INSERT INTO block_table (start_hash) VALUES (?)" c.execute(query2, (last_hash,)) self.conn.commit() self.conn.close() return 'Block {0} Built.'.format(last_block) except LookupError: return 'Block Empty.'
def args_prepare(required_args, parsed_args): """ Filling all missed, but required by the core API function arguments. Return dictionary that will be passed to the API function. :param required_args: list of required argument's names for the API function :type required_args: list of stings :param parsed_args: can be any object with appropriate names of attributes required by the core API function :type parsed_args: argparse.Namespace :returns: dictionary that will be used like the ``**kwargs`` argument :rtype: dictionary """ prepared_args = {} if 'sender_key' in required_args and 'btctx_api' in required_args: btctx_api = BtcTxStore(testnet=True, dryrun=True) args_base = dict( sender_key=btctx_api.create_key(), btctx_api=btctx_api, ) for required_arg in required_args: try: prepared_args[required_arg] = getattr(parsed_args, required_arg) except AttributeError: prepared_args[required_arg] = args_base[required_arg] return prepared_args
def setUp(self): app.config["SKIP_AUTHENTICATION"] = True # monkey patch app.config["DISABLE_CACHING"] = True self.btctxstore = BtcTxStore() db.create_all()
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) header_authorization = blockchain.sign_unicode(wif, "lalala-wrong") farmer.authenticate(header_authorization, header_date)
def test_authentication_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) self.assertTrue(farmer.authenticate(header_authorization, header_date))
def __init__(self, net, wif=None, store_config=None, handlers=None): # Accept direct connections. self.net = net # Returned by callbacks. self.success_value = ("127.0.0.1", 7777) # Used for signing messages. self.wallet = BtcTxStore(testnet=False, dryrun=True) self.wif = wif or self.wallet.create_key() # Where will the data be stored? self.store_config = store_config assert(len(list(store_config))) # Handlers for certain events. self.handlers = handlers if self.handlers is None: self.handlers = {} if "complete" not in self.handlers: self.handlers["complete"] = [] if "accept" not in self.handlers: self.handlers["accept"] = [] # Start networking. if not self.net.is_net_started: self.net.start() # Dict of data requests: [contract_id] > contract self.contracts = {} # List of Sock objects returned from UNL.connect. self.cons = [] # Dict of defers for contracts: [contract_id] > defer self.defers = {} # Three-way handshake status for contracts: [contract_id] > state self.handshake = {} # All contracts associated with this connection. # [con] > [contract_id] > con_info self.con_info = {} # File transfer currently active on connection. # [con] > contract_id self.con_transfer = {} # List of active downloads. # (Never try to download multiple copies of the same thing at once.) self.downloading = {} # Lock threads. self.mutex = Lock()
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime(datetime.now().timetuple()) , localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
def __init__(self, url=common.DEFAULT_URL, debug=False, quiet=False, use_folder_tree=False, max_size=common.DEFAULT_MAX_SIZE, min_free_size=common.DEFAULT_MIN_FREE_SIZE, store_path=common.DEFAULT_STORE_PATH, config_path=common.DEFAULT_CONFIG_PATH, connection_retry_limit=common.DEFAULT_CONNECTION_RETRY_LIMIT, connection_retry_delay=common.DEFAULT_CONNECTION_RETRY_DELAY, nop2p=True): debug = deserialize.flag(debug) quiet = deserialize.flag(quiet) self.storjnode = None self.nop2p = nop2p self.url = deserialize.url(url) self.use_folder_tree = deserialize.flag(use_folder_tree) self.max_size = deserialize.byte_count(max_size) self.min_free_size = deserialize.byte_count(min_free_size) self.messenger = None # lazy self.btctxstore = BtcTxStore() self.retry_limit = deserialize.positive_integer(connection_retry_limit) self.retry_delay = deserialize.positive_integer(connection_retry_delay) # paths self.cfg_path = os.path.realpath(config_path) storjnode.util.ensure_path_exists(os.path.dirname(self.cfg_path)) self.store_path = os.path.realpath(store_path) storjnode.util.ensure_path_exists(self.store_path) # check for vfat partions try: fstype = storjnode.util.get_fs_type(self.store_path) # FileNotFoundError: [Errno 2] No such file or directory: '/etc/mtab' # psutil: https://code.google.com/p/psutil/issues/detail?id=434 except EnvironmentError as e: logger.warning(e) fstype = None if fstype == "vfat": logger.info("Detected vfat partition, using folder tree.") self.use_folder_tree = True if fstype is None: msg = "Couldn't detected partition type for '{0}'" logger.warning(msg.format(self.store_path)) self.cfg = storjnode.config.get(self.btctxstore, self.cfg_path)
class TestSignTx(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_sign_tx(self): txins = fixtures["sign_tx"]["txins"] txouts = fixtures["sign_tx"]["txouts"] wifs = fixtures["sign_tx"]["wifs"] expected = fixtures["sign_tx"]["expected"] rawtx = self.api.create_tx(txins, txouts) rawtx = self.api.add_nulldata(rawtx, "f483") result = self.api.sign_tx(rawtx, wifs) self.assertEqual(result, expected)
class TestConfirms(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=False) def test_confirmed(self): confirms = self.api.confirms(CONFIRMED) self.assertGreater(confirms, 0) def test_unconfirmed(self): pass # TODO find always unconfirmed tx and test def test_unpublished(self): confirms = self.api.confirms(UNPUBLISHED) self.assertIsNone(confirms)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() date = datetime.now() - timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) farmer.authenticate(header_authorization, header_date)
def callback(): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) header_date = formatdate(timeval=mktime( datetime.now().timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": None, "Authorization": header_authorization} farmer.authenticate(headers)
class TestSignTx(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_signtx(self): txins = fixtures["signtx"]["txins"] txouts = fixtures["signtx"]["txouts"] wifs = fixtures["signtx"]["wifs"] expected = fixtures["signtx"]["expected"] rawtx = self.api.createtx(txins, txouts) rawtx = self.api.addnulldata(rawtx, "f483") result = self.api.signtx(rawtx, wifs) self.assertEqual(result, expected)
class TestSplitUtxos(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_singleinput(self): wif = "cNHPbjVpkv4oqqKimBNp1UfQ2dhjETtRZw4KkHWtPgnU36SBtXub" # address n4RHA7mxH8EYV7wMS8evtYRYwCpQYz6KuE txids = self.api.split_utxos(wif, 10000000) # 100mBTC self.assertEqual(len(txids), 1) def test_manyinputs(self): wif = "cRoboMG5KM19VP8ZcVCDXGCfi1JJraKpw58ofe8v57j7vqDxaQ5m" # address mqox6abLAiado9kFvX3EsHaVFbYVimSMCK txids = self.api.split_utxos(wif, 100000) # 1mBTC self.assertEqual(len(txids), 6)
class TestRetrieve(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_retrieve(self): txid = fixtures["retrieve"]["nulldata_txid"] result = self.api.retrieve_nulldata(txid) self.assertEqual(result, "f483") def test_retrieve_nothing(self): def callback(): txid = fixtures["retrieve"]["nonulldata_txid"] result = self.api.retrieve_nulldata(txid) self.assertRaises(exceptions.NoNulldataOutput, callback)
def test_authentication_timeout_future_success(self): blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) farmer = Farmer(address) timeout = farmer.get_server_authentication_timeout() - 5 date = datetime.now() + timedelta(seconds=timeout) header_date = formatdate(timeval=mktime(date.timetuple()), localtime=True, usegmt=True) message = farmer.get_server_address() + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": header_date, "Authorization": header_authorization} self.assertTrue(farmer.authenticate(headers))
class TestRetrieve(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_retrieve(self): txid = fixtures["retrieve"]["nulldata_txid"] result = self.api.retrievenulldata(txid) self.assertEqual(result, "f483") def test_retrieve_nothing(self): def callback(): txid = fixtures["retrieve"]["nonulldata_txid"] result = self.api.retrievenulldata(txid) self.assertRaises(exceptions.NoNulldataOutput, callback)
def __init__(self, net, bandwidth, wif=None, store_config=None, handlers=None): # Accept direct connections. self.net = net # Control bandwidth. self.bandwidth = bandwidth # Returned by callbacks. self.success_value = ("127.0.0.1", 7777) # Used for signing messages. self.wallet = BtcTxStore(testnet=False, dryrun=True) self.wif = wif or self.wallet.create_key() # Where will the data be stored? self.store_config = store_config assert(len(list(store_config))) # Handlers for certain events. self.handlers = handlers if self.handlers is None: self.handlers = {} if "complete" not in self.handlers: self.handlers["complete"] = set() if "accept" not in self.handlers: self.handlers["accept"] = set() if "start" not in self.handlers: self.handlers["start"] = set() # Start networking. if not self.net.is_net_started: self.net.start() # Dict of data requests: [contract_id] > contract self.contracts = {} # List of Sock objects returned from UNL.connect. self.cons = [] # Dict of defers for contracts: [contract_id] > defer self.defers = {} # Three-way handshake status for contracts: [contract_id] > state self.handshake = {} # All contracts associated with this connection. # [con] > [contract_id] > con_info self.con_info = {} # File transfer currently active on connection. # [con] > contract_id self.con_transfer = {} # List of active downloads. # (Never try to download multiple copies of the same thing at once.) self.downloading = {} # Lock threads. self.mutex = Lock()
def _setup_data_transfer_client(self, store_config, passive_port, passive_bind, node_type, nat_type, wan_ip): # Setup handlers for callbacks registered via the API. handlers = { "complete": self._transfer_complete_handlers, "request": self._transfer_request_handlers } self._data_transfer = FileTransfer( net=Net( net_type="direct", node_type=node_type, nat_type=nat_type, dht_node=SimDHT(), # Replace with self.server later on. debug=1, passive_port=passive_port, passive_bind=passive_bind, wan_ip=wan_ip ), # FIXME use same key as dht wif=BtcTxStore(testnet=True, dryrun=True).create_key(), store_config=store_config, handlers=handlers ) # Setup success callback values. self._data_transfer.success_value = (self.sync_get_wan_ip(), self.port) self.process_data_transfers()
def test_success(self): # create header date and authorization signature blockchain = BtcTxStore() wif = blockchain.create_key() address = blockchain.get_address(wif) header_date = formatdate(timeval=mktime(datetime.now().timetuple()), localtime=True, usegmt=True) message = app.config["ADDRESS"] + " " + header_date header_authorization = blockchain.sign_unicode(wif, message) headers = {"Date": header_date, "Authorization": header_authorization} url = '/api/register/{0}'.format(address) rv = self.app.get(url, headers=headers) data = json.loads(rv.data.decode("utf-8")) self.assertEqual(address, data["btc_addr"]) self.assertEqual(rv.status_code, 200)
def __init__(self, url=common.DEFAULT_URL, debug=False, quiet=False, use_folder_tree=False, max_size=common.DEFAULT_MAX_SIZE, store_path=common.DEFAULT_STORE_PATH, config_path=common.DEFAULT_CONFIG_PATH, connection_retry_limit=common.DEFAULT_CONNECTION_RETRY_LIMIT, connection_retry_delay=common.DEFAULT_CONNECTION_RETRY_DELAY): debug = deserialize.flag(debug) quiet = deserialize.flag(quiet) self.url = deserialize.url(url) self.use_folder_tree = deserialize.flag(use_folder_tree) self.max_size = deserialize.byte_count(max_size) self.messenger = None # lazy self.btctxstore = BtcTxStore() self.retry_limit = deserialize.positive_integer(connection_retry_limit) self.retry_delay = deserialize.positive_integer(connection_retry_delay) # paths self.cfg_path = os.path.realpath(config_path) control.util.ensure_path_exists(os.path.dirname(self.cfg_path)) self.store_path = os.path.realpath(store_path) control.util.ensure_path_exists(self.store_path) # check for vfat partions if control.util.get_fs_type(self.store_path) == "vfat": self.use_folder_tree = True self.cfg = control.config.get(self.btctxstore, self.cfg_path)
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, [])
class TestCreateKey(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_createkey(self): wif = self.api.createkey() self.assertTrue(validate.is_wif_valid(wif, allowable_netcodes=['XTN']))
class TestGetUtxos(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_getutxos(self): address = fixtures["wallet"]["address"] expected = fixtures["getutxos"]["expected"] result = self.api.retrieve_utxos([address]) self.assertEqual(result, expected)
def setUp(self): app.config["SKIP_AUTHENTICATION"] = True # monkey patch app.config["DISABLE_CACHING"] = True self.btctxstore = BtcTxStore() self.bad_addr = 'notvalidaddress' self.app = app.test_client() db.create_all()
class TestGetAddress(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_getaddress(self): wif = fixtures["wallet"]["wif"] result = self.api.getaddress(wif) expected = fixtures["wallet"]["address"] self.assertEqual(result, expected)
class TestGetUtxos(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_getutxos(self): address = fixtures["wallet"]["address"] expected = fixtures["getutxos"]["expected"] result = self.api.retrieveutxos([address]) self.assertEqual(result, expected)
class TestAuth(unittest.TestCase): def setUp(self): self.btctxstore = BtcTxStore() self.sender_wif = self.btctxstore.create_key() self.sender = self.btctxstore.get_address(self.sender_wif) recipient_wif = self.btctxstore.create_key() self.recipient = self.btctxstore.get_address(recipient_wif) def test_self_validates(self): headers = storjcore.auth.create_headers(self.btctxstore, self.recipient, self.sender_wif) self.assertTrue(storjcore.auth.verify_headers(self.btctxstore, headers, 5, self.sender, self.recipient)) def test_invalid_signature(self): def callback(): headers = storjcore.auth.create_headers(self.btctxstore, self.recipient, self.sender_wif) headers["Authorization"] = base64.b64encode(65 * b"x") storjcore.auth.verify_headers(self.btctxstore, headers, 5, self.sender, self.recipient) self.assertRaises(storjcore.auth.AuthError, callback) def test_timeout_to_old(self): def callback(): headers = storjcore.auth.create_headers(self.btctxstore, self.recipient, self.sender_wif) time.sleep(5) storjcore.auth.verify_headers(self.btctxstore, headers, 5, self.sender, self.recipient) self.assertRaises(storjcore.auth.AuthError, callback) @unittest.skip("TODO implement") def test_timeout_to_young(self): pass # FIXME how to test this?
class TestCreateTx(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_create_tx(self): lock_time = 0 txins = fixtures["create_tx"]["txins"] txouts = fixtures["create_tx"]["txouts"] expected = fixtures["create_tx"]["expected"] result = self.api.create_tx(txins, txouts, lock_time) self.assertEqual(result, expected)
def mph_status(assets=None): with etc.database_lock: verify.status_input(assets) btctxstore = BtcTxStore(testnet=etc.testnet) wif = lib.load_wif() address = btctxstore.get_address(wif) message = util.b2h(os.urandom(32)) signature = btctxstore.sign_unicode(wif, message) if isinstance(signature, bytes): # XXX update btctxstore instead !!! signature = signature.decode("utf-8") return { "funds": { "address": address, "message": message, "signature": signature, "liquidity": lib.get_hub_liquidity(assets=assets), }, "current_terms": lib.get_terms(assets=assets), "connections": lib.get_connections_status(assets=assets) }
def __init__(self, asset, user=DEFAULT_COUNTERPARTY_RPC_USER, password=DEFAULT_COUNTERPARTY_RPC_PASSWORD, api_url=None, testnet=DEFAULT_TESTNET, dryrun=False, fee=DEFAULT_TXFEE, dust_size=DEFAULT_DUSTSIZE): """Initialize payment channel controler. Args: asset (str): Counterparty asset name. user (str): Counterparty API username. password (str): Counterparty API password. api_url (str): Counterparty API url. testnet (bool): True if running on testnet, otherwise mainnet. dryrun (bool): If True nothing will be published to the blockchain. fee (int): The transaction fee to use. dust_size (int): The default dust size for counterparty outputs. """ if testnet: default_url = DEFAULT_COUNTERPARTY_RPC_TESTNET_URL else: default_url = DEFAULT_COUNTERPARTY_RPC_MAINNET_URL self.dryrun = dryrun self.fee = fee self.dust_size = dust_size self.api_url = api_url or default_url self.testnet = testnet self.user = user self.password = password self.asset = asset self.netcode = "BTC" if not self.testnet else "XTN" self.btctxstore = BtcTxStore(testnet=self.testnet, dryrun=dryrun, service="insight") self.bitcoind_rpc = AuthServiceProxy( # XXX to publish "http://*****:*****@127.0.0.1:18332")
class TestCreateTx(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_createtx(self): locktime = 0 txins = fixtures["createtx"]["txins"] txouts = fixtures["createtx"]["txouts"] expected = fixtures["createtx"]["expected"] result = self.api.createtx(txins, txouts, locktime) self.assertEqual(result, expected)
class TestGetAddress(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_standard(self): wif = self.api.create_key() address = self.api.get_address(wif) self.assertTrue( validate.is_address_valid(address, allowable_netcodes=['XTN'])) def test_input_validation(self): # test correct types a = self.api.get_address(S_WIF) b = self.api.get_address(B_WIF) c = self.api.get_address(U_WIF) self.assertEqual(a, b, c) # TODO invalid types # TODO invalid input data def test_standards_compliant(self): wif = self.api.create_key() address = self.api.get_address(S_WIF) self.assertEqual(address, EXPECTED)
class TestGetAddress(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_standard(self): wif = self.api.create_key() address = self.api.get_address(wif) self.assertTrue(validate.is_address_valid(address, allowable_netcodes=['XTN'])) def test_input_validation(self): # test correct types a = self.api.get_address(S_WIF) b = self.api.get_address(B_WIF) c = self.api.get_address(U_WIF) self.assertEqual(a, b, c) # TODO invalid types # TODO invalid input data def test_standards_compliant(self): wif = self.api.create_key() address = self.api.get_address(S_WIF) self.assertEqual(address, EXPECTED)
def generate_block(self): """Close the current block, and generate a new one.""" try: last_block = latest_block(self.conn) last_hash = latest_hash(self.conn) # Get Merkle Root self.find_leaves() self.close() merkle_root = self.merkle_root() # Get TXID hexdata = merkle_root privatekeys = app.config["PRIVATE_KEYS"] changeaddress = app.config["CHANGE_ADDRESS"] fee = app.config["FEE"] testnet = app.config["TESTNET"] blockchain = BtcTxStore(testnet=testnet) tx_id = blockchain.storenulldata(hexdata, privatekeys, changeaddress=changeaddress, fee=fee) # Close current block c = self.conn.cursor() query1 = "UPDATE block_table SET end_hash=?, closed=?, merkle_root=?, tx_id=? WHERE id=?" c.execute(query1, (last_hash, True, merkle_root, tx_id, last_block)) # Start new block query2 = "INSERT INTO block_table (start_hash) VALUES (?)" c.execute(query2, (last_hash, )) self.conn.commit() self.conn.close() return 'Block {0} Built.'.format(last_block) except LookupError: return 'Block Empty.'
class TestGetWalletKey(unittest.TestCase): def setUp(self): self.api = BtcTxStore(dryrun=True, testnet=True) def test_standard(self): hwif = self.api.create_wallet() wif = self.api.get_key(hwif) self.assertTrue(validate.is_wif_valid(wif, allowable_netcodes=['XTN'])) def test_input_validation(self): # test correct types a = self.api.get_key(S_HWIF) b = self.api.get_key(B_HWIF) c = self.api.get_key(U_HWIF) self.assertEqual(a, b, c) # TODO invalid types # TODO invalid input data def test_standards_compliant(self): pass # FIXME check generated against expected output from 3rd parties
def send_to_bitcoin(wif, change_address, payload: bytes, is_testnet=True, is_dryrun=True, fee=10000): if is_dryrun: print("*** DRY RUN ***") else: print("*** LIVE ***") if is_testnet: print("*** TEST NET ***") else: print("*** MAIN NET ***") api = BtcTxStore(testnet=is_testnet, dryrun=is_dryrun) data = hexlify(payload) transaction_id = api.store_nulldata(hexdata=data, wifs=[wif], change_address=change_address, fee=fee) return transaction_id