fix Output source offset calculation and made generat_channel_private_key async

This commit is contained in:
Lex Berezhny 2020-05-08 10:23:50 -04:00
parent 8c91777e5d
commit 41ce3e4ad8

View file

@ -1,6 +1,7 @@
import struct import struct
import hashlib import hashlib
import logging import logging
import asyncio
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from typing import List, Iterable, Optional from typing import List, Iterable, Optional
@ -266,11 +267,12 @@ class Output(InputOutput):
return cls(amount, OutputScript.pay_pubkey_hash(pubkey_hash)) return cls(amount, OutputScript.pay_pubkey_hash(pubkey_hash))
@classmethod @classmethod
def deserialize_from(cls, stream, offset): def deserialize_from(cls, stream, transaction_offset: int = 0):
return cls( amount = stream.read_uint64()
amount=stream.read_uint64(), length = stream.read_compact_size()
script=OutputScript(stream.read_string(), offset=offset+9) offset = stream.tell()-transaction_offset
) script = OutputScript(stream.read(length), offset=offset)
return cls(amount=amount, script=script)
def serialize_to(self, stream, alternate_script=None): def serialize_to(self, stream, alternate_script=None):
stream.write_uint64(self.amount) stream.write_uint64(self.amount)
@ -391,9 +393,18 @@ class Output(InputOutput):
self.channel = None self.channel = None
self.claim.clear_signature() self.claim.clear_signature()
def generate_channel_private_key(self): @staticmethod
self.private_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256) def _sync_generate_channel_private_key():
self.claim.channel.public_key_bytes = self.private_key.get_verifying_key().to_der() private_key = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1, hashfunc=hashlib.sha256)
public_key_bytes = private_key.get_verifying_key().to_der()
return private_key, public_key_bytes
async def generate_channel_private_key(self):
private_key, public_key_bytes = await asyncio.get_running_loop().run_in_executor(
None, Output._sync_generate_channel_private_key
)
self.private_key = private_key
self.claim.channel.public_key_bytes = public_key_bytes
self.script.generate() self.script.generate()
return self.private_key return self.private_key
@ -674,7 +685,7 @@ class Transaction:
]) ])
output_count = stream.read_compact_size() output_count = stream.read_compact_size()
self._add(self._outputs, [ self._add(self._outputs, [
Output.deserialize_from(stream, stream.tell()-start) for _ in range(output_count) Output.deserialize_from(stream, start) for _ in range(output_count)
]) ])
if self.is_segwit_flag: if self.is_segwit_flag:
# drain witness portion of transaction # drain witness portion of transaction