示例#1
0
    def _generate_cancel_lease_transaction(self, transaction_id, transaction_fee, timestamp):
        """
        Prepare data for cancel lease transaction

        :param transaction_id:
        :param transaction_fee:
        :param timestamp:
        :return:
        """

        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        sign_data = [
            TRANSACTION_TYPE_CANCEL_LEASE.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param),
            base58.b58decode(transaction_id),
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        transaction_data = {
            "senderPublicKey": self.public_key,
            "txId": transaction_id,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "signature": signature
        }

        return transaction_data
示例#2
0
    def _generate_asset_burn_transaction(self, asset_id, quantity, transaction_fee, timestamp):
        """
        Prepare asset reissue transaction data

        :param asset_id:
        :param quantity:
        :param reissuable:
        :param timestamp:
        :return:
        """
        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        sign_data = [
            TRANSACTION_TYPE_BURN.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            base58.b58decode(asset_id),
            struct.pack(">Q", quantity),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param)
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        transaction_data = {
            "senderPublicKey": self.public_key,
            "assetId": asset_id,
            "quantity": quantity,
            "timestamp": timestamp_param,
            "fee": transaction_fee,
            "signature": signature
        }
        return transaction_data
示例#3
0
    def _generate_alias_transaction(self, alias, transaction_fee, timestamp):
        """
        Prepare data for alias transaction

        :return:
        """
        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        alias_data = b''.join((
            b'\x02', self.chain_id.encode('latin-1'), struct.pack(">H", len(alias)), alias.encode('latin-1')
        ))
        sign_data = [
            TRANSACTION_TYPE_ALIAS.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            struct.pack(">H", len(alias_data)),
            alias_data,
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param),
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))

        transaction_data = {
            "senderPublicKey": self.public_key,
            "alias": alias,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "signature": signature,
        }

        return transaction_data
示例#4
0
    def _generate_set_asset_script_transaction(self, script, asset_id, transaction_fee, timestamp, version):
        """
        Generate set script transaction data

        :param asset_id:
        :param script:
        :param transaction_fee:
        :return:
        """
        if not timestamp:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        script_bytes = base64.b64decode(script)
        sign_data = [
            TRANSACTION_TYPE_SET_ASSET_SCRIPT.to_bytes(1, 'big'),
            version.to_bytes(1, 'big'),
            self.chain_id.encode('latin-1'),
            base58.b58decode(self.public_key),
            base58.b58decode(asset_id),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param),
            b'\1',
            struct.pack(">H", len(script_bytes)),
            script_bytes,
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        transaction_data = {
            "type": TRANSACTION_TYPE_SET_ASSET_SCRIPT,
            "version": version,
            "assetId": asset_id,
            "senderPublicKey": self.public_key,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "script": 'base64:' + script,
            "proofs": [signature]
        }

        return transaction_data
示例#5
0
    def _generate_lease_transaction(self, recipient, amount, transaction_fee, timestamp):
        """
        Prepare data for lease transaction

        :param recipient:
        :param amount:
        :param transaction_fee:
        :param timestamp:
        :return:
        """
        if isinstance(recipient, WavesAddress):
            recipient_address = recipient.value
        else:
            recipient_address = recipient

        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        sign_data = [
            TRANSACTION_TYPE_LEASE.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            base58.b58decode(recipient_address),
            struct.pack(">Q", amount),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param),
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        transaction_data = {
            "senderPublicKey": self.public_key,
            "recipient": recipient_address,
            "amount": amount,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "signature": signature
        }

        return transaction_data
示例#6
0
    def _generate_asset_sponsorship_transaction(self, asset_id, min_sponsored_asset_fee, transaction_fee, timestamp):
        """
        Prepare data for sponsorship transaction

        :param asset_id:
        :param min_sponsored_asset_fee:
        :param transaction_fee:
        :param timestamp:
        :return:
        """
        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        version = 1
        sign_data = [
            TRANSACTION_TYPE_SPONSORSHIP.to_bytes(1, 'big'),
            version.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            base58.b58decode(asset_id),
            struct.pack(">Q", min_sponsored_asset_fee),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param),
        ]
        signature = sign_with_private_key(self.private_key, b''.join(sign_data))

        transaction_data = {
            "type": TRANSACTION_TYPE_SPONSORSHIP,
            "version": version,
            "senderPublicKey": self.public_key,
            "assetId": asset_id,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "minSponsoredAssetFee": min_sponsored_asset_fee,
            "proofs": [signature]
        }

        return transaction_data
示例#7
0
    def _generate_mass_transfer_transaction(self, transfer_data, asset_id, attachment, version, timestamp):
        """
        Setup mass transfer transaction data

        :param transfer_data:
        :param attachment:
        :param timestamp:
        :return:
        """
        if not timestamp:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        if not attachment:
            encoded_attachment = b''
        else:
            encoded_attachment = attachment.encode('latin-1')

        mass_fee = 100000 + math.ceil(0.5 * len(transfer_data)) * 100000

        recipients_sign_data = []
        for recipient_data in transfer_data:
            recipients_sign_data.append(base58.b58decode(recipient_data['recipient']))
            recipients_sign_data.append(struct.pack(">Q", recipient_data['amount']))

        sign_data = [
            TRANSACTION_TYPE_MASS_TRANSFER.to_bytes(1, 'big'),
            version.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            b'\0',  # for default asset (Waves)
            struct.pack(">H", len(transfer_data)),
            b''.join(recipients_sign_data),
            struct.pack(">Q", timestamp_param),
            struct.pack(">Q", mass_fee),
            struct.pack(">H", len(encoded_attachment)),
            encoded_attachment
        ]

        if asset_id:
            sign_data[3] = b'\1'
            sign_data.insert(4, base58.b58decode(asset_id))

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))

        transaction_data = {
            "type": TRANSACTION_TYPE_MASS_TRANSFER,
            "version": 1,
            "assetId": asset_id or "",
            "senderPublicKey": self.public_key,
            "fee": mass_fee,
            "timestamp": timestamp_param,
            "transfers": transfer_data,
            "attachment": base58.b58encode(encoded_attachment).decode(),
            "signature": signature,
            "proofs": [
                signature
            ]
        }

        return transaction_data
示例#8
0
    def _generate_data_transaction(self, data, fee=0, version=1, timestamp=0):
        """
        Prepare data for data transaction

        :return:
        """
        if not timestamp:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        data_buffer = []
        for item in data:
            key_encoded = item['key'].encode('latin-1')
            data_buffer.extend([struct.pack(">H", len(key_encoded)), key_encoded])
            if item['type'] == 'integer':
                item_value = [
                    DATA_TRANSACTION_INT_TYPE.to_bytes(1, 'big'),
                    struct.pack(">Q", item['value'])
                ]
            elif item['type'] == 'boolean':
                item_value = [
                    DATA_TRANSACTION_BOOLEAN_TYPE.to_bytes(1, 'big'),
                    item['value'].to_bytes(1, 'big')
                ]
            elif item['type'] == 'binary':
                item_value = [
                    DATA_TRANSACTION_STRING_TYPE.to_bytes(1, 'big'),
                    struct.pack(">H", len(item['value'])),
                    item['value']
                ]
                item['value'] = "base64:" + base64.b64encode(item['value']).decode('latin-1')

            elif item['type'] == 'string':
                item_value = [
                    DATA_TRANSACTION_BINARY_TYPE.to_bytes(1, 'big'),
                    struct.pack(">H", len(item['value'])),
                    item['value'].encode('latin-1')
                ]
            else:
                raise ValueError("Unknown data type: '{}'".format(item['type']))

            data_buffer.extend(item_value)

        if not fee:
            transaction_fee = int(math.floor(1 + (len(json.dumps(data)) + 8 - 1) / 1024) *
                                  DEFAULT_DATA_TRANSACTION_FEE_MULTIPLICATOR)
        else:
            transaction_fee = fee
        sign_data = [
            TRANSACTION_TYPE_DATA.to_bytes(1, 'big'),
            version.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            struct.pack(">H", len(data)),
            b''.join(data_buffer),
            struct.pack(">Q", timestamp_param),
            struct.pack(">Q", transaction_fee),
        ]
        transaction_data = {
            "type": TRANSACTION_TYPE_DATA,
            "version": version,
            "senderPublicKey": self.public_key,
            "data": data,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "proofs": [sign_with_private_key(self.private_key, b''.join(sign_data))]
        }
        return transaction_data
