lbry-sdk/lbrynet/wallet/transaction.py

59 lines
2.1 KiB
Python
Raw Normal View History

2018-06-14 06:53:38 +02:00
import struct
from typing import List, Iterable # pylint: disable=unused-import
2018-06-14 06:53:38 +02:00
2018-07-05 05:16:52 +02:00
from twisted.internet import defer # pylint: disable=unused-import
2018-06-14 21:18:36 +02:00
from .account import Account # pylint: disable=unused-import
2018-06-14 06:53:38 +02:00
from torba.basetransaction import BaseTransaction, BaseInput, BaseOutput
from torba.hash import hash160
2018-07-05 05:16:52 +02:00
from lbryschema.claim import ClaimDict # pylint: disable=unused-import
2018-06-14 06:53:38 +02:00
from .script import InputScript, OutputScript
2018-07-15 05:02:19 +02:00
def claim_id_hash(tx_hash, n):
return hash160(tx_hash + struct.pack('>I', n))
2018-06-14 06:53:38 +02:00
class Input(BaseInput):
script_class = InputScript
class Output(BaseOutput):
script_class = OutputScript
def get_fee(self, ledger):
name_fee = 0
if self.script.is_claim_name:
name_fee = len(self.script.values['claim_name']) * ledger.fee_per_name_char
return max(name_fee, super().get_fee(ledger))
2018-06-14 06:53:38 +02:00
@classmethod
def pay_claim_name_pubkey_hash(cls, amount, claim_name, claim, pubkey_hash):
script = cls.script_class.pay_claim_name_pubkey_hash(claim_name, claim, pubkey_hash)
return cls(amount, script)
class Transaction(BaseTransaction):
input_class = Input
output_class = Output
def get_claim_id(self, output_index):
output = self.outputs[output_index] # type: Output
assert output.script.is_claim_name, 'Not a name claim.'
2018-06-14 06:53:38 +02:00
return claim_id_hash(self.hash, output_index)
2018-06-14 21:18:36 +02:00
@classmethod
def claim(cls, name, meta, amount, holding_address, funding_accounts, change_account, spend=None):
# type: (bytes, ClaimDict, int, bytes, List[Account], Account) -> defer.Deferred
2018-06-14 21:18:36 +02:00
ledger = cls.ensure_all_have_same_ledger(funding_accounts, change_account)
claim_output = Output.pay_claim_name_pubkey_hash(
2018-07-16 00:40:12 +02:00
amount, name, meta.serialized, ledger.address_to_hash160(holding_address)
2018-06-14 21:18:36 +02:00
)
return cls.create(spend or [], [claim_output], funding_accounts, change_account)
@classmethod
def abandon(cls, claims: Iterable[Output], funding_accounts: Iterable[Account], change_account: Account):
return cls.create([Input.spend(txo) for txo in claims], [], funding_accounts, change_account)