def checksum(self, wallet_transfer_context, is_last_transfer=False, starting_balance=None, assume_active_state_exists=False): if not self.is_swap(): nonce = int(self.nonce) if self.passive: if self.position is None or is_last_transfer and self.sender_finalization_active_state is None: hash_position = 2**256 - 1 else: hash_position = self.position # nonce is hash(position || nonce) hash_values = [ crypto.uint256(int(hash_position)), crypto.uint256(int(self.nonce)), ] nonce = crypto.big_endian_to_int( crypto.hash_array(hash_values)) representation = [ keccak( crypto.address(self.recipient.address if self.wallet == wallet_transfer_context.wallet else self. wallet.address)), crypto.uint256(int(self.amount)), crypto.uint64(int(self.recipient.trail_identifier)), crypto.uint256(nonce), ] else: if starting_balance is None: starting_balance = int( wallet_transfer_context.starting_balance_in_eon( self.eon_number)) is_cancelled = self.cancelled and ( assume_active_state_exists or self.recipient_cancellation_active_state is not None) if self.recipient == wallet_transfer_context.wallet and ( self.complete or is_cancelled): starting_balance = 2**256 - 1 representation = [ keccak(crypto.address(self.wallet.token.address)), keccak(crypto.address(self.recipient.token.address)), crypto.uint64(int(self.recipient.trail_identifier)), crypto.uint256(int(self.amount)), crypto.uint256(int(self.amount_swapped)), crypto.uint256(starting_balance), crypto.uint256(int(self.nonce)), ] return crypto.hash_array(representation)
def combine_hash(left_node, right_node): representation = [ crypto.uint32(left_node.get('height')), left_node.get('hash'), right_node.get('hash') ] return crypto.hash_array(representation)
def internal_node_hash(internal_node): representation = [ crypto.uint32(internal_node.get('left_child').get('height')), internal_node.get('left_child').get('hash'), internal_node.get('right_child').get('hash') ] return crypto.hash_array(representation)
def swap_cancellation_message_checksum(self): representation = [ keccak(crypto.address(self.wallet.token.address)), keccak(crypto.address(self.recipient.token.address)), crypto.uint256(int(self.nonce)), ] return crypto.hash_array(representation)
def digest(self): return remove_0x_prefix( encode_hex( hash_array([ decode_hex(self.terms_of_service_digest), decode_hex(self.privacy_policy_digest) ])))
def save(self, *args, **kwargs): if not self.pk: result = crypto.hash_array([ crypto.address(self.token_from.address), crypto.address(self.token_to.address) ]) self.conduit = crypto.hex_address(crypto.hex_value(result)) super(TokenPair, self).save(*args, **kwargs)
def checksum(self): representation = [ keccak(crypto.address(settings.HUB_LQD_CONTRACT_ADDRESS)), keccak(crypto.address(self.wallet.token.address)), keccak(crypto.address(self.wallet.address)), crypto.uint256(self.eon_number), crypto.uint256(self.amount) ] return crypto.hash_array(representation)
def checksum(self): representation = [ keccak(crypto.address(settings.HUB_LQD_CONTRACT_ADDRESS)), keccak(crypto.address(self.wallet.token.address)), keccak(crypto.address(self.wallet.address)), crypto.uint64(self.wallet.trail_identifier if self.wallet. trail_identifier is not None else 0), crypto.uint256(self.eon_number), crypto.decode_hex(self.tx_set_hash), crypto.uint256(self.updated_spendings), crypto.uint256(self.updated_gains) ] return crypto.hash_array(representation)
def setUp(self): super().setUp() message = [ crypto.address(self.wallet.address), # wallet crypto.address(self.eth_token.address), # token crypto.uint256(10000), # amount crypto.uint256(57) # expiry ] message = crypto.hash_array(message) signature = crypto.encode_signature( crypto.sign_message(message, self.private.to_string())) self.right_sig = signature self.url = reverse("delegated-withdrawal-endpoint") self.update_registration(signature, message) self.payload = { "signature": self.right_sig, "wallet": self.wallet, "token": self.eth_token.address, "amount": 10000, "expiry": 57 }
def delegated_withdrawal(request): signature = request.POST.get("signature") wallet_address = request.POST.get("wallet") amount = int(request.POST.get("amount")) token = request.POST.get("token") expiry = int(request.POST.get("expiry")) message = [ crypto.address(wallet_address), crypto.address(token), crypto.uint256(amount), crypto.uint256(expiry) ] message = crypto.hash_array(message) v, r, s = crypto.decode_signature(signature) trust = crypto.verify_message_signature(crypto.address(wallet_address), message, (v, r, s)) if not trust: return Response(data="Signature is invalid", status=400) wallet = Wallet.objects.filter(address=remove_0x_prefix(wallet_address), token__address=token).get() nc = NOCUSTContractInterface() current_eon = nc.get_current_eon_number() wallet_view_context = WalletTransferContext(wallet, None) available_amount = wallet_view_context.loosely_available_funds_at_eon( current_eon, current_eon, False, False) if available_amount < amount: return Response(data="Not enough funds", status=400) nc.delegated_withdraw(token, wallet_address, amount, expiry, crypto.uint256(r), crypto.uint256(s), v) return Response(data="Ok", status=200)