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
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 micro_send(self, handle, quantity, token=None): """TODO doc string""" if token is None: token = util.b2h(os.urandom(32)) self.payments_queued.append({ "payee_handle": handle, "amount": quantity, "token": token }) return token
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_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_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 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_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 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 create_hub_connection(asset, client_pubkey, h2c_spend_secret_hash, hub_rpc_url): # current terms and asset data = {"asset": asset} current_terms = get_terms().get(asset) data.update(current_terms) # new hub key hub_key = create_key(asset, netcode=etc.netcode) data["hub_wif"] = hub_key["wif"] data["hub_pubkey"] = hub_key["pubkey"] data["hub_address"] = hub_key["address"] # client key data["client_pubkey"] = client_pubkey data["client_address"] = keys.address_from_pubkey(client_pubkey, netcode=etc.netcode) # spend secret for receive channel data.update(create_secret()) # send micropayment channel data["h2c_spend_secret_hash"] = h2c_spend_secret_hash # connection handle = util.b2h(os.urandom(32)) data["handle"] = handle data["hub_rpc_url"] = hub_rpc_url db.add_hub_connection(data) return ( { "handle": handle, "spend_secret_hash": data["secret_hash"], "channel_terms": current_terms }, hub_key["wif"] )
def create_hub_connection(asset, client_pubkey, h2c_spend_secret_hash, hub_rpc_url): # current terms and asset data = {"asset": asset} current_terms = terms().get(asset) data.update(current_terms) # new hub key hub_key = create_key(asset, netcode=etc.netcode) data["hub_wif"] = hub_key["wif"] data["hub_pubkey"] = hub_key["pubkey"] data["hub_address"] = hub_key["address"] # client key data["client_pubkey"] = client_pubkey data["client_address"] = keys.address_from_pubkey(client_pubkey, netcode=etc.netcode) # spend secret for receive channel data.update(create_secret()) # send micropayment channel data["h2c_spend_secret_hash"] = h2c_spend_secret_hash # connection handle = util.b2h(os.urandom(32)) data["handle"] = handle data["hub_rpc_url"] = hub_rpc_url db.add_hub_connection(data) return ({ "handle": handle, "spend_secret_hash": data["secret_hash"], "channel_terms": current_terms }, hub_key["wif"])
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
def create_secret(): secret = util.b2h(os.urandom(32)) return {"secret_value": secret, "secret_hash": util.hash160hex(secret)}