named tuples
This commit is contained in:
parent
bcd2c7d90b
commit
3e826d0a5d
2 changed files with 17 additions and 6 deletions
|
@ -26,7 +26,7 @@
|
||||||
# and warranty status of this software.
|
# and warranty status of this software.
|
||||||
|
|
||||||
"""Transaction-related classes and functions."""
|
"""Transaction-related classes and functions."""
|
||||||
|
import typing
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
from lbry.wallet.server.hash import sha256, double_sha256, hash_to_hex_str
|
from lbry.wallet.server.hash import sha256, double_sha256, hash_to_hex_str
|
||||||
|
@ -41,11 +41,20 @@ ZERO = bytes(32)
|
||||||
MINUS_1 = 4294967295
|
MINUS_1 = 4294967295
|
||||||
|
|
||||||
|
|
||||||
class Tx(namedtuple("Tx", "version inputs outputs locktime raw")):
|
class Tx(typing.NamedTuple):
|
||||||
"""Class representing a transaction."""
|
version: int
|
||||||
|
inputs: typing.List['TxInput']
|
||||||
|
outputs: typing.List['TxOutput']
|
||||||
|
locktime: int
|
||||||
|
raw: bytes
|
||||||
|
|
||||||
|
|
||||||
class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")):
|
class TxInput(typing.NamedTuple):
|
||||||
|
prev_hash: bytes
|
||||||
|
prev_idx: int
|
||||||
|
script: bytes
|
||||||
|
sequence: int
|
||||||
|
|
||||||
"""Class representing a transaction input."""
|
"""Class representing a transaction input."""
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
script = self.script.hex()
|
script = self.script.hex()
|
||||||
|
@ -65,7 +74,9 @@ class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")):
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
||||||
class TxOutput(namedtuple("TxOutput", "value pk_script")):
|
class TxOutput(typing.NamedTuple):
|
||||||
|
value: int
|
||||||
|
pk_script: bytes
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
return b''.join((
|
return b''.join((
|
||||||
|
|
|
@ -340,7 +340,7 @@ pack_le_int64 = struct_le_q.pack
|
||||||
pack_le_uint16 = struct_le_H.pack
|
pack_le_uint16 = struct_le_H.pack
|
||||||
pack_le_uint32 = struct_le_I.pack
|
pack_le_uint32 = struct_le_I.pack
|
||||||
pack_be_uint64 = lambda x: x.to_bytes(8, byteorder='big')
|
pack_be_uint64 = lambda x: x.to_bytes(8, byteorder='big')
|
||||||
pack_be_uint16 = struct_be_H.pack
|
pack_be_uint16 = lambda x: x.to_bytes(2, byteorder='big')
|
||||||
pack_be_uint32 = struct_be_I.pack
|
pack_be_uint32 = struct_be_I.pack
|
||||||
pack_byte = structB.pack
|
pack_byte = structB.pack
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue