Пример #1
0
    def _structure_from_instance(self):
        """ Override parent method to add tx special data. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'coin_to_sell': int(self.coin_to_sell),
                'value_to_sell': MinterHelper.to_pip(self.value_to_sell),
                'coin_to_buy': int(self.coin_to_buy),
                'min_value_to_buy': MinterHelper.to_pip(self.min_value_to_buy)
            }
        })

        return struct
Пример #2
0
    def estimate_coin_buy(self, coin_to_sell, value_to_buy, coin_to_buy,
                          height=None, pip2bip=False):
        """
        Return estimate of buy coin transaction
        Args:
            coin_to_sell (string): coin name to sell
            value_to_buy (string): Amount of coins to buy in PIP.
                    Provide `value_to_buy` in PIP, if `pip2bip` False.
                    Provide `value_to_buy` in BIP, if `pip2bip` True.
            coin_to_buy (string): coin name to buy
            height (int): block height
            pip2bip (bool): Convert coin amounts to BIP (default is in PIP)
        """
        # Convert `value_to_buy` to PIP, if needed
        if pip2bip:
            value_to_buy = MinterHelper.to_pip(value_to_buy)

        # Get default response
        response = self._request(
            command='estimate_coin_buy',
            params={
                'coin_to_sell': coin_to_sell,
                'value_to_buy': value_to_buy,
                'coin_to_buy': coin_to_buy,
                'height': height
            }
        )

        # Convert response values from PIP to BIP, if needed
        if pip2bip:
            return self.__response_processor(
                data=response, funcs=[self.__pip_to_bip]
            )

        return response
Пример #3
0
    def _structure_from_instance(self):
        """ Override parent method. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'name': self.name,
                'symbol': MinterHelper.encode_coin_name(self.symbol),
                'initial_amount': MinterHelper.to_pip(self.initial_amount),
                'initial_reserve': MinterHelper.to_pip(self.initial_reserve),
                'crr': '' if self.crr == 0 else self.crr,
                'max_supply': MinterHelper.to_pip(self.max_supply)
            }
        })

        return struct
Пример #4
0
    def _structure_from_instance(self):
        """ Override parent method. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'coin_to_buy': MinterHelper.encode_coin_name(self.coin_to_buy),
                'value_to_buy': MinterHelper.to_pip(self.value_to_buy),
                'coin_to_sell':
                MinterHelper.encode_coin_name(self.coin_to_sell),
                'max_value_to_sell':
                MinterHelper.to_pip(self.max_value_to_sell)
            }
        })

        return struct
Пример #5
0
    def _structure_from_instance(self):
        """ Override parent method to add tx special data. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'coin': int(self.coin),
                'to': bytes.fromhex(MinterHelper.prefix_remove(self.to)),
                'value': MinterHelper.to_pip(self.value)
            }
        })

        return struct
Пример #6
0
    def _structure_from_instance(self):
        """ Override parent method to add tx special data. """

        struct = super()._structure_from_instance()

        struct.update({'type': self.TYPE, 'data': {'txs': []}})

        # Populate multi data from each single tx.
        for item in self.txs:
            struct['data']['txs'].append([
                int(item['coin']),
                bytes.fromhex(MinterHelper.prefix_remove(item['to'])),
                MinterHelper.to_pip(item['value'])
            ])

        return struct
Пример #7
0
    def _structure_from_instance(self):
        """ Override parent method to add tx special data. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'pub_key':
                bytes.fromhex(MinterHelper.prefix_remove(self.pub_key)),
                'coin': MinterHelper.encode_coin_name(self.coin),
                'stake': MinterHelper.to_pip(self.stake)
            }
        })

        return struct
Пример #8
0
    def sign(self, private_key):
        """
        Sign check
        Args:
            private_key (str)
        """
        # Prepare structure
        # It contains nonce, chain_id, due_block, coin, value, gas_coin,
        # lock, v, r, s.
        # lock, v, r, s appended later in code
        structure = [
            int(str(self.nonce).encode().hex(), 16), self.chain_id,
            self.due_block,
            MinterHelper.encode_coin_name(self.coin),
            MinterHelper.to_pip(self.value),
            MinterHelper.encode_coin_name(self.gas_coin)
        ]

        # Create msg hash
        msg_hash = self.__hash(structure)

        # SHA256 from passphrase
        sha = hashlib.sha256()
        sha.update(self.passphrase.encode())
        passphrase = sha.hexdigest()

        # Create lock from signature
        self.lock = self.__lockfromsignature(
            signature=ECDSA.sign(message=msg_hash, private_key=passphrase))

        # Re-create msg hash with adding lock to structure
        structure.append(self.lock)
        msg_hash = self.__hash(structure)

        # Re-create signature, add it to check attrs and to structure
        signature = ECDSA.sign(message=msg_hash, private_key=private_key)
        self.signature = {
            'v': signature[0],
            'r': format(signature[1], 'x'),
            's': format(signature[2], 'x')
        }
        structure += signature

        # Get RLP, which will be the check
        check = rlp.encode(structure).hex()

        return MinterHelper.prefix_add(check, PREFIX_CHECK)
Пример #9
0
    def from_raw(cls, rawcheck):
        """
        Create check instance from raw check
        Args:
            rawcheck (str)
        Returns:
            MinterCheck
        """

        # Remove check prefix and RLP decode it
        rawcheck = MinterHelper.prefix_remove(rawcheck)
        rawcheck = bytes.fromhex(rawcheck)
        decoded = rlp.decode(rawcheck)

        # Create MinterCheck instance
        kwargs = {
            'nonce': int(decoded[0].decode()),
            'chain_id': int.from_bytes(decoded[1], 'big'),
            'due_block': int.from_bytes(decoded[2], 'big'),
            'coin': MinterHelper.decode_coin_name(decoded[3]),
            'value': MinterHelper.to_bip(int.from_bytes(decoded[4], 'big')),
            'gas_coin': MinterHelper.decode_coin_name(decoded[5]),
            'lock': decoded[6].hex(),
            'signature': {
                'v': int.from_bytes(decoded[7], 'big'),
                'r': decoded[8].hex(),
                's': decoded[9].hex()
            }
        }
        check = MinterCheck(**kwargs)

        # Recover owner address
        msg_hash = cls.__hash(data=[
            int(str(check.nonce).encode().hex(), 16), check.chain_id,
            check.due_block,
            MinterHelper.encode_coin_name(check.coin),
            MinterHelper.to_pip(check.value),
            MinterHelper.encode_coin_name(check.gas_coin),
            bytes.fromhex(check.lock)
        ])
        public_key = ECDSA.recover(msg_hash, tuple(check.signature.values()))
        public_key = MinterHelper.prefix_add(public_key, PREFIX_PUBKEY)

        check.owner = MinterWallet.get_address_from_public_key(public_key)

        return check
Пример #10
0
    def _structure_to_kwargs(cls, structure):
        """ Prepare decoded structure data to instance kwargs. """

        kwargs = super()._structure_to_kwargs(structure)

        # Convert data values to verbose.
        # Data will be passed as additional kwarg
        for index, item in enumerate(kwargs['data']['txs']):
            kwargs['data']['txs'][index] = {
                'coin': MinterHelper.decode_coin_name(item[0]),
                'to': MinterHelper.prefix_add(item[1].hex(), PREFIX_ADDR),
                'value': MinterHelper.to_pip(int.from_bytes(item[2], 'big'))
            }

        # Populate data key values as kwargs
        kwargs.update(kwargs['data'])

        return kwargs
Пример #11
0
    def _structure_from_instance(self):
        """ Override parent method. """

        struct = super()._structure_from_instance()

        struct.update({
            'type': self.TYPE,
            'data': {
                'address':
                bytes.fromhex(MinterHelper.prefix_remove(self.address)),
                'pub_key':
                bytes.fromhex(MinterHelper.prefix_remove(self.pub_key)),
                'commission': '' if self.commission == 0 else self.commission,
                'coin': int(self.coin),
                'stake': MinterHelper.to_pip(self.stake)
            }
        })

        return struct
Пример #12
0
def to_pip(value):
    return MinterHelper.to_pip(value)