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 kwargs['data'].update({ 'name': kwargs['data']['name'].decode(), 'symbol': MinterConvertor.decode_coin_name(kwargs['data']['symbol']), 'initial_amount': MinterConvertor.convert_value( value=MinterHelper.bin2int(kwargs['data']['initial_amount']), to='bip' ), 'initial_reserve': MinterConvertor.convert_value( value=MinterHelper.bin2int(kwargs['data']['initial_reserve']), to='bip' ), 'crr': MinterHelper.bin2int(kwargs['data']['crr']) }) # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
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': MinterConvertor.decode_coin_name(item[0]), 'to': MinterPrefix.ADDRESS + MinterHelper.bin2hex(item[1]), 'value': MinterConvertor.convert_value(value=MinterHelper.bin2int( item[2]), to='bip') } # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
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': MinterConvertor.encode_coin_name(self.coin_to_sell), 'value_to_sell': MinterConvertor.convert_value(value=self.value_to_sell, to='pip'), 'coin_to_buy': MinterConvertor.encode_coin_name(self.coin_to_buy), 'min_value_to_buy': MinterConvertor.convert_value(value=self.min_value_to_buy, to='pip') }, 'signature_type': self.SIGNATURE_SINGLE_TYPE }) return struct
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 kwargs['data'].update({ 'coin_to_sell': MinterConvertor.decode_coin_name(kwargs['data']['coin_to_sell']), 'value_to_sell': MinterConvertor.convert_value(value=MinterHelper.bin2int( kwargs['data']['value_to_sell']), to='bip'), 'coin_to_buy': MinterConvertor.decode_coin_name(kwargs['data']['coin_to_buy']), 'min_value_to_buy': MinterConvertor.convert_value(value=MinterHelper.bin2int( kwargs['data']['min_value_to_buy']), to='bip') }) # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
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 kwargs['data'].update({ 'address': MinterPrefix.ADDRESS + MinterHelper.bin2hex(kwargs['data']['address']), 'pub_key': MinterPrefix.PUBLIC_KEY + MinterHelper.bin2hex(kwargs['data']['pub_key']), 'comission': MinterHelper.bin2int(kwargs['data']['comission']), 'coin': MinterConvertor.decode_coin_name(kwargs['data']['coin']), 'stake': MinterConvertor.convert_value( MinterHelper.bin2int(kwargs['data']['stake']), 'bip') }) # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
def _structure_from_instance(self): """ Override parent method. """ struct = super()._structure_from_instance() struct.update({ 'type': self.TYPE, 'data': { 'address': MinterHelper.hex2bin( MinterPrefix.remove_prefix(string=self.address, prefix=MinterPrefix.ADDRESS)), 'pub_key': MinterHelper.hex2bin( MinterPrefix.remove_prefix( string=self.pub_key, prefix=MinterPrefix.PUBLIC_KEY)), 'comission': '' if self.comission == 0 else self.comission, 'coin': MinterConvertor.encode_coin_name(self.coin), 'stake': MinterConvertor.convert_value(value=self.stake, to='pip') }, 'signature_type': self.SIGNATURE_SINGLE_TYPE }) return struct
def _structure_from_instance(self): """ Override parent method. """ struct = super()._structure_from_instance() struct.update({ 'type': self.TYPE, 'data': { 'name': self.name, 'symbol': MinterConvertor.encode_coin_name(self.symbol), 'initial_amount': MinterConvertor.convert_value(value=self.initial_amount, to='pip'), 'initial_reserve': MinterConvertor.convert_value(value=self.initial_reserve, to='pip'), 'crr': '' if self.crr == 0 else self.crr }, 'signature_type': self.SIGNATURE_SINGLE_TYPE }) return struct
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': [] }, 'signature_type': self.SIGNATURE_SINGLE_TYPE }) # Populate multi data from each single tx. for item in self.txs: struct['data']['txs'].append([ MinterConvertor.encode_coin_name(item['coin']), MinterHelper.hex2bin( MinterPrefix.remove_prefix(string=item['to'], prefix=MinterPrefix.ADDRESS)), MinterConvertor.convert_value(value=item['value'], to='pip') ]) return struct
def sign(self, private_key): """ Sign check Args: private_key (str) """ if not self.passphrase: raise ValueError('Passphrase should be not empty string') # Prepare structure # It contains nonce, chain_id, due_block, coin, value, lock, v, r, s # lock, v, r, s appended later in code structure = [ int(binascii.hexlify(str(self.nonce).encode()), 16), self.chain_id, self.due_block, MinterConvertor.encode_coin_name(self.coin), MinterConvertor.convert_value(value=self.value, to='pip') ] # 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 = binascii.hexlify(rlp.encode(structure)) return MinterPrefix.CHECK + check.decode()
def from_raw(cls, rawcheck): """ Create check instance from raw check Args: rawcheck (str) Returns: MinterCheck """ # Remove check prefix and RLP decode it rawcheck = MinterPrefix.remove_prefix( string=rawcheck, prefix=MinterPrefix.CHECK ) rawcheck = binascii.unhexlify(rawcheck) decoded = rlp.decode(rawcheck) # Create MinterCheck instance kwargs = { 'nonce': int(decoded[0].decode()), 'chain_id': MinterHelper.bin2int(decoded[1]), 'due_block': MinterHelper.bin2int(decoded[2]), 'coin': MinterConvertor.decode_coin_name(decoded[3]), 'value': MinterConvertor.convert_value( value=MinterHelper.bin2int(decoded[4]), to='bip' ), 'lock': binascii.hexlify(decoded[5]).decode(), 'signature': { 'v': MinterHelper.bin2int(decoded[6]), 'r': MinterHelper.bin2hex(decoded[7]), 's': MinterHelper.bin2hex(decoded[8]) } } check = MinterCheck(**kwargs) # Recover owner address msg_hash = cls.__hash(data=[ int(binascii.hexlify(str(check.nonce).encode()), 16), check.chain_id, check.due_block, MinterConvertor.encode_coin_name(check.coin), MinterConvertor.convert_value(value=check.value, to='pip'), MinterHelper.hex2bin(check.lock) ]) public_key = ECDSA.recover(msg_hash, list(check.signature.values())) public_key = MinterPrefix.PUBLIC_KEY + public_key check.owner = MinterWallet.get_address_from_public_key(public_key) return check
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': MinterHelper.hex2bin(MinterPrefix.remove_prefix(self.pub_key, MinterPrefix.PUBLIC_KEY)), 'coin': MinterConvertor.encode_coin_name(self.coin), 'stake': MinterConvertor.convert_value(value=self.stake, to='pip') } }) return struct
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': MinterConvertor.encode_coin_name(self.coin), 'to': MinterHelper.hex2bin(MinterPrefix.remove_prefix(string=self.to, prefix=MinterPrefix.ADDRESS)), 'value': MinterConvertor.convert_value(value=self.value, to='pip') } }) return struct
def generate(self, password=None): """ Generate deeplink Args: password (str): Check password Returns: deeplink (str) """ # Create deeplink structure gas_coin = MinterConvertor.encode_coin_name(self.gas_coin) if self.gas_coin else '' deep_structure = [self.__type, self.__data, self.payload, self.nonce, self.gas_price, gas_coin] # Create deeplink hash (`d` URL param) deephash = rlp.encode(deep_structure) deephash = binascii.hexlify(deephash).decode() # Create deeplink URL deeplink = self.base_url + '?d=' + deephash # If password check needed, add (`p` URL param) if password: password = binascii.hexlify(rlp.encode(password)).decode() deeplink += '&p=' + password return deeplink
def _structure_from_instance(self): """ Override parent method. """ struct = super()._structure_from_instance() struct.update({ 'type': self.TYPE, 'data': { 'coin_to_buy': MinterConvertor.encode_coin_name(self.coin_to_buy), 'value_to_buy': MinterConvertor.convert_value(value=self.value_to_buy, to='pip'), 'coin_to_sell': MinterConvertor.encode_coin_name(self.coin_to_sell), 'max_value_to_sell': MinterConvertor.convert_value(value=self.max_value_to_sell, to='pip') } }) return struct
def _structure_to_kwargs(cls, structure): """ Works with already populated structure and prepare **kwargs for creating new instance of tx. """ structure.update({ 'gas_coin': MinterConvertor.decode_coin_name(structure['gas_coin']) }) return structure
def check_balance(user_or_wallet): w = Wallet.get(user=user_or_wallet) if isinstance(user_or_wallet, Users) \ else user_or_wallet if isinstance(user_or_wallet, Wallet) \ else Wallet.get(address=user_or_wallet) if not w: return r = api.get_balance(w.address) balance = float( MinterConvertor.convert_value(r['result']['balance']['BIP'], 'bip')) w.balance = balance w.updated_dt = datetime.utcnow() w.save() return balance
def _structure_from_instance(self): """ Populating structure dict by instance values. Prepare values for signing process. """ struct = copy.copy(self._STRUCTURE_DICT) struct.update({ 'nonce': self.nonce, 'chain_id': self.chain_id, 'gas_price': self.gas_price, 'gas_coin': MinterConvertor.encode_coin_name(self.gas_coin), 'payload': self.payload, 'service_data': self.service_data, 'signature_type': self.signature_type }) return struct
def to_pip(bip): return MinterConvertor.convert_value(bip, 'pip')
def to_bip(pip): return MinterConvertor.convert_value(pip, 'bip')