def get_context_data(self, **kwargs): context = super(AcceptPreviewView, self).get_context_data(**kwargs) view_obj = context.get("view") memo_serialized = view_obj.kwargs["memo"] memo = validators.validate_memo( json_util.deserialize_memo(memo_serialized)) signature = kwargs.get("signature") if signature: signature = validators.pre_validate_signature(signature) result = ln.verifymessage(memo=json.dumps(memo, sort_keys=True), sig=signature) if result["valid"]: identity_pubkey = result["identity_pubkey"] context['user'] = User(id=1, pubkey=identity_pubkey) memo["sig"] = signature # add signature to memo context['post'] = self.get_model(memo) # re-serialized because signature was added context['memo'] = json_util.serialize_memo(memo) # gives user a friendy error message if no LN nodes are avaialble context["nodes_list"] = [n["node_name"] for n in ln.get_nodes_list()] return context
def serialize_memo(self): assert self.date.tzinfo == utc, "date must be in UTC" if self.is_toplevel: obj = dict( title=validators.validate_signable_field(self.title), post_type=self.type, tag_val=self.tag_val, content=validators.validate_signable_field(self.content), unixtime=int((self.date - datetime.datetime( 1970, 1, 1).replace(tzinfo=utc)).total_seconds()), amt=settings.POST_PAYMENT_AMOUNT, ) else: obj = dict( parent_post_id=self.parent_post_id, post_type=self.type, content=validators.validate_signable_field(self.content), unixtime=int((self.date - datetime.datetime( 1970, 1, 1).replace(tzinfo=utc)).total_seconds()), amt=settings.POST_PAYMENT_AMOUNT, ) memo = json_util.serialize_memo(obj) return memo
def get_context_data(self, **kwargs): context = super(PostPreviewView, self).get_context_data(**kwargs) view_obj = context.get("view") memo_serialized = view_obj.kwargs["memo"] memo = validators.validate_memo( json_util.deserialize_memo(memo_serialized)) signature = kwargs.get("signature") if signature: signature = validators.pre_validate_signature(signature) result = ln.verifymessage(memo=json.dumps(memo, sort_keys=True), sig=signature) if result["valid"]: identity_pubkey = result["identity_pubkey"] context['user'] = User(id=1, pubkey=identity_pubkey) memo["sig"] = signature # add signature to memo if "sig" not in memo: context['user'] = User(id=1, pubkey="Unknown") # re-serialized because signature was added memo_serialized = json_util.serialize_memo(memo) post_preview = self.get_model(memo) context["memo"] = memo_serialized context["post"] = post_preview context["publish_url"] = post_preview.get_publish_url( memo=memo_serialized) return context
def get_accept_url(self): obj = dict( action="Accept", post_id=self.id, unixtime=int(time.time()), amt=1, ) memo = json_util.serialize_memo(obj) url = reverse("accept-preview", kwargs=dict(memo=memo)) return url
def get_vote_url(self): obj = dict( action="Upvote", post_id=self.id, unixtime=int(time.time()), amt=10, ) memo = json_util.serialize_memo(obj) url = reverse("vote-publish", kwargs=dict(memo=memo)) return url
def post(self, request, *args, **kwargs): amt = request.POST.get("amt") assert amt is not None, "bug" memo = kwargs.get("memo") assert memo is not None, "bug" # change the memo to have new amt memo_deserialized = validators.validate_memo( json_util.deserialize_memo(memo) ) memo_deserialized["amt"] = amt memo = json_util.serialize_memo(memo_deserialized) return HttpResponseRedirect( reverse( "vote-publish", kwargs=dict(memo=memo) ) )
def post(self, request, *args, **kwargs): try: context = self.get_context_data(**kwargs) except Exception as e: logger.exception(e) raise # Validating the form. form = self.form_class(request.POST) if not form.is_valid(): try: context = self.get_context_data(**kwargs) except Exception as e: logger.exception(e) raise context['form'] = form context['errors_detected'] = True return render(request, self.template_name, context) pid = context["pid"] amt = form.cleaned_data.get("amt") logger.debug("Bounty amount is {} for post_id {}".format(amt, pid)) memo = { "action": "Bounty", "post_id": pid, "amt": amt, "unixtime": int(time.time()) } memo_serialized = json_util.serialize_memo(memo) return HttpResponseRedirect( reverse("bounty-publish", kwargs=dict(memo=memo_serialized)))