From 3179d46b24fbdf9c90033cb9bb7d1eda94bb9004 Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Tue, 16 Jan 2024 22:38:51 +0100 Subject: [PATCH] redeem-nuggit: add many comments on what we are doing This is now not a "threat for reverse-engineering" any more, since the comments are stripped when "building" --- src/redeem-nuggit.sh | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/redeem-nuggit.sh b/src/redeem-nuggit.sh index cdbc892..bf0aedf 100755 --- a/src/redeem-nuggit.sh +++ b/src/redeem-nuggit.sh @@ -24,21 +24,40 @@ already_redeemed="'$nuggit' already redeemed" tried_before="You tried '$nuggit' before. It still isn't a valid answer... 🙄" redeemed=0 +# check if this nuggit was already_redeemed git cat-file -e "$(echo "$already_redeemed" | git hash-object --stdin)" 2>/dev/null && redeemed=1 +# The total number of nuggits is one bigger than the already committed ones +# because of the root commit, so we have to subtract 1 if this one was already +# redeemed redeemed_nuggits="$(($(git rev-list --count nuggits) - redeemed))" +# For the second last nuggit we want to show a hint that one is still missing +# to give a hint that LocalCodeExecution is self-deleting. +# CREDITS_TREE is a tree-object and CREDITS_TREE:almost means the blob with the +# name "almost" inside of it [ "$redeemed_nuggits" -ne $((NUMBER_OF_NUGGITS - 1)) ] || git cat-file -p CREDITS_TREE:almost; -# shellcheck disable=2170 +# shellcheck disable=2170 # NUMBER_OF_NUGGITS will be replaced by an integer, once we "build" it. [ "$redeemed_nuggits" -ne NUMBER_OF_NUGGITS ] || { - git cat-file -e "$(git hash-object --stdin <<< "$((NUMBER_OF_NUGGITS - 1))" | git hash-object --stdin)" 2>/dev/null || { echo Noughty boy!; exit 1; } + # check if the player did not just add a commit to our "nuggits" + # pseudobranch by looking up if we wrote the last number of redeemed + # nuggits to the objects. This is just a very basic "cheat-detection"... + git cat-file -e "$(git hash-object --stdin <<< "$((NUMBER_OF_NUGGITS - 1))" | git hash-object --stdin)" 2>/dev/null || { echo Naughty boy!; exit 1; } + # print the final credits. See "almost" above for syntax description + # Also do rot13 on the result in order to make it just a bit harder to just + # find the blob and read it git cat-file -p CREDITS_TREE:final | tr 'A-Za-z' 'N-ZA-Mn-za-m'; } +# if we already have redeemed this nuggit, print that it was already redeemed and exit git cat-file -p "$(echo "$already_redeemed" | git hash-object --stdin)" 2>/dev/null && exit +# if the user already tried to submit this wrong string, print the error and exit git cat-file -p "$(echo "$tried_before" | git hash-object --stdin)" 2>/dev/null && exit 1 +# Print the success message for this nuggit if it exists git cat-file -p "NUGGIT_DESCRIPTION_TREE:$(echo "$nuggit" | git hash-object --stdin)/success" 2>/dev/null || { + # if it does not exist, then this is not a valid nuggit (or it was + # LocalCodeExecution and that was deleted) echo "Unfortunately that is not a valid nuggit :/ Try again!" >&2 echo "$tried_before" | git hash-object --stdin -w >/dev/null 2>&1 exit 1 @@ -48,16 +67,20 @@ commit_nuggit() { # Manage our own little "branch" manually local tree # get the tree object from the last commit in nuggits tree="$(git rev-parse "nuggits^{tree}")" + # get the description from our "nuggit tree object" from the folder with + # the hash of the nuggit and inside of that the description file description="$(git cat-file -p "NUGGIT_DESCRIPTION_TREE:$(echo "$1" | git hash-object --stdin)/description")" # add an empty commit with the parent being nuggits and "reset nuggits to that new commit" - git commit-tree "$tree" -p "$(cat .git/nuggits)" -m "$1 - -$description" > .git/nuggits.bak + git commit-tree "$tree" -p "$(cat .git/nuggits)" -m "$(printf "%s\n\n" "$nuggit" "$description")" > .git/nuggits.bak # We can't directly pipe it into the file, because it will empty it before we read it... + # Therefore write it into a backup file and then replace it mv .git/nuggits.bak .git/nuggits } commit_nuggit "$nuggit" +# Print some stats for the player, so they know if they still need to look for other nuggits echo "Number of redeemed nuggits: $redeemed_nuggits of NUMBER_OF_NUGGITS" +# Write to our database, that this nuggit is now redeemed echo "$already_redeemed" | git hash-object --stdin -w >/dev/null 2>&1 +# Write as a "cheat-detection" for the final credits how many we have redeemed git hash-object --stdin <<< "$redeemed_nuggits"| git hash-object --stdin -w >/dev/null 2>&1