Skip to content

Commit

Permalink
Drop comment_* apis.
Browse files Browse the repository at this point in the history
Refactored dangling functions.
Added unit test.
  • Loading branch information
cristi-zz committed Sep 2, 2021
1 parent fef0cc7 commit 45bf6c3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
41 changes: 0 additions & 41 deletions lbry/extras/daemon/comment_client.py

This file was deleted.

8 changes: 6 additions & 2 deletions lbry/extras/daemon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from lbry.extras.daemon.componentmanager import RequiredCondition
from lbry.extras.daemon.componentmanager import ComponentManager
from lbry.extras.daemon.json_response_encoder import JSONResponseEncoder
from lbry.extras.daemon import comment_client
from lbry.extras.daemon.undecorated import undecorated
from lbry.extras.daemon.security import ensure_request_allowed
from lbry.file_analysis import VideoFileAnalyzer
Expand Down Expand Up @@ -2837,7 +2836,12 @@ async def jsonrpc_channel_sign(
signing_channel = await self.get_channel_or_error(
wallet, channel_account_id, channel_id, channel_name, for_signing=True
)
return comment_client.sign(signing_channel, unhexlify(hexdata))
timestamp = str(int(time.time()))
signature = signing_channel.sign_data(unhexlify(hexdata), timestamp)
return {
'signature': signature,
'signing_ts': timestamp
}

@requires(WALLET_COMPONENT)
async def jsonrpc_channel_abandon(
Expand Down
6 changes: 6 additions & 0 deletions lbry/wallet/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ def sign(self, channel: 'Output', first_input_id=None):
self.signable.signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
self.script.generate()

def sign_data(self, data:bytes, timestamp:str) -> str:
pieces = [timestamp.encode(), self.claim_hash, data]
digest = sha256(b''.join(pieces))
signature = self.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
return hexlify(signature).decode()

def clear_signature(self):
self.channel = None
self.signable.clear_signature()
Expand Down
25 changes: 22 additions & 3 deletions tests/integration/blockchain/test_claim_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,38 @@
from binascii import unhexlify
from unittest import skip
from urllib.request import urlopen
import ecdsa

from lbry.error import InsufficientFundsError
from lbry.extras.daemon.comment_client import verify

from lbry.extras.daemon.daemon import DEFAULT_PAGE_SIZE
from lbry.testcase import CommandTestCase
from lbry.wallet.orchstr8.node import SPVNode
from lbry.wallet.transaction import Transaction
from lbry.wallet.transaction import Transaction, Output
from lbry.wallet.util import satoshis_to_coins as lbc

from lbry.crypto.hash import sha256

log = logging.getLogger(__name__)

def get_encoded_signature(signature):
signature = signature.encode() if isinstance(signature, str) else signature
r = int(signature[:int(len(signature) / 2)], 16)
s = int(signature[int(len(signature) / 2):], 16)
return ecdsa.util.sigencode_der(r, s, len(signature) * 4)


def verify(channel, data, signature, channel_hash=None):
pieces = [
signature['signing_ts'].encode(),
channel_hash or channel.claim_hash,
data
]
return Output.is_signature_valid(
get_encoded_signature(signature['signature']),
sha256(b''.join(pieces)),
channel.claim.channel.public_key_bytes
)


class ClaimTestCase(CommandTestCase):

Expand Down
21 changes: 19 additions & 2 deletions tests/unit/wallet/test_schema_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from lbry.testcase import AsyncioTestCase
from lbry.wallet.constants import CENT, NULL_HASH32

from lbry.wallet import Ledger, Database, Headers, Transaction, Input, Output
from lbry.schema.claim import Claim

from lbry.crypto.hash import sha256

def get_output(amount=CENT, pubkey_hash=NULL_HASH32):
return Transaction() \
Expand Down Expand Up @@ -114,3 +113,21 @@ def test_signed_claim_made_by_ytsync(self):
})

self.assertTrue(stream.is_signed_by(channel, ledger))


class TestValidateSignContent(AsyncioTestCase):

async def test_sign_some_content(self):
some_content = "MEANINGLESS CONTENT AEE3353320".encode()
timestamp_str = "1630564175"
channel = await get_channel()
stream = get_stream()
signature = channel.sign_data(some_content, timestamp_str)
stream.signable.signature = unhexlify(signature.encode())
encoded_signature = stream.get_encoded_signature()
pieces = [timestamp_str.encode(), channel.claim_hash, some_content]
self.assertTrue(Output.is_signature_valid(
encoded_signature,
sha256(b''.join(pieces)),
channel.claim.channel.public_key_bytes
))

0 comments on commit 45bf6c3

Please sign in to comment.