示例#9
0
    def _generate_asset_issue_transaction(self, name, description, quantity, decimals, reissuable, transaction_fee,
                                          script, version, timestamp):
        """
        Prepare asset issue transaction data

        :param name:
        :param description:
        :param quantity:
        :param decimals:
        :param reissuable:
        :param version
        :param timestamp:
        :return:
        """
        if not timestamp:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        sign_data = [
            TRANSACTION_TYPE_ISSUE.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            struct.pack(">H", len(name)),
            name.encode('latin-1'),
            struct.pack(">H", len(description)),
            description.encode('latin-1'),
            struct.pack(">Q", quantity),
            struct.pack(">B", decimals),
            reissuable.to_bytes(1, 'big'),
            struct.pack(">Q", transaction_fee),
            struct.pack(">Q", timestamp_param)
        ]

        transaction_data = {
            "senderPublicKey": self.public_key,
            "name": name,
            "quantity": quantity,
            "timestamp": timestamp_param,
            "description": description,
            "decimals": decimals,
            "reissuable": reissuable,
            "fee": transaction_fee,
        }

        if version > 1:
            sign_data.insert(1, version.to_bytes(1, 'big'))
            sign_data.insert(2, self.chain_id.encode('latin-1'))
            transaction_data.update({
                "type": TRANSACTION_TYPE_ISSUE,
                "senderPublicKey": self.public_key,
                "version": version,
                "chainId": self.chain_id
            })

        if script:
            if version < 2:
                raise ValueError("Smart assets require at least 2 transaction version")

            compiled_script = base64.b64decode(script)
            sign_data.extend([
                b'\1',  # smart asset
                struct.pack(">H", len(compiled_script)),
                compiled_script
            ])
            transaction_data["script"] = 'base64:' + script
        else:
            if version >= 2:
                sign_data.append(b'\0')

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        if version == 1:
            transaction_data["signature"] = signature
        else:
            transaction_data["proofs"] = [signature]

        return transaction_data
示例#10
0
    def _generate_transfer_transaction(self, recipient, asset_id, fee_asset_id, amount, attachment, transaction_fee,
                                       timestamp):
        """
        Prepare data for transfer transaction

        :param recipient:
        :param amount:
        :param attachment:
        :param transaction_fee:
        :param timestamp:
        :return:
        """
        if isinstance(recipient, WavesAddress):
            recipient_address = recipient.value
        else:
            recipient_address = recipient

        if timestamp == 0:
            timestamp_param = int(time.time() * 1000)
        else:
            timestamp_param = timestamp

        if not attachment:
            encoded_attachment = b''
        else:
            encoded_attachment = attachment.encode('latin-1')

        if asset_id:
            asset_sign_data = b'\1%s' % base58.b58decode(asset_id)
        else:
            asset_sign_data = b'\0'

        if fee_asset_id:
            fee_asset_sign_data = b'\1%s' % base58.b58decode(fee_asset_id)
        else:
            fee_asset_sign_data = b'\0'

        sign_data = [
            TRANSACTION_TYPE_TRANSFER.to_bytes(1, 'big'),
            base58.b58decode(self.public_key),
            asset_sign_data,
            fee_asset_sign_data,
            struct.pack(">Q", timestamp_param),
            struct.pack(">Q", amount),
            struct.pack(">Q", transaction_fee),
            base58.b58decode(recipient_address),
            struct.pack(">H", len(b'' or encoded_attachment)),
            encoded_attachment
        ]

        signature = sign_with_private_key(self.private_key, b''.join(sign_data))
        transaction_data = {
            "senderPublicKey": self.public_key,
            "recipient": recipient_address,
            "amount": amount,
            "fee": transaction_fee,
            "timestamp": timestamp_param,
            "attachment": base58.b58encode(encoded_attachment).decode(),
            "signature": signature,
        }
        if asset_id:
            transaction_data["assetId"] = asset_id

        if fee_asset_id:
            transaction_data["feeAssetId"] = fee_asset_id

        return transaction_data