def test_block_send(): alice_wif = util.gen_funded_wif("XCP", 1000000, 1000000) client = Mpc(util.MockAPI(auth_wif=alice_wif)) bob_wif = keys.generate_wif(netcode=etc.netcode) bob_address = keys.address_from_wif(bob_wif) txid = client.block_send(source=alice_wif, destination=bob_address, asset="XCP", quantity=42) assert txid is not None
def publish_commits(): with etc.database_lock: txids = [] cursor = sql.get_cursor() for hub_connection in db.hub_connections_complete(cursor=cursor): asset = hub_connection["asset"] c2h_mpc_id = hub_connection["c2h_channel_id"] c2h_state = db.load_channel_state(c2h_mpc_id, asset, cursor=cursor) h2c_mpc_id = hub_connection["h2c_channel_id"] h2c_state = db.load_channel_state(h2c_mpc_id, asset, cursor=cursor) h2c_spend_secret_hash = get_deposit_spend_secret_hash( h2c_state["deposit_script"]) h2c_spend_secret = lib.get_secret(h2c_spend_secret_hash) c2h_expired = lib.is_expired(c2h_state, etc.expire_clearance) h2c_expired = lib.is_expired(h2c_state, etc.expire_clearance) expired = c2h_expired or h2c_expired h2c_commits_published = api.mpc_published_commits(state=h2c_state) closed = hub_connection["closed"] != 0 # connection expired or commit published or spend secret known if expired or closed or h2c_commits_published or h2c_spend_secret: if not closed: db.set_connection_closed(handle=hub_connection["handle"]) c2h_commits_published = api.mpc_published_commits( state=c2h_state) if len(c2h_commits_published) == 0: txid = Mpc(api).finalize_commit(lib.get_wif, c2h_state) if txid: txids.append(txid) return txids
def recover_funds(hub_connection, cursor=None): from picopayments_hub import api asset = hub_connection["asset"] c2h_mpc_id = hub_connection["c2h_channel_id"] h2c_mpc_id = hub_connection["h2c_channel_id"] c2h_state = db.load_channel_state(c2h_mpc_id, asset, cursor=cursor) h2c_state = db.load_channel_state(h2c_mpc_id, asset, cursor=cursor) return Mpc(api).full_duplex_recover_funds(get_wif, get_secret, c2h_state, h2c_state)
def blocksend(asset, destination, quantity, extra_btc=0): """ Send funds using via blockchain transaction. Args: asset (str): Asset to send. destination (address): Address to receive the funds. quantity (int): Quantity of the given asset to transfer. extra_btc (int, default=0): Optional bitcoin to also be sent. Returns: txid of published transaction. """ hub_api = _hub_api() kwargs = dict(source=_load_wif(), destination=destination, asset=asset, quantity=int(quantity)) if extra_btc > 0: kwargs["regular_dust_size"] = extra_btc return Mpc(hub_api).block_send(**kwargs)
def get_status(hub_conn, clearance=6, cursor=None): from picopayments_hub import api send_state = db.load_channel_state(hub_conn["h2c_channel_id"], hub_conn["asset"], cursor=cursor) recv_state = db.load_channel_state(hub_conn["c2h_channel_id"], hub_conn["asset"], cursor=cursor) status = Mpc(api).full_duplex_channel_status(hub_conn["handle"], etc.netcode, send_state, recv_state, get_secret, clearance=clearance) return { "asset": status["asset"], "balance": status["balance"], "ttl": status["ttl"], "status": status["status"] }
def balances(asset=None, address=None): """ Get balances for address or current wallet. Args: asset (str, default=None): Optionally filter for given asset. address (str, default=None): Optionally provide address to check, uses wallet by default Returns: Dict mapping asset to available quantity in satoshis, Unconfirmed assets are ignored. { "BTC": 926109330, "XCP": 140982404156 } """ hub_api = _hub_api() assets = [asset] if asset else None if address is None: address = keys.address_from_wif(_load_wif()) return Mpc(hub_api).get_balances(address, assets=assets)
def _send_client_funds(connection_data, quantity): from picopayments_hub import api c2h_state = connection_data["c2h_state"] h2c_state = connection_data["h2c_state"] handle = connection_data["connection"]["handle"] result = db.get_next_revoke_secret_hash(handle=handle) next_revoke_secret_hash = result["next_revoke_secret_hash"] deposit_script_bin = h2c_state["deposit_script"] hub_pubkey = scripts.get_deposit_payer_pubkey(deposit_script_bin) wif = db.key(pubkey=hub_pubkey)["wif"] result = Mpc(api).full_duplex_transfer(wif, get_secret, h2c_state, c2h_state, quantity, next_revoke_secret_hash, etc.delay_time) return { "c2h_revoke_secrets": result["revokes"], "h2c_unnotified_commit": result["commit"], "h2c_state": result["send_state"], "c2h_state": result["recv_state"] }
def get_balances(address, assets=None): from picopayments_hub import api return Mpc(api).get_balances(address=address, assets=assets)