2019-04-07 20:37:04 +02:00
|
|
|
import logging
|
2019-06-13 02:20:30 +02:00
|
|
|
import time
|
|
|
|
import hashlib
|
|
|
|
import binascii
|
2019-05-24 18:12:27 +02:00
|
|
|
|
2019-04-07 20:37:04 +02:00
|
|
|
import aiohttp
|
2019-06-13 02:20:30 +02:00
|
|
|
import ecdsa
|
|
|
|
from torba.client.hash import sha256
|
|
|
|
from lbrynet.wallet.transaction import Output
|
2019-04-07 20:37:04 +02:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-06-13 02:20:30 +02:00
|
|
|
def get_encoded_signature(signature):
|
|
|
|
signature = signature.encode() if type(signature) is 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 is_comment_signed_by_channel(comment: dict, channel: Output):
|
|
|
|
try:
|
|
|
|
pieces = [
|
|
|
|
comment['signing_ts'].encode(),
|
|
|
|
channel.claim_hash,
|
|
|
|
comment['comment'].encode()
|
|
|
|
]
|
|
|
|
return Output.is_signature_valid(
|
|
|
|
get_encoded_signature(comment['signature']),
|
|
|
|
sha256(b''.join(pieces)),
|
|
|
|
channel.claim.channel.public_key_bytes
|
|
|
|
)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def sign_comment(comment: dict, channel: Output):
|
|
|
|
timestamp = str(int(time.time())).encode()
|
|
|
|
pieces = [timestamp, channel.claim_hash, comment['comment'].encode()]
|
|
|
|
digest = sha256(b''.join(pieces))
|
|
|
|
signature = channel.private_key.sign_digest_deterministic(digest, hashfunc=hashlib.sha256)
|
|
|
|
comment['signature'] = binascii.hexlify(signature).decode()
|
|
|
|
comment['signing_ts'] = timestamp.decode()
|
2019-04-07 20:37:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def jsonrpc_post(url: str, method: str, **params) -> any:
|
2019-05-24 18:12:27 +02:00
|
|
|
json_body = {'jsonrpc': '2.0', 'id': None, 'method': method, 'params': params}
|
2019-04-07 20:37:04 +02:00
|
|
|
headers = {'Content-Type': 'application/json'}
|
2019-05-24 18:12:27 +02:00
|
|
|
async with aiohttp.request('POST', url, json=json_body, headers=headers) as response:
|
|
|
|
try:
|
|
|
|
result = await response.json()
|
|
|
|
return result['result'] if 'result' in result else result
|
|
|
|
except aiohttp.client.ContentTypeError as cte:
|
|
|
|
log.exception('Unable to decode respose from server: %s', cte)
|
|
|
|
return await response.text()
|