def test_get_hub_liquidity(server_db): # confirmed fund hub_wif = lib.load_wif() hub_address = keys.address_from_wif(hub_wif) unsigned_rawtx = api.create_send(**{ 'source': FUNDING_ADDRESS, 'destination': hub_address, 'asset': ASSET, 'quantity': 2000000, 'regular_dust_size': 2000000 }) signed_rawtx = scripts.sign_deposit(get_txs, FUNDING_WIF, unsigned_rawtx) api.sendrawtransaction(tx_hex=signed_rawtx) assert lib.get_hub_liquidity() == { 'A7736697071037023001': 0, 'BTC': 2000000, 'XCP': 2000000 } # unconfirmed send wif = keys.generate_wif("XTN") address = keys.address_from_wif(wif) unsigned_rawtx = api.create_send(**{ 'source': hub_address, 'destination': address, 'asset': ASSET, 'quantity': 1000000, 'fee': 1000000 }) signed_rawtx = scripts.sign_deposit(get_txs, hub_wif, unsigned_rawtx) util_test.insert_unconfirmed_raw_transaction(signed_rawtx, server_db) # counts unconfurmed sends assert lib.get_hub_liquidity() == { 'A7736697071037023001': 0, 'BTC': 0, # no utxos left 'XCP': 1000000 } # unconfurmed fund unsigned_rawtx = api.create_send(**{ 'source': FUNDING_ADDRESS, 'destination': hub_address, 'asset': ASSET, 'quantity': 2000000, 'regular_dust_size': 2000000 }) signed_rawtx = scripts.sign_deposit(get_txs, FUNDING_WIF, unsigned_rawtx) util_test.insert_unconfirmed_raw_transaction(signed_rawtx, server_db) # doesnt counts unconfurmed funds assert lib.get_hub_liquidity() == { 'A7736697071037023001': 0, 'BTC': 0, # no utxos left 'XCP': 1000000 } utxos = api.get_unspent_txouts(address=hub_address, unconfirmed=False) assert utxos == []
def test_block_send(): alice_wif = util.gen_funded_wif("XCP", 1000000, 1000000) client = Mpc(util.MockAPI(auth_wif=alice_wif)) bob_wif = keys.generate_wif(netcode=etc.netcode) bob_address = keys.address_from_wif(bob_wif) txid = client.block_send(source=alice_wif, destination=bob_address, asset="XCP", quantity=42) assert txid is not None
def _load_wif(): if not os.path.exists(etc.wallet_path): wif = keys.generate_wif(etc.netcode) with open(etc.wallet_path, 'w') as outfile: outfile.write(wif) else: with open(etc.wallet_path, 'r', encoding="utf-8") as infile: wif = infile.read().strip() return wif
def test_validate_asset_exists(): try: asset = "NONEXISTINGASSET" wif = keys.generate_wif(etc.netcode) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = {"asset": asset, "spend_secret_hash": secret_hash} params = auth.sign_json(params, wif) api.mph_request(**params) assert False except err.AssetDoesNotExist: assert True
def test_validate_asset_in_terms(): try: asset = "DIVISIBLE" wif = keys.generate_wif(etc.netcode) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = {"asset": asset, "spend_secret_hash": secret_hash} params = auth.sign_json(params, wif) api.mph_request(**params) assert False except err.AssetNotInTerms: assert True
def test_standard_usage_xcp(): asset = "XCP" wif = keys.generate_wif(etc.netcode) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "asset": asset, "spend_secret_hash": secret_hash, "hub_rpc_url": "https://does.not.exist", } params = auth.sign_json(params, wif) result = api.mph_request(**params) assert result is not None jsonschema.validate(result, REQUEST_RESULT_SCHEMA)
def test_validate_deposit_not_already_given(): try: asset = "XCP" wif = keys.generate_wif(etc.netcode) client_pubkey = keys.pubkey_from_wif(wif) h2c_spend_secret = util.b2h(os.urandom(32)) h2c_spend_secret_hash = util.hash160hex( h2c_spend_secret ) params = {"asset": asset, "spend_secret_hash": h2c_spend_secret_hash} params = auth.sign_json(params, wif) result = api.mph_request(**params) handle = result["handle"] hub_pubkey = result["pubkey"] c2h_spend_secret_hash = result["spend_secret_hash"] c2h_deposit_script = compile_deposit_script( client_pubkey, hub_pubkey, c2h_spend_secret_hash, 1337 ) # submit deposit next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "handle": handle, "deposit_script": c2h_deposit_script, "next_revoke_secret_hash": next_revoke_secret_hash } params = auth.sign_json(params, wif) result = api.mph_deposit(**params) assert result is not None jsonschema.validate(result, DEPOSIT_RESULT_SCHEMA) # resubmit deposit next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "handle": handle, "deposit_script": c2h_deposit_script, "next_revoke_secret_hash": next_revoke_secret_hash } params = auth.sign_json(params, wif) result = api.mph_deposit(**params) assert False except err.DepositAlreadyGiven: assert True
def gen_funded_wif(asset, asset_quantity, btc_quantity): src_wif = DP["addresses"][0][2] src_address = address_from_wif(src_wif) dest_wif = generate_wif(netcode=etc.netcode) dest_address = address_from_wif(dest_wif) unsigned_rawtx = api.create_send(**{ 'source': src_address, 'destination': dest_address, 'asset': asset, 'quantity': asset_quantity, 'regular_dust_size': btc_quantity }) signed_rawtx = scripts.sign_deposit(get_txs, src_wif, unsigned_rawtx) api.sendrawtransaction(tx_hex=signed_rawtx) return dest_wif
def test_validate_url(): try: asset = "XCP" wif = keys.generate_wif(etc.netcode) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "asset": asset, "spend_secret_hash": secret_hash, "hub_rpc_url": "?? invalid url ??", } params = auth.sign_json(params, wif) api.mph_request(**params) assert False except err.InvalidUrl: assert True
def test_validate_handle_exists(): try: wif = keys.generate_wif(etc.netcode) next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32))) c2h_deposit_script = util.b2h(os.urandom(32)), params = { "handle": "deadbeef", "deposit_script": c2h_deposit_script, "next_revoke_secret_hash": next_revoke_secret_hash } params = auth.sign_json(params, wif) api.mph_deposit(**params) assert False except err.HandleNotFound: assert True
def gen_funded_wif(asset, asset_quantity, btc_quantity): src_wif = DP["addresses"][0][2] src_address = address_from_wif(src_wif) dest_wif = generate_wif(netcode=etc.netcode) dest_address = address_from_wif(dest_wif) unsigned_rawtx = api.create_send( **{ 'source': src_address, 'destination': dest_address, 'asset': asset, 'quantity': asset_quantity, 'regular_dust_size': btc_quantity }) signed_rawtx = scripts.sign_deposit(get_tx, src_wif, unsigned_rawtx) api.sendrawtransaction(tx_hex=signed_rawtx) return dest_wif
def test_validate_deposit_not_already_given(): try: asset = "XCP" wif = keys.generate_wif(etc.netcode) client_pubkey = keys.pubkey_from_wif(wif) h2c_spend_secret = util.b2h(os.urandom(32)) h2c_spend_secret_hash = util.hash160hex(h2c_spend_secret) params = {"asset": asset, "spend_secret_hash": h2c_spend_secret_hash} params = auth.sign_json(params, wif) result = api.mph_request(**params) handle = result["handle"] hub_pubkey = result["pubkey"] c2h_spend_secret_hash = result["spend_secret_hash"] c2h_deposit_script = compile_deposit_script(client_pubkey, hub_pubkey, c2h_spend_secret_hash, 1337) # submit deposit next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "handle": handle, "deposit_script": c2h_deposit_script, "next_revoke_secret_hash": next_revoke_secret_hash } params = auth.sign_json(params, wif) result = api.mph_deposit(**params) assert result is not None jsonschema.validate(result, DEPOSIT_RESULT_SCHEMA) # resubmit deposit next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = { "handle": handle, "deposit_script": c2h_deposit_script, "next_revoke_secret_hash": next_revoke_secret_hash } params = auth.sign_json(params, wif) result = api.mph_deposit(**params) assert False except err.DepositAlreadyGiven: assert True
def test_pubkey_missmatch(connected_clients): alice, bob, charlie, david, eric, fred = connected_clients try: secret = lib.create_secret() handle = alice.handle wif = keys.generate_wif(netcode=etc.netcode) params = { "handle": handle, "sends": [], "commit": None, "revokes": None, "next_revoke_secret_hash": secret["secret_hash"] } params = auth.sign_json(params, wif) api.mph_sync(**params) assert False except err.ClientPubkeyMissmatch: assert True
def gen_funded_wif(asset, asset_quantity, btc_quantity): src_wif = DP["addresses"][0][2] src_address = address_from_wif(src_wif) dest_wif = generate_wif(netcode=etc.netcode) dest_address = address_from_wif(dest_wif) rawtx = api.create_send( **{ 'source': src_address, 'destination': dest_address, 'asset': asset, 'quantity': asset_quantity, 'regular_dust_size': btc_quantity }) api.sendrawtransaction(tx_hex=rawtx) # entries = api.get_balances(filters=[ # {"field": "address", "op": "==", "value": dest_address}, # {"field": "asset", "op": "==", "value": asset}, # ]) # assert entries[0]["quantity"] == asset_quantity # assert entries[0]["address"] == dest_address # assert entries[0]["asset"] == asset return dest_wif
def func(): auth_wif = keys.generate_wif() data = {"foo": "bar", "pubkey": "invalid"} signed_json_data = auth.sign_json(data, auth_wif) auth.verify_json(signed_json_data)
def test_sign_verify_json(self): auth_wif = keys.generate_wif() signed_json_data = auth.sign_json({"foo": "bar"}, auth_wif) valid = auth.verify_json(signed_json_data) self.assertTrue(valid)
# from counterpartylib.test import conftest from counterpartylib.test.util_test import CURR_DIR as CPLIB_TESTDIR from counterpartylib.test.fixtures.params import DP from counterpartylib.lib.micropayments.control import get_balance from counterpartylib.lib.api import dispatcher from micropayment_core.keys import address_from_wif from micropayment_core.keys import generate_wif from picopayments import api FIXTURE_SQL_FILE = CPLIB_TESTDIR + '/fixtures/scenarios/unittest_fixture.sql' FIXTURE_DB = tempfile.gettempdir() + '/fixtures.unittest_fixture.db' ALICE_WIF = DP["addresses"][0][2] ALICE_ADDRESS = address_from_wif(ALICE_WIF) BOB_WIF = generate_wif(netcode="XTN") BOB_ADDRESS = address_from_wif(BOB_WIF) @pytest.mark.usefixtures("picopayments_server") def test_commit_tx_alpha(): rawtx = api.create_send( **{ 'source': ALICE_ADDRESS, 'destination': BOB_ADDRESS, 'asset': 'XCP', 'quantity': 33, 'regular_dust_size': 42 })