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}
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        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
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        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
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        result = api.mph_deposit(**params)

        assert False
    except err.DepositAlreadyGiven:
        assert True
Esempio n. 2
0
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, keys.wif_to_privkey(alice.client_wif))
    result = api.mph_sync(**params)

    assert result["receive"] == [{
        "payer_handle": alice.handle,
        "token": "deadbeef",
        "amount": 5
    }]
    assert result["commit"] is not None
Esempio n. 3
0
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
        }

        privkey = keys.wif_to_privkey(david.client_wif)
        params = auth.sign_json(params, privkey)
        api.mph_sync(**params)

        assert False
    except err.AssetMissmatch:
        assert True
Esempio n. 4
0
 def wrapper(*args, **kwargs):
     kwargs = copy.deepcopy(kwargs)  # simulate http serialization
     if name in auth_methods:
         privkey = keys.wif_to_privkey(self.auth_wif)
         kwargs = auth.sign_json(kwargs, privkey)
     result = object.__getattribute__(api, name)(**kwargs)
     if name in auth_methods:
         auth.verify_json(result)
     return result
Esempio n. 5
0
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, keys.wif_to_privkey(authwif))
Esempio n. 6
0
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, keys.wif_to_privkey(authwif))
Esempio n. 7
0
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, keys.wif_to_privkey(authwif))
Esempio n. 8
0
def hub_connections(handles, assets):
    connections = []
    for connection in db.hub_connections():
        if assets is not None and connection["asset"] not in assets:
            continue
        if handles is not None and connection["handle"] not in handles:
            continue
        wif = connection.pop("wif")
        connection = auth.sign_json(connection, keys.wif_to_privkey(wif))
        connections.append(connection)
    return connections
Esempio n. 9
0
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}
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        api.mph_request(**params)
        assert False
    except err.AssetDoesNotExist:
        assert True
Esempio n. 10
0
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}
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        api.mph_request(**params)
        assert False
    except err.AssetNotInTerms:
        assert True
Esempio n. 11
0
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",
    }
    privkey = keys.wif_to_privkey(client_key["wif"])
    params = auth.sign_json(params, privkey)
    result = api.mph_request(**params)
    assert result is not None
    jsonschema.validate(result, REQUEST_RESULT_SCHEMA)
Esempio n. 12
0
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, keys.wif_to_privkey(alice.client_wif))
        api.mph_sync(**params)
        assert False
    except jsonschema.exceptions.ValidationError:
        assert True
Esempio n. 13
0
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 ??",
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        api.mph_request(**params)
        assert False
    except err.InvalidUrl:
        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
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        api.mph_deposit(**params)
        assert False
    except err.HandleNotFound:
        assert True
Esempio n. 15
0
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, keys.wif_to_privkey(wif))
        api.mph_sync(**params)
        assert False
    except err.ClientPubkeyMissmatch:
        assert True
Esempio n. 16
0
def sign_json(json_data, auth_wif):
    privkey = keys.wif_to_privkey(auth_wif)

    # add pubkey to json data if needed
    pubkey = keys.pubkey_from_privkey(privkey)
    if "pubkey" in json_data and not json_data["pubkey"] == pubkey:
        raise AuthPubkeyMissmatch(pubkey, json_data["pubkey"])
    else:
        json_data["pubkey"] = pubkey

    # sign serialized data (keys must be ordered!)
    data = json.dumps(json_data, sort_keys=True)
    signature = keys.sign_sha256(privkey, data.encode("utf-8"))

    # add signature to json data
    json_data["signature"] = signature

    return json_data
Esempio n. 17
0
def test_validate_handles_exist(connected_clients):
    alice, bob, charlie, david, eric, fred = connected_clients
    try:
        secret = lib.create_secret()
        params = {
            "handle": alice.handle,
            "sends": [{
                "payee_handle": "deadbeef",
                "amount": 1337,
                "token": "deadbeef"
            }],
            "commit": None,
            "revokes": None,
            "next_revoke_secret_hash": secret["secret_hash"]
        }
        params = auth.sign_json(params, keys.wif_to_privkey(alice.client_wif))
        api.mph_sync(**params)
        assert False
    except err.HandlesNotFound:
        assert True
Esempio n. 18
0
def test_send_exceeds_max(connected_clients):
    alice, bob, charlie, david, eric, fred = connected_clients
    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, keys.wif_to_privkey(alice.client_wif))
        api.mph_sync(**params)
        assert False
    except err.PaymentExceedsSpendable:
        assert True