|
| 1 | +import base58 |
| 2 | +import struct |
| 3 | +from lto import crypto |
| 4 | +from lto.transaction import Transaction |
| 5 | + |
| 6 | + |
| 7 | +class Burn(Transaction): |
| 8 | + TYPE = 15 |
| 9 | + DEFAULT_FEE = 100000000 |
| 10 | + DEFAULT_VERSION = 3 |
| 11 | + |
| 12 | + def __init__(self, amount): |
| 13 | + super().__init__() |
| 14 | + if amount < 1: |
| 15 | + raise Exception("Minimum burn amount = 1") |
| 16 | + self.amount = amount |
| 17 | + self.version = self.DEFAULT_VERSION |
| 18 | + self.tx_fee = self.DEFAULT_FEE |
| 19 | + |
| 20 | + def __to_binary_v3(self): |
| 21 | + return (self.TYPE.to_bytes(1, 'big') + |
| 22 | + b'\3' + |
| 23 | + crypto.str2bytes(self.chain_id) + |
| 24 | + struct.pack(">Q", self.timestamp) + |
| 25 | + crypto.key_type_id(self.sender_key_type) + |
| 26 | + base58.b58decode(self.sender_public_key) + |
| 27 | + struct.pack(">Q", self.tx_fee) + |
| 28 | + struct.pack(">Q", self.amount)) |
| 29 | + |
| 30 | + def to_binary(self): |
| 31 | + if self.version == 3: |
| 32 | + return self.__to_binary_v3() |
| 33 | + else: |
| 34 | + raise Exception('Incorrect Version') |
| 35 | + |
| 36 | + def to_json(self): |
| 37 | + return (crypto.merge_dicts({ |
| 38 | + "type": self.TYPE, |
| 39 | + "version": self.version, |
| 40 | + "id": self.id if self.id else "", |
| 41 | + "sender": self.sender, |
| 42 | + "senderKeyType": self.sender_key_type, |
| 43 | + "senderPublicKey": self.sender_public_key, |
| 44 | + "fee": self.tx_fee, |
| 45 | + "timestamp": self.timestamp, |
| 46 | + "amount": self.amount, |
| 47 | + "proofs": self.proofs, |
| 48 | + "height": self.height if self.height else "" |
| 49 | + }, self._sponsor_json())) |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def from_data(data): |
| 53 | + tx = Burn(data['amount']) |
| 54 | + tx.type = data['type'] |
| 55 | + tx.version = data['version'] |
| 56 | + tx.id = data['id'] if 'id' in data else '' |
| 57 | + tx.sender = data['sender'] if 'sender' in data else '' |
| 58 | + tx.sender_key_type = data['senderKeyType'] if 'senderKeyType' in data else 'ed25519' |
| 59 | + tx.sender_public_key = data['senderPublicKey'] |
| 60 | + tx.fee = data['fee'] |
| 61 | + tx.timestamp = data['timestamp'] |
| 62 | + tx.amount = data['amount'] |
| 63 | + tx.proofs = data['proofs'] if 'proofs' in data else [] |
| 64 | + tx.height = data['height'] if 'height' in data else '' |
| 65 | + |
| 66 | + if "sponsor_public_key" in data: |
| 67 | + tx.sponsor = data['sponsor'] |
| 68 | + tx.sponsor_public_key = data['sponsorPublicKey'] |
| 69 | + tx.sponsor_key_type = data['sponsorKeyType'] |
| 70 | + |
| 71 | + return tx |
0 commit comments