class IssueToken(BaseModel): from lunespy.utils import now from requests import Response description: str = Field( ..., description="description with size smaller than 1000 char", ) reissuable: bool = Field(..., description="token will be reissued") quantity: int = Field(..., description="total supply of token") decimals: int = Field(..., description="token fractionability") senderPublicKey: str = Field(..., description="public_key") name: str = Field(..., description="name of token") timestamp: int = Field(now(), ge=1483228800) message: str = Field("", exclude=True) fee: int = Field(100000000, ge=100000000) type: int = Field(3, const=True) signature: str = Field("") def sign(cls, private_key: str): from lunespy.tx.issue.utils import sign_issue cls.signature = sign_issue(private_key, cls) return cls def broadcast(cls, node: str = None) -> Response: from lunespy.utils import broadcast_tx return broadcast_tx( cls.dict(), node if not node == None else "https://lunesnode-testnet.lunes.io")
def mount_burn(sender: Account, burn_data: dict) -> dict: from lunespy.utils import now from lunespy.client.transactions.constants import BurnType from lunespy.utils.crypto.converters import sign from base58 import b58decode import struct timestamp: int = burn_data.get('timestamp', now()) fee: int = burn_data.get('fee', BurnType.fee.value) quantity: int = burn_data.get('quantity', 0) asset_id: int = burn_data['asset_id'] bytes_data: bytes = BurnType.to_byte.value + \ b58decode(sender.public_key) + \ b58decode(asset_id) + \ struct.pack(">Q", quantity) + \ struct.pack(">Q", fee) + \ struct.pack(">Q", timestamp) signature: bytes = sign(sender.private_key, bytes_data) mount_tx = { "type": BurnType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "assetId": asset_id, "quantity": quantity } return mount_tx
def mount_lease(sender: Account, lease_data: dict) -> dict: from lunespy.client.transactions.constants import LeaseType from lunespy.utils.crypto.converters import sign from lunespy.utils import now, lunes_to_unes from base58 import b58decode import struct node_address: str = lease_data['node_address'] timestamp: int = lease_data.get('timestamp', now()) amount: int = lunes_to_unes(lease_data['amount']) fee: int = lease_data.get('fee', LeaseType.fee.value) bytes_data: bytes = LeaseType.to_byte.value + \ b58decode(sender.public_key) + \ b58decode(node_address) + \ struct.pack(">Q", amount) + \ struct.pack(">Q", fee) + \ struct.pack(">Q", timestamp) signature: bytes = sign(sender.private_key, bytes_data) mount_tx: dict = { "type": LeaseType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "recipient": node_address, "amount": amount } return mount_tx
def mount_cancel(sender: Account, cancel_data: dict) -> dict: from lunespy.client.transactions.constants import CancelLeaseType from lunespy.utils.crypto.converters import sign from lunespy.utils import now from base58 import b58decode import struct lease_tx_id: str = cancel_data['lease_tx_id'] timestamp: int = cancel_data.get('timestamp', now()) fee: int = cancel_data.get('fee', CancelLeaseType.fee.value) bytes_data: bytes = CancelLeaseType.to_byte.value + \ b58decode(sender.public_key) + \ struct.pack(">Q", fee) + \ struct.pack(">Q", timestamp) + \ b58decode(lease_tx_id) signature: bytes = sign(sender.private_key, bytes_data) mount_tx: dict = { "type": CancelLeaseType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "leaseId": lease_tx_id } return mount_tx
def mount_reissue(sender: Account, reissue_data: dict) -> dict: from lunespy.client.transactions.constants import ReissueType from lunespy.utils.crypto.converters import sign from lunespy.utils import now from base58 import b58decode import struct fee = reissue_data.get('fee', ReissueType.fee.value) quantity = reissue_data['quantity'] reissuable = reissue_data.get('reissuable', False) timestamp = reissue_data.get('timestamp', now()) asset_id = reissue_data['asset_id'] bytes_data = ReissueType.to_byte.value + \ b58decode(sender.public_key) + \ b58decode(asset_id) + \ struct.pack(">Q", quantity) + \ (b'\1' if reissuable else b'\0') + \ struct.pack(">Q",fee) + \ struct.pack(">Q", timestamp) signature: bytes = sign(sender.private_key, bytes_data) mount_tx = { "type": ReissueType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "assetId": asset_id, "reissuable": reissuable, "quantity": quantity } return mount_tx
class SponsorToken(BaseModel): from lunespy.utils import now from requests import Response minSponsoredAssetFee: int = Field(..., description="minimal fee") senderPublicKey: str = Field(..., description="public_key") assetId: str = Field(..., description="token id | NFT id") timestamp: int = Field(now(), ge=1483228800) message: str = Field("", exclude=True) fee: int = Field(1000000, ge=1000000) type: int = Field(14, const=True) version: int = Field(1, const=True) proofs: str = Field([""]) def sign(cls, private_key: str): from lunespy.tx.sponsor.utils import sign_sponsor cls.proofs[0] = sign_sponsor(private_key, cls) return cls def broadcast(cls, node: str = None) -> Response: from lunespy.utils import broadcast_tx return broadcast_tx( cls.dict(), node if not node == None else "https://lunesnode-testnet.lunes.io")
def mount_mass_transfer(sender: Account, receivers_list: list, mass_transfer_data: dict) -> dict: from lunespy.client.transactions.constants import TransferType from lunespy.client.transactions.constants import MassType from lunespy.utils.crypto.converters import sign from lunespy.utils import lunes_to_unes from lunespy.utils import now from base58 import b58decode import struct fee: int = TransferType.fee.value + len( receivers_list) * mass_transfer_data.get('fee', MassType.fee.value) timestamp: int = mass_transfer_data.get('timestamp', now()) asset_id: str = mass_transfer_data.get('asset_id', "") receivers_list: list = [{ 'receiver': tx['receiver'], 'amount': lunes_to_unes(tx['amount']) } for tx in receivers_list] hash_transfer: bytes = lambda tx: b58decode(tx['receiver']) + struct.pack( ">Q", tx['amount']) receivers_list_data = b''.join(map(hash_transfer, receivers_list)) bytes_data: bytes = MassType.to_byte.value + \ b'\1' + \ b58decode(sender.public_key) + \ (b'\1' + b58decode(asset_id) if asset_id != "" else b'\0') + \ struct.pack(">H", len(receivers_list)) + \ receivers_list_data + \ struct.pack(">Q", timestamp) + \ struct.pack(">Q", fee) signature: bytes = sign(sender.private_key, bytes_data) data = { "type": MassType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "version": 1, "assetId": asset_id, "transfers": [{ 'recipient': tx['receiver'], 'amount': tx['amount'] } for tx in receivers_list], "proofs": [signature.decode()] } return data
def mount_alias(sender: Account, alias_data: dict) -> dict: from lunespy.utils import now from lunespy.utils.crypto.converters import sign from lunespy.client.transactions.constants import AliasType from lunespy.utils.crypto.converters import string_to_bytes from base58 import b58decode import struct timestamp: int = alias_data.get('timestamp', now()) fee: int = alias_data.get('fee', AliasType.fee.value) alias: str = alias_data['alias'] chain_id: str = sender.chain_id aliasWithNetwork = AliasType.mount.value +\ string_to_bytes(str(chain_id)) + \ struct.pack(">H", len(alias)) + \ string_to_bytes(alias) bytes_data = AliasType.to_byte.value + \ b58decode(sender.public_key) + \ struct.pack(">H", len(aliasWithNetwork)) + \ aliasWithNetwork + \ struct.pack(">Q", fee) + \ struct.pack(">Q", timestamp) signature: bytes = sign(sender.private_key, bytes_data) mount_tx = { "type": AliasType.to_int.value, "senderPublicKey": sender.public_key, "signature": signature.decode(), "timestamp": timestamp, "fee": fee, "alias": alias } return mount_tx
class TransferToken(BaseModel): from lunespy.utils import now from requests import Response senderPublicKey: str = Field(..., description="public_key") assetId: str = Field("", description="token id | NFT id") sender: str = Field("", description="address of sender") recipient: str = Field(..., description="address") timestamp: int = Field(now(), ge=1483228800) message: str = Field("", exclude=True) fee: int = Field(1000000, ge=1000000) type: int = Field(4, const=True) amount: int = Field(..., gt=0) signature: str = Field("") feeAsset: str = Field("") @validator("recipient") def same_chain(cls, recipient, values): from lunespy.crypto import same_chain_address if not same_chain_address(recipient, values['sender']): raise ValueError( f"Different chain addresses ({recipient}, {values['sender']})") return recipient def sign(cls, private_key: str): from lunespy.tx.transfer.utils import sign_transfer cls.signature = sign_transfer(private_key, cls) return cls def broadcast(cls, node: str = None) -> Response: from lunespy.utils import broadcast_tx return broadcast_tx( cls.dict(), node if not node == None else "https://lunesnode-testnet.lunes.io")