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_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
Example #3
0
def close_connection(handle, h2c_spend_secret=None):

    cursor = sql.get_cursor()
    hub_connection = db.hub_connection(handle=handle, cursor=cursor)

    # save secret if given and not already known
    if h2c_spend_secret is not None:
        secret_hash = util.hash160hex(h2c_spend_secret)
        if not get_secret(secret_hash):
            db.add_secret(secret_value=h2c_spend_secret,
                          secret_hash=secret_hash,
                          cursor=cursor)

    # close connection if not already done
    if not hub_connection["closed"]:
        db.set_connection_closed(handle=handle, cursor=cursor)

    # get c2h spend secret if no commits for channel
    c2h_spend_secret = None
    c2h_state = db.load_channel_state(hub_connection["c2h_channel_id"],
                                      hub_connection["asset"],
                                      cursor=cursor)
    if len(c2h_state["commits_active"]) == 0:
        c2h_spend_secret_hash = scripts.get_deposit_spend_secret_hash(
            c2h_state["deposit_script"])
        c2h_spend_secret = get_secret(c2h_spend_secret_hash)

    hub_key = db.channel_payer_key(id=hub_connection["h2c_channel_id"])
    return ({"spend_secret": c2h_spend_secret}, hub_key["wif"])
Example #4
0
    def close(self):

        # publish h2c commit if possible
        commit_rawtx = self.finalize_commit(self._get_wif, self.h2c_state)
        if commit_rawtx is not None:
            self._history_add_published_h2c_commit(commit_rawtx)

        # get h2c spend secret if no commits for channel
        h2c_spend_secret = None
        if len(self.h2c_state["commits_active"]) == 0:
            deposit_script = self.h2c_state["deposit_script"]
            spend_hash = scripts.get_deposit_spend_secret_hash(deposit_script)
            h2c_spend_secret = self.secrets[spend_hash]

        # tell hub to close the channel
        result = self.api.mph_close(handle=self.handle,
                                    spend_secret=h2c_spend_secret)

        # remember c2h spend secret if given
        c2h_spend_secret = result["spend_secret"]
        if c2h_spend_secret:
            secret_hash = util.hash160hex(c2h_spend_secret)
            self.secrets[secret_hash] = c2h_spend_secret

        return commit_rawtx
Example #5
0
def close_connection(handle, h2c_spend_secret=None):

    cursor = sql.get_cursor()
    hub_connection = db.hub_connection(handle=handle, cursor=cursor)

    # save secret if given and not already known
    if h2c_spend_secret is not None:
        secret_hash = util.hash160hex(h2c_spend_secret)
        if not get_secret(secret_hash):
            db.add_secret(secret_value=h2c_spend_secret,
                          secret_hash=secret_hash, cursor=cursor)

    # close connection if not already done
    if not hub_connection["closed"]:
        db.set_connection_closed(handle=handle, cursor=cursor)

    # get c2h spend secret if no commits for channel
    c2h_spend_secret = None
    c2h_state = db.load_channel_state(hub_connection["c2h_channel_id"],
                                      hub_connection["asset"], cursor=cursor)
    if len(c2h_state["commits_active"]) == 0:
        c2h_spend_secret_hash = scripts.get_deposit_spend_secret_hash(
            c2h_state["deposit_script"]
        )
        c2h_spend_secret = get_secret(c2h_spend_secret_hash)

    hub_wif = load_wif()
    return ({"spend_secret": c2h_spend_secret}, hub_wif)
Example #6
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}
        params = auth.sign_json(params, client_key["wif"])
        api.mph_request(**params)
        assert False
    except err.AssetNotInTerms:
        assert True
Example #7
0
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
Example #8
0
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
Example #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}
        params = auth.sign_json(params, client_key["wif"])
        api.mph_request(**params)
        assert False
    except err.AssetDoesNotExist:
        assert True
Example #10
0
def close_input(handle, client_pubkey, spend_secret):
    hub_connection(handle)
    _channel_client(handle, client_pubkey)

    # validate spend secret for h2c deposit
    if spend_secret is not None:
        validate.is_string(spend_secret)
        spend_secret_hash = util.hash160hex(spend_secret)
        deposit_script = db.h2c_channel(handle=handle)["deposit_script"]
        expected_hash = scripts.get_deposit_spend_secret_hash(deposit_script)
        if expected_hash != spend_secret_hash:
            raise err.InvalidSpendSecret(expected_hash, spend_secret)
Example #11
0
def close_input(handle, client_pubkey, spend_secret):
    hub_connection(handle)
    _channel_client(handle, client_pubkey)

    # validate spend secret for h2c deposit
    if spend_secret is not None:
        validate.is_string(spend_secret)
        spend_secret_hash = util.hash160hex(spend_secret)
        deposit_script = db.h2c_channel(handle=handle)["deposit_script"]
        expected_hash = scripts.get_deposit_spend_secret_hash(deposit_script)
        if expected_hash != spend_secret_hash:
            raise err.InvalidSpendSecret(expected_hash, spend_secret)
Example #12
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",
    }
    params = auth.sign_json(params, client_key["wif"])
    result = api.mph_request(**params)
    assert result is not None
    jsonschema.validate(result, REQUEST_RESULT_SCHEMA)
Example #13
0
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
Example #14
0
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
Example #15
0
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
Example #16
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 ??",
        }
        params = auth.sign_json(params, client_key["wif"])
        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
        }
        params = auth.sign_json(params, client_key["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
Example #19
0
    def close(self):
        commit_txid = self.finalize_commit(self._get_wif, self.h2c_state)

        # get h2c spend secret if no commits for channel
        h2c_spend_secret = None
        if len(self.h2c_state["commits_active"]) == 0:
            deposit_script = self.h2c_state["deposit_script"]
            spend_hash = scripts.get_deposit_spend_secret_hash(deposit_script)
            h2c_spend_secret = self.secrets[spend_hash]

        # tell hub to close the channel
        result = self.api.mph_close(handle=self.handle,
                                    spend_secret=h2c_spend_secret)

        # remember c2h spend secret if given
        c2h_spend_secret = result["spend_secret"]
        if c2h_spend_secret:
            secret_hash = util.hash160hex(c2h_spend_secret)
            self.secrets[secret_hash] = c2h_spend_secret

        return commit_txid
Example #20
0
 def test_hash160(self):
     hex_digest = util.hash160hex("f483")
     expected = "4e0123796bee558240c5945ac9aff553fcc6256d"
     self.assertEqual(hex_digest, expected)
Example #21
0
def create_secret():
    secret = util.b2h(os.urandom(32))
    return {"secret_value": secret, "secret_hash": util.hash160hex(secret)}
Example #22
0
def create_secret():
    secret = util.b2h(os.urandom(32))
    return {"secret_value": secret, "secret_hash": util.hash160hex(secret)}
Example #23
0
 def _gen_secret(self):
     secret_value = util.b2h(os.urandom(32))
     secret_hash = util.hash160hex(secret_value)
     self.secrets[secret_hash] = secret_value
     return secret_hash