From b6482d86fa58dee7d55889fa873463274f81d3db Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Tue, 15 Nov 2022 18:54:30 -0500 Subject: [dynamic] Cut chalkline --- haunt/jakob/builder/blog.scm | 3 ++- haunt/jakob/utils/comments.scm | 12 +----------- haunt/static/js/proof-of-work.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 haunt/static/js/proof-of-work.js (limited to 'haunt') diff --git a/haunt/jakob/builder/blog.scm b/haunt/jakob/builder/blog.scm index 76782dc..b1e19fa 100644 --- a/haunt/jakob/builder/blog.scm +++ b/haunt/jakob/builder/blog.scm @@ -26,8 +26,8 @@ #:use-module (jakob utils pagination) #:use-module (jakob utils sxml) #:use-module (jakob utils tags) - #:use-module (jakob utils webmention) #:use-module (jakob utils comments) + ;; #:use-module (jakob utils webmention) #:use-module (srfi srfi-1) #:use-module (srfi srfi-19) #:use-module (srfi srfi-26) @@ -86,6 +86,7 @@ (h2 ,(hyperlink "https://indieweb.org/Webmention" "Webmentions") " for this Page") (ul (@ (id "webmention-container")) + ;; ,@(render-comment-view (fetch-webmentions (post-identifier post)))) ,@(render-comment-view (fetch-comments (post-identifier post)))) (form (@ (action "https://webmention.io/jakob.space/webmention") diff --git a/haunt/jakob/utils/comments.scm b/haunt/jakob/utils/comments.scm index 7cb4768..3f55345 100644 --- a/haunt/jakob/utils/comments.scm +++ b/haunt/jakob/utils/comments.scm @@ -102,14 +102,4 @@ (define (fetch-comments slug) "Blocking call to webmention.io to retrieve a vector of all Webmentions for `slug'" - (get-comments-by-slug slug) - ;; (list '((id . 1) - ;; (published . "20220514122120") - ;; (name . "Jakob") - ;; (subject . "Test") - ;; (email . "jakob@memeware.net") - ;; (comment . "Test") - ;; (url . "jakob.space") - ;; (reactions . (("🎖️" . 2))))) - ) -;; dynamic + (get-comments-by-slug slug)) diff --git a/haunt/static/js/proof-of-work.js b/haunt/static/js/proof-of-work.js new file mode 100644 index 0000000..5207ac6 --- /dev/null +++ b/haunt/static/js/proof-of-work.js @@ -0,0 +1,34 @@ +const text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.'; + +function makeid(length) { + var result = ''; + var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + var charactersLength = characters.length; + for ( var i = 0; i < length; i++ ) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +} + +async function digestMessage(message) { + const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array + const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message + const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array + const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string + return hashHex; +} + +const HARDNESS = 4; + +async function findPrefix(challenge) { + while (true) { + let prefix = makeid(32); + let digestHex = await digestMessage(prefix + challenge); + if (digestHex.startsWith("0".repeat(HARDNESS))) { + return [prefix + challenge, digestHex]; + } + } +} + +const suffix = "p12IyPB2dZsmBHYZyaEMrR5OUxqNc5Z9Cijal+9/iuQ="; +findPrefix(suffix).then(console.log); -- cgit v1.3