From 88bad1f86858f7ec14eefeee506af3d344adf2b1 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Tue, 18 Sep 2018 22:23:41 -0400 Subject: [PATCH] publish accepts channel_id or channel_name --- lbrynet/daemon/Daemon.py | 30 +++++++++++++++++++----------- lbrynet/wallet/database.py | 21 ++++++++++++++------- lbrynet/wallet/manager.py | 4 ++-- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lbrynet/daemon/Daemon.py b/lbrynet/daemon/Daemon.py index 599beb7a3..04479d410 100644 --- a/lbrynet/daemon/Daemon.py +++ b/lbrynet/daemon/Daemon.py @@ -2305,6 +2305,10 @@ class Daemon(AuthJSONRPCServer): # waiting to find out when we go to publish the claim (after having made the stream) raise Exception("invalid publish metadata: %s" % err.message) + certificate = None + if channel_id or channel_name: + certificate = yield self.get_channel_or_error(channel_id, channel_name) + log.info("Publish: %s", { 'name': name, 'file_path': file_path, @@ -2316,19 +2320,9 @@ class Daemon(AuthJSONRPCServer): 'channel_name': channel_name }) - certificate = None - if channel_name: - certificates = yield self.wallet_manager.get_certificates(channel_name) - for cert in certificates: - if cert.claim_id == channel_id: - certificate = cert - break - if certificate is None: - raise Exception("Cannot publish using channel %s" % channel_name) - result = yield self._publish_stream(name, amount, claim_dict, file_path, certificate, claim_address, change_address) - defer.returnValue(result) + return result @requires(WALLET_COMPONENT, conditions=[WALLET_IS_UNLOCKED]) @defer.inlineCallbacks @@ -3338,6 +3332,20 @@ class Daemon(AuthJSONRPCServer): response['head_blob_availability'].get('is_available') defer.returnValue(response) + @defer.inlineCallbacks + def get_channel_or_error(self, channel_id: str = None, name: str = None): + if channel_id is not None: + certificates = yield self.wallet_manager.get_certificates(claim_id=channel_id) + if not certificates: + raise ValueError("Couldn't find channel with claim_id '{}'." .format(channel_id)) + return certificates[0] + if name is not None: + certificates = yield self.wallet_manager.get_certificates(name=name) + if not certificates: + raise ValueError("Couldn't find channel with name '{}'.".format(name)) + return certificates[0] + raise ValueError("Couldn't find channel because a channel name or channel_id was not provided.") + def get_account_or_error(self, argument: str, account_id: str, lbc_only=True): for account in self.default_wallet.accounts: if account.id == account_id: diff --git a/lbrynet/wallet/database.py b/lbrynet/wallet/database.py index 2503096a0..763248ab1 100644 --- a/lbrynet/wallet/database.py +++ b/lbrynet/wallet/database.py @@ -49,14 +49,23 @@ class WalletDatabase(BaseDatabase): return row @defer.inlineCallbacks - def get_certificates(self, name, private_key_accounts=None, exclude_without_key=False): + def get_certificates(self, name=None, claim_id=None, private_key_accounts=None, exclude_without_key=False): + if name is not None: + filter_sql = 'claim_name=?' + filter_value = name + elif claim_id is not None: + filter_sql = 'claim_id=?' + filter_value = claim_id + else: + raise ValueError("'name' or 'claim_id' is required") + txos = yield self.db.runQuery( """ SELECT tx.txid, txo.position, txo.claim_id FROM txo JOIN tx ON tx.txid=txo.txid - WHERE claim_name=? AND (is_claim OR is_update) + WHERE {} AND (is_claim OR is_update) GROUP BY txo.claim_id ORDER BY tx.height DESC; - """, (name,) + """.format(filter_sql), (filter_value,) ) certificates = [] @@ -70,11 +79,9 @@ class WalletDatabase(BaseDatabase): certificates.append(Certificate(txid, nout, claim_id, name, private_key)) if exclude_without_key: - defer.returnValue([ - c for c in certificates if c.private_key is not None - ]) + return [c for c in certificates if c.private_key is not None] - defer.returnValue(certificates) + return certificates @defer.inlineCallbacks def get_claim(self, account, claim_id): diff --git a/lbrynet/wallet/manager.py b/lbrynet/wallet/manager.py index ad168feb5..f7db62a3f 100644 --- a/lbrynet/wallet/manager.py +++ b/lbrynet/wallet/manager.py @@ -284,8 +284,8 @@ class LbryWalletManager(BaseWalletManager): def channel_list(self): return self.default_account.get_channels() - def get_certificates(self, name): - return self.db.get_certificates(name, self.accounts, exclude_without_key=True) + def get_certificates(self, name=None, claim_id=None): + return self.db.get_certificates(name, claim_id, self.accounts, exclude_without_key=True) def update_peer_address(self, peer, address): pass # TODO: Data payments is disabled