def _script_data(script): delay_time = scripts.get_commit_delay_time(script) secret_hash = scripts.get_commit_revoke_secret_hash(script) commit_address = util.script_address(script, netcode=etc.netcode) return { "commit_address": commit_address, "delay_time": delay_time, "revoke_secret_hash": secret_hash, }
def can_cull(self): netcode = keys.netcode_from_wif(self.api.auth_wif) scripts = [self.c2h_state["deposit_script"]] scripts += [c["script"] for c in self.c2h_state["commits_active"]] scripts += [c["script"] for c in self.c2h_state["commits_revoked"]] scripts += [c["script"] for c in self.h2c_state["commits_active"]] scripts += [c["script"] for c in self.h2c_state["commits_revoked"]] for script in scripts: address = util.script_address(script, netcode) if self.address_in_use(address): return False return True
def complete_connection(handle, c2h_deposit_script, next_revoke_secret_hash): hub_conn, h2c, expire_time, hub_wif = _load_incomplete_connection( handle, c2h_deposit_script ) h2c_deposit_script = scripts.compile_deposit_script( h2c["payer_pubkey"], h2c["payee_pubkey"], h2c["spend_secret_hash"], expire_time ) data = { "handle": handle, "expire_time": expire_time, "c2h_channel_id": hub_conn["c2h_channel_id"], "c2h_deposit_script": c2h_deposit_script, "c2h_deposit_address": util.script_address( c2h_deposit_script, netcode=etc.netcode ), "h2c_channel_id": hub_conn["h2c_channel_id"], "h2c_deposit_script": h2c_deposit_script, "h2c_deposit_address": util.script_address( h2c_deposit_script, netcode=etc.netcode ), "next_revoke_secret_hash": next_revoke_secret_hash, } data.update(create_secret()) # revoke secret db.complete_hub_connection(data) return ( { "deposit_script": h2c_deposit_script, "next_revoke_secret_hash": data["secret_hash"] }, hub_wif )
def complete_connection(handle, c2h_deposit_script, next_revoke_secret_hash): hub_conn, h2c, expire_time, hub_key = _load_incomplete_connection( handle, c2h_deposit_script) h2c_deposit_script = scripts.compile_deposit_script( h2c["payer_pubkey"], h2c["payee_pubkey"], h2c["spend_secret_hash"], expire_time) data = { "handle": handle, "expire_time": expire_time, "c2h_channel_id": hub_conn["c2h_channel_id"], "c2h_deposit_script": c2h_deposit_script, "c2h_deposit_address": util.script_address(c2h_deposit_script, netcode=etc.netcode), "h2c_channel_id": hub_conn["h2c_channel_id"], "h2c_deposit_script": h2c_deposit_script, "h2c_deposit_address": util.script_address(h2c_deposit_script, netcode=etc.netcode), "next_revoke_secret_hash": next_revoke_secret_hash, } data.update(create_secret()) # revoke secret db.complete_hub_connection(data) return ({ "deposit_script": h2c_deposit_script, "next_revoke_secret_hash": data["secret_hash"] }, hub_key["wif"])
def finalize_commit(self, get_wif_func, state): commit = self.api.mpc_highest_commit(state=state) if commit is None: return None deposit_script = state["deposit_script"] pubkey = scripts.get_deposit_payee_pubkey(deposit_script) wif = get_wif_func(pubkey=pubkey) rawtx = scripts.sign_finalize_commit(self.get_rawtx, wif, commit["rawtx"], deposit_script) netcode = keys.netcode_from_wif(wif) deposit_address = util.script_address(deposit_script, netcode) deposit_utxos = self.api.get_unspent_txouts(address=deposit_address, unconfirmed=False) if self._can_publish(rawtx, deposit_utxos) and self.publish(rawtx): return rawtx return None
def test_script2address(self): for address, script_hex in FIXTURES["scripts"].items(): result = util.script_address(script_hex, netcode="XTN") self.assertEqual(result, address)
def get_script_address(script): return util.script_address(script, netcode=etc.netcode)
def full_duplex_channel_status(self, handle, netcode, send_state, recv_state, get_secret_func, clearance=6): assert (send_state["asset"] == recv_state["asset"]) asset = send_state["asset"] send_ttl = self.api.mpc_deposit_ttl(state=send_state, clearance=clearance) send_script = send_state["deposit_script"] send_deposit_expire_time = scripts.get_deposit_expire_time(send_script) send_deposit_address = util.script_address(send_script, netcode=netcode) send_balances = self.get_balances(send_deposit_address, ["BTC", asset]) send_deposit = send_balances.get(asset, 0) send_transferred = 0 if len(send_state["commits_active"]) > 0: send_transferred = self.api.mpc_transferred_amount( state=send_state) recv_ttl = self.api.mpc_deposit_ttl(state=recv_state, clearance=clearance) recv_script = recv_state["deposit_script"] recv_deposit_expire_time = scripts.get_deposit_expire_time(recv_script) recv_deposit_address = util.script_address(recv_script, netcode=netcode) recv_balances = self.get_balances(recv_deposit_address, ["BTC", asset]) recv_deposit = recv_balances.get(asset, 0) recv_transferred = 0 if len(recv_state["commits_active"]) > 0: recv_transferred = self.api.mpc_transferred_amount( state=recv_state) send_balance = send_deposit + recv_transferred - send_transferred recv_balance = recv_deposit + send_transferred - recv_transferred ttl = None if send_ttl is not None and recv_ttl is not None: ttl = min(send_ttl, recv_ttl) # get connection status status = "opening" if send_ttl and recv_ttl: status = "open" send_secret_hash = scripts.get_deposit_spend_secret_hash(send_script) send_secret = get_secret_func(send_secret_hash) send_commits_published = self.api.mpc_published_commits( state=send_state) expired = ttl == 0 # None explicitly ignore as channel opening if expired or send_secret or send_commits_published: status = "closed" send_balance = send_deposit recv_balance = 0 return { # FIXME get channel tx history "status": status, "asset": asset, "netcode": netcode, "balance": send_balance, "ttl": ttl, "send_balance": send_balance, "send_deposit_address": send_deposit_address, "send_deposit_ttl": send_ttl, "send_deposit_balances": send_balances, "send_deposit_expire_time": send_deposit_expire_time, "send_commits_published": send_commits_published, "send_transferred_quantity": send_transferred, "recv_balance": recv_balance, "recv_deposit_address": recv_deposit_address, "recv_deposit_ttl": recv_ttl, "recv_deposit_balances": recv_balances, "recv_deposit_expire_time": recv_deposit_expire_time, "recv_transferred_quantity": recv_transferred, }