summaryrefslogtreecommitdiff
path: root/static-files/js/comment-submission.js
blob: 13c38494d79659a13d633720baf92df3648a1b7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2011, Matthias Andreas Benkard.

"use strict";

jQuery(function($) {
  var acceptable_cashhash = function (hash) {
    return (hash[0] === '0' &&
            hash[1] === '0' &&
            hash[2] === '0' &&
            hash[3] === '0');
  };

  var form_augmented_p = false;
  $('.comment-form').submit(function() {
    var form = $(this);
    $.ajax({
      url: "/RPC/generate-transaction-key",
      dataType: 'json',
      success: function(tkey) {
        if (!form_augmented_p) {
          form.find(':submit').attr("disabled", true);
          var salt = 0;
          var text = form.find('textarea');
          while (!acceptable_cashhash(Sha256.hash(text + ":" + tkey + ":" + salt))) {
            salt++;
          }
          form.prepend('<input type="hidden" name="transaction-key" value="' + tkey + '" />');
          form.prepend('<input type="hidden" name="salt"            value="' + salt + '" />');
          form_augmented_p = true;
          form.submit();
          return false;
        } else {
          return true;
        }
      }
    });
    return false;
  });
  $('.spam-detection-method').text("Hashcash");
  $('.irrelevant-for-hashcash').text('');
});