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_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({ 'pub_key': MinterPrefix.PUBLIC_KEY + MinterHelper.bin2hex(kwargs['data']['pub_key']), 'reward_address': MinterPrefix.ADDRESS + MinterHelper.bin2hex(kwargs['data']['reward_address']), 'owner_address': MinterPrefix.ADDRESS + MinterHelper.bin2hex(kwargs['data']['owner_address']) }) # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
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_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({ 'check': MinterPrefix.CHECK + MinterHelper.bin2hex(kwargs['data']['check']), 'proof': MinterHelper.bin2hex(kwargs['data']['proof']) }) # 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_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': MinterConvertor.decode_coin_name(kwargs['data']['coin']), 'to': MinterPrefix.ADDRESS + MinterHelper.bin2hex(kwargs['data']['to']), 'value': MinterConvertor.convert_value(value=MinterHelper.bin2int(kwargs['data']['value']), to='bip') }) # Populate data key values as kwargs kwargs.update(kwargs['data']) return kwargs
def from_raw(cls, raw_tx): """ Generate tx object from raw tx Args: raw_tx (string) Returns: MinterTx child instance """ tx = rlp.decode(binascii.unhexlify(raw_tx)) # Populate structure dict with decoded tx data struct = copy.copy(cls._STRUCTURE_DICT) struct.update({ 'nonce': int(binascii.hexlify(tx[0]), 16), 'chain_id': int(binascii.hexlify(tx[1]), 16), 'gas_price': int(binascii.hexlify(tx[2]), 16), 'gas_coin': tx[3].decode(), 'type': int(binascii.hexlify(tx[4]), 16), 'payload': tx[6].decode().replace(chr(0), ''), 'service_data': tx[7].decode().replace(chr(0), ''), 'signature_type': int(binascii.hexlify(tx[8]), 16) }) # Get signature data signature_data = rlp.decode(tx[9]) if struct['signature_type'] == cls.SIGNATURE_SINGLE_TYPE: signature_data = { 'v': int(binascii.hexlify(signature_data[0]), 16), 'r': binascii.hexlify(signature_data[1]).decode(), 's': binascii.hexlify(signature_data[2]).decode() } else: # Decode signatures signatures = [] for signature in signature_data[1]: signatures.append({ 'v': int(binascii.hexlify(signature[0]), 16), 'r': binascii.hexlify(signature[1]).decode(), 's': binascii.hexlify(signature[2]).decode() }) # Create decoded signature data signature_data = { 'from_mx': MinterPrefix.ADDRESS + MinterHelper.bin2hex(signature_data[0]), 'signatures': signatures } struct['signature_data'] = signature_data # Find out which of tx instance need to create depending on it's type data = rlp.decode(tx[5]) if struct['type'] == MinterDelegateTx.TYPE: _class = MinterDelegateTx elif struct['type'] == MinterSendCoinTx.TYPE: _class = MinterSendCoinTx elif struct['type'] == MinterBuyCoinTx.TYPE: _class = MinterBuyCoinTx elif struct['type'] == MinterCreateCoinTx.TYPE: _class = MinterCreateCoinTx elif struct['type'] == MinterDeclareCandidacyTx.TYPE: _class = MinterDeclareCandidacyTx elif struct['type'] == MinterRedeemCheckTx.TYPE: _class = MinterRedeemCheckTx elif struct['type'] == MinterSellAllCoinTx.TYPE: _class = MinterSellAllCoinTx elif struct['type'] == MinterSellCoinTx.TYPE: _class = MinterSellCoinTx elif struct['type'] == MinterSetCandidateOffTx.TYPE: _class = MinterSetCandidateOffTx elif struct['type'] == MinterSetCandidateOnTx.TYPE: _class = MinterSetCandidateOnTx elif struct['type'] == MinterUnbondTx.TYPE: _class = MinterUnbondTx elif struct['type'] == MinterEditCandidateTx.TYPE: _class = MinterEditCandidateTx elif struct['type'] == MinterMultiSendCoinTx.TYPE: _class = MinterMultiSendCoinTx else: raise Exception('Undefined tx type.') # Set tx data struct['data'] = _class._data_from_raw(data) # Set sender address and raw tx to minter dict ONLY AFTER tx data was set struct.update({ 'from_mx': cls.get_sender_address(tx=copy.copy(struct)), 'signed_tx': raw_tx }) # Prepare **kwargs for creating _class instance. # Pass copy of the struct. kwargs = _class._structure_to_kwargs(copy.copy(struct)) return _class(**kwargs)