def test_validate_deposit_not_already_given(): try: asset = "XCP" client_key = lib.create_key(asset, netcode="XTN") client_pubkey = client_key["pubkey"] 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, client_key["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, client_key["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, client_key["wif"]) result = api.mph_deposit(**params) assert False except err.DepositAlreadyGiven: assert True
def test_asset_missmatch(connected_clients): alice, bob, charlie, david, eric, fred = connected_clients try: quantity = 5 sync_fee = david.channel_terms["sync_fee"] commit = _create_commit(david, quantity + sync_fee) h2c_next_revoke_secret_hash = david._gen_secret() david._add_to_commits_requested(h2c_next_revoke_secret_hash) params = { "handle": david.handle, "sends": [{ "payee_handle": alice.handle, "amount": quantity, "token": "deadbeef" }], "commit": commit, "revokes": None, "next_revoke_secret_hash": h2c_next_revoke_secret_hash } params = auth.sign_json(params, david.api.auth_wif) api.mph_sync(**params) assert False except err.AssetMissmatch: assert True
def test_standard_commit(connected_clients): alice, bob, charlie, david, eric, fred = connected_clients quantity = 5 sync_fee = alice.channel_terms["sync_fee"] commit = _create_commit(alice, quantity + sync_fee) h2c_next_revoke_secret_hash = alice._gen_secret() alice._add_to_commits_requested(h2c_next_revoke_secret_hash) params = { "handle": alice.handle, "sends": [{ "payee_handle": alice.handle, "amount": quantity, "token": "deadbeef" }], "commit": commit, "revokes": None, "next_revoke_secret_hash": h2c_next_revoke_secret_hash } params = auth.sign_json(params, alice.api.auth_wif) result = api.mph_sync(**params) assert result["receive"] == [{ "payer_handle": alice.handle, "token": "deadbeef", "amount": 5 }] assert result["commit"] is not None
def test_validate_deposit_not_already_given(): try: asset = "XCP" client_key = lib.create_key(asset, netcode="XTN") client_pubkey = client_key["pubkey"] 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, client_key["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, client_key["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, client_key["wif"]) result = api.mph_deposit(**params) assert False except err.DepositAlreadyGiven: assert True
def wrapper(*args, **kwargs): kwargs = copy.deepcopy(kwargs) # simulate http serialization if name in auth_methods: kwargs = auth.sign_json(kwargs, self.auth_wif) result = object.__getattribute__(api, name)(**kwargs) if name in auth_methods: auth.verify_json(result) return result
def wrapper(*args, **kwargs): kwargs = copy.deepcopy(kwargs) # simulate http serialization if name in auth_methods: kwargs = auth.sign_json(kwargs, self.auth_wif) result = object.__getattribute__(api, name)(**kwargs) if name in auth_methods: auth.verify_json(result) return result
def test_validate_asset_in_terms(): try: asset = "DIVISIBLE" client_key = lib.create_key(asset) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = {"asset": asset, "spend_secret_hash": secret_hash} params = auth.sign_json(params, client_key["wif"]) api.mph_request(**params) assert False except err.AssetNotInTerms: assert True
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_validate_asset_exists(): try: asset = "NONEXISTINGASSET" client_key = lib.create_key(asset) secret_hash = util.hash160hex(util.b2h(os.urandom(32))) params = {"asset": asset, "spend_secret_hash": secret_hash} params = auth.sign_json(params, client_key["wif"]) api.mph_request(**params) assert False except err.AssetDoesNotExist: assert True
def test_standard_usage_xcp(): asset = "XCP" client_key = lib.create_key(asset) 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, client_key["wif"]) result = api.mph_request(**params) assert result is not None jsonschema.validate(result, REQUEST_RESULT_SCHEMA)
def mph_close(**kwargs): with etc.database_lock: auth.verify_json(kwargs) verify.close_input( kwargs["handle"], kwargs["pubkey"], kwargs.get("spend_secret"), ) result, authwif = lib.close_connection( kwargs["handle"], kwargs.get("spend_secret"), ) return auth.sign_json(result, authwif)
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 test_validate_url(): try: asset = "XCP" client_key = lib.create_key(asset) 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, client_key["wif"]) api.mph_request(**params) assert False except err.InvalidUrl: assert True
def mph_deposit(**kwargs): with etc.database_lock: auth.verify_json(kwargs) verify.deposit_input( kwargs["handle"], kwargs["deposit_script"], kwargs["next_revoke_secret_hash"], kwargs["pubkey"] ) result, authwif = lib.complete_connection( kwargs["handle"], kwargs["deposit_script"], kwargs["next_revoke_secret_hash"] ) return auth.sign_json(result, authwif)
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 test_validate_handle_exists(): try: asset = "XCP" client_key = lib.create_key(asset, netcode="XTN") 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, client_key["wif"]) api.mph_deposit(**params) assert False except err.HandleNotFound: assert True
def mph_request(**kwargs): with etc.database_lock: auth.verify_json(kwargs) verify.request_input( kwargs["asset"], kwargs["pubkey"], kwargs["spend_secret_hash"], kwargs.get("hub_rpc_url") ) result, authwif = lib.create_hub_connection( kwargs["asset"], kwargs["pubkey"], kwargs["spend_secret_hash"], kwargs.get("hub_rpc_url") ) return auth.sign_json(result, authwif)
def test_validate_handle_exists(): try: asset = "XCP" client_key = lib.create_key(asset, netcode="XTN") 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, client_key["wif"]) api.mph_deposit(**params) assert False except err.HandleNotFound: assert True
def test_validate_commit_format(connected_clients): alice, bob, charlie, david, eric, fred = connected_clients try: secret = lib.create_secret() params = { "handle": alice.handle, "sends": [], "commit": "invalidformat", "revokes": None, "next_revoke_secret_hash": secret["secret_hash"] } params = auth.sign_json(params, alice.api.auth_wif) api.mph_sync(**params) assert False except jsonschema.exceptions.ValidationError: 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 mph_sync(**kwargs): with etc.database_lock: auth.verify_json(kwargs) verify.sync_input( kwargs["handle"], kwargs["next_revoke_secret_hash"], kwargs["pubkey"], kwargs.get("sends"), kwargs.get("commit"), kwargs.get("revokes") ) result, authwif = lib.sync_hub_connection( kwargs["handle"], kwargs["next_revoke_secret_hash"], kwargs.get("sends"), kwargs.get("commit"), kwargs.get("revokes") ) return auth.sign_json(result, authwif)
def test_send_exceeds_max(connected_clients): alice, bob, charlie, david, eric, fred = connected_clients # check before status alice_status = alice.get_status() assert alice_status["send_balance"] == 1000000 bob_status = bob.get_status() assert bob_status["send_balance"] == 1000000 try: secret = lib.create_secret() params = { "handle": alice.handle, "sends": [{ "payee_handle": bob.handle, "amount": 1000000, "token": "deadbeef" }], "commit": None, "revokes": None, "next_revoke_secret_hash": secret["secret_hash"] } params = auth.sign_json(params, alice.api.auth_wif) api.mph_sync(**params) assert False except err.AmountExceedsSpendable: assert True # check after status alice_status = alice.get_status() assert alice_status["send_balance"] == 1000000 bob_status = bob.get_status() assert bob_status["send_balance"] == 1000000 # actors can still sync assert alice.sync() == [] assert bob.sync() == []
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)
def mph_sync(**kwargs): auth.verify_json(kwargs) auth_wif = "cT9pEqELRn5v67hJmmmYQmPnsuezJeup7CqQiJBUTZnLLoxdydAb" return auth.sign_json({"foo": "bar"}, auth_wif